From 93c9ed78e786bdc3f8f28f6e9313eec11ab6c987 Mon Sep 17 00:00:00 2001 From: Alejandro Peralta Date: Sun, 12 Jan 2025 15:17:44 +0100 Subject: [PATCH] feat(api): Ensure that the project scorecards are wiped before new ones are imported --- api/src/modules/import/import.repostiory.ts | 6 ++- .../import/import.repository.spec.ts | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 api/test/integration/import/import.repository.spec.ts diff --git a/api/src/modules/import/import.repostiory.ts b/api/src/modules/import/import.repostiory.ts index 32accacb..42e68213 100644 --- a/api/src/modules/import/import.repostiory.ts +++ b/api/src/modules/import/import.repostiory.ts @@ -34,7 +34,11 @@ export class ImportRepository { constructor(private readonly dataSource: DataSource) {} async importProjectScorecard(projectScorecards: ProjectScorecard[]) { - return this.dataSource.transaction(async (manager) => { + return this.dataSource.transaction('READ COMMITTED', async (manager) => { + // Wipe current project scorecards + await manager.clear(ProjectScorecard); + + // Insert await manager.save(projectScorecards); }); } diff --git a/api/test/integration/import/import.repository.spec.ts b/api/test/integration/import/import.repository.spec.ts new file mode 100644 index 00000000..22011421 --- /dev/null +++ b/api/test/integration/import/import.repository.spec.ts @@ -0,0 +1,38 @@ +import { ProjectScorecard } from '@shared/entities/project-scorecard.entity'; +import { TestManager } from 'api/test/utils/test-manager'; + +describe('Import Repository', () => { + let testManager: TestManager; + let jwtToken: string; + + beforeAll(async () => { + testManager = await TestManager.createTestManager(); + const response = await testManager.setUpTestUser(); + jwtToken = response.jwtToken; + }); + + afterEach(async () => { + await testManager.clearDatabase(); + }); + + afterAll(async () => { + await testManager.close(); + }); + + it('should wipe previous project scorecards before importing new ones', async () => { + // Given + await testManager.ingestCountries(); + await testManager.ingestProjectScoreCards(jwtToken); + const scorecardRepository = + testManager.dataSource.getRepository(ProjectScorecard); + + const scorecardCount = await scorecardRepository.count(); + + // When + await testManager.ingestProjectScoreCards(jwtToken); + const latestScorecardCount = await scorecardRepository.count(); + + // Then + expect(scorecardCount).toEqual(latestScorecardCount); + }); +});