From 8f10f86cb6fe1b7c979a98b1a1994a3ac24d6d0a Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 12 Sep 2023 15:13:01 +0200 Subject: [PATCH] feat(CostSurface): More test fixes for the CostSurface to Scenario relationship --- ...AddScenarioToCostSurfaceAPIRelationship.ts | 0 ...tart-legacy-project-import.handler.spec.ts | 19 +++++++++++++- api/apps/api/test/fixtures/test-data.sql | 18 +++++++++---- .../test/integration/cloning/fixtures.ts | 26 +++++++++++++++---- .../legacy-project-import/fixtures.ts | 20 +++++++++++++- .../marxan-run/marxan-run.e2e-spec.ts | 10 +++++-- 6 files changed, 79 insertions(+), 14 deletions(-) rename api/apps/api/src/migrations/api/{api/apps/api/src/migrations/api => }/1694192071502-AddScenarioToCostSurfaceAPIRelationship.ts (100%) diff --git a/api/apps/api/src/migrations/api/api/apps/api/src/migrations/api/1694192071502-AddScenarioToCostSurfaceAPIRelationship.ts b/api/apps/api/src/migrations/api/1694192071502-AddScenarioToCostSurfaceAPIRelationship.ts similarity index 100% rename from api/apps/api/src/migrations/api/api/apps/api/src/migrations/api/1694192071502-AddScenarioToCostSurfaceAPIRelationship.ts rename to api/apps/api/src/migrations/api/1694192071502-AddScenarioToCostSurfaceAPIRelationship.ts diff --git a/api/apps/api/src/modules/legacy-project-import/application/start-legacy-project-import.handler.spec.ts b/api/apps/api/src/modules/legacy-project-import/application/start-legacy-project-import.handler.spec.ts index 91ea398ebb..cf59269529 100644 --- a/api/apps/api/src/modules/legacy-project-import/application/start-legacy-project-import.handler.spec.ts +++ b/api/apps/api/src/modules/legacy-project-import/application/start-legacy-project-import.handler.spec.ts @@ -9,7 +9,7 @@ import { FixtureType } from '@marxan/utils/tests/fixture-type'; import { CqrsModule } from '@nestjs/cqrs'; import { Test } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; -import { isLeft, isRight, Right } from 'fp-ts/Either'; +import { isLeft, isRight } from 'fp-ts/Either'; import { DeepPartial } from 'typeorm'; import { v4 } from 'uuid'; @@ -27,6 +27,7 @@ import { StartLegacyProjectImportResponse, } from './start-legacy-project-import.command'; import { StartLegacyProjectImportHandler } from './start-legacy-project-import.handler'; +import { CostSurfaceService } from '@marxan-api/modules/cost-surface/cost-surface.service'; let fixtures: FixtureType; @@ -68,6 +69,18 @@ const getFixtures = async () => { const saveProjectMock = jest.fn(); const saveScenarioMock = jest.fn(); const findRandomOrganizationMock = jest.fn(); + const createDefaultCostSurfaceForProject = jest.fn( + (projectId: string, name: string) => { + return { + id: v4(), + projectId, + name: `${name} Default Cost Surface`, + min: 0, + max: 0, + isDefault: true, + }; + }, + ); const sandbox = await Test.createTestingModule({ imports: [CqrsModule], @@ -84,6 +97,10 @@ const getFixtures = async () => { provide: getRepositoryToken(Scenario), useValue: { save: saveScenarioMock }, }, + { + provide: CostSurfaceService, + useValue: { createDefaultCostSurfaceForProject }, + }, { provide: getRepositoryToken(Organization), useValue: { find: findRandomOrganizationMock }, diff --git a/api/apps/api/test/fixtures/test-data.sql b/api/apps/api/test/fixtures/test-data.sql index 713b8d2220..7b25b6c4dd 100644 --- a/api/apps/api/test/fixtures/test-data.sql +++ b/api/apps/api/test/fixtures/test-data.sql @@ -11,6 +11,14 @@ VALUES ('Example Project 1 Org 2', (SELECT id FROM organizations WHERE name = 'Example Org 1'), (SELECT id FROM users WHERE email = 'aa@example.com'), 'Lorem Ipsum', '[23.719482421875, 21.654052734375, -18.802318121688117, -20.756113874762068]'::jsonb), ('Example Project 2 Org 2', (SELECT id FROM organizations WHERE name = 'Example Org 1'), (SELECT id FROM users WHERE email = 'aa@example.com'), 'Lorem Ipsum', '[23.719482421875, 21.654052734375, -18.802318121688117, -20.756113874762068]'::jsonb); +INSERT INTO cost_surfaces +(name, project_id, is_default, min, max) +VALUES +('Example Project 1 Org 1 Default Cost Surface', (SELECT id FROM projects WHERE name = 'Example Project 1 Org 1'), true, 0, 0), +('Example Project 2 Org 1 Default Cost Surface', (SELECT id FROM projects WHERE name = 'Example Project 2 Org 1'), true, 0, 0), +('Example Project 1 Org 2 Default Cost Surface', (SELECT id FROM projects WHERE name = 'Example Project 1 Org 2'), true, 0, 0), +('Example Project 2 Org 2 Default Cost Surface', (SELECT id FROM projects WHERE name = 'Example Project 2 Org 2'), true, 0, 0); + INSERT INTO users_organizations (user_id, organization_id, role_id) VALUES @@ -34,12 +42,12 @@ VALUES ((SELECT id FROM users WHERE lower(email) = 'dd@example.com'), (SELECT id FROM projects WHERE name = 'Example Project 2 Org 2'), 'project_owner'); INSERT INTO scenarios -(name, project_id, type, wdpa_threshold, number_of_runs, blm, created_by) +(name, project_id, cost_surface_id, type, wdpa_threshold, number_of_runs, blm, created_by) VALUES -('Example scenario 1 Project 1 Org 1', (select id from projects where name = 'Example Project 1 Org 1'), 'marxan', 30, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ), -('Example scenario 2 Project 1 Org 1', (select id from projects where name = 'Example Project 1 Org 1'), 'marxan', 50, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ), -('Example scenario 1 Project 2 Org 2', (select id from projects where name = 'Example Project 2 Org 2'), 'marxan', 30, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ), -('Example scenario 2 Project 2 Org 2', (select id from projects where name = 'Example Project 2 Org 2'), 'marxan', 50, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ); +('Example scenario 1 Project 1 Org 1', (select id from projects where name = 'Example Project 1 Org 1'), (select id from cost_surfaces where name = 'Example Project 1 Org 1 Default Cost Surface'), 'marxan', 30, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ), +('Example scenario 2 Project 1 Org 1', (select id from projects where name = 'Example Project 1 Org 1'), (select id from cost_surfaces where name = 'Example Project 1 Org 1 Default Cost Surface'), 'marxan', 50, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ), +('Example scenario 1 Project 2 Org 2', (select id from projects where name = 'Example Project 2 Org 2'), (select id from cost_surfaces where name = 'Example Project 2 Org 2 Default Cost Surface'), 'marxan', 30, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ), +('Example scenario 2 Project 2 Org 2', (select id from projects where name = 'Example Project 2 Org 2'), (select id from cost_surfaces where name = 'Example Project 2 Org 2 Default Cost Surface'), 'marxan', 50, 100, 1, (SELECT id FROM users WHERE email = 'aa@example.com') ); INSERT INTO platform_admins (user_id) diff --git a/api/apps/geoprocessing/test/integration/cloning/fixtures.ts b/api/apps/geoprocessing/test/integration/cloning/fixtures.ts index da7b6bac84..c39eacb759 100644 --- a/api/apps/geoprocessing/test/integration/cloning/fixtures.ts +++ b/api/apps/geoprocessing/test/integration/cloning/fixtures.ts @@ -22,7 +22,6 @@ import { Readable, Transform } from 'stream'; import { DeepPartial, EntityManager, In } from 'typeorm'; import { v4 } from 'uuid'; import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'; -import { CostSurfaceService } from '@marxan-api/modules/cost-surface/cost-surface.service'; export type TestSpecification = { id: string; @@ -102,11 +101,11 @@ export async function GivenProjectExists( projectId: string, organizationId: string, projectData: Record = {}, + costSurfaceId = v4(), ) { await GivenOrganizationExists(em, organizationId); - await GivenDefaultCostSurfaceForProject(em, projectId); - return em + const insertResult = await em .createQueryBuilder() .insert() .into(`projects`) @@ -118,19 +117,28 @@ export async function GivenProjectExists( ...projectData, }) .execute(); + + await GivenDefaultCostSurfaceForProject(em, projectId, costSurfaceId); + + return insertResult; } async function GivenDefaultCostSurfaceForProject( em: EntityManager, projectId: string, + id: string, name?: string, ) { + const nameForCostSurface = name || projectId; return em .createQueryBuilder() .insert() .into(`cost_surfaces`) .values({ - name: CostSurfaceService.defaultCostSurfaceName(name || projectId), + id, + name: `${ + nameForCostSurface ? nameForCostSurface + ' - ' : '' + }Default Cost Surface`, project_id: projectId, min: 0, max: 0, @@ -147,7 +155,14 @@ export async function GivenScenarioExists( scenarioData: Record = {}, projectData: Record = {}, ) { - await GivenProjectExists(em, projectId, organizationId, projectData); + const costSurfaceId = v4(); + await GivenProjectExists( + em, + projectId, + organizationId, + projectData, + costSurfaceId, + ); return em .createQueryBuilder() @@ -157,6 +172,7 @@ export async function GivenScenarioExists( id: scenarioId, name: `test scenario - ${scenarioId}`, project_id: projectId, + cost_surface_id: costSurfaceId, ...scenarioData, }) .execute(); diff --git a/api/apps/geoprocessing/test/integration/legacy-project-import/fixtures.ts b/api/apps/geoprocessing/test/integration/legacy-project-import/fixtures.ts index 8c5d02396a..ebcde959f3 100644 --- a/api/apps/geoprocessing/test/integration/legacy-project-import/fixtures.ts +++ b/api/apps/geoprocessing/test/integration/legacy-project-import/fixtures.ts @@ -1,4 +1,5 @@ import { EntityManager } from 'typeorm'; +import { v4 } from 'uuid'; export async function GivenProjectAndScenarioShells( em: EntityManager, @@ -16,17 +17,33 @@ export async function GivenProjectAndScenarioShells( }) .execute(); + const projectName = `test project - ${projectId}`; await em .createQueryBuilder() .insert() .into('projects') .values({ id: projectId, - name: `test project - ${projectId}`, + name: projectName, organization_id: organizationId, }) .execute(); + const costSurfaceId = v4(); + await em + .createQueryBuilder() + .insert() + .into(`cost_surfaces`) + .values({ + id: costSurfaceId, + name: `${projectName} - Default Cost Surface`, + project_id: projectId, + min: 0, + max: 0, + is_default: true, + }) + .execute(); + await em .createQueryBuilder() .insert() @@ -35,6 +52,7 @@ export async function GivenProjectAndScenarioShells( id: scenarioId, name: `test scenario - ${scenarioId}`, project_id: projectId, + cost_surface_id: costSurfaceId, }) .execute(); } diff --git a/api/apps/geoprocessing/test/integration/marxan-run/marxan-run.e2e-spec.ts b/api/apps/geoprocessing/test/integration/marxan-run/marxan-run.e2e-spec.ts index de8402625d..99fbcb94ef 100644 --- a/api/apps/geoprocessing/test/integration/marxan-run/marxan-run.e2e-spec.ts +++ b/api/apps/geoprocessing/test/integration/marxan-run/marxan-run.e2e-spec.ts @@ -23,6 +23,7 @@ import { v4 } from 'uuid'; import { GivenScenarioAndProjectPuData } from '../../steps/given-scenario-pu-data-exists'; import { bootstrapApplication } from '../../utils'; import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig'; + const TEST_TIMEOUT_MULTIPLIER = 35000; let fixtures: PromiseType>; @@ -112,6 +113,7 @@ const getFixtures = async () => { const projectId = v4(); const scenarioId = v4(); const featureId = v4(); + const costSurfaceId = v4(); const organizationId = v4(); const outputsIds: string[] = []; const scenarioFeatures: string[] = []; @@ -221,8 +223,12 @@ const getFixtures = async () => { [projectId, 'test_project', organizationId, 'legacy_import'], ); await apiEntityManager.query( - `INSERT INTO scenarios (id, name, project_id) VALUES ($1, $2, $3)`, - [scenarioId, 'test_scenario', projectId], + `INSERT INTO cost_surfaces (id, name, project_id, is_default, min, max) VALUES ($1, $2, $3, $4, $5, $6)`, + [costSurfaceId, 'test_cost_surface', projectId, true, 0, 0], + ); + await apiEntityManager.query( + `INSERT INTO scenarios (id, name, project_id, cost_surface_id) VALUES ($1, $2, $3, $4)`, + [scenarioId, 'test_scenario', projectId, costSurfaceId], ); }, GivenScenarioPuDataExists: async () => {