Skip to content

Commit

Permalink
Merge pull request #31 from Northeastern-Electric-Racing/#23-CRUD-ups…
Browse files Browse the repository at this point in the history
…ert-system

#23 Upsert System
  • Loading branch information
Peyton-McKee committed Sep 19, 2023
2 parents c03aed9 + 2a2a837 commit c3f29f0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
20 changes: 19 additions & 1 deletion scylla-server/src/services/systems.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<void>
*/
export const upsertSystems = async (system_name: string) => {
await prisma.system.upsert({
where: {
name: system_name
},
update: {},
create: {
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
58 changes: 50 additions & 8 deletions scylla-server/tests/systems-services.test.ts
Original file line number Diff line number Diff line change
@@ -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'
}
});
});
});
});

0 comments on commit c3f29f0

Please sign in to comment.