From 9c5c8ddcf4e881ca119086d344fba98d752195a0 Mon Sep 17 00:00:00 2001 From: andrea rota Date: Tue, 23 Apr 2024 13:03:22 +0100 Subject: [PATCH] add test: no rows with amount=0 should be generated for puvspr.dat files --- .../compute-area.service.spec.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/api/apps/api/src/modules/scenarios-features/compute-area.service.spec.ts b/api/apps/api/src/modules/scenarios-features/compute-area.service.spec.ts index 1a816ec9ed..b65b8cc474 100644 --- a/api/apps/api/src/modules/scenarios-features/compute-area.service.spec.ts +++ b/api/apps/api/src/modules/scenarios-features/compute-area.service.spec.ts @@ -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(); @@ -111,6 +126,7 @@ const getFixtures = async () => { sandbox.get(FeatureAmountsPerPlanningUnitRepository); const expectedPuid = v4(); + const expectedPuidWithAmountEqualToZero = v4(); const expectedAmount = 20; return { GivenProject: () => { @@ -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() }, @@ -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(); },