Skip to content

Commit

Permalink
Fix tsp sync strategy test.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Dec 18, 2024
1 parent a34c3a8 commit 1b0627b
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ export class TspSyncMigrationService {

private getOldIdBatches(oldToNewMappings: Map<string, string>): string[][] {
const oldIds = Array.from(oldToNewMappings.keys());
const batchSize = this.configService.get<number>('TSP_SYNC_MIGRATION_LIMIT', 100);
const batchSize = this.configService.getOrThrow<number>('TSP_SYNC_MIGRATION_LIMIT');

const batchCount = Math.ceil(oldIds.length / batchSize);
const batches: string[][] = [];
24 changes: 24 additions & 0 deletions apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ describe(TspSyncStrategy.name, () => {
let tspOauthDataMapper: DeepMocked<TspOauthDataMapper>;
let tspLegacyMigrationService: DeepMocked<TspLegacyMigrationService>;
let tspSyncMigrationService: DeepMocked<TspSyncMigrationService>;
let configService: DeepMocked<ConfigService<TspSyncConfig, true>>;

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],
});
};

10 changes: 5 additions & 5 deletions apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts
Original file line number Diff line number Diff line change
@@ -50,19 +50,19 @@ export class TspSyncStrategy extends SyncStrategy {

const schools = await this.tspSyncService.findSchoolsForSystem(system);

if (this.configService.get<boolean>('FEATURE_TSP_MIGRATION_ENABLED', false)) {
if (this.configService.getOrThrow<boolean>('FEATURE_TSP_MIGRATION_ENABLED')) {
await this.runMigration(system);
}

await this.syncData(system, schools);
}

private async syncSchools(system: System): Promise<School[]> {
const schoolDaysToFetch = this.configService.get<number>('TSP_SYNC_SCHOOL_DAYS_TO_FETCH', 1);
const schoolDaysToFetch = this.configService.getOrThrow<number>('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<number>('TSP_SYNC_SCHOOL_LIMIT', 1);
const schoolLimit = this.configService.getOrThrow<number>('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<void> {
const schoolDataDaysToFetch = this.configService.get<number>('TSP_SYNC_SCHOOL_DAYS_TO_FETCH', 1);
const schoolDataDaysToFetch = this.configService.getOrThrow<number>('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<number>('TSP_SYNC_DATA_LIMIT', 1);
const dataLimit = this.configService.getOrThrow<number>('TSP_SYNC_DATA_LIMIT');
const dataLimitFn = pLimit(dataLimit);

const dataPromises = oauthDataDtos.map((oauthDataDto) =>

0 comments on commit 1b0627b

Please sign in to comment.