diff --git a/scylla-server/src/services/systems.services.ts b/scylla-server/src/services/systems.services.ts index 948158a5..055cc13c 100644 --- a/scylla-server/src/services/systems.services.ts +++ b/scylla-server/src/services/systems.services.ts @@ -3,9 +3,27 @@ import { ResponseFunction } from '../utils/message-maps.utils'; /** * CRUD operation to get all systems with ResponseFunction type - * @returns string contianing all the systems in the db + * @returns Promise contianing all the systems in the db */ export const getAllSystems: ResponseFunction = async () => { const data = await prisma.system.findMany(); return JSON.stringify(data); }; + +/** + * CRUD opertation that creates system if it doesn't exist, otherwise does nothing. + * Currently designated private so not hooked up to server. + * @param system_name name of the system as string + * @returns Promise + */ +export const upsertSystems = async (system_name: string) => { + await prisma.system.upsert({ + where: { + name: system_name + }, + update: {}, + create: { + name: system_name + } + }); +}; diff --git a/scylla-server/src/utils/message-maps.utils.ts b/scylla-server/src/utils/message-maps.utils.ts index 08aa7e55..f4691423 100644 --- a/scylla-server/src/utils/message-maps.utils.ts +++ b/scylla-server/src/utils/message-maps.utils.ts @@ -1,7 +1,7 @@ import { getAllDataTypes } from '../services/dataTypes.services'; import { getAllSystems } from '../services/systems.services'; -export type ResponseFunction = (data: JSON) => Promise; +export type ResponseFunction = (data?: JSON) => Promise; /** * Creates a map of messages received from the client to functions that handle the messages diff --git a/scylla-server/tests/systems-services.test.ts b/scylla-server/tests/systems-services.test.ts index afab3ab2..9c957c3c 100644 --- a/scylla-server/tests/systems-services.test.ts +++ b/scylla-server/tests/systems-services.test.ts @@ -1,16 +1,58 @@ -import prisma from '../src/prisma/prisma-client'; -import { describe, test, expect } from 'vitest'; +import { describe, test, expect, afterAll } from 'vitest'; +import { getAllSystems, upsertSystems } from '../src/services/systems.services'; -/** - * Tests for CRUD service functions - */ +import prisma from '../src/prisma/prisma-client'; /** - * basic ahh unit test for get all systems + * Tests for CRUD Service functions */ describe('CRUD Systems', () => { + /** + * unit test for upsert system + * testing creating system if doesn't exist + */ + test('Upsert System Creates', async () => { + const expected = [{ name: 'test' }]; + await upsertSystems('test'); + const result = JSON.parse(await getAllSystems()); + + // Use toEqual to compare parsedResult with the expected array + expect(result).toEqual(expected); + }); + + /** + * updated unit test for get all systems + */ test('Get All Systems Works', async () => { - const allSystems = await prisma.system.findMany(); - expect(allSystems).toStrictEqual([]); + const expected = [{ name: 'test' }]; + const result = await getAllSystems(); + + // Parse result to a JavaScript object from the JSON string + const parsedResult = JSON.parse(result); + + // Use toEqual to compare parsedResult with the expected array + expect(parsedResult).toEqual(expected); + }); + + /** + * unit test for upsert system + * testing does nothing if system does exist + */ + test('Upsert System Does Nothing', async () => { + const expected = [{ name: 'test' }]; + await upsertSystems('test'); + const result = JSON.parse(await getAllSystems()); + + // Use toEqual to compare result with the expected array + expect(result).toEqual(expected); + + //cleaning up + afterAll(async () => { + await prisma.system.delete({ + where: { + name: 'test' + } + }); + }); }); });