Skip to content

Commit

Permalink
clean-up testing flow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 7, 2024
1 parent d3850a0 commit bc53ccb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 129 deletions.
21 changes: 0 additions & 21 deletions api/test/common-steps/and-associated-materials.ts

This file was deleted.

30 changes: 0 additions & 30 deletions api/test/common-steps/and-associated-suppliers.ts

This file was deleted.

18 changes: 0 additions & 18 deletions api/test/common-steps/given-geo-region.ts

This file was deleted.

55 changes: 36 additions & 19 deletions api/test/e2e/eudr/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import {
LOCATION_TYPES,
SourcingLocation,
} from 'modules/sourcing-locations/sourcing-location.entity';
import { createAdminRegion, createSourcingLocation } from '../../entity-mocks';
import {
createAdminRegion,
createGeoRegion,
createMaterial,
createSourcingLocation,
createSupplier,
} from '../../entity-mocks';
import { AdminRegion } from 'modules/admin-regions/admin-region.entity';
import { AndAssociatedMaterials } from '../../common-steps/and-associated-materials';
import { AndAssociatedSuppliers } from '../../common-steps/and-associated-suppliers';
import { TestManager } from '../../utils/test-manager';
import { GeoRegion } from '../../../src/modules/geo-regions/geo-region.entity';
import { Material } from 'modules/materials/material.entity';
import { generateRandomName } from '../../utils/generate-random-name';
import { Supplier } from '../../../src/modules/suppliers/supplier.entity';

export class EUDRTestManager extends TestManager {
url = '/api/v1/eudr';
Expand Down Expand Up @@ -38,17 +45,29 @@ export class EUDRTestManager extends TestManager {
sourcingLocations: SourcingLocation[],
materialNames?: string[],
) => {
const materials = await this.createMaterials(materialNames);
await AndAssociatedMaterials(materials, sourcingLocations);
return materials;
const materials: Material[] = [];
for (const name of materialNames || [generateRandomName()]) {
materials.push(await createMaterial({ name }));
}
const limitLength = Math.min(materials.length, sourcingLocations.length);
for (let i = 0; i < limitLength; i++) {
sourcingLocations[i].materialId = materials[i].id;
await sourcingLocations[i].save();
}
};
AndAssociatedSuppliers = async (
sourcingLocations: SourcingLocation[],
supplierNames?: string[],
) => {
const suppliers = await this.createSuppliers(supplierNames);
await AndAssociatedSuppliers(suppliers, sourcingLocations);
return suppliers;
const suppliers: Supplier[] = [];
for (const name of supplierNames || [generateRandomName()]) {
suppliers.push(await createSupplier({ name }));
}
const limitLength = Math.min(suppliers.length, sourcingLocations.length);
for (let i = 0; i < limitLength; i++) {
sourcingLocations[i].materialId = suppliers[i].id;
await sourcingLocations[i].save();
}
};
GivenEUDRAdminRegions = async () => {
const adminRegion = await createAdminRegion({
Expand Down Expand Up @@ -80,8 +99,8 @@ export class EUDRTestManager extends TestManager {
ThenIShouldOnlyReceiveCorrespondingAdminRegions = (
eudrAdminRegions: AdminRegion[],
) => {
expect(this.response!.status).toBe(200);
expect(this.response!.body.data.length).toBe(eudrAdminRegions.length);
expect(this.response?.status).toBe(200);
expect(this.response?.body.data.length).toBe(eudrAdminRegions.length);
for (const adminRegion of eudrAdminRegions) {
expect(
this.response!.body.data.find(
Expand All @@ -93,10 +112,9 @@ export class EUDRTestManager extends TestManager {
};

GivenGeoRegionsOfSourcingLocations = async () => {
const [geoRegion, geoRegion2] = await this.createGeoRegions([
'Regular GeoRegion',
'Regular GeoRegion 2',
]);
const geoRegion = await createGeoRegion({ name: 'Regular GeoRegion' });
const geoRegion2 = await createGeoRegion({ name: 'Regular GeoRegion 2' });

await createSourcingLocation({ geoRegionId: geoRegion.id });
await createSourcingLocation({ geoRegionId: geoRegion2.id });
return {
Expand All @@ -105,10 +123,9 @@ export class EUDRTestManager extends TestManager {
};

GivenEUDRGeoRegions = async () => {
const [geoRegion, geoRegion2] = await this.createGeoRegions([
'EUDR GeoRegion',
'EUDR GeoRegion 2',
]);
const geoRegion = await createGeoRegion({ name: 'EUDR GeoRegion' });
const geoRegion2 = await createGeoRegion({ name: 'EUDR GeoRegion 2' });

await createSourcingLocation({
geoRegionId: geoRegion.id,
locationType: LOCATION_TYPES.EUDR,
Expand Down
21 changes: 14 additions & 7 deletions api/test/e2e/geo-regions/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { createSourcingLocation } from '../../entity-mocks';
import { createGeoRegion, createSourcingLocation } from '../../entity-mocks';
import * as request from 'supertest';
import { GeoRegion } from '../../../src/modules/geo-regions/geo-region.entity';
import { LOCATION_TYPES } from '../../../src/modules/sourcing-locations/sourcing-location.entity';
import { TestManager } from '../../utils/test-manager';
import { Feature, Geometry } from 'geojson';
import { GivenGeoRegionWithGeometry } from '../../common-steps/given-geo-region';
import { Feature } from 'geojson';

export class GeoRegionsTestManager extends TestManager {
constructor(manager: TestManager) {
super(manager.testApp, manager.jwtToken, manager.dataSource);
}

GivenRegularSourcingLocationsWithGeoRegions = async () => {
const geoRegion = await GivenGeoRegionWithGeometry();
const geoRegion2 = await GivenGeoRegionWithGeometry();
const geoRegion = await createGeoRegion({
name: this.generateRandomName(),
});
const geoRegion2 = await createGeoRegion({
name: this.generateRandomName(),
});
const sourcingLocation1 = await createSourcingLocation({
geoRegionId: geoRegion.id,
locationType: LOCATION_TYPES.ADMINISTRATIVE_REGION_OF_PRODUCTION,
Expand All @@ -29,8 +32,12 @@ export class GeoRegionsTestManager extends TestManager {
};

GivenEUDRSourcingLocationsWithGeoRegions = async () => {
const geoRegion = await GivenGeoRegionWithGeometry();
const geoRegion2 = await GivenGeoRegionWithGeometry();
const geoRegion = await createGeoRegion({
name: this.generateRandomName(),
});
const geoRegion2 = await createGeoRegion({
name: this.generateRandomName(),
});
const sourcingLocation1 = await createSourcingLocation({
geoRegionId: geoRegion.id,
locationType: LOCATION_TYPES.EUDR,
Expand Down
7 changes: 6 additions & 1 deletion api/test/entity-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import { User } from '../src/modules/users/user.entity';
import { faker } from '@faker-js/faker';
import { genSalt, hash } from 'bcrypt';
import { v4 as uuidv4 } from 'uuid';
import { generateRandomName } from './utils/generate-random-name';

const wkt = require('wellknown');

async function createAdminRegion(
additionalData: Partial<AdminRegion> = {},
Expand Down Expand Up @@ -286,6 +287,9 @@ async function createMaterial(
async function createGeoRegion(
additionalData: Partial<GeoRegion> = {},
): Promise<GeoRegion> {
const geomstring =
'MULTIPOLYGON (((-74.94038756755259 -8.789917036555973, -74.9404292564128 -8.78987345274758, -74.94037947090003 -8.789320280383404, -74.94007769168027 -8.78832128710426, -74.94014586742746 -8.788218619928239, -74.94016386858105 -8.78780352397087, -74.94010104117281 -8.787799975977926, -74.94010288471793 -8.78778799293464, -74.9395829026956 -8.787693953632717, -74.93871995380748 -8.787450557792479, -74.93826369227841 -8.787672292367361, -74.93588217957925 -8.78758885088354, -74.93581959017885 -8.787667135197673, -74.9357715871026 -8.788277919312772, -74.93494257105218 -8.78845185941805, -74.93498553672873 -8.788177110353425, -74.93374345713029 -8.788556626292976, -74.9326813890679 -8.788562556226449, -74.93185593961496 -8.788775235806332, -74.93064363729047 -8.789016035583105, -74.92945984843114 -8.789701969314692, -74.92933888309716 -8.79004576552706, -74.92900915373343 -8.790418620740047, -74.92907515796331 -8.791877374183946, -74.92920716642304 -8.792695696771696, -74.92975920180011 -8.793276823308746, -74.9315893190828 -8.793092997666013, -74.93331142944389 -8.792891382340077, -74.93551357056765 -8.792594889014318, -74.93895179090532 -8.79218572783506, -74.9395578297432 -8.79241699290495, -74.9406559001128 -8.79224502658469, -74.94093191780134 -8.79210863944635, -74.94088391472505 -8.79196039249988, -74.94060189665201 -8.791729127144945, -74.94034388011707 -8.791438562776397, -74.94015186781199 -8.791088699254734, -74.94014586742746 -8.790721045706444, -74.94026587511813 -8.790288162835186, -74.9404098843469 -8.78997980760475, -74.94038756755259 -8.789917036555973), (-74.9373868084098 -8.7894253831326, -74.9374531890935 -8.789707501038329, -74.93732595944972 -8.789840262405733, -74.93722085670055 -8.789856857576657, -74.93708256360951 -8.789541549329078, -74.9373868084098 -8.7894253831326)))';

const defaultData: DeepPartial<GeoRegion> = {
h3Compact: [
'8667737afffffff',
Expand All @@ -307,6 +311,7 @@ async function createGeoRegion(
],
h3FlatLength: 7,
name: 'ABC',
theGeom: wkt.parse(geomstring),
};

const geoRegion = GeoRegion.merge(
Expand Down
35 changes: 2 additions & 33 deletions api/test/utils/test-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import ApplicationManager, { TestApplication } from './application-manager';
import { clearTestDataFromDatabase } from './database-test-helper';
import { setupTestUser } from './userAuth';
import * as request from 'supertest';
import {
createGeoRegion,
createMaterial,
createSupplier,
} from '../entity-mocks';
import { Material } from 'modules/materials/material.entity';
import { Supplier } from 'modules/suppliers/supplier.entity';
import { GeoRegion } from 'modules/geo-regions/geo-region.entity';
Expand Down Expand Up @@ -71,33 +66,7 @@ export class TestManager {
await this.testApp.close();
}

async createMaterials(names?: string[]) {
const namesToCreate = names || [generateRandomName()];
const createdMaterials: Material[] = [];
for (let i = 0; i < namesToCreate.length; i++) {
createdMaterials.push(await createMaterial({ name: namesToCreate[i] }));
}
this.materials?.push(...createdMaterials);
return createdMaterials;
}

async createSuppliers(names?: string[]) {
const namesToCreate = names || [generateRandomName()];
const createdSuppliers: Supplier[] = [];
for (let i = 0; i < namesToCreate.length; i++) {
createdSuppliers.push(await createSupplier({ name: namesToCreate[i] }));
}
this.suppliers?.push(...createdSuppliers);
return createdSuppliers;
}

async createGeoRegions(names?: string[]) {
const namesToCreate = names || [generateRandomName()];
const createdGeoRegions: GeoRegion[] = [];
for (let i = 0; i < namesToCreate.length; i++) {
createdGeoRegions.push(await createGeoRegion({ name: namesToCreate[i] }));
}
this.geoRegions?.push(...createdGeoRegions);
return createdGeoRegions;
generateRandomName(): string {
return generateRandomName();
}
}

0 comments on commit bc53ccb

Please sign in to comment.