-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
254 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
84 changes: 84 additions & 0 deletions
84
api/test/e2e/admin-regions/admin-regions-eudr-smart-filters.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}, | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters