Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#23 Upsert System #31

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions scylla-server/src/services/systems.services.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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 string contianing all the systems in the db
* @returns Promise<string> contianing all the systems in the db
*/
export const getAllSystems: ResponseFunction = async () => {
export async function getAllSystems(): Promise<string> {
leoleader marked this conversation as resolved.
Show resolved Hide resolved
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> {
leoleader marked this conversation as resolved.
Show resolved Hide resolved
await prisma.system.upsert({
where: {
name: system_name
},
update: {},
create: {
name: system_name,
runs: undefined
}
});
}
54 changes: 47 additions & 7 deletions scylla-server/tests/systems-services.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
import prisma from '../src/prisma/prisma-client';
import { describe, test, expect } 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', () => {
/**
* updated unit test for get all systems
*/
test('Get All Systems Works', async () => {
const allSystems = await prisma.system.findMany();
expect(allSystems).toStrictEqual([]);
const expected = [];
leoleader marked this conversation as resolved.
Show resolved Hide resolved
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
*/
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);
});

/**
* unit test for upsert system
* testing does nothing if system does exist
*/
test('Upsert System Does Nothing', async () => {
const expected = [{ name: 'test' }];
upsertSystems('test');
leoleader marked this conversation as resolved.
Show resolved Hide resolved
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({
leoleader marked this conversation as resolved.
Show resolved Hide resolved
where: {
name: 'test'
}
});
});
});