13
src/database/database.test.ts
Normal file
13
src/database/database.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { createAddition, readAddition } from './database';
|
||||
|
||||
describe('Database', () => {
|
||||
it('creates a new record in the Addition table and reads it', async () => {
|
||||
const value = 13;
|
||||
|
||||
const createResult = await createAddition(value);
|
||||
|
||||
const readResult = await readAddition(createResult.id);
|
||||
expect(readResult).toBeDefined();
|
||||
expect(readResult.value).toBe(value);
|
||||
});
|
||||
});
|
||||
32
src/database/database.ts
Normal file
32
src/database/database.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
/**
|
||||
* Create a new record in the Addition table.
|
||||
* @param value - The value number to add as a new record.
|
||||
* @returns Whether the database operation succeeded.
|
||||
*/
|
||||
export async function createAddition(value: number) {
|
||||
return await prisma.addition.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
datetime: new Date(),
|
||||
value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a record from the Addition table.
|
||||
* @param id - The id of the record.
|
||||
* @returns The retrieved record, or an error.
|
||||
*/
|
||||
export async function readAddition(id: string) {
|
||||
return await prisma.addition.findUniqueOrThrow({
|
||||
where: {
|
||||
id
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import server from './server/server';
|
||||
|
||||
server.listen(3000, () => {
|
||||
console.log('Server running on port 3000');
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import requests from 'supertest';
|
||||
import server from './server';
|
||||
|
||||
beforeAll(() => {
|
||||
jest.mock('../utils/addition', () => ({
|
||||
addition: jest.fn((value: number) => value + 1)
|
||||
}));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
jest.mock('../utils/addition', () => ({
|
||||
addition: jest.fn((value: number) => value + 1)
|
||||
}));
|
||||
|
||||
describe('server', () => {
|
||||
it('returns input value increased by one', async () => {
|
||||
|
||||
@@ -22,14 +22,18 @@ const schema = {
|
||||
required: ['value']
|
||||
};
|
||||
|
||||
server.post('/', (req: Request, res: Response) => {
|
||||
server.post('/', async (req: Request, res: Response) => {
|
||||
logger.info(`POST / with ${JSON.stringify(req.body)}`);
|
||||
if (!validator.validate(req.body, schema).valid) {
|
||||
return res.status(400).json({ message: 'Malformed query parameters' });
|
||||
}
|
||||
|
||||
const { value } = req.body;
|
||||
|
||||
const result = await addition(value);
|
||||
|
||||
return res.json({
|
||||
response: addition(parseInt(req.body.value))
|
||||
response: result
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import { addition } from './addition';
|
||||
|
||||
jest.mock('../database/database', () => ({
|
||||
createAddition: jest.fn((value: number) => ({
|
||||
id: randomUUID(),
|
||||
datetime: new Date(),
|
||||
value
|
||||
}))
|
||||
}));
|
||||
|
||||
describe('call', () => {
|
||||
it('returns the input value increased by one', async () => {
|
||||
const response = addition(3);
|
||||
const response = await addition(3);
|
||||
|
||||
expect(response).toBe(4);
|
||||
});
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { createAddition } from '../database/database';
|
||||
import { logger } from './logger';
|
||||
|
||||
/**
|
||||
* Format a response message containing a user input.
|
||||
* Increase the user imput value by one, and creates an Addition record with that value.
|
||||
* @param number - The user input value.
|
||||
* @returns The value increased by one.
|
||||
*/
|
||||
export function addition(value: number) {
|
||||
export async function addition(value: number) {
|
||||
logger.info(`addition(${value})`);
|
||||
await createAddition(value);
|
||||
|
||||
return value + 1;
|
||||
}
|
||||
|
||||
14
src/utils/clearDatabase.ts
Normal file
14
src/utils/clearDatabase.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
/**
|
||||
* Clear the database of all records.
|
||||
*/
|
||||
export const clearDatabase = async () => {
|
||||
await prisma.addition.deleteMany({});
|
||||
};
|
||||
|
||||
global.beforeAll(async () => {
|
||||
await clearDatabase();
|
||||
});
|
||||
Reference in New Issue
Block a user