From 9ecda96ef097a80a8ceb812826cbe60d1a048b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daute=20Rodr=C3=ADguez=20Rodr=C3=ADguez?= Date: Wed, 5 Jan 2022 10:07:19 +0000 Subject: [PATCH] fix issues with BlmValuesCalculator --- .../projects/blm/change-blm-range.handler.ts | 5 +-- .../blm/domain/blm-values-calculator.spec.ts | 43 ++++++++++++------- .../blm/domain/blm-values-calculator.ts | 14 +++--- .../projects/blm/set-project-blm-handler.ts | 4 +- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/api/apps/api/src/modules/projects/blm/change-blm-range.handler.ts b/api/apps/api/src/modules/projects/blm/change-blm-range.handler.ts index 2422d350aa..7b4e91cb5c 100644 --- a/api/apps/api/src/modules/projects/blm/change-blm-range.handler.ts +++ b/api/apps/api/src/modules/projects/blm/change-blm-range.handler.ts @@ -10,14 +10,13 @@ import { invalidRange, updateFailure, } from './change-blm-range.command'; -import { SetProjectBlm } from './set-project-blm'; import { PlanningUnitAreaFetcher } from './planning-unit-area-fetcher'; import { BlmValuesPolicyFactory } from './blm-values-policy-factory'; @CommandHandler(ChangeBlmRange) export class ChangeBlmRangeHandler implements IInferredCommandHandler { - private readonly logger: Logger = new Logger(SetProjectBlm.name); + private readonly logger: Logger = new Logger(ChangeBlmRange.name); constructor( private readonly blmRepository: ProjectBlmRepo, @@ -48,7 +47,7 @@ export class ChangeBlmRangeHandler } const calculator = this.blmPolicyFactory.get(); - const blmValues = calculator.with(range, result.right); + const blmValues = calculator.with(range); const updateResult = await this.blmRepository.update( projectId, diff --git a/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.spec.ts b/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.spec.ts index a698d699da..1c39c318b5 100644 --- a/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.spec.ts +++ b/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.spec.ts @@ -4,28 +4,39 @@ describe('blm-values-calculator', () => { const blmValuesCalculator = new BlmValuesCalculator(); it('should calculate the correct default values', async () => { - const defaultValues = blmValuesCalculator.withDefaultRange(1500); + const defaultValues = blmValuesCalculator.withDefaultRange(); expect(defaultValues).toStrictEqual([ - 0.03872983346207417, - 606.799665767047, - 1213.5606017006319, - 1820.3215376342168, - 2427.0824735678016, - 3033.8434095013868, + 0.001, + 20.0008, + 40.0006, + 60.0004, + 80.0002, + 100, ]); }); it('should calculate the correct values with a given range', async () => { - const values = blmValuesCalculator.with([1, 50], 1500); + const values = blmValuesCalculator.with([0, 50]); - expect(values).toStrictEqual([ - 38.72983346207417, - 316.2936399402723, - 593.8574464184705, - 871.4212528966688, - 1148.985059374867, - 1426.548865853065, - ]); + expect(values).toStrictEqual([0, 10, 20, 30, 40, 50]); + }); + + it('should return values within the given range', async () => { + const ranges: [number, number][] = [ + [0, 1], + [0, 10], + [10, 20], + [100, 1000], + [0, 100000], + ]; + + ranges.forEach(([min, max]) => { + const values = blmValuesCalculator.with([min, max]); + values.forEach((value) => { + expect(value).toBeGreaterThanOrEqual(min); + expect(value).toBeLessThanOrEqual(max); + }); + }); }); }); diff --git a/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.ts b/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.ts index 4386f04f56..2559afa143 100644 --- a/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.ts +++ b/api/apps/api/src/modules/projects/blm/domain/blm-values-calculator.ts @@ -4,26 +4,26 @@ export class BlmValuesCalculator { private range: [number, number] = [0.001, 100], ) {} - private execute(area: number) { + private execute() { const [min, max] = this.range; const initialArray = Array(this.cardinality - 1) .fill(0) .map((_, i) => i + 1); const formulaResults = initialArray.map( - (i) => min + ((max - min) / this.cardinality - 1) * i, + (i) => min + ((max - min) / (this.cardinality - 1)) * i, ); const blmValues = [min, ...formulaResults]; - return blmValues.map((value) => value * Math.sqrt(area)); + return blmValues.map((value) => value); } - with(range: [number, number], area: number) { + with(range: [number, number]) { this.range = range; - return this.execute(area); + return this.execute(); } - withDefaultRange(area: number) { - return this.execute(area); + withDefaultRange() { + return this.execute(); } } diff --git a/api/apps/api/src/modules/projects/blm/set-project-blm-handler.ts b/api/apps/api/src/modules/projects/blm/set-project-blm-handler.ts index afc5e80122..fbe42f03b2 100644 --- a/api/apps/api/src/modules/projects/blm/set-project-blm-handler.ts +++ b/api/apps/api/src/modules/projects/blm/set-project-blm-handler.ts @@ -4,7 +4,7 @@ import { Logger } from '@nestjs/common'; import { SetProjectBlm } from './set-project-blm'; import { ProjectBlmRepo } from '@marxan-api/modules/blm'; -import { PlanningUnitAreaFetcher } from '@marxan-api/modules/projects/blm/planning-unit-area-fetcher'; +import { PlanningUnitAreaFetcher } from './planning-unit-area-fetcher'; import { BlmValuesPolicyFactory } from './blm-values-policy-factory'; @CommandHandler(SetProjectBlm) @@ -29,7 +29,7 @@ export class SetProjectBlmHandler return; } const calculator = this.blmPolicyFactory.get(); - const defaultBlm = calculator.withDefaultRange(areaResult.right); + const defaultBlm = calculator.withDefaultRange(); const result = await this.blmRepository.create(projectId, defaultBlm); if (isLeft(result))