Skip to content

Commit

Permalink
Report tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 20, 2023
1 parent 530e467 commit 08186a5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
19 changes: 12 additions & 7 deletions api/src/modules/impact/reports/impact.report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ export class ImpactReportService {
yearKey = yearKey.concat(' (projected)');
}
if ('baseScenarioValue' in nodeValue) {
resultObject[`${yearKey} (base Scenario)`] =
resultObject[`${yearKey} (Base Scenario)`] =
nodeValue.baseScenarioValue;
resultObject[`${yearKey} (compared Scenario)`] =
resultObject[`${yearKey} (Compared Scenario)`] =
nodeValue.comparedScenarioValue;
} else if ('comparedScenarioValue' in nodeValue) {
resultObject[yearKey] = nodeValue.value;
resultObject[`${yearKey} (compared Scenario)`] =
resultObject[`${yearKey} (Compared Scenario)`] =
nodeValue.comparedScenarioValue;
} else {
resultObject[yearKey] = nodeValue.value;
Expand All @@ -111,20 +111,25 @@ export class ImpactReportService {
const indicatorKey: string[] = [];
const groupByKey: string[] = [];
const yearKeys: string[] = [];
const differenceKeys: string[] = [];
const comparisonKeys: string[] = [];
Object.keys(dataSample).forEach((key: string) => {
if (key.includes(IndicatorColumnKey)) {
groupByKey.push(key);
} else if (key.includes(GroupByColumnKey)) {
groupByKey.push(key);
} else if (key.includes(AbsoluteDifferenceColumnKey)) {
differenceKeys.push(key);
comparisonKeys.push(key);
} else if (key.includes(PercentageDifferenceColumnKey)) {
differenceKeys.push(key);
comparisonKeys.push(key);
} else {
yearKeys.push(key);
}
});
return [...indicatorKey, ...groupByKey, ...yearKeys, ...differenceKeys];
return [
...indicatorKey,
...groupByKey,
...yearKeys.sort(),
...comparisonKeys,
];
}
}
16 changes: 16 additions & 0 deletions api/test/e2e/impact/impact-reports/impact-reports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ describe('Impact Reports', () => {
materials,
});
});
it('should create an actual vs scenario impact report', async () => {
const { indicator, scenarioIntervention } =
await fixtures.GivenAScenarioIntervention();
const response = await fixtures.WhenIRequestAnActualVsScenarioImpactReport({
app: testApplication,
jwtToken,
indicatorIds: [indicator.id],
comparedScenarioId: scenarioIntervention.scenarioId,
});

await fixtures.ThenIShouldGetAnImpactReportAboutProvidedFilters(response, {
indicators: [indicator],
isActualVsScenario: true,
});
});
it('should create a scenario vs scenario impact report', async () => {});
});
48 changes: 48 additions & 0 deletions api/test/e2e/impact/impact-reports/impactReportFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import * as request from 'supertest';
import { GROUP_BY_VALUES } from '../../../../src/modules/impact/dto/impact-table.dto';
import { range } from 'lodash';
import { Material } from '../../../../src/modules/materials/material.entity';
import { Scenario } from 'modules/scenarios/scenario.entity';
import { createNewCoefficientsInterventionPreconditions } from '../mocks/actual-vs-scenario-preconditions/new-coefficients-intervention.preconditions';
import { ScenarioIntervention } from '../../../../src/modules/scenario-interventions/scenario-intervention.entity';

export const impactReportFixtures = () => ({
GivenSourcingLocationWithImpact: async () => {
Expand Down Expand Up @@ -89,6 +92,20 @@ export const impactReportFixtures = () => ({
sourcingRecords,
};
},
GivenAScenarioIntervention: async (
customScenario?: Scenario,
): Promise<{
indicator: Indicator;
scenarioIntervention: ScenarioIntervention;
}> => {
const { indicator, scenarioIntervention } =
await createNewCoefficientsInterventionPreconditions(customScenario);
return {
indicator,
scenarioIntervention,
};
},

WhenIRequestAnImpactReport: (options: {
app: TestApplication;
jwtToken: string;
Expand All @@ -104,12 +121,32 @@ export const impactReportFixtures = () => ({
groupBy: 'material',
});
},
WhenIRequestAnActualVsScenarioImpactReport: (options: {
app: TestApplication;
jwtToken: string;
indicatorIds: string[];
comparedScenarioId: string;
}): Promise<request.Response> => {
return request(options.app.getHttpServer())
.get('/api/v1/impact/compare/scenario/vs/actual/report')
.set('Authorization', `Bearer ${options.jwtToken}`)
.query({
'indicatorIds[]': [...options.indicatorIds],
startYear: 2010,
endYear: 2027,
groupBy: 'material',
comparedScenarioId: options.comparedScenarioId,
});
},

ThenIShouldGetAnImpactReportAboutProvidedFilters: (
response: request.Response,
filters?: {
groupBy?: GROUP_BY_VALUES;
indicators?: Indicator[];
materials?: Material[];
isActualVsScenario?: boolean;
isScenarioVsScenario?: boolean;
},
) => {
expect(response.status).toBe(200);
Expand All @@ -126,6 +163,17 @@ export const impactReportFixtures = () => ({
expect(response.text).toContain(material.name);
}
}
if (filters?.isActualVsScenario) {
expect(response.text).toContain('Compared Scenario');
expect(response.text).toContain('Absolute Difference');
expect(response.text).toContain('Percentage Difference');
}
if (filters?.isScenarioVsScenario) {
expect(response.text).toContain('Base Scenario');
expect(response.text).toContain('Compared Scenario');
expect(response.text).toContain('Absolute Difference');
expect(response.text).toContain('Percentage Difference');
}
for (const year of range(2010, 2027)) {
expect(response.text).toContain(year.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
import { INDICATOR_NAME_CODES } from 'modules/indicators/indicator.entity';
import { Scenario } from 'modules/scenarios/scenario.entity';

// TODO: Pending to start refactoring all precondition creation mess to follow the fixture approach

export async function createNewCoefficientsInterventionPreconditions(
customScenario?: Scenario,
): Promise<{
Expand All @@ -48,6 +50,7 @@ export async function createNewCoefficientsInterventionPreconditions(
name: 'Deforestation',
unit,
nameCode: INDICATOR_NAME_CODES.DF_SLUC,
shortName: 'Deforestation',
});

// Creating Materials
Expand Down

0 comments on commit 08186a5

Please sign in to comment.