From 8e1d9caae19d521a3f245ca3d1921ade80781d35 Mon Sep 17 00:00:00 2001 From: K Gajowy Date: Mon, 5 Jul 2021 14:05:40 +0200 Subject: [PATCH] spec(marxan-run): baseground for e2e spec of marxan run --- .../marxan-run/execute-marxan-run.e2e-spec.ts | 28 ++++++++ .../business-critical/marxan-run/fixtures.ts | 66 +++++++++++++++++++ api/apps/api/test/steps/given-project.ts | 7 +- .../api/test/steps/given-scenario-exists.ts | 2 + 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 api/apps/api/test/business-critical/marxan-run/execute-marxan-run.e2e-spec.ts create mode 100644 api/apps/api/test/business-critical/marxan-run/fixtures.ts diff --git a/api/apps/api/test/business-critical/marxan-run/execute-marxan-run.e2e-spec.ts b/api/apps/api/test/business-critical/marxan-run/execute-marxan-run.e2e-spec.ts new file mode 100644 index 0000000000..94bded0fa8 --- /dev/null +++ b/api/apps/api/test/business-critical/marxan-run/execute-marxan-run.e2e-spec.ts @@ -0,0 +1,28 @@ +import { PromiseType } from 'utility-types'; +import { getFixtures } from './fixtures'; + +let fixtures: PromiseType>; + +beforeAll(async () => { + fixtures = await getFixtures(); +}); + +afterAll(async () => { + await fixtures.cleanup(); +}); + +describe(`Marxan run`, () => { + beforeAll(async () => { + await fixtures.GivenUserIsLoggedIn(); + await fixtures.GivenProjectOrganizationExists(); + await fixtures.GivenScenarioExists(`Mouse`); + await fixtures.GivenCostSurfaceTemplateFilled(); + await fixtures.WhenMarxanExecutionIsRequested(); + await fixtures.WhenMarxanExecutionIsCompleted(); + await fixtures.ThenResultsAreAvailable(); + }); + + it(`should work in near future`, () => { + expect(true).toBeTruthy(); + }); +}); diff --git a/api/apps/api/test/business-critical/marxan-run/fixtures.ts b/api/apps/api/test/business-critical/marxan-run/fixtures.ts new file mode 100644 index 0000000000..f1ab8ef387 --- /dev/null +++ b/api/apps/api/test/business-critical/marxan-run/fixtures.ts @@ -0,0 +1,66 @@ +import { INestApplication } from '@nestjs/common'; +import * as request from 'supertest'; +import { bootstrapApplication } from '../../utils/api-application'; +import { GivenUserIsLoggedIn } from '../../steps/given-user-is-logged-in'; +import { GivenProjectExists } from '../../steps/given-project'; +import { GivenScenarioExists } from '../../steps/given-scenario-exists'; +import { ScenarioType } from '@marxan-api/modules/scenarios/scenario.api.entity'; +import { ScenariosTestUtils } from '../../utils/scenarios.test.utils'; + +export const getFixtures = async () => { + const app: INestApplication = await bootstrapApplication(); + const cleanups: (() => Promise)[] = []; + + let authToken: string; + let project: string; + let scenario: string; + + return { + GivenUserIsLoggedIn: async () => { + authToken = await GivenUserIsLoggedIn(app); + }, + GivenProjectOrganizationExists: async () => { + const organizationProject = await GivenProjectExists(app, authToken, { + countryCode: 'AGO', + adminAreaLevel1Id: 'AGO.15_1', + adminAreaLevel2Id: 'AGO.15.4_1', + }); + + project = organizationProject.projectId; + cleanups.push(organizationProject.cleanup); + }, + GivenScenarioExists: async (name: string) => { + scenario = ( + await GivenScenarioExists(app, project, authToken, { + name, + type: ScenarioType.marxan, + }) + ).id; + cleanups.push(() => + ScenariosTestUtils.deleteScenario(app, authToken, scenario), + ); + }, + GivenCostSurfaceTemplateFilled: async () => { + const template = await request(app.getHttpServer()) + .get(`/api/v1/scenarios/${scenario}/cost-surface/shapefile-template`) + .set('Authorization', `Bearer ${authToken}`); + console.log(template.body); + }, + WhenMarxanExecutionIsRequested: async () => { + // TODO currently not implemented yet: 501 + await request(app.getHttpServer()) + .post(`/api/v1/scenarios/${scenario}/marxan`) + .set('Authorization', `Bearer ${authToken}`); + }, + WhenMarxanExecutionIsCompleted: async () => { + return void 0; + }, + ThenResultsAreAvailable: async () => { + return void 0; + }, + cleanup: async () => { + await Promise.all(cleanups.map((c) => c())); + await app.close(); + }, + }; +}; diff --git a/api/apps/api/test/steps/given-project.ts b/api/apps/api/test/steps/given-project.ts index 9801f44a4d..0e9f60f348 100644 --- a/api/apps/api/test/steps/given-project.ts +++ b/api/apps/api/test/steps/given-project.ts @@ -6,6 +6,11 @@ import { E2E_CONFIG } from '../e2e.config'; export const GivenProjectExists = async ( app: INestApplication, jwt: string, + adminArea?: { + countryCode?: string; + adminAreaLevel1Id?: string; + adminAreaLevel2Id?: string; + }, ): Promise<{ projectId: string; organizationId: string; @@ -20,7 +25,7 @@ export const GivenProjectExists = async ( ).data.id; const projectId = ( await ProjectsTestUtils.createProject(app, jwt, { - ...E2E_CONFIG.projects.valid.minimal(), + ...E2E_CONFIG.projects.valid.minimalInGivenAdminArea(adminArea), organizationId, }) ).data.id; diff --git a/api/apps/api/test/steps/given-scenario-exists.ts b/api/apps/api/test/steps/given-scenario-exists.ts index 7ee27c3b4a..644511d6b9 100644 --- a/api/apps/api/test/steps/given-scenario-exists.ts +++ b/api/apps/api/test/steps/given-scenario-exists.ts @@ -7,9 +7,11 @@ export const GivenScenarioExists = async ( app: INestApplication, projectId: string, jwtToken: string, + options?: Partial, ) => { const createScenarioDTO: Partial = { ...E2E_CONFIG.scenarios.valid.minimal(), + ...options, projectId, }; return (