From 1b0627bc8b4009caabd25203ead0f535d44454f5 Mon Sep 17 00:00:00 2001 From: Maximilian Kreuzkam Date: Wed, 18 Dec 2024 14:50:12 +0100 Subject: [PATCH] Fix tsp sync strategy test. --- .../sync/tsp/tsp-sync-migration.service.ts | 2 +- .../infra/sync/tsp/tsp-sync.strategy.spec.ts | 24 +++++++++++++++++++ .../src/infra/sync/tsp/tsp-sync.strategy.ts | 10 ++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/apps/server/src/infra/sync/tsp/tsp-sync-migration.service.ts b/apps/server/src/infra/sync/tsp/tsp-sync-migration.service.ts index 5ba2bcd7e1..05b30a81e5 100644 --- a/apps/server/src/infra/sync/tsp/tsp-sync-migration.service.ts +++ b/apps/server/src/infra/sync/tsp/tsp-sync-migration.service.ts @@ -106,7 +106,7 @@ export class TspSyncMigrationService { private getOldIdBatches(oldToNewMappings: Map): string[][] { const oldIds = Array.from(oldToNewMappings.keys()); - const batchSize = this.configService.get('TSP_SYNC_MIGRATION_LIMIT', 100); + const batchSize = this.configService.getOrThrow('TSP_SYNC_MIGRATION_LIMIT'); const batchCount = Math.ceil(oldIds.length / batchSize); const batches: string[][] = []; diff --git a/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts b/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts index 5e1ccb52a2..d1a9942cc9 100644 --- a/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts +++ b/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts @@ -37,6 +37,7 @@ describe(TspSyncStrategy.name, () => { let tspOauthDataMapper: DeepMocked; let tspLegacyMigrationService: DeepMocked; let tspSyncMigrationService: DeepMocked; + let configService: DeepMocked>; beforeAll(async () => { module = await Test.createTestingModule({ @@ -84,6 +85,7 @@ describe(TspSyncStrategy.name, () => { tspOauthDataMapper = module.get(TspOauthDataMapper); tspLegacyMigrationService = module.get(TspLegacyMigrationService); tspSyncMigrationService = module.get(TspSyncMigrationService); + configService = module.get(ConfigService); }); afterEach(() => { @@ -125,6 +127,12 @@ describe(TspSyncStrategy.name, () => { foundSystem?: System; updatedAccount?: Account; updatedUser?: UserDO; + configValues?: unknown[]; + migrationResult?: { + totalAmount: number; + totalUsers: number; + totalAccounts: number; + }; }) => { tspFetchService.fetchTspSchools.mockResolvedValueOnce(params.fetchedSchools ?? []); tspFetchService.fetchTspClasses.mockResolvedValueOnce(params.fetchedClasses ?? []); @@ -138,6 +146,16 @@ describe(TspSyncStrategy.name, () => { tspSyncService.findTspSystemOrFail.mockResolvedValueOnce(params.foundSystem ?? systemFactory.build()); tspOauthDataMapper.mapTspDataToOauthData.mockReturnValueOnce(params.mappedOauthDto ?? []); + + params.configValues?.forEach((value) => configService.getOrThrow.mockReturnValueOnce(value)); + + tspSyncMigrationService.migrateTspUsers.mockResolvedValueOnce( + params.migrationResult ?? { + totalAccounts: faker.number.int(), + totalAmount: faker.number.int(), + totalUsers: faker.number.int(), + } + ); }; describe('sync', () => { @@ -167,12 +185,15 @@ describe(TspSyncStrategy.name, () => { fetchedStudentMigrations: [tspStudent], fetchedTeacherMigrations: [tspTeacher], mappedOauthDto: [oauthDataDto], + configValues: [1, 10, true, 10, 1, 50], }); return { oauthDataDto }; }; it('should find the tsp system', async () => { + setup(); + await sut.sync(); expect(tspSyncService.findTspSystemOrFail).toHaveBeenCalled(); @@ -265,6 +286,7 @@ describe(TspSyncStrategy.name, () => { setupMockServices({ fetchedSchools: tspSchools, + configValues: [1, 10, true, 10, 1, 50], }); }; @@ -289,6 +311,7 @@ describe(TspSyncStrategy.name, () => { setupMockServices({ fetchedSchools: tspSchools, foundSchool: school, + configValues: [1, 10, true, 10, 1, 50], }); }; @@ -311,6 +334,7 @@ describe(TspSyncStrategy.name, () => { setupMockServices({ fetchedSchools: tspSchools, + configValues: [1, 10, true, 10, 1, 50], }); }; diff --git a/apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts b/apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts index 07877de9e2..a84b8e740d 100644 --- a/apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts +++ b/apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts @@ -50,7 +50,7 @@ export class TspSyncStrategy extends SyncStrategy { const schools = await this.tspSyncService.findSchoolsForSystem(system); - if (this.configService.get('FEATURE_TSP_MIGRATION_ENABLED', false)) { + if (this.configService.getOrThrow('FEATURE_TSP_MIGRATION_ENABLED')) { await this.runMigration(system); } @@ -58,11 +58,11 @@ export class TspSyncStrategy extends SyncStrategy { } private async syncSchools(system: System): Promise { - const schoolDaysToFetch = this.configService.get('TSP_SYNC_SCHOOL_DAYS_TO_FETCH', 1); + const schoolDaysToFetch = this.configService.getOrThrow('TSP_SYNC_SCHOOL_DAYS_TO_FETCH'); const tspSchools = await this.tspFetchService.fetchTspSchools(system, schoolDaysToFetch); this.logger.info(new TspSchoolsFetchedLoggable(tspSchools.length, schoolDaysToFetch)); - const schoolLimit = this.configService.get('TSP_SYNC_SCHOOL_LIMIT', 1); + const schoolLimit = this.configService.getOrThrow('TSP_SYNC_SCHOOL_LIMIT'); const schoolLimitFn = pLimit(schoolLimit); const schoolPromises = tspSchools.map((tspSchool) => @@ -100,7 +100,7 @@ export class TspSyncStrategy extends SyncStrategy { } private async syncData(system: System, schools: School[]): Promise { - const schoolDataDaysToFetch = this.configService.get('TSP_SYNC_SCHOOL_DAYS_TO_FETCH', 1); + const schoolDataDaysToFetch = this.configService.getOrThrow('TSP_SYNC_SCHOOL_DAYS_TO_FETCH'); const tspTeachers = await this.tspFetchService.fetchTspTeachers(system, schoolDataDaysToFetch); const tspStudents = await this.tspFetchService.fetchTspStudents(system, schoolDataDaysToFetch); const tspClasses = await this.tspFetchService.fetchTspClasses(system, schoolDataDaysToFetch); @@ -118,7 +118,7 @@ export class TspSyncStrategy extends SyncStrategy { this.logger.info(new TspSyncingUsersLoggable(oauthDataDtos.length)); - const dataLimit = this.configService.get('TSP_SYNC_DATA_LIMIT', 1); + const dataLimit = this.configService.getOrThrow('TSP_SYNC_DATA_LIMIT'); const dataLimitFn = pLimit(dataLimit); const dataPromises = oauthDataDtos.map((oauthDataDto) =>