-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(geoprocessing): cost-surface: integration test
- Loading branch information
Showing
18 changed files
with
341 additions
and
56 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
2 changes: 2 additions & 0 deletions
2
api/apps/geoprocessing/src/modules/surface-cost/adapters/shapefile-converter.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
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
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
10 changes: 9 additions & 1 deletion
10
api/apps/geoprocessing/src/modules/surface-cost/ports/planning-unit-cost.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 |
---|---|---|
@@ -1,4 +1,12 @@ | ||
/** | ||
* when considering Shapefile metadata, properties names are limited to 10 | ||
* characters: | ||
* | ||
* https://support.esri.com/en/technical-article/000022868#:~:text=This%20is%20a%20known%20limitation,character%20limitation%20for%20field%20names | ||
* | ||
* Thus, we shouldn't base on longer key names (like "planningUnitId") | ||
*/ | ||
export interface PlanningUnitCost { | ||
planningUnitId: string; | ||
puId: string; | ||
cost: number; | ||
} |
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
32 changes: 32 additions & 0 deletions
32
...integration/cost-surface/cost-surface-job/process-shapefile-with-cost-surface.e2e-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,32 @@ | ||
import { SurfaceCostProcessor } from '@marxan-geoprocessing/modules/surface-cost/application/surface-cost-processor'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { PromiseType } from 'utility-types'; | ||
import { bootstrapApplication, delay } from '../../../utils'; | ||
import { createWorld } from './steps/world'; | ||
|
||
let app: INestApplication; | ||
let sut: SurfaceCostProcessor; | ||
let world: PromiseType<ReturnType<typeof createWorld>>; | ||
|
||
beforeAll(async () => { | ||
app = await bootstrapApplication(); | ||
world = await createWorld(app); | ||
sut = app.get(SurfaceCostProcessor); | ||
}); | ||
|
||
afterAll(async () => { | ||
await world.cleanup(); | ||
await app?.close(); | ||
}, 500 * 1000); | ||
|
||
describe(`given scenario has some planning units`, () => { | ||
beforeEach(async () => { | ||
await world.GivenPuCostDataExists(); | ||
}); | ||
it(`updates cost surface`, async () => { | ||
await sut.process(world.getShapefileWithCost()); | ||
await delay(1000); | ||
const costs = (await world.GetPuCostsData()).map((pu) => pu.cost); | ||
expect(costs.every((cost) => cost === world.newCost)).toBeTruthy(); | ||
}, 10000); | ||
}); |
76 changes: 76 additions & 0 deletions
76
api/apps/geoprocessing/test/integration/cost-surface/cost-surface-job/steps/world.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,76 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import { convert } from 'geojson2shp'; | ||
|
||
import { INestApplication } from '@nestjs/common'; | ||
import { Feature, Polygon } from 'geojson'; | ||
import { Job } from 'bullmq'; | ||
|
||
import { AppConfig } from '@marxan-geoprocessing/utils/config.utils'; | ||
import { defaultSrid } from '@marxan-geoprocessing/types/spatial-data-format'; | ||
import { PlanningUnitCost } from '@marxan-geoprocessing/modules/surface-cost/ports/planning-unit-cost'; | ||
import { CostSurfaceJobInput } from '@marxan-geoprocessing/modules/surface-cost/cost-surface-job-input'; | ||
|
||
import { getFixtures } from '../../planning-unit-fixtures'; | ||
|
||
export const createWorld = async (app: INestApplication) => { | ||
const newCost = 199.99; | ||
const fixtures = await getFixtures(app); | ||
const shapefile = await getShapefileForPlanningUnits( | ||
fixtures.planningUnitsIds, | ||
newCost, | ||
); | ||
|
||
return { | ||
newCost, | ||
cleanup: async () => { | ||
await fixtures.cleanup(); | ||
}, | ||
GivenPuCostDataExists: fixtures.GivenPuCostDataExists, | ||
GetPuCostsData: () => fixtures.GetPuCostsData(fixtures.scenarioId), | ||
getShapefileWithCost: () => | ||
(({ | ||
data: { | ||
scenarioId: fixtures.scenarioId, | ||
shapefile, | ||
}, | ||
id: 'test-job', | ||
} as unknown) as Job<CostSurfaceJobInput>), | ||
}; | ||
}; | ||
|
||
const getShapefileForPlanningUnits = async ( | ||
ids: string[], | ||
cost: number, | ||
): Promise<CostSurfaceJobInput['shapefile']> => { | ||
const baseDir = AppConfig.get<string>( | ||
'storage.sharedFileStorage.localPath', | ||
) as string; | ||
const fileName = 'shape-with-cost'; | ||
const fileFullPath = `${baseDir}/${fileName}.zip`; | ||
const features: Feature<Polygon, PlanningUnitCost>[] = ids.map((puId) => ({ | ||
type: 'Feature', | ||
bbox: [0, 0, 0, 0, 0, 0], | ||
geometry: { type: 'Polygon', coordinates: [[[0, 0]]] }, | ||
properties: { cost, puId }, | ||
})); | ||
await convert(features, fileFullPath, options); | ||
|
||
return { | ||
filename: fileName, | ||
buffer: {} as any, | ||
mimetype: 'application/zip', | ||
path: fileFullPath, | ||
destination: baseDir, | ||
fieldname: 'attachment', | ||
size: 1, | ||
originalname: `${fileName}.zip`, | ||
stream: {} as any, | ||
encoding: '', | ||
}; | ||
}; | ||
|
||
const options = { | ||
layer: 'my-layer', | ||
targetCrs: defaultSrid, | ||
}; |
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,2 @@ | ||
export const delay = (ms = 1000) => | ||
new Promise((resolve) => setTimeout(resolve, ms)); |
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,2 @@ | ||
export { delay } from './delay'; | ||
export { bootstrapApplication } from './geo-application'; |
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
Oops, something went wrong.