Skip to content

Commit

Permalink
fix unit test and merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorCapCoder committed Nov 13, 2023
1 parent f7d65ac commit dc3bf1d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { ToolPermissionHelper } from '../../common/uc/tool-permission-helper';
import { SchoolExternalTool } from '../../school-external-tool/domain';
import { SchoolExternalToolService } from '../../school-external-tool/service';
import { ContextExternalTool } from '../domain';
import { ContextExternalToolService, ContextExternalToolValidationService } from '../service';
import { ContextExternalToolService } from '../service';
import { ContextExternalToolValidationService } from '../service/context-external-tool-validation.service';
import { ContextExternalToolUc } from './context-external-tool.uc';

describe('ContextExternalToolUc', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import { Test, TestingModule } from '@nestjs/testing';
import { SchoolExternalToolRepo } from '@shared/repo';
import { externalToolFactory } from '@shared/testing/factory/domainobject/tool/external-tool.factory';
import { schoolExternalToolFactory } from '@shared/testing/factory/domainobject/tool/school-external-tool.factory';
import { ApiValidationError } from '@shared/common';
import { ToolConfigurationStatus } from '../../common/enum';
import { ExternalTool } from '../../external-tool/domain';
import { ExternalToolService } from '../../external-tool/service';
import { SchoolExternalTool } from '../domain';
import { SchoolExternalToolService } from './school-external-tool.service';
import { IToolFeatures, ToolFeatures } from '../../tool-config';
import { SchoolExternalToolValidationService } from './school-external-tool-validation.service';

describe('SchoolExternalToolService', () => {
let module: TestingModule;
let service: SchoolExternalToolService;

let schoolExternalToolRepo: DeepMocked<SchoolExternalToolRepo>;
let externalToolService: DeepMocked<ExternalToolService>;
let schoolExternalToolValidationService: DeepMocked<SchoolExternalToolValidationService>;
let toolFearures: DeepMocked<IToolFeatures>;

beforeAll(async () => {
module = await Test.createTestingModule({
Expand All @@ -28,19 +33,32 @@ describe('SchoolExternalToolService', () => {
provide: ExternalToolService,
useValue: createMock<ExternalToolService>(),
},
{
provide: SchoolExternalToolValidationService,
useValue: createMock<SchoolExternalToolValidationService>(),
},
{
provide: ToolFeatures,
useValue: {
toolStatusWithoutVersions: false,
},
},
],
}).compile();

service = module.get(SchoolExternalToolService);
schoolExternalToolRepo = module.get(SchoolExternalToolRepo);
externalToolService = module.get(ExternalToolService);
schoolExternalToolValidationService = module.get(SchoolExternalToolValidationService);
toolFearures = module.get(ToolFeatures);
});

const setup = () => {
const legacySetup = () => {
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build();
const externalTool: ExternalTool = externalToolFactory.buildWithId();

schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
toolFearures.toolStatusWithoutVersions = false;

return {
schoolExternalTool,
Expand All @@ -52,15 +70,15 @@ describe('SchoolExternalToolService', () => {
describe('findSchoolExternalTools', () => {
describe('when called with query', () => {
it('should call repo with query', async () => {
const { schoolExternalTool } = setup();
const { schoolExternalTool } = legacySetup();

await service.findSchoolExternalTools(schoolExternalTool);

expect(schoolExternalToolRepo.find).toHaveBeenCalledWith({ schoolId: schoolExternalTool.schoolId });
});

it('should return schoolExternalTool array', async () => {
const { schoolExternalTool } = setup();
const { schoolExternalTool } = legacySetup();
schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool, schoolExternalTool]);

const result: SchoolExternalTool[] = await service.findSchoolExternalTools(schoolExternalTool);
Expand All @@ -72,7 +90,7 @@ describe('SchoolExternalToolService', () => {

describe('enrichDataFromExternalTool', () => {
it('should call the externalToolService', async () => {
const { schoolExternalTool } = setup();
const { schoolExternalTool } = legacySetup();
schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);

await service.findSchoolExternalTools(schoolExternalTool);
Expand All @@ -81,44 +99,102 @@ describe('SchoolExternalToolService', () => {
});

describe('when determine status', () => {
describe('when external tool version is greater', () => {
it('should return status outdated', async () => {
const { schoolExternalTool, externalTool } = setup();
externalTool.version = 1337;
schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
externalToolService.findById.mockResolvedValue(externalTool);
describe('when FEATURE_COMPUTE_TOOL_STATUS_WITHOUT_VERSIONS_ENABLED is false', () => {
describe('when external tool version is greater', () => {
it('should return status outdated', async () => {
const { schoolExternalTool, externalTool } = legacySetup();
externalTool.version = 1337;
schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
externalToolService.findById.mockResolvedValue(externalTool);

const schoolExternalToolDOs: SchoolExternalTool[] = await service.findSchoolExternalTools(
schoolExternalTool
);

expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.OUTDATED);
});
});

const schoolExternalToolDOs: SchoolExternalTool[] = await service.findSchoolExternalTools(schoolExternalTool);
describe('when external tool version is lower', () => {
it('should return status latest', async () => {
const { schoolExternalTool, externalTool } = legacySetup();
schoolExternalTool.toolVersion = 1;
externalTool.version = 0;
schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
externalToolService.findById.mockResolvedValue(externalTool);

expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.OUTDATED);
const schoolExternalToolDOs: SchoolExternalTool[] = await service.findSchoolExternalTools(
schoolExternalTool
);

expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.LATEST);
});
});

describe('when external tool version is equal', () => {
it('should return status latest', async () => {
const { schoolExternalTool, externalTool } = legacySetup();
schoolExternalTool.toolVersion = 1;
externalTool.version = 1;
schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
externalToolService.findById.mockResolvedValue(externalTool);

const schoolExternalToolDOs: SchoolExternalTool[] = await service.findSchoolExternalTools(
schoolExternalTool
);

expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.LATEST);
});
});
});

describe('when external tool version is lower', () => {
it('should return status latest', async () => {
const { schoolExternalTool, externalTool } = setup();
schoolExternalTool.toolVersion = 1;
externalTool.version = 0;
describe('when FEATURE_COMPUTE_TOOL_STATUS_WITHOUT_VERSIONS_ENABLED is true and validation goes through', () => {
const setup = () => {
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build();
const externalTool: ExternalTool = externalToolFactory.buildWithId();

schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
externalToolService.findById.mockResolvedValue(externalTool);
schoolExternalToolValidationService.validate.mockResolvedValue();
toolFearures.toolStatusWithoutVersions = true;

return {
schoolExternalTool,
};
};

it('should return latest tool status', async () => {
const { schoolExternalTool } = setup();

const schoolExternalToolDOs: SchoolExternalTool[] = await service.findSchoolExternalTools(schoolExternalTool);

expect(schoolExternalToolValidationService.validate).toHaveBeenCalledWith(schoolExternalTool);
expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.LATEST);
});
});

describe('when external tool version is equal', () => {
it('should return status latest', async () => {
const { schoolExternalTool, externalTool } = setup();
schoolExternalTool.toolVersion = 1;
externalTool.version = 1;
describe('when FEATURE_COMPUTE_TOOL_STATUS_WITHOUT_VERSIONS_ENABLED is true and validation throws error', () => {
const setup = () => {
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory.build();
const externalTool: ExternalTool = externalToolFactory.buildWithId();

schoolExternalToolRepo.find.mockResolvedValue([schoolExternalTool]);
externalToolService.findById.mockResolvedValue(externalTool);
schoolExternalToolValidationService.validate.mockRejectedValue(ApiValidationError);
toolFearures.toolStatusWithoutVersions = true;

return {
schoolExternalTool,
};
};

it('should return outdated tool status', async () => {
const { schoolExternalTool } = setup();

const schoolExternalToolDOs: SchoolExternalTool[] = await service.findSchoolExternalTools(schoolExternalTool);

expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.LATEST);
expect(schoolExternalToolValidationService.validate).toHaveBeenCalledWith(schoolExternalTool);
expect(schoolExternalToolDOs[0].status).toEqual(ToolConfigurationStatus.OUTDATED);
});
});
});
Expand All @@ -127,7 +203,7 @@ describe('SchoolExternalToolService', () => {
describe('deleteSchoolExternalToolById', () => {
describe('when schoolExternalToolId is given', () => {
it('should call the schoolExternalToolRepo', async () => {
const { schoolExternalToolId } = setup();
const { schoolExternalToolId } = legacySetup();

await service.deleteSchoolExternalToolById(schoolExternalToolId);

Expand All @@ -139,7 +215,7 @@ describe('SchoolExternalToolService', () => {
describe('findById', () => {
describe('when schoolExternalToolId is given', () => {
it('should call schoolExternalToolRepo.findById', async () => {
const { schoolExternalToolId } = setup();
const { schoolExternalToolId } = legacySetup();

await service.findById(schoolExternalToolId);

Expand All @@ -151,15 +227,15 @@ describe('SchoolExternalToolService', () => {
describe('saveSchoolExternalTool', () => {
describe('when schoolExternalTool is given', () => {
it('should call schoolExternalToolRepo.save', async () => {
const { schoolExternalTool } = setup();
const { schoolExternalTool } = legacySetup();

await service.saveSchoolExternalTool(schoolExternalTool);

expect(schoolExternalToolRepo.save).toHaveBeenCalledWith(schoolExternalTool);
});

it('should enrich data from externalTool', async () => {
const { schoolExternalTool } = setup();
const { schoolExternalTool } = legacySetup();

await service.saveSchoolExternalTool(schoolExternalTool);

Expand Down

0 comments on commit dc3bf1d

Please sign in to comment.