Skip to content

Commit

Permalink
fix: planning units processor e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
angelhigueraacid committed May 9, 2022
1 parent 2edf71f commit 0db1dd4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type CustomPlanningAreaJob = Required<
>
> & { planningUnitGridShape: RegularPlanningUnitGridShape };

type RegularPlanningAreaJob = Omit<PlanningUnitsJob, 'planningAreaId'> & {
export type RegularPlanningAreaJob = Omit<
PlanningUnitsJob,
'planningAreaId'
> & {
planningUnitGridShape: RegularPlanningUnitGridShape;
};

Expand Down
8 changes: 6 additions & 2 deletions api/apps/geoprocessing/test/e2e.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { PlanningUnitGridShape } from '@marxan/scenarios-planning-unit';

interface OptionsWithCountryCode {
countryCode: string;
adminAreaLevel1Id?: string;
adminAreaLevel2Id?: string;
}

export const E2E_CONFIG: {
Expand All @@ -25,8 +27,10 @@ export const E2E_CONFIG: {
valid: {
customArea: (options: OptionsWithCountryCode): PlanningUnitsJob => ({
countryId: options.countryCode,
adminAreaLevel1Id: faker.random.alphaNumeric(7),
adminAreaLevel2Id: faker.random.alphaNumeric(12),
adminAreaLevel1Id:
options.adminAreaLevel1Id ?? faker.random.alphaNumeric(7),
adminAreaLevel2Id:
options.adminAreaLevel2Id ?? faker.random.alphaNumeric(12),
planningUnitGridShape: PlanningUnitGridShape.Hexagon,
planningUnitAreakm2: 100,
projectId: 'a9d965a2-35ce-44b2-8112-50bcdfe98447',
Expand Down
49 changes: 42 additions & 7 deletions api/apps/geoprocessing/test/planning-units-processor.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import {
PlanningUnitsGeom,
ProjectsPuEntity,
} from '@marxan-jobs/planning-unit-geometry';
import { PlanningUnitsJob } from '@marxan-jobs/planning-unit-geometry/create.regular.planning-units.dto';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { getRepositoryToken, TypeOrmModule } from '@nestjs/typeorm';
import { Job } from 'bullmq';
import { PlanningUnitsJobProcessor } from '../src/modules/planning-units/planning-units.job';
import { In, Repository } from 'typeorm';
import {
PlanningUnitsJobProcessor,
RegularPlanningAreaJob,
} from '../src/modules/planning-units/planning-units.job';
import { E2E_CONFIG } from './e2e.config';

/**
Expand All @@ -12,6 +20,9 @@ import { E2E_CONFIG } from './e2e.config';
*/
describe('planning units jobs (e2e)', () => {
let sut: PlanningUnitsJobProcessor;
let data: PlanningUnitsJob;
let projectsPuRepo: Repository<ProjectsPuEntity>;
let planningUnitsRepo: Repository<PlanningUnitsGeom>;

beforeEach(async () => {
const sandbox = await Test.createTestingModule({
Expand All @@ -21,26 +32,50 @@ describe('planning units jobs (e2e)', () => {
keepConnectionAlive: true,
logging: false,
}),
TypeOrmModule.forFeature(
[ProjectsPuEntity, PlanningUnitsGeom],
geoprocessingConnections.default,
),
],
providers: [PlanningUnitsJobProcessor],
}).compile();

projectsPuRepo = sandbox.get(getRepositoryToken(ProjectsPuEntity));
planningUnitsRepo = sandbox.get(getRepositoryToken(PlanningUnitsGeom));
sut = sandbox.get(PlanningUnitsJobProcessor);
});

afterEach(async () => {
const projectId = data.projectId;

const projectPus = await projectsPuRepo.find({ projectId });
const geometriesIds = projectPus.map((projectPu) => projectPu.geomId);

await planningUnitsRepo.delete({ id: In(geometriesIds) });
});

it(
'executes the child job processor with mock data',
async () => {
data = E2E_CONFIG.planningUnits.creationJob.valid.customArea({
countryCode: 'NAM',
adminAreaLevel1Id: 'NAM.13_1',
adminAreaLevel2Id: 'NAM.13.5_1',
});

const createPlanningUnitsDTO = {
id: '1',
name: 'create-regular-pu',
data: E2E_CONFIG.planningUnits.creationJob.valid.customArea({
countryCode: 'NAM',
}),
} as Job<PlanningUnitsJob>;
data,
} as Job<RegularPlanningAreaJob>;

// TODO do actual verification & cleanup (table: planning_units_geom) after test
await expect(sut.process(createPlanningUnitsDTO)).resolves.not.toThrow();

const projectPus = await projectsPuRepo.find({
projectId: data.projectId,
});

expect(projectPus.length).toBeGreaterThan(0);
},
50 * 1000,
);
Expand Down

0 comments on commit 0db1dd4

Please sign in to comment.