Skip to content

Commit

Permalink
test: unit test on addOrUpdateDisasterTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisvisser committed Jan 24, 2025
1 parent a588ae4 commit 172064d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export class DisasterController {
public async addOrUpdateCountries(
@Body() disasters: AddDisastersDto,
): Promise<void> {
await this.disasterService.addOrUpdateDisasters(disasters);
await this.disasterService.addOrUpdateDisasterTypes(disasters);
}
}
77 changes: 77 additions & 0 deletions services/API-service/src/api/disaster/disaster.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';

import { beforeEach, describe, expect, it } from '@jest/globals';
import { Repository } from 'typeorm';

import disasters from '../../scripts/json/disasters.json';
import {
LeadTime,
LeadTimeUnit,
} from '../admin-area-dynamic-data/enum/lead-time.enum';
import { DisasterEntity } from '../disaster/disaster.entity';
import { DisasterType } from './disaster-type.enum';
import { DisasterService } from './disaster.service';
import { DisasterDto } from './dto/add-disaster.dto';

describe('DisasterService', () => {
let service: DisasterService;
let disasterRepository: Repository<DisasterEntity>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DisasterService,
{
provide: getRepositoryToken(DisasterEntity),
useClass: Repository,
},
],
}).compile();

service = module.get<DisasterService>(DisasterService);
disasterRepository = module.get<Repository<DisasterEntity>>(
getRepositoryToken(DisasterEntity),
);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('addOrUpdateDisasterTypes', () => {
it('should add or update disasterTypes', async () => {
// Arrange
jest
.spyOn(disasterRepository, 'findOne')
.mockResolvedValue(new DisasterEntity());
jest
.spyOn(disasterRepository, 'save')
.mockResolvedValue(new DisasterEntity());

const disasterDtos = disasters.map((disaster): DisasterDto => {
const disasterDto = new DisasterDto();
disasterDto.disasterType = disaster.disasterType as DisasterType;
disasterDto.label = disaster.label;
disasterDto.triggerUnit = disaster.triggerUnit;
disasterDto.actionsUnit = disaster.actionsUnit;
disasterDto.showOnlyTriggeredAreas = disaster.showOnlyTriggeredAreas;
disasterDto.leadTimeUnit = disaster.leadTimeUnit as LeadTimeUnit;
disasterDto.minLeadTime = disaster.minLeadTime as LeadTime;
disasterDto.maxLeadTime = disaster.maxLeadTime as LeadTime;
return disasterDto;
});

// Act
await service.addOrUpdateDisasterTypes({ disasters: disasterDtos });

// Assert
for (const disaster of disasters) {
expect(disasterRepository.findOne).toHaveBeenCalledWith({
where: { disasterType: disaster.disasterType },
});
expect(disasterRepository.save).toHaveBeenCalled();
}
});
});
});
10 changes: 6 additions & 4 deletions services/API-service/src/api/disaster/disaster.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@ export class DisasterService {
@InjectRepository(DisasterEntity)
private readonly disasterRepository: Repository<DisasterEntity>;

public async addOrUpdateDisasters(disasters: AddDisastersDto): Promise<void> {
public async addOrUpdateDisasterTypes(
disasters: AddDisastersDto,
): Promise<void> {
for await (const disaster of disasters.disasters) {
const existingDisaster = await this.disasterRepository.findOne({
where: {
disasterType: disaster.disasterType,
},
});
if (existingDisaster) {
await this.addOrUpdateDisaster(existingDisaster, disaster);
await this.addOrUpdateDisasterType(existingDisaster, disaster);
continue;
}

const newDisasterType = new DisasterEntity();
newDisasterType.disasterType = disaster.disasterType;
await this.addOrUpdateDisaster(newDisasterType, disaster);
await this.addOrUpdateDisasterType(newDisasterType, disaster);
}
}

private async addOrUpdateDisaster(
private async addOrUpdateDisasterType(
disasterEntity: DisasterEntity,
disaster: DisasterDto,
): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion services/API-service/src/scripts/seed-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class SeedInit implements InterfaceScript {
disasterDto.maxLeadTime = disaster.maxLeadTime as LeadTime;
return disasterDto;
});
await this.disasterService.addOrUpdateDisasters({
await this.disasterService.addOrUpdateDisasterTypes({
disasters: disasterDtos,
});

Expand Down

0 comments on commit 172064d

Please sign in to comment.