-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typeorm car rental write repo, refactor db master/slave config
- Loading branch information
1 parent
4b82685
commit 0523650
Showing
16 changed files
with
272 additions
and
65 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
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
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,29 @@ | ||
import CarRentalWriteRepositoryInterface from "src/core/domain/carRental/interfaces/repositories/write"; | ||
import CarRentalDTO from "src/core/domain/carRental/dto"; | ||
import {TypeORMCarRental} from "src/driven/repositories/typeorm/entities"; | ||
import {DataSource, Repository} from "typeorm"; | ||
import {inject, singleton} from "tsyringe"; | ||
|
||
@singleton() | ||
export default class TypeORMCarRentalWriteRepository implements CarRentalWriteRepositoryInterface { | ||
|
||
private readonly dataSource: DataSource; | ||
|
||
constructor(@inject("DataSource") dataSource: DataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
async create(carRentalDTO: CarRentalDTO): Promise<void> { | ||
const repository = this.dataSource.getRepository(TypeORMCarRental); | ||
const typeORMCarRental = new TypeORMCarRental(); | ||
typeORMCarRental.id = carRentalDTO.id; | ||
typeORMCarRental.totalPrice = carRentalDTO.totalPrice; | ||
typeORMCarRental.pickupDateTime = carRentalDTO.pickupDateTime; | ||
typeORMCarRental.dropOffDateTime = carRentalDTO.dropOffDateTime; | ||
typeORMCarRental.customer = {id: carRentalDTO.customerId}; | ||
typeORMCarRental.car = <any>{id: carRentalDTO.car.id}; | ||
|
||
await repository.save(typeORMCarRental); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
tests/integration/typeorm/carRental/command/create.test.ts
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,97 @@ | ||
import 'reflect-metadata'; | ||
import {container} from "tsyringe"; | ||
import TypeORMCarRentalWriteRepository from "src/driven/repositories/typeorm/carRental/write"; | ||
import useAppDataSources from "src/configuration/injection/containers/database"; | ||
import useTypeORMRepositories from "src/configuration/injection/containers/repositories/typeorm"; | ||
import CarRentalDTO from "src/core/domain/carRental/dto"; | ||
import TypeORMCarRentalReadRepository from "src/driven/repositories/typeorm/carRental/read"; | ||
import {runDataSourceBeforeEachOps} from "tests/integration/typeorm/utils/setup"; | ||
import DateParser from "tests/utils/dateParser"; | ||
import {advanceTo} from "jest-date-mock"; | ||
import useTestingUtilities from "tests/configuration/containers/utils"; | ||
import {DataSource} from "typeorm"; | ||
import {v4} from "uuid"; | ||
import {runDataSourceAfterEachOps} from "tests/integration/typeorm/utils/tearDown"; | ||
import { | ||
populateCarAndCarModelFromCarRentalTestCase, | ||
populateCustomerFromCarRentalTestCase | ||
} from "tests/integration/typeorm/carRental/utils/populateFromTestCase"; | ||
import {expectedCarRentalFromTestCase} from "tests/integration/typeorm/utils/misc"; | ||
|
||
describe.each([ | ||
{ | ||
rental: { | ||
id: v4(), | ||
customerId: v4(), | ||
car: { | ||
id: v4(), | ||
model: { | ||
id: v4(), | ||
dailyRate: 100, | ||
} | ||
}, | ||
totalPrice: 100, | ||
pickupDateTime: 'today', | ||
dropOffDateTime: 'tomorrow', | ||
} | ||
}, | ||
{ | ||
rental: { | ||
id: v4(), | ||
customerId: v4(), | ||
car: { | ||
id: v4(), | ||
model: { | ||
id: v4(), | ||
dailyRate: 100, | ||
} | ||
}, | ||
totalPrice: 200, | ||
pickupDateTime: 'tomorrow', | ||
dropOffDateTime: 'in 2 days', | ||
} | ||
}, | ||
])("Integration tests to create car rental in a postgres database using typeorm", (testCase) => { | ||
let repository: TypeORMCarRentalWriteRepository; | ||
let readRepository: TypeORMCarRentalReadRepository; | ||
let carRentalToCreate: CarRentalDTO; | ||
let expectedCarRental: CarRentalDTO; | ||
let dateParser: DateParser; | ||
|
||
beforeAll(() => { | ||
advanceTo(Date.now()); | ||
useTestingUtilities(); | ||
useAppDataSources(); | ||
useTypeORMRepositories(); | ||
dateParser = container.resolve("DateParser"); | ||
}) | ||
|
||
beforeEach(async () => { | ||
repository = container.resolve("CarRentalWriteRepositoryInterface"); | ||
readRepository = container.resolve("CarRentalReadRepositoryInterface"); | ||
await runDataSourceBeforeEachOps(); | ||
await populateCustomerFromCarRentalTestCase(testCase.rental); | ||
await populateCarAndCarModelFromCarRentalTestCase(testCase.rental); | ||
expectedCarRental = expectedCarRentalFromTestCase( | ||
testCase.rental, | ||
dateParser, | ||
); | ||
carRentalToCreate = expectedCarRental; | ||
}) | ||
|
||
afterEach(async () => { | ||
await runDataSourceAfterEachOps(); | ||
}) | ||
|
||
test(`Create a car rental ${testCase.rental.id} should create one car rental in the database`, async () => { | ||
const dataSource: DataSource = container.resolve("DataSource"); | ||
const queryRunner = dataSource.createQueryRunner(); | ||
await queryRunner.connect(); | ||
await queryRunner.startTransaction(); | ||
await repository.create(carRentalToCreate); | ||
await queryRunner.commitTransaction(); | ||
await queryRunner.release(); | ||
const retrievedCarRental = await readRepository.read(testCase.rental.id); | ||
expect(retrievedCarRental.toDTO()).toEqual(expectedCarRental); | ||
}) | ||
}) |
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
27 changes: 27 additions & 0 deletions
27
tests/integration/typeorm/carRental/utils/populateFromTestCase.ts
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 {populateCar, populateCarModel, populateCustomer} from "tests/integration/typeorm/utils/populate"; | ||
import {TypeORMCar, TypeORMCarModel, TypeORMCustomer} from "src/driven/repositories/typeorm/entities"; | ||
import {CarRentalTestCaseEntry} from "tests/integration/typeorm/carRental/utils/testCase.types"; | ||
|
||
export const populateCustomerFromCarRentalTestCase = async ( | ||
testCase: CarRentalTestCaseEntry, | ||
): Promise<TypeORMCustomer> => { | ||
return await populateCustomer({id: testCase.customerId}); | ||
} | ||
|
||
export const populateCarModelFromCarRentalTestCase = async ( | ||
testCase: CarRentalTestCaseEntry, | ||
): Promise<TypeORMCarModel> => { | ||
return await populateCarModel({ | ||
id: testCase.car.model.id, | ||
dailyRate: testCase.car.model.dailyRate, | ||
}); | ||
} | ||
|
||
export const populateCarAndCarModelFromCarRentalTestCase = async ( | ||
testCase: CarRentalTestCaseEntry, | ||
): Promise<TypeORMCar> => { | ||
return await populateCar({ | ||
id: testCase.car.id, | ||
model: await populateCarModelFromCarRentalTestCase(testCase), | ||
}) | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/integration/typeorm/carRental/utils/testCase.types.ts
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,18 @@ | ||
export type CarRentalTestCaseEntry = { | ||
id: string; | ||
pickupDateTime: string; | ||
dropOffDateTime: string; | ||
customerId: string; | ||
car: CarTestCaseEntry; | ||
totalPrice: number; | ||
}; | ||
|
||
export type CarModelTestCaseEntry = { | ||
id: string; | ||
dailyRate: number; | ||
} | ||
|
||
export type CarTestCaseEntry = { | ||
id: string; | ||
model: CarModelTestCaseEntry; | ||
}; |
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
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
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,23 @@ | ||
import CarRentalDTO from "src/core/domain/carRental/dto"; | ||
import {CarRentalTestCaseEntry} from "tests/integration/typeorm/carRental/utils/testCase.types"; | ||
import DateParser from "tests/utils/dateParser"; | ||
|
||
export const expectedCarRentalFromTestCase = ( | ||
testCase: CarRentalTestCaseEntry, | ||
dateParser: DateParser, | ||
): CarRentalDTO => { | ||
return { | ||
id: testCase.id, | ||
customerId: testCase.customerId, | ||
car: { | ||
id: testCase.car.id, | ||
model: { | ||
id: testCase.car.model.id, | ||
dailyRate: testCase.car.model.dailyRate, | ||
}, | ||
}, | ||
pickupDateTime: dateParser.parse(testCase.pickupDateTime), | ||
dropOffDateTime: dateParser.parse(testCase.dropOffDateTime), | ||
totalPrice: testCase.totalPrice, | ||
} as CarRentalDTO; | ||
} |
Oops, something went wrong.