Skip to content

Commit

Permalink
eudr admin region filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Feb 29, 2024
1 parent e5a0050 commit 6867a97
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 2 deletions.
21 changes: 21 additions & 0 deletions api/test/common-steps/and-associated-materials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Material } from 'modules/materials/material.entity';
import { SourcingLocation } from '../../src/modules/sourcing-locations/sourcing-location.entity';

/**
* @description Associate materials with sourcing locations for tests
*/

export const AndAssociatedMaterials = async (
materials: Material[],
existingSourcingLocations: SourcingLocation[],
): Promise<SourcingLocation[]> => {
const limitLength = Math.min(
materials.length,
existingSourcingLocations.length,
);
for (let i = 0; i < limitLength; i++) {
existingSourcingLocations[i].materialId = materials[i].id;
await existingSourcingLocations[i].save();
}
return existingSourcingLocations;
};
30 changes: 30 additions & 0 deletions api/test/common-steps/and-associated-suppliers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SourcingLocation } from '../../src/modules/sourcing-locations/sourcing-location.entity';
import {
Supplier,
SUPPLIER_TYPES,
} from '../../src/modules/suppliers/supplier.entity';

/**
* @description Associate suppliers with sourcing locations for tests
*/

export const AndAssociatedSuppliers = async (
supplier: Supplier[],
existingSourcingLocations: SourcingLocation[],
supplierType?: SUPPLIER_TYPES,
): Promise<SourcingLocation[]> => {
const limitLength = Math.min(
supplier.length,
existingSourcingLocations.length,
);
for (let i = 0; i < limitLength; i++) {
if (supplierType === SUPPLIER_TYPES.PRODUCER || !supplierType) {
existingSourcingLocations[i].producerId = supplier[i].id;
}
if (supplierType === SUPPLIER_TYPES.T1SUPPLIER) {
existingSourcingLocations[i].t1SupplierId = supplier[i].id;
}
await existingSourcingLocations[i].save();
}
return existingSourcingLocations;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { DataSource } from 'typeorm';
import { createMaterial, createSupplier } from '../../entity-mocks';
import ApplicationManager from '../../utils/application-manager';
import { TestApplication } from '../../utils/application-manager';
import { clearTestDataFromDatabase } from '../../utils/database-test-helper';
import { setupTestUser } from '../../utils/userAuth';
import { adminRegionsFixtures } from './fixtures';

describe('GeoRegions Filters (e2e)', () => {
const fixtures = adminRegionsFixtures();
let testApplication: TestApplication;
let jwtToken: string;
let dataSource: DataSource;

beforeAll(async () => {
testApplication = await ApplicationManager.init();

dataSource = testApplication.get<DataSource>(DataSource);
});
beforeEach(async () => {
({ jwtToken } = await setupTestUser(testApplication));
});

afterEach(async () => {
await clearTestDataFromDatabase(dataSource);
});

afterAll(async () => {
await testApplication.close();
});
describe('EUDR Admin Regions Filters', () => {
it('should only get geo-regions that are part of EUDR data', async () => {
await fixtures.GivenAdminRegionsOfSourcingLocations();
const { eudrAdminRegions } = await fixtures.GivenEUDRAdminRegions();
const response = await fixtures.WhenIRequestEUDRAdminRegions({
app: testApplication,
jwtToken,
});
fixtures.ThenIShouldOnlyReceiveEUDRAdminRegions(
response,
eudrAdminRegions,
);
});
it('should only get geo-regions that are part of EUDR data and are filtered', async () => {
const { sourcingLocations } =
await fixtures.GivenAdminRegionsOfSourcingLocations();
const regularMaterial = await createMaterial({
name: 'Regular Material',
});
await fixtures.AndAssociatedMaterials(
[regularMaterial],
sourcingLocations,
);
const regularSupplier = await createSupplier({
name: 'Regular Supplier',
});
await fixtures.AndAssociatedSuppliers(
[regularSupplier],
sourcingLocations,
);
const { eudrAdminRegions, eudrSourcingLocations } =
await fixtures.GivenEUDRAdminRegions();
const eudrMaterial = await createMaterial({ name: 'EUDR Material' });
await fixtures.AndAssociatedMaterials(
[eudrMaterial],
[eudrSourcingLocations[0]],
);
const eudrSupplier = await createSupplier({ name: 'EUDR Supplier' });
await fixtures.AndAssociatedSuppliers(
[eudrSupplier],
eudrSourcingLocations,
);
const response = await fixtures.WhenIRequestEUDRAdminRegionWithFilters({
app: testApplication,
jwtToken,
materialIds: [eudrMaterial.id],
supplierIds: [eudrSupplier.id],
});
fixtures.ThenIShouldOnlyReceiveFilteredEUDRAdminRegions(response, [
eudrAdminRegions[0],
]);
});
});
});
117 changes: 117 additions & 0 deletions api/test/e2e/admin-regions/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import * as request from 'supertest';
import {
LOCATION_TYPES,
SourcingLocation,
} from 'modules/sourcing-locations/sourcing-location.entity';
import { createAdminRegion, createSourcingLocation } from '../../entity-mocks';
import { TestApplication } from '../../utils/application-manager';
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 { Material } from 'modules/materials/material.entity';
import { Supplier } from '../../../src/modules/suppliers/supplier.entity';

export const adminRegionsFixtures = () => ({
GivenAdminRegionsOfSourcingLocations: async () => {
const adminRegion = await createAdminRegion({
name: 'Regular AdminRegion',
});
const adminRegion2 = await createAdminRegion({
name: 'Regular AdminRegion 2',
});
const sourcingLocation1 = await createSourcingLocation({
adminRegionId: adminRegion.id,
});
const sourcingLocation2 = await createSourcingLocation({
adminRegionId: adminRegion2.id,
});
return {
adminRegions: [adminRegion, adminRegion2],
sourcingLocations: [sourcingLocation1, sourcingLocation2],
};
},
AndAssociatedMaterials: async (
materials: Material[],
sourcingLocations: SourcingLocation[],
) => {
return AndAssociatedMaterials(materials, sourcingLocations);
},
AndAssociatedSuppliers: async (
suppliers: Supplier[],
sourcingLocations: SourcingLocation[],
) => {
return AndAssociatedSuppliers(suppliers, sourcingLocations);
},
GivenEUDRAdminRegions: async () => {
const adminRegion = await createAdminRegion({
name: 'EUDR AdminRegion',
});
const adminRegion2 = await createAdminRegion({
name: 'EUDR AdminRegion 2',
});
const eudrSourcingLocation1 = await createSourcingLocation({
adminRegionId: adminRegion.id,
locationType: LOCATION_TYPES.EUDR,
});
const eudrSourcingLocation2 = await createSourcingLocation({
adminRegionId: adminRegion2.id,
locationType: LOCATION_TYPES.EUDR,
});
return {
eudrAdminRegions: [adminRegion, adminRegion2],
eudrSourcingLocations: [eudrSourcingLocation1, eudrSourcingLocation2],
};
},
WhenIRequestEUDRAdminRegions: async (options: {
app: TestApplication;
jwtToken: string;
}) => {
return request(options.app.getHttpServer())
.get(`/api/v1/admin-regions/trees/eudr`)
.set('Authorization', `Bearer ${options.jwtToken}`);
},
WhenIRequestEUDRAdminRegionWithFilters: async (options: {
app: TestApplication;
jwtToken: string;
supplierIds?: string[];
materialIds?: string[];
}) => {
return request(options.app.getHttpServer())
.get(`/api/v1/admin-regions/trees/eudr`)
.set('Authorization', `Bearer ${options.jwtToken}`)
.query({
'producerIds[]': options.supplierIds,
'materialIds[]': options.materialIds,
});
},
ThenIShouldOnlyReceiveFilteredEUDRAdminRegions: (
response: request.Response,
eudrAdminRegions: AdminRegion[],
) => {
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(eudrAdminRegions.length);
for (const adminRegion of eudrAdminRegions) {
expect(
response.body.data.find(
(adminRegionResponse: AdminRegion) =>
adminRegionResponse.id === adminRegion.id,
),
).toBeDefined();
}
},
ThenIShouldOnlyReceiveEUDRAdminRegions: (
response: request.Response,
eudrAdminRegions: AdminRegion[],
) => {
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(eudrAdminRegions.length);
for (const adminRegion of eudrAdminRegions) {
expect(
response.body.data.find(
(adminRegionResponse: AdminRegion) =>
adminRegionResponse.id === adminRegion.id,
),
).toBeDefined();
}
},
});
4 changes: 2 additions & 2 deletions api/test/e2e/impact/impact-reports/impact-reports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Impact Reports', () => {
comparedScenarioId: scenarioIntervention.scenarioId,
});

await fixtures.ThenIShouldGetAnImpactReportAboutProvidedFilters(response, {
fixtures.ThenIShouldGetAnImpactReportAboutProvidedFilters(response, {
indicators: [indicator],
isActualVsScenario: true,
});
Expand All @@ -69,7 +69,7 @@ describe('Impact Reports', () => {
indicatorIds: [indicator.id],
},
);
await fixtures.ThenIShouldGetAnImpactReportAboutProvidedFilters(response, {
fixtures.ThenIShouldGetAnImpactReportAboutProvidedFilters(response, {
indicators: [indicator],
isScenarioVsScenario: true,
});
Expand Down

0 comments on commit 6867a97

Please sign in to comment.