Skip to content

Commit

Permalink
RN-175: Switched to using TransformTable in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-bes committed Sep 28, 2022
1 parent 4c08d6d commit b99c161
Show file tree
Hide file tree
Showing 14 changed files with 694 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { ReportServerAggregator } from '../../../aggregator';
import { buildOutput } from '../../../reportBuilder/output';
import { TransformTable } from '../../../reportBuilder/transform';
import {
MULTIPLE_TRANSFORMED_DATA,
MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES,
Expand All @@ -28,7 +29,7 @@ describe('matrix', () => {
);

const outputFn = async () => {
await output([]);
await output(new TransformTable());
};

await expect(outputFn()).rejects.toThrow("columns must be either '*' or an array");
Expand Down Expand Up @@ -88,7 +89,9 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES);
const result = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES),
);
expect(result).toEqual(expectedData);
});

Expand Down Expand Up @@ -147,7 +150,9 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES);
const result = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES),
);
expect(result).toEqual(expectedData);
});

Expand Down Expand Up @@ -185,7 +190,9 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA_FOR_SPECIFIED_COLUMNS);
const result = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_FOR_SPECIFIED_COLUMNS),
);
expect(result).toEqual(expectedData);
});

Expand Down Expand Up @@ -239,7 +246,9 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA_FOR_SPECIFIED_COLUMNS);
const result = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_FOR_SPECIFIED_COLUMNS),
);
expect(result).toEqual(expectedData);
});
});
Expand All @@ -257,7 +266,7 @@ describe('matrix', () => {
reportServerAggregator,
);
await expect(async () => {
await output([]);
await output(new TransformTable());
}).rejects.toThrow(
'categoryField cannot be one of: [InfrastructureType,Laos,Tonga] they are already specified as columns',
);
Expand All @@ -274,7 +283,7 @@ describe('matrix', () => {
reportServerAggregator,
);
await expect(async () => {
await output([]);
await output(new TransformTable());
}).rejects.toThrow(
'rowField cannot be: FacilityType, it is already specified as categoryField',
);
Expand Down Expand Up @@ -323,7 +332,7 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA);
const result = await output(TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA));
expect(result).toEqual(expectedData);
});

Expand Down Expand Up @@ -382,7 +391,9 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES);
const result = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES),
);
expect(result).toEqual(expectedData);
});
});
Expand All @@ -400,7 +411,7 @@ describe('matrix', () => {
reportServerAggregator,
);
await expect(async () => {
await output([]);
await output(new TransformTable());
}).rejects.toThrow(
'rowField cannot be one of: [FacilityType,Laos,Tonga] they are already specified as columns',
);
Expand All @@ -417,7 +428,7 @@ describe('matrix', () => {
reportServerAggregator,
);
await expect(async () => {
await output([]);
await output(new TransformTable());
}).rejects.toThrow(
'rowField cannot be: FacilityType, it is already specified as categoryField',
);
Expand All @@ -433,7 +444,7 @@ describe('matrix', () => {
reportServerAggregator,
);
await expect(async () => {
await output([]);
await output(new TransformTable());
}).rejects.toThrow('rowField is a required field');
});

Expand Down Expand Up @@ -488,7 +499,9 @@ describe('matrix', () => {
{},
reportServerAggregator,
);
const result = await output(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES);
const result = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_WITH_CATEGORIES),
);
expect(result).toEqual(expectedData);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Aggregator } from '@tupaia/aggregator';
import { ReportServerAggregator } from '../../../aggregator';
import { buildOutput } from '../../../reportBuilder/output';
import { TransformTable } from '../../../reportBuilder/transform';
import { MULTIPLE_TRANSFORMED_DATA_FOR_RAW_DATA_EXPORT } from './output.fixtures';

describe('rawDataExport', () => {
Expand Down Expand Up @@ -80,7 +81,9 @@ describe('rawDataExport', () => {
const reportServerAggregator = new ReportServerAggregator(aggregator);
const output = buildOutput(config, context, reportServerAggregator);

const results = await output(MULTIPLE_TRANSFORMED_DATA_FOR_RAW_DATA_EXPORT);
const results = await output(
TransformTable.fromRows(MULTIPLE_TRANSFORMED_DATA_FOR_RAW_DATA_EXPORT),
);
expect(results).toEqual(expectedData);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,122 +4,168 @@
*/

import {
MULTIPLE_ANALYTICS,
MERGEABLE_ANALYTICS,
MULTIPLE_ANALYTICS,
MULTIPLE_MERGEABLE_ANALYTICS,
SINGLE_ANALYTIC,
SINGLE_EVENT,
TRANSFORMED_SUMMARY_BINARY,
TRANSFORMED_SUMMARY_VARIOUS,
} from './transform.fixtures';
import { buildTransform } from '../../../reportBuilder/transform';
import { buildTransform, TransformTable } from '../../../reportBuilder/transform';

describe('aliases', () => {
it('keyValueByDataElementName', () => {
const transform = buildTransform(['keyValueByDataElementName']);
expect(transform(MULTIPLE_ANALYTICS)).toEqual([
{ period: '20200101', organisationUnit: 'TO', BCD1: 4 },
{ period: '20200102', organisationUnit: 'TO', BCD1: 2 },
{ period: '20200103', organisationUnit: 'TO', BCD1: 5 },
]);
expect(transform(TransformTable.fromRows(MULTIPLE_ANALYTICS))).toEqual(
TransformTable.fromRows([
{ period: '20200101', organisationUnit: 'TO', BCD1: 4 },
{ period: '20200102', organisationUnit: 'TO', BCD1: 2 },
{ period: '20200103', organisationUnit: 'TO', BCD1: 5 },
]),
);
});

it('keyValueByOrgUnit', () => {
const transform = buildTransform(['keyValueByOrgUnit']);
expect(transform(MULTIPLE_ANALYTICS)).toEqual([
{ period: '20200101', TO: 4, dataElement: 'BCD1' },
{ period: '20200102', TO: 2, dataElement: 'BCD1' },
{ period: '20200103', TO: 5, dataElement: 'BCD1' },
]);
expect(transform(TransformTable.fromRows(MULTIPLE_ANALYTICS))).toEqual(
TransformTable.fromRows([
{ period: '20200101', dataElement: 'BCD1', TO: 4 },
{ period: '20200102', dataElement: 'BCD1', TO: 2 },
{ period: '20200103', dataElement: 'BCD1', TO: 5 },
]),
);
});

it('keyValueByPeriod', () => {
const transform = buildTransform(['keyValueByPeriod']);
expect(transform(MULTIPLE_ANALYTICS)).toEqual([
{ '20200101': 4, organisationUnit: 'TO', dataElement: 'BCD1' },
{ '20200102': 2, organisationUnit: 'TO', dataElement: 'BCD1' },
{ '20200103': 5, organisationUnit: 'TO', dataElement: 'BCD1' },
]);
expect(transform(TransformTable.fromRows(MULTIPLE_ANALYTICS))).toEqual(
TransformTable.fromRows(
[
{ organisationUnit: 'TO', dataElement: 'BCD1', '20200101': 4 },
{ organisationUnit: 'TO', dataElement: 'BCD1', '20200102': 2 },
{ organisationUnit: 'TO', dataElement: 'BCD1', '20200103': 5 },
],
['organisationUnit', 'dataElement', '20200101', '20200102', '20200103'],
),
);
});

it('mostRecentValuePerOrgUnit', () => {
const transform = buildTransform(['mostRecentValuePerOrgUnit']);
expect(transform(MERGEABLE_ANALYTICS)).toEqual([
{ period: '20200103', organisationUnit: 'TO', BCD1: 5, BCD2: 0 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 2, BCD2: -1 },
]);
expect(transform(TransformTable.fromRows(MERGEABLE_ANALYTICS))).toEqual(
TransformTable.fromRows([
{ period: '20200103', organisationUnit: 'TO', BCD1: 5, BCD2: 0 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 2, BCD2: -1 },
]),
);
});

it('firstValuePerPeriodPerOrgUnit', () => {
const transform = buildTransform(['firstValuePerPeriodPerOrgUnit']);
expect(transform(MULTIPLE_MERGEABLE_ANALYTICS)).toEqual([
{ period: '20200101', organisationUnit: 'TO', BCD1: 4, BCD2: 11 },
{ period: '20200102', organisationUnit: 'TO', BCD1: 2, BCD2: 1 },
{ period: '20200103', organisationUnit: 'TO', BCD1: 5, BCD2: 0 },
{ period: '20200101', organisationUnit: 'PG', BCD1: 7, BCD2: 13 },
{ period: '20200102', organisationUnit: 'PG', BCD1: 8, BCD2: 99 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 2, BCD2: -1 },
]);
expect(transform(TransformTable.fromRows(MULTIPLE_MERGEABLE_ANALYTICS))).toEqual(
TransformTable.fromRows([
{ period: '20200101', organisationUnit: 'TO', BCD1: 4, BCD2: 11 },
{ period: '20200102', organisationUnit: 'TO', BCD1: 2, BCD2: 1 },
{ period: '20200103', organisationUnit: 'TO', BCD1: 5, BCD2: 0 },
{ period: '20200101', organisationUnit: 'PG', BCD1: 7, BCD2: 13 },
{ period: '20200102', organisationUnit: 'PG', BCD1: 8, BCD2: 99 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 2, BCD2: -1 },
]),
);
});

it('lastValuePerPeriodPerOrgUnit', () => {
const transform = buildTransform(['lastValuePerPeriodPerOrgUnit']);
expect(transform(MULTIPLE_MERGEABLE_ANALYTICS)).toEqual([
{ period: '20200101', organisationUnit: 'TO', BCD1: 7, BCD2: 4 },
{ period: '20200102', organisationUnit: 'TO', BCD1: 12, BCD2: 18 },
{ period: '20200103', organisationUnit: 'TO', BCD1: 23, BCD2: 9 },
{ period: '20200101', organisationUnit: 'PG', BCD1: 17, BCD2: 23 },
{ period: '20200102', organisationUnit: 'PG', BCD1: 4, BCD2: -4 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 1, BCD2: 12 },
]);
expect(transform(TransformTable.fromRows(MULTIPLE_MERGEABLE_ANALYTICS))).toEqual(
TransformTable.fromRows([
{ period: '20200101', organisationUnit: 'TO', BCD1: 7, BCD2: 4 },
{ period: '20200102', organisationUnit: 'TO', BCD1: 12, BCD2: 18 },
{ period: '20200103', organisationUnit: 'TO', BCD1: 23, BCD2: 9 },
{ period: '20200101', organisationUnit: 'PG', BCD1: 17, BCD2: 23 },
{ period: '20200102', organisationUnit: 'PG', BCD1: 4, BCD2: -4 },
{ period: '20200103', organisationUnit: 'PG', BCD1: 1, BCD2: 12 },
]),
);
});

it('convertPeriodToWeek', () => {
const transform = buildTransform(['convertPeriodToWeek']);
expect(transform(SINGLE_ANALYTIC)).toEqual([{ ...SINGLE_ANALYTIC[0], period: '2020W01' }]);
expect(transform(TransformTable.fromRows(SINGLE_ANALYTIC))).toEqual(
TransformTable.fromRows([{ ...SINGLE_ANALYTIC[0], period: '2020W01' }]),
);
});

it('convertEventDateToWeek', () => {
const transform = buildTransform(['convertEventDateToWeek']);
expect(transform(SINGLE_EVENT)).toEqual([{ ...SINGLE_EVENT[0], period: '2020W01' }]);
expect(transform(TransformTable.fromRows(SINGLE_EVENT))).toEqual(
TransformTable.fromRows([{ ...SINGLE_EVENT[0], period: '2020W01' }]),
);
});

it('insertNumberOfFacilitiesColumn', () => {
const transform = buildTransform(['insertNumberOfFacilitiesColumn'], {
facilityCountByOrgUnit: { TO: 14 },
});
expect(transform(SINGLE_ANALYTIC)).toEqual([{ ...SINGLE_ANALYTIC[0], numberOfFacilities: 14 }]);
expect(transform(TransformTable.fromRows(SINGLE_ANALYTIC))).toEqual(
TransformTable.fromRows([{ ...SINGLE_ANALYTIC[0], numberOfFacilities: 14 }]),
);
});
});

describe('insertSummaryRowAndColumn', () => {
it('inserts a summary row and summary column', () => {
const transform = buildTransform(['insertSummaryRowAndColumn']);
expect(transform(TRANSFORMED_SUMMARY_BINARY)).toEqual([
{ summaryColumn: '75.0%', dataElement: 'Male condoms', TO: 'N', FJ: 'N', NR: 'Y', KI: 'N' },
{ summaryColumn: '25.0%', dataElement: 'Female condoms', TO: 'N', FJ: 'Y', NR: 'Y', KI: 'Y' },
{
summaryColumn: '0.0%',
dataElement: 'Injectable contraceptives',
TO: 'Y',
FJ: 'Y',
},
{ TO: '66.7%', FJ: '33.3%', NR: '0.0%', KI: '50.0%' },
]);
expect(transform(TransformTable.fromRows(TRANSFORMED_SUMMARY_BINARY))).toEqual(
TransformTable.fromRows([
{ dataElement: 'Male condoms', TO: 'N', FJ: 'N', NR: 'Y', KI: 'N', summaryColumn: '75.0%' },
{
dataElement: 'Female condoms',
TO: 'N',
FJ: 'Y',
NR: 'Y',
KI: 'Y',
summaryColumn: '25.0%',
},
{
dataElement: 'Injectable contraceptives',
TO: 'Y',
FJ: 'Y',
summaryColumn: '0.0%',
},
{ TO: '66.7%', FJ: '33.3%', NR: '0.0%', KI: '50.0%' },
]),
);
});

it('only summarises columns that have only Y | N | undefined values', () => {
const transform = buildTransform(['insertSummaryRowAndColumn']);
expect(transform(TRANSFORMED_SUMMARY_VARIOUS)).toEqual([
{ summaryColumn: '66.7%', dataElement: 'Male condoms', TO: 'Yes', FJ: 'N', NR: 'Y', KI: 'N' },
{ summaryColumn: '0.0%', dataElement: 'Female condoms', TO: 'N', FJ: 'Y', NR: 'Y', KI: 'Y' },
{
summaryColumn: '0.0%',
dataElement: 'Injectable contraceptives',
TO: 'Y',
FJ: 'Y',
},
{ FJ: '33.3%', NR: '0.0%', KI: '50.0%' },
]);
expect(transform(TransformTable.fromRows(TRANSFORMED_SUMMARY_VARIOUS))).toEqual(
TransformTable.fromRows([
{
dataElement: 'Male condoms',
TO: 'Yes',
FJ: 'N',
NR: 'Y',
KI: 'N',
summaryColumn: '66.7%',
},
{
dataElement: 'Female condoms',
TO: 'N',
FJ: 'Y',
NR: 'Y',
KI: 'Y',
summaryColumn: '0.0%',
},
{
dataElement: 'Injectable contraceptives',
TO: 'Y',
FJ: 'Y',
summaryColumn: '0.0%',
},
{ FJ: '33.3%', NR: '0.0%', KI: '50.0%' },
]),
);
});
});
Loading

0 comments on commit b99c161

Please sign in to comment.