Skip to content

Commit

Permalink
Added service functions for the driver along with unit testing for both.
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiantBunny633 committed Sep 21, 2023
1 parent c3f29f0 commit aff3451
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scylla-server/src/services/driver.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import prisma from '../prisma/prisma-client';
import { ResponseFunction } from '../utils/message-maps.utils';

/**
* CRUD operation to get all dataTypes
* @returns string containing all the dataTypes
*/
export const getAllDrivers: ResponseFunction = async () => {
const data = await prisma.driver.findMany();
return JSON.stringify(data);
};

/**
* CRUD operation to create a driver in the database if it doesn't already exist, does nothing otherwise.
*
*/
export const upsertDriver = async (driver_name: string) => {
await prisma.driver.upsert({
where: {
username: driver_name
},
update: {},
create: {
username: driver_name
}
});
};
2 changes: 2 additions & 0 deletions scylla-server/src/utils/message-maps.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getAllDataTypes } from '../services/dataTypes.services';
import { getAllDrivers } from '../services/driver.services';
import { getAllSystems } from '../services/systems.services';

export type ResponseFunction = (data?: JSON) => Promise<string>;
Expand All @@ -11,5 +12,6 @@ export const createClientMessageMap = (): Map<string, ResponseFunction> => {
const clientMessageMap = new Map<string, ResponseFunction>();
clientMessageMap.set('getAllSystems', getAllSystems);
clientMessageMap.set('getAllDataTypes', getAllDataTypes);
clientMessageMap.set('getAllDrivers', getAllDrivers);
return clientMessageMap;
};
33 changes: 33 additions & 0 deletions scylla-server/tests/driver-services.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, test, expect } from 'vitest';
import { getAllDrivers, upsertDriver } from '../src/services/driver.services';

/**
* Tests for CRUD Service functions
*/
describe('CRUD Driver', () => {
/**
* unit test for get all drivers
*/
test('Get All Data Types Works', async () => {
const expected = [{ username: 'test' }];
const result = await getAllDrivers();

// 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 testing for upsert driver
* test driver creation if the name doesn't already exist
* */
test('Upsert Driver Creates', async () => {
const expected = [{ username: 'test' }];
await upsertDriver('test');
const result = JSON.parse(await getAllDrivers());

expect(result).toEqual(expected);
});
});

0 comments on commit aff3451

Please sign in to comment.