Skip to content

Commit

Permalink
Update marxan run tests to check ranAtLeastOnce property
Browse files Browse the repository at this point in the history
  • Loading branch information
yulia-bel committed Aug 11, 2023
1 parent 6a31d9c commit a2916bd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SandboxRunnerOutputHandler } from '../ports/sandbox-runner-output-handl
import { Workspace } from '../ports/workspace';
import { WorkspaceBuilder } from '../ports/workspace-builder';
import { BlmPartialResultsRepository } from './blm-partial-results.repository';
import {EntityManager} from "typeorm";
import { EntityManager } from 'typeorm';

@Injectable()
export class MarxanRunnerFactory {
Expand All @@ -22,7 +22,7 @@ export class MarxanRunnerFactory {
projectId: string,
blmValue: number,
workspace: Workspace,
entityManager: EntityManager
entityManager: EntityManager,
): SandboxRunner<JobData, ExecutionResult> {
const workspaceBuilder: WorkspaceBuilder = {
get: async () => workspace,
Expand Down Expand Up @@ -58,7 +58,7 @@ export class MarxanRunnerFactory {
workspaceBuilder,
inputFilesHandler,
outputFilesHandler,
entityManager
entityManager,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class MarxanSandboxBlmRunnerService
projectId,
blmValue,
workspace,
this.apiEntityManager
this.apiEntityManager,
);

const abortEventListener = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Cancellable } from '../ports/cancellable';
import { SandboxRunnerInputFiles } from '../ports/sandbox-runner-input-files';
import { SandboxRunner } from '../ports/sandbox-runner';
import { SandboxRunnerOutputHandler } from '../ports/sandbox-runner-output-handler';
import { EntityManager} from 'typeorm';
import { EntityManager } from 'typeorm';
import { InjectEntityManager } from '@nestjs/typeorm';
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';

Expand All @@ -25,7 +25,7 @@ export class MarxanSandboxRunnerService
private readonly inputFilesHandler: SandboxRunnerInputFiles,
private readonly outputFilesHandler: SandboxRunnerOutputHandler<ExecutionResult>,
@InjectEntityManager(geoprocessingConnections.apiDB)
private readonly apiEntityManager: EntityManager
private readonly apiEntityManager: EntityManager,
) {}

kill(ofScenarioId: string): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { PromiseType } from 'utility-types';
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 All @@ -32,19 +32,21 @@ beforeEach(async () => {
});

describe(`given input data is delayed`, () => {
beforeEach(() => {
beforeEach(async () => {
fixtures.GivenInputFilesAreAvailable(500000);
await fixtures.GivenScenarioExistsInApiDb();
});

test(
`cancelling marxan run during fetching assets shouldn't finish Marxan run.`,
async () => {
expect.assertions(1);
expect.assertions(2);
setTimeout(fixtures.WhenKillingMarxanRun, 1000);
try {
await fixtures.GivenBLMCalibrationIsRunning();
fail();
} catch (e) {
await fixtures.ThenRanAtLeastOncePropertyForScenarioIsFalse();
expect(e).toHaveProperty('signal', 'SIGTERM');
}
},
Expand All @@ -60,6 +62,7 @@ describe(`given input data is available`, () => {
fixtures.GivenInputFilesAreAvailable(500);
await fixtures.GivenScenarioDataExists();
await fixtures.GivenScenarioPuDataExists();
await fixtures.GivenScenarioExistsInApiDb();
}, TEST_TIMEOUT_MULTIPLIER * 4);
test(
`marxan run during binary execution`,
Expand All @@ -68,6 +71,7 @@ describe(`given input data is available`, () => {
fixtures.ThenHasValidOutput(output);
await fixtures.ThenOutputScenarioPuDataWasPersisted();
fixtures.ThenProgressWasReported();
await fixtures.ThenRanAtLeastOncePropertyForScenarioIsTrue();
},
TEST_TIMEOUT_MULTIPLIER * 30,
);
Expand Down Expand Up @@ -109,6 +113,9 @@ const getFixtures = async () => {

const app = await bootstrapApplication();
const entityManager = app.get<EntityManager>(getEntityManagerToken());
const apiEntityManager: EntityManager = app.get(
getEntityManagerToken(geoprocessingConnections.apiDB),
);
const featuresData: Repository<GeoFeatureGeometry> = app.get(
getRepositoryToken(GeoFeatureGeometry),
);
Expand Down Expand Up @@ -155,6 +162,14 @@ const getFixtures = async () => {
id: In(projectPus.map((pu) => pu.geomId)),
});
// featuresOutputRepo removes on cascade

await apiEntityManager.query(`DELETE FROM scenarios WHERE id=$1`, [
scenarioId,
]);
await apiEntityManager.query(`DELETE FROM projects WHERE id=$1`, [
projectId,
]);
await apiEntityManager.query(`DELETE FROM organizations`);
nockScope.done();
nock.enableNetConnect();
},
Expand Down Expand Up @@ -183,6 +198,21 @@ const getFixtures = async () => {
'content-type': 'plain/text',
});
}),
GivenScenarioExistsInApiDb: async () => {
const organizationId = v4();
await apiEntityManager.query(
`INSERT INTO organizations (id, name) VALUES ($1, $2)`,
[organizationId, 'test_organization'],
);
await apiEntityManager.query(
`INSERT INTO projects (id, name, organization_id, sources) VALUES ($1, $2, $3, $4)`,
[projectId, 'test_project', organizationId, 'legacy_import'],
);
await apiEntityManager.query(
`INSERT INTO scenarios (id, name, project_id) VALUES ($1, $2, $3)`,
[scenarioId, 'test_scenario', projectId],
);
},
GivenScenarioPuDataExists: async () => {
outputsIds.push(
...(
Expand Down Expand Up @@ -263,6 +293,22 @@ const getFixtures = async () => {
const { calls } = this.progressMock.mock;
expect(last(calls)).toEqual([1]);
},

ThenRanAtLeastOncePropertyForScenarioIsFalse: async () => {
const scenario = await apiEntityManager.query(
`SELECT * FROM scenarios WHERE id = $1;`,
[scenarioId],
);
expect(scenario[0].ran_at_least_once).toBe(false);
},

ThenRanAtLeastOncePropertyForScenarioIsTrue: async () => {
const scenario = await apiEntityManager.query(
`SELECT * FROM scenarios WHERE id = $1;`,
[scenarioId],
);
expect(scenario[0].ran_at_least_once).toBe(true);
},
};
};

Expand Down

0 comments on commit a2916bd

Please sign in to comment.