Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Lopez committed May 22, 2024
1 parent 81040f3 commit 6d55db2
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 25 deletions.
156 changes: 156 additions & 0 deletions src/interface/src/app/plan/scenario-helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { MetricConfig, ScenarioConfig, ScenarioResult } from '@types';
import { ChartData } from './project-areas-metrics/chart-data';
import { processScenarioResultsToChartData } from './scenario-helpers';

describe('processScenarioResultsToChartData', () => {
const metrics: MetricConfig[] = [
{
metric_name: 'metric1',
display_name: 'Metric 1',
data_units: 'units',
raw_layer: 'layer1',
},
{
metric_name: 'metric2',
display_name: 'Metric 2',
data_units: 'units',
raw_layer: 'layer2',
},
{
metric_name: 'metric3',
display_name: 'Metric 3',
data_units: 'units',
raw_layer: 'layer3',
},
];

const scenarioConfig: ScenarioConfig = {
scenario_priorities: ['metric1'],
scenario_output_fields: ['metric2', 'metric3'],
};

const scenarioResults: ScenarioResult = {
status: 'PENDING',
completed_at: '0',
result: {
type: 'Feature',
features: [
{
properties: { metric1: 10, metric2: 20, metric3: 30 },
type: 'FeatureCollection',
features: [],
},
{
properties: { metric1: 15, metric2: 25, metric3: 35 },
type: 'FeatureCollection',
features: [],
},
{
properties: { metric1: 20, metric2: 30, metric3: 40 },
type: 'FeatureCollection',
features: [],
},
],
},
};

it('should process metrics and return ChartData correctly', () => {
const expectedOutput: ChartData[] = [
{
label: 'Metric 1',
measurement: 'units',
key: 'metric1',
values: [10, 15, 20],
metric_layer: 'layer1',
is_primary: true,
},
{
label: 'Metric 2',
measurement: 'units',
key: 'metric2',
values: [20, 25, 30],
metric_layer: 'layer2',
is_primary: false,
},
{
label: 'Metric 3',
measurement: 'units',
key: 'metric3',
values: [30, 35, 40],
metric_layer: 'layer3',
is_primary: false,
},
];

const result = processScenarioResultsToChartData(
metrics,
scenarioConfig,
scenarioResults
);
expect(result).toEqual(expectedOutput);
});

it('should handle empty metrics array', () => {
const emptyMetrics: MetricConfig[] = [];
const result = processScenarioResultsToChartData(
emptyMetrics,
scenarioConfig,
scenarioResults
);
expect(result).toEqual([]);
});

it('should handle empty scenario results', () => {
const emptyScenarioResults: ScenarioResult = {
status: 'PENDING',
completed_at: '0',
result: {
features: [],
type: '',
},
};
const expectedOutput: ChartData[] = [
{
label: 'Metric 1',
measurement: 'units',
key: 'metric1',
values: [],
metric_layer: 'layer1',
is_primary: true,
},
{
label: 'Metric 2',
measurement: 'units',
key: 'metric2',
values: [],
metric_layer: 'layer2',
is_primary: false,
},
{
label: 'Metric 3',
measurement: 'units',
key: 'metric3',
values: [],
metric_layer: 'layer3',
is_primary: false,
},
];

const result = processScenarioResultsToChartData(
metrics,
scenarioConfig,
emptyScenarioResults
);
expect(result).toEqual(expectedOutput);
});

it('should handle missing priorities and output fields', () => {
const emptyConfig: ScenarioConfig = {};
const result = processScenarioResultsToChartData(
metrics,
emptyConfig,
scenarioResults
);
expect(result).toEqual([]);
});
});
32 changes: 16 additions & 16 deletions src/interface/src/app/plan/scenario-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ export function processScenarioResultsToChartData(
scenarioConfig: ScenarioConfig,
scenarioResults: ScenarioResult
): ChartData[] {
const priorities = scenarioConfig.scenario_priorities;
const outputFields = new Set([
...(scenarioConfig.scenario_output_fields || []),
...(priorities || []),
]);
const {
scenario_priorities: priorities = [],
scenario_output_fields: outputFields = [],
} = scenarioConfig;
const relevantFields = new Set([...outputFields, ...priorities]);

return metrics
.filter((metric) => outputFields?.has(metric.metric_name))
.map((metric) => {
let metricData: number[] = [];
scenarioResults.result.features.map((featureCollection) => {
metricData.push(featureCollection.properties[metric.metric_name]);
});
return metrics.reduce((acc: ChartData[], metric) => {
if (relevantFields.has(metric.metric_name)) {
const metricData = scenarioResults.result.features.map(
(featureCollection) => featureCollection.properties[metric.metric_name]
);

return <ChartData>{
acc.push({
label: metric.display_name,
measurement: metric.data_units,
key: metric.metric_name,
values: metricData,
metric_layer: metric.raw_layer,
is_primary: priorities?.includes(metric.metric_name) || false,
};
});
is_primary: priorities.includes(metric.metric_name),
} as ChartData);
}
return acc;
}, []);
}
14 changes: 5 additions & 9 deletions src/interface/src/app/services/metrics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
HttpTestingController,
} from '@angular/common/http/testing';
import { MetricsService } from './metrics.service';
import { ConditionsConfig, Region, regionToString } from '@types';
import { MetricConfig, Region, regionToString } from '@types';
import { environment } from '../../environments/environment';

describe('MetricsService', () => {
Expand All @@ -26,9 +26,7 @@ describe('MetricsService', () => {
});

it('should return cached conditions if they exist', () => {
const mockConditions: ConditionsConfig = {
/* mock data here */
};
const mockConditions: MetricConfig[] = [];
service.conditions[Region.CENTRAL_COAST] = mockConditions;

service
Expand All @@ -40,15 +38,13 @@ describe('MetricsService', () => {
// Ensure no HTTP requests are made
httpMock.expectNone(
environment.backend_endpoint +
'/conditions/config/?region_name=' +
'/conditions/config/?flat=true&region_name=' +
regionToString(Region.CENTRAL_COAST)
);
});

it('should fetch conditions from the backend if not cached', () => {
const mockConditions: ConditionsConfig = {
/* mock data here */
};
const mockConditions: MetricConfig[] = [];
service.conditions[Region.CENTRAL_COAST] = null;

service
Expand All @@ -62,7 +58,7 @@ describe('MetricsService', () => {

const req = httpMock.expectOne(
environment.backend_endpoint +
'/conditions/config/?region_name=' +
'/conditions/config/?flat=true&region_name=' +
regionToString(Region.CENTRAL_COAST)
);
expect(req.request.method).toBe('GET');
Expand Down

0 comments on commit 6d55db2

Please sign in to comment.