Skip to content

Commit

Permalink
N21-2103 wip tests, mark some code for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GordonNicholasCap committed Dec 12, 2024
1 parent 847dc2a commit 31b4d37
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
// TODO: tests
import { VidisItemDto } from '@src/modules/school-license/dto';
import { vidisItemResponseFactory } from '../testing/vidis-item.response.factory';
import { VidisItemResponse } from '../response/vidis-item.response';
import { VidisItemMapper } from './vidis-item.mapper';

describe(VidisItemMapper.name, () => {
describe('mapToVidisItem', () => {
describe('when a VIDIS item response is given', () => {
const setup = () => {
const vidisItemResponse = vidisItemResponseFactory.build();

return { vidisItemResponse };
};

it('should return a mapped VidisItemDto', () => {
const { vidisItemResponse } = setup();

const result: VidisItemDto = VidisItemMapper.mapToVidisItem(vidisItemResponse);

expect(result).toEqual(
expect.objectContaining({
offerId: vidisItemResponse.offerId,
schoolActivations: vidisItemResponse.schoolActivations,
} as VidisItemDto)
);
});
});
});

describe('mapToVidisItems', () => {
const setup = () => {
const vidisItemResponses = vidisItemResponseFactory.buildList(3);
const expectedItemDtos: VidisItemDto[] = vidisItemResponses.map(
(response: VidisItemResponse) =>
({
offerId: response.offerId,
schoolActivations: response.schoolActivations,
} as VidisItemDto)
);

return { vidisItemResponses, expectedItemDtos };
};

it('should return a mapped VidisItemDto', () => {
const { vidisItemResponses, expectedItemDtos } = setup();

const results: VidisItemDto[] = VidisItemMapper.mapToVidisItems(vidisItemResponses);

expect(results.length).toEqual(vidisItemResponses.length);
expect(results).toEqual(expect.arrayContaining(expectedItemDtos));
});
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { VidisItemDto } from '@modules/school-license/dto';
import { VidisItemDto } from '@src/modules/school-license/dto';
import { VidisItemResponse } from '../response/vidis-item.response';

export class VidisItemMapper {
static mapToVidisItems(vidisItemResponse: VidisItemResponse[]): VidisItemDto[] {
public static mapToVidisItems(vidisItemResponse: VidisItemResponse[]): VidisItemDto[] {
const mapped: VidisItemDto[] = vidisItemResponse.map((item) => this.mapToVidisItem(item));

return mapped;
}

static mapToVidisItem(vidisItemResponse: VidisItemResponse): VidisItemDto {
public static mapToVidisItem(vidisItemResponse: VidisItemResponse): VidisItemDto {
return new VidisItemDto({
offerId: vidisItemResponse.offerId,
schoolActivations: vidisItemResponse.schoolActivations,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
// TODO: tests
import { Test, TestingModule } from '@nestjs/testing';
import { HttpService } from '@nestjs/axios';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { MediaSourceService } from '@modules/mediasource/service';
import { MediaSchoolLicenseService } from '@modules/school-license/service/media-school-license.service';
import { DefaultEncryptionService, EncryptionService, SymetricKeyEncryptionService } from '@infra/encryption';
import { VidisSyncService } from './vidis-sync.service';

describe(VidisSyncService.name, () => {
let module: TestingModule;
let httpService: DeepMocked<HttpService>;

Check failure on line 11 in apps/server/src/infra/sync/media-licenses/service/vidis-sync.service.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'httpService' is assigned a value but never used
let mediaSourceService: DeepMocked<MediaSourceService>;

Check failure on line 12 in apps/server/src/infra/sync/media-licenses/service/vidis-sync.service.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'mediaSourceService' is assigned a value but never used
let mediaSchoolLicenseService: DeepMocked<MediaSchoolLicenseService>;

Check failure on line 13 in apps/server/src/infra/sync/media-licenses/service/vidis-sync.service.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'mediaSchoolLicenseService' is assigned a value but never used
let encryptionService: DeepMocked<SymetricKeyEncryptionService>;

Check failure on line 14 in apps/server/src/infra/sync/media-licenses/service/vidis-sync.service.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'encryptionService' is assigned a value but never used

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
VidisSyncService,
{
provide: HttpService,
useValue: createMock<HttpService>(),
},
{
provide: MediaSourceService,
useValue: createMock<MediaSourceService>(),
},
{
provide: MediaSchoolLicenseService,
useValue: createMock<MediaSchoolLicenseService>(),
},
{
provide: DefaultEncryptionService,
useValue: createMock<EncryptionService>(),
},
],
}).compile();

httpService = module.get(HttpService);
mediaSourceService = module.get(MediaSourceService);
mediaSchoolLicenseService = module.get(MediaSchoolLicenseService);
encryptionService = module.get(DefaultEncryptionService);
});

afterAll(async () => {
await module.close();
});

afterEach(() => {
jest.clearAllMocks();
});

// TODO: implementation
describe('syncMediaSchoolLicenses', () => {
describe('when the media source with correct configs is found', () => {
const setup = () => {};

it('should not throw any error', () => {
setup();
});
});

describe('when there is an error fetching data from media source', () => {
const setup = () => {};

it('should throw an error', () => {
setup();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { MediaSource } from '@src/modules/mediasource/domain';
import { MediaSourceDataFormat } from '@src/modules/mediasource/enum';
import { MediaSourceForSyncNotFoundLoggableException } from '@src/modules/mediasource/loggable';
import { MediaSourceService } from '@src/modules/mediasource/service';
import { DefaultEncryptionService, EncryptionService } from '@infra/encryption';
import { HttpService } from '@nestjs/axios';
import { Inject, Injectable } from '@nestjs/common';
import { AxiosResponse } from 'axios';
import { lastValueFrom, Observable } from 'rxjs';
import { VidisItemMapper } from '../mapper/vidis-item.mapper';
import { VidisResponse } from '../response';
import { VidisItemResponse } from '../response/vidis-item.response';
import { DefaultEncryptionService, EncryptionService } from '@infra/encryption';

@Injectable()
export class VidisSyncService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { VidisSyncStrategy } from './vidis-sync.strategy';
describe(VidisSyncService.name, () => {
let module: TestingModule;
let sut: VidisSyncStrategy;
// TODO: cleanup
// let vidisSyncStrategy: DeepMocked<VidisSyncStrategy>;
let vidisSyncService: DeepMocked<VidisSyncService>;

Check failure on line 12 in apps/server/src/infra/sync/media-licenses/strategy/vidis-sync.strategy.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'vidisSyncService' is assigned a value but never used

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Factory } from 'fishery';
import { VidisItemResponse } from '@infra/sync/media-licenses/response/vidis-item.response';

export const vidisItemResponseFactory = Factory.define<VidisItemResponse>(({ sequence }) => {
return {
offerId: `${sequence}`,
offerTitle: 'VIDIS Test Response',
offerVersion: 1,
schoolActivations: ['00100', '00200', '00300'],
};
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
describe('MediaSchoolLicenseService', () => {
// TODO: write tests
import { MediaSchoolLicenseService } from '@modules/school-license/service/media-school-license.service';
import { Test, TestingModule } from '@nestjs/testing';
import { MediaSchoolLicenseRepo } from '@modules/school-license/repo';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { SchoolService } from '@modules/school';
import { Logger } from '@src/core/logger';

describe(MediaSchoolLicenseService.name, () => {
let module: TestingModule;
let mediaSchoolLicenseRepo: DeepMocked<MediaSchoolLicenseRepo>;
let schoolService: DeepMocked<SchoolService>;
let logger: DeepMocked<Logger>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
MediaSchoolLicenseService,
{
provide: MediaSchoolLicenseRepo,
useValue: createMock<MediaSchoolLicenseRepo>,
},
{
provide: SchoolService,
useValue: createMock<SchoolService>,
},
{
provide: Logger,
useValue: createMock<Logger>,
},
],
}).compile();

mediaSchoolLicenseRepo = module.get(MediaSchoolLicenseRepo);
schoolService = module.get(SchoolService);
logger = module.get(Logger);
});

afterAll(async () => {
await module.close();
});

afterEach(() => {
jest.clearAllMocks();
});

// TODO: implementation
describe('syncMediaSchoolLicenses', () => {
describe('when a media source and items are given', () => {
const setup = () => {};

it('it should save the school licenses', () => {
setup();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class MediaSchoolLicenseService {
) {}

public async syncMediaSchoolLicenses(mediaSource: MediaSource, items: VidisItemDto[]): Promise<void> {
// TODO: use Promise.all instead of for-of
for (const item of items) {
const schoolNumbers = item.schoolActivations.map((activation) => this.removePrefix(activation));
const schoolActivationsSet = new Set(schoolNumbers);
Expand Down

0 comments on commit 31b4d37

Please sign in to comment.