Skip to content

Commit

Permalink
#23 resolving review
Browse files Browse the repository at this point in the history
  • Loading branch information
leoleader committed Sep 19, 2023
1 parent 8e723c7 commit 2a2a837
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
12 changes: 6 additions & 6 deletions scylla-server/src/services/systems.services.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/message-maps.utils';

/**
* CRUD operation to get all systems with ResponseFunction type
* @returns Promise<string> contianing all the systems in the db
*/
export async function getAllSystems(): Promise<string> {
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<void>
*/
export async function upsertSystems(system_name: string): Promise<void> {
export const upsertSystems = async (system_name: string) => {
await prisma.system.upsert({
where: {
name: system_name
},
update: {},
create: {
name: system_name,
runs: undefined
name: system_name
}
});
}
};
2 changes: 1 addition & 1 deletion scylla-server/src/utils/message-maps.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAllDataTypes } from '../services/dataTypes.services';
import { getAllSystems } from '../services/systems.services';

export type ResponseFunction = (data: JSON) => Promise<string>;
export type ResponseFunction = (data?: JSON) => Promise<string>;

/**
* Creates a map of messages received from the client to functions that handle the messages
Expand Down
42 changes: 22 additions & 20 deletions scylla-server/tests/systems-services.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, test, expect } from 'vitest';
import { describe, test, expect, afterAll } from 'vitest';
import { getAllSystems, upsertSystems } from '../src/services/systems.services';

import prisma from '../src/prisma/prisma-client';
Expand All @@ -7,20 +7,6 @@ import prisma from '../src/prisma/prisma-client';
* Tests for CRUD Service functions
*/
describe('CRUD Systems', () => {
/**
* updated unit test for get all systems
*/
test('Get All Systems Works', async () => {
const expected = [];
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 creating system if doesn't exist
Expand All @@ -34,23 +20,39 @@ describe('CRUD Systems', () => {
expect(result).toEqual(expected);
});

/**
* updated unit test for get all systems
*/
test('Get All Systems Works', async () => {
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' }];
upsertSystems('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
await prisma.system.delete({
where: {
name: 'test'
}
afterAll(async () => {
await prisma.system.delete({
where: {
name: 'test'
}
});
});
});
});

0 comments on commit 2a2a837

Please sign in to comment.