Skip to content

Commit

Permalink
feat(CostSurface): More test fixes for the CostSurface to Scenario re…
Browse files Browse the repository at this point in the history
…lationship
  • Loading branch information
KevSanchez committed Sep 13, 2023
1 parent 521526d commit 8f10f86
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<typeof getFixtures>;

Expand Down Expand Up @@ -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],
Expand All @@ -84,6 +97,10 @@ const getFixtures = async () => {
provide: getRepositoryToken(Scenario),
useValue: { save: saveScenarioMock },
},
{
provide: CostSurfaceService,
useValue: { createDefaultCostSurfaceForProject },
},
{
provide: getRepositoryToken(Organization),
useValue: { find: findRandomOrganizationMock },
Expand Down
18 changes: 13 additions & 5 deletions api/apps/api/test/fixtures/test-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]'), '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 = '[email protected]'), '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
Expand All @@ -34,12 +42,12 @@ VALUES
((SELECT id FROM users WHERE lower(email) = '[email protected]'), (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 = '[email protected]') ),
('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 = '[email protected]') ),
('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 = '[email protected]') ),
('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 = '[email protected]') );
('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 = '[email protected]') ),
('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 = '[email protected]') ),
('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 = '[email protected]') ),
('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 = '[email protected]') );

INSERT INTO platform_admins
(user_id)
Expand Down
26 changes: 21 additions & 5 deletions api/apps/geoprocessing/test/integration/cloning/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,11 +101,11 @@ export async function GivenProjectExists(
projectId: string,
organizationId: string,
projectData: Record<string, any> = {},
costSurfaceId = v4(),
) {
await GivenOrganizationExists(em, organizationId);
await GivenDefaultCostSurfaceForProject(em, projectId);

return em
const insertResult = await em
.createQueryBuilder()
.insert()
.into(`projects`)
Expand All @@ -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,
Expand All @@ -147,7 +155,14 @@ export async function GivenScenarioExists(
scenarioData: Record<string, any> = {},
projectData: Record<string, any> = {},
) {
await GivenProjectExists(em, projectId, organizationId, projectData);
const costSurfaceId = v4();
await GivenProjectExists(
em,
projectId,
organizationId,
projectData,
costSurfaceId,
);

return em
.createQueryBuilder()
Expand All @@ -157,6 +172,7 @@ export async function GivenScenarioExists(
id: scenarioId,
name: `test scenario - ${scenarioId}`,
project_id: projectId,
cost_surface_id: costSurfaceId,
...scenarioData,
})
.execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EntityManager } from 'typeorm';
import { v4 } from 'uuid';

export async function GivenProjectAndScenarioShells(
em: EntityManager,
Expand All @@ -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()
Expand All @@ -35,6 +52,7 @@ export async function GivenProjectAndScenarioShells(
id: scenarioId,
name: `test scenario - ${scenarioId}`,
project_id: projectId,
cost_surface_id: costSurfaceId,
})
.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof getFixtures>>;
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 8f10f86

Please sign in to comment.