-
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.
Fix pg json equality operator error, add tests
- Loading branch information
Showing
7 changed files
with
282 additions
and
4 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
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
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
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,168 @@ | ||
import { | ||
LOCATION_TYPES, | ||
SourcingLocation, | ||
} from 'modules/sourcing-locations/sourcing-location.entity'; | ||
import { | ||
createAdminRegion, | ||
createBusinessUnit, | ||
createIndicator, | ||
createIndicatorRecord, | ||
createMaterial, | ||
createSourcingLocation, | ||
createSourcingRecord, | ||
createSupplier, | ||
createUnit, | ||
} from '../entity-mocks'; | ||
import { | ||
Indicator, | ||
INDICATOR_NAME_CODES, | ||
} from '../../src/modules/indicators/indicator.entity'; | ||
import { SourcingRecord } from '../../src/modules/sourcing-records/sourcing-record.entity'; | ||
import { Material } from '../../src/modules/materials/material.entity'; | ||
|
||
type MockSourcingLocations = { | ||
materials: Material[]; | ||
sourcingLocations: SourcingLocation[]; | ||
sourcingRecords: SourcingRecord[]; | ||
}; | ||
|
||
type MockSourcingLocationsWithIndicators = MockSourcingLocations & { | ||
indicators: Indicator[]; | ||
}; | ||
|
||
export const CreateSourcingLocationsWithImpact = | ||
async (): Promise<MockSourcingLocationsWithIndicators> => { | ||
const parentMaterial = await createMaterial({ | ||
name: 'Parent Material', | ||
}); | ||
const childMaterial = await createMaterial({ | ||
parentId: parentMaterial.id, | ||
name: 'Child Material', | ||
}); | ||
const supplier = await createSupplier(); | ||
const businessUnit = await createBusinessUnit(); | ||
const adminRegion = await createAdminRegion(); | ||
const sourcingLocationParentMaterial = await createSourcingLocation({ | ||
materialId: parentMaterial.id, | ||
producerId: supplier.id, | ||
businessUnitId: businessUnit.id, | ||
adminRegionId: adminRegion.id, | ||
}); | ||
|
||
const sourcingLocationChildMaterial = await createSourcingLocation({ | ||
materialId: childMaterial.id, | ||
producerId: supplier.id, | ||
businessUnitId: businessUnit.id, | ||
adminRegionId: adminRegion.id, | ||
}); | ||
const unit = await createUnit(); | ||
const indicators: Indicator[] = []; | ||
for (const indicator of Object.values(INDICATOR_NAME_CODES)) { | ||
indicators.push( | ||
await createIndicator({ | ||
nameCode: indicator, | ||
name: indicator, | ||
unit, | ||
shortName: indicator, | ||
}), | ||
); | ||
} | ||
const sourcingRecords: SourcingRecord[] = []; | ||
for (const year of [2018, 2019, 2020, 2021, 2022, 2023]) { | ||
sourcingRecords.push( | ||
await createSourcingRecord({ | ||
sourcingLocationId: sourcingLocationParentMaterial.id, | ||
year, | ||
tonnage: 100 * year, | ||
}), | ||
); | ||
sourcingRecords.push( | ||
await createSourcingRecord({ | ||
sourcingLocationId: sourcingLocationChildMaterial.id, | ||
year, | ||
tonnage: 100 * year, | ||
}), | ||
); | ||
} | ||
for (const sourcingRecord of sourcingRecords) { | ||
for (const indicator of indicators) { | ||
await createIndicatorRecord({ | ||
sourcingRecordId: sourcingRecord.id, | ||
indicatorId: indicator.id, | ||
value: sourcingRecord.tonnage * 2, | ||
}); | ||
} | ||
} | ||
return { | ||
materials: [parentMaterial, childMaterial], | ||
indicators, | ||
sourcingRecords, | ||
sourcingLocations: [ | ||
sourcingLocationParentMaterial, | ||
sourcingLocationChildMaterial, | ||
], | ||
}; | ||
}; | ||
|
||
export const CreateEUDRSourcingLocations = | ||
async (): Promise<MockSourcingLocations> => { | ||
const parentMaterial = await createMaterial({ | ||
name: 'EUDR Parent Material', | ||
}); | ||
const childMaterial = await createMaterial({ | ||
parentId: parentMaterial.id, | ||
name: 'EUDR Child Material', | ||
}); | ||
const supplier = await createSupplier(); | ||
const businessUnit = await createBusinessUnit(); | ||
const adminRegion = await createAdminRegion(); | ||
const sourcingLocationParentMaterial = await createSourcingLocation({ | ||
materialId: parentMaterial.id, | ||
producerId: supplier.id, | ||
businessUnitId: businessUnit.id, | ||
adminRegionId: adminRegion.id, | ||
locationType: LOCATION_TYPES.EUDR, | ||
}); | ||
|
||
const sourcingLocationChildMaterial = await createSourcingLocation({ | ||
materialId: childMaterial.id, | ||
producerId: supplier.id, | ||
businessUnitId: businessUnit.id, | ||
adminRegionId: adminRegion.id, | ||
locationType: LOCATION_TYPES.EUDR, | ||
}); | ||
const unit = await createUnit(); | ||
for (const indicator of Object.values(INDICATOR_NAME_CODES)) { | ||
await createIndicator({ | ||
nameCode: indicator, | ||
name: indicator, | ||
unit, | ||
shortName: indicator, | ||
}); | ||
} | ||
const sourcingRecords: SourcingRecord[] = []; | ||
for (const year of [2018, 2019, 2020, 2021, 2022, 2023]) { | ||
sourcingRecords.push( | ||
await createSourcingRecord({ | ||
sourcingLocationId: sourcingLocationParentMaterial.id, | ||
year, | ||
tonnage: 100 * year, | ||
}), | ||
); | ||
sourcingRecords.push( | ||
await createSourcingRecord({ | ||
sourcingLocationId: sourcingLocationChildMaterial.id, | ||
year, | ||
tonnage: 100 * year, | ||
}), | ||
); | ||
} | ||
return { | ||
materials: [parentMaterial, childMaterial], | ||
sourcingLocations: [ | ||
sourcingLocationParentMaterial, | ||
sourcingLocationChildMaterial, | ||
], | ||
sourcingRecords, | ||
}; | ||
}; |
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,63 @@ | ||
import { createGeoRegion, createSourcingLocation } from '../../entity-mocks'; | ||
import { TestApplication } from '../../utils/application-manager'; | ||
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'; | ||
|
||
export const geoRegionFixtures = () => ({ | ||
GivenGeoRegionsOfSourcingLocations: async () => { | ||
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 { | ||
geoRegions: [geoRegion, geoRegion2], | ||
}; | ||
}, | ||
GivenEUDRGeoRegions: async () => { | ||
const geoRegion = await createGeoRegion({ | ||
name: 'EUDR GeoRegion', | ||
}); | ||
const geoRegion2 = await createGeoRegion({ | ||
name: 'EUDR GeoRegion 2', | ||
}); | ||
await createSourcingLocation({ | ||
geoRegionId: geoRegion.id, | ||
locationType: LOCATION_TYPES.EUDR, | ||
}); | ||
await createSourcingLocation({ | ||
geoRegionId: geoRegion2.id, | ||
locationType: LOCATION_TYPES.EUDR, | ||
}); | ||
return { | ||
eudrGeoRegions: [geoRegion, geoRegion2], | ||
}; | ||
}, | ||
WhenIRequestEUDRGeoRegions: async (options: { | ||
app: TestApplication; | ||
jwtToken: string; | ||
}) => { | ||
return request(options.app.getHttpServer()) | ||
.get(`/api/v1/geo-regions/eudr`) | ||
.set('Authorization', `Bearer ${options.jwtToken}`); | ||
}, | ||
ThenIShouldOnlyReceiveEUDRGeoRegions: ( | ||
response: request.Response, | ||
eudrGeoRegions: GeoRegion[], | ||
) => { | ||
expect(response.status).toBe(200); | ||
expect(response.body.data.length).toBe(eudrGeoRegions.length); | ||
for (const geoRegion of eudrGeoRegions) { | ||
expect( | ||
response.body.data.find( | ||
(geoRegionResponse: GeoRegion) => | ||
geoRegionResponse.id === geoRegion.id, | ||
), | ||
).toBeDefined(); | ||
} | ||
}, | ||
}); |
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,42 @@ | ||
import { DataSource } from 'typeorm'; | ||
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 { geoRegionFixtures } from './fixtures'; | ||
|
||
describe('GeoRegions Filters (e2e)', () => { | ||
const fixtures = geoRegionFixtures(); | ||
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 Geo Regions Filters', () => { | ||
it('should only get geo-regions that are part of EUDR data', async () => { | ||
await fixtures.GivenGeoRegionsOfSourcingLocations(); | ||
const { eudrGeoRegions } = await fixtures.GivenEUDRGeoRegions(); | ||
const response = await fixtures.WhenIRequestEUDRGeoRegions({ | ||
app: testApplication, | ||
jwtToken, | ||
}); | ||
fixtures.ThenIShouldOnlyReceiveEUDRGeoRegions(response, eudrGeoRegions); | ||
}); | ||
}); | ||
}); |
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