Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter out pusvpr rows where amount is zero [MRXNM-43] #1685

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ describe(ComputeArea, () => {
await fixtures.ThenMinMaxAmountWasSaved();
});

it('does not include rows with amount = 0 in puvspr.dat files', async () => {
const { projectId, scenarioId } = fixtures.GivenProject();
const featureId =
fixtures.GivenFeatureWasCreatedWithSomeAmountsPerPuEqualToZero();
fixtures.GivenMinMaxAmount(featureId, undefined, undefined);

await fixtures.WhenComputing(projectId, scenarioId, featureId);

await fixtures.ThenNoPuvsprRowsWithAmountEqualToZeroShouldBeGenerated(
projectId,
featureId,
);
await fixtures.ThenMinMaxAmountWasSaved();
});

it('does not save saves amounts per planning unit when computation has already been made but saves min/max amount for the feature', async () => {
const { projectId, scenarioId } = fixtures.GivenProject();
const featureId = fixtures.GivenNoComputationHasBeenSaved();
Expand Down Expand Up @@ -111,6 +126,7 @@ const getFixtures = async () => {
sandbox.get(FeatureAmountsPerPlanningUnitRepository);

const expectedPuid = v4();
const expectedPuidWithAmountEqualToZero = v4();
const expectedAmount = 20;
return {
GivenProject: () => {
Expand Down Expand Up @@ -146,6 +162,27 @@ const getFixtures = async () => {

return featureId;
},
GivenFeatureWasCreatedWithSomeAmountsPerPuEqualToZero: () => {
const featureId = v4();
computeMarxanAmountPerPlanningUnitMock.mockImplementation(async () => {
return [
{
featureId,
projectPuId: expectedPuid,
amount: expectedAmount,
puId: 1,
},
{
featureId,
projectPuId: expectedPuidWithAmountEqualToZero,
amount: 0,
puId: 2,
},
];
});

return featureId;
},
GivenComputationAlreadySaved: (projectId: string, featureId: string) => {
featureAmountsPerPlanningUnitRepo.memory[projectId] = [
{ featureId, amount: 42, projectPuId: v4() },
Expand Down Expand Up @@ -177,6 +214,29 @@ const getFixtures = async () => {
featureId,
});
},
ThenNoPuvsprRowsWithAmountEqualToZeroShouldBeGenerated: async (
projectId: string,
featureId: string,
) => {
const savedCalculations =
await featureAmountsPerPlanningUnitRepo.getAmountPerPlanningUnitAndFeature(
projectId,
[featureId],
);

expect(savedCalculations).toBeDefined();
expect(savedCalculations[0]).toEqual({
amount: expectedAmount,
projectPuId: expectedPuid,
featureId,
});
expect(savedCalculations).not.toContain({
amount: 0,
projectPuId: expectedPuidWithAmountEqualToZero,
featureId,
});
expect(savedCalculations.length).toBe(1);
},
ThenComputationsWasNotDone: async () => {
expect(computeMarxanAmountPerPlanningUnitMock).not.toHaveBeenCalled();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ export class MemoryFeatureAmountsPerPlanningUnitRepository

if (!featureAmountsPerPlanningUnit) return [];

return featureAmountsPerPlanningUnit.filter(({ featureId }) =>
featureIds.includes(featureId),
return (
featureAmountsPerPlanningUnit
.filter(({ featureId }) => featureIds.includes(featureId))
/** The Marxan solver will show unexpected behaviour when seeing
* puvspr.dat rows with amount = 0 */
.filter(({ amount }) => amount > 0)
);
}
async saveAmountPerPlanningUnitAndFeature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ export class TypeormFeatureAmountsPerPlanningUnitRepository
projectId: string,
featureIds: string[],
): Promise<FeatureAmountPerProjectPlanningUnit[]> {
return this.geoEntityManager
.createQueryBuilder()
.select('amount')
.addSelect('project_pu_id', 'projectPuId')
.addSelect('feature_id', 'featureId')
.from(FeatureAmountsPerPlanningUnitEntity, 'fappu')
.where('project_id = :projectId', { projectId })
.andWhere('feature_id IN (:...featureIds)', { featureIds })
.execute();
return (
this.geoEntityManager
.createQueryBuilder()
.select('amount')
.addSelect('project_pu_id', 'projectPuId')
.addSelect('feature_id', 'featureId')
.from(FeatureAmountsPerPlanningUnitEntity, 'fappu')
.where('project_id = :projectId', { projectId })
.andWhere('feature_id IN (:...featureIds)', { featureIds })
/** The Marxan solver will show unexpected behaviour when seeing
* puvspr.dat rows with amount = 0 */
.andWhere('amount > 0')
.execute()
);
}

public async saveAmountPerPlanningUnitAndFeature(
Expand Down