-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ert-driver #24-CRUD-upsert-driver
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (driverName: string) => { | ||
await prisma.driver.upsert({ | ||
where: { | ||
username: driverName | ||
}, | ||
update: {}, | ||
create: { | ||
username: driverName | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
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); | ||
}); | ||
}); |