Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rn 588 data broker tei #4045

Merged
merged 19 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { expect } from 'chai';
import { flatten } from 'lodash';
import sinon from 'sinon';

import { generateTestId } from '@tupaia/database';
import { generateTestId, createModelsStub as baseCreateModelsStub } from '@tupaia/database';
import { SurveyResponseImporter } from '../../../apiV2/utilities';
import * as SurveyResponse from '../../../apiV2/surveyResponse';

Expand Down Expand Up @@ -45,24 +45,12 @@ const ALL_RESULTS = flatten(Object.values(RESULTS_BY_SURVEY_ID));
const TIMESTAMP = 1570000000;
const USER_ID = 'userId';

const transactingModelsStub = {
transacting: true,
};

const createModelsStub = () => {
const findOneSurveyStub = sinon.stub();
findOneSurveyStub
.withArgs({ name: SURVEY1.name })
.returns(SURVEY1)
.withArgs({ name: SURVEY2.name })
.returns(SURVEY2);

return {
return baseCreateModelsStub({
survey: {
findOne: findOneSurveyStub,
records: [SURVEY1, SURVEY2],
},
wrapInTransaction: callback => callback(transactingModelsStub),
};
});
};

const createResponseExtractors = () => {
Expand Down
13 changes: 5 additions & 8 deletions packages/central-server/src/tests/dhis/pushers/entity/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* Copyright (c) 2019 Beyond Essential Systems Pty Ltd
*/

import { keyBy } from 'lodash';
import sinon from 'sinon';

import { DhisApi, DHIS2_RESOURCE_TYPES } from '@tupaia/dhis-api';
import { createModelsStub as baseCreateModelsStub } from '@tupaia/database';

const { ORGANISATION_UNIT, TRACKED_ENTITY_ATTRIBUTE, TRACKED_ENTITY_TYPE } = DHIS2_RESOURCE_TYPES;

Expand All @@ -31,17 +31,14 @@ export const createEntityStub = (
};

export const createModelsStub = ({ entityRecords, dhisSyncLogRecords } = {}) => {
const entitiesById = keyBy(entityRecords, 'id');
const syncLogsByRecordId = keyBy(dhisSyncLogRecords, 'record_id');

return {
return baseCreateModelsStub({
entity: {
findById: id => entitiesById[id],
records: entityRecords || [],
},
dhisSyncLog: {
findOne: ({ record_id: recordId }) => syncLogsByRecordId[recordId],
records: dhisSyncLogRecords || [],
},
};
});
};

export const createDhisApiStub = ({
Expand Down
35 changes: 21 additions & 14 deletions packages/data-broker/src/__tests__/DataBroker/DataBroker.stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { createJestMockInstance } from '@tupaia/utils';
import { createModelsStub as baseCreateModelsStub } from '@tupaia/database';
import * as CreateService from '../../services/createService';

export const stubCreateService = services =>
Expand Down Expand Up @@ -42,17 +43,23 @@ export const createServiceStub = serviceData => {
return createJestMockInstance('@tupaia/data-broker/src/services/Service', 'Service', { pull });
};

export const createModelsStub = (dataElements, dataGroups) => ({
dataElement: {
find: spec => dataElements.filter(({ code }) => spec.code.includes(code)),
getTypes: () => ({
DATA_ELEMENT: 'dataElement',
DATA_GROUP: 'dataGroup',
SYNC_GROUP: 'syncGroup',
}),
},
dataGroup: {
find: spec => dataGroups.filter(({ code }) => spec.code.includes(code)),
getDataElementsInDataGroup: () => [],
},
});
export const createModelsStub = (dataElements, dataGroups) => {
return baseCreateModelsStub({
dataElement: {
records: dataElements,
extraMethods: {
getTypes: () => ({
DATA_ELEMENT: 'dataElement',
DATA_GROUP: 'dataGroup',
SYNC_GROUP: 'syncGroup',
}),
},
},
dataGroup: {
records: dataGroups,
extraMethods: {
getDataElementsInDataGroup: () => [],
},
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jest.mock('@tupaia/database', () => ({
},
TupaiaDatabase: () => {},
ModelRegistry: jest.fn().mockImplementation(() => mockModels),
createModelsStub: jest.requireActual('@tupaia/database').createModelsStub, // don't mock needed testUtility
}));

jest.mock('@tupaia/server-boilerplate', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,30 @@ export const DATA_GROUPS = {
}),
};

export const ENTITIES = {
DL_FACILITY_A: {
code: 'DL_FACILITY_A',
name: 'DL FACILITY A',
type: 'facility',
metadata: {},
},
DL_HOUSEHOLD_1: {
code: 'DL_HOUSEHOLD_1',
name: 'DL HOUSEHOLD 1',
type: 'household',
metadata: {
dhis: { isDataRegional: false, trackedEntityId: 'tracked_entity_id_dl_household_1' },
},
},
};

export const DATA_VALUES = {
POP01: { code: 'POP01', value: '1' },
POP02: { code: 'POP02', value: '2' },
DIF01: { code: 'DIF01', value: '3' },
};

export const DATA_ELEMENTS = {
export const DHIS_RESPONSE_DATA_ELEMENTS = {
POP01: { code: 'POP01', uid: 'id000POP01', name: 'Population 1' },
POP02: { code: 'POP02', uid: 'id000POP02', name: 'Population 2' },
DIF01_DHIS: { code: 'DIF01_DHIS', uid: 'id000DIF01_DHIS', name: 'Different 1' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd
*/

import { createModelsStub as baseCreateModelsStub } from '@tupaia/database';
import * as GetDhisApiInstance from '../../../services/dhis/getDhisApiInstance';
import {
DATA_ELEMENTS_BY_GROUP,
DATA_ELEMENTS,
DHIS_RESPONSE_DATA_ELEMENTS,
DATA_SOURCES,
DATA_GROUPS,
ENTITIES,
SERVER_NAME,
} from './DhisService.fixtures';
import { createJestMockInstance } from '../../../../../utils/src/testUtilities';
Expand All @@ -33,7 +35,10 @@ export const stubDhisApi = ({
fetchDataElements: jest
.fn()
.mockImplementation(async codes =>
codes.map(code => ({ code, id: DATA_ELEMENTS[code].uid, valueType: 'NUMBER' }), {}),
codes.map(
code => ({ code, id: DHIS_RESPONSE_DATA_ELEMENTS[code].uid, valueType: 'NUMBER' }),
{},
),
),
getServerName: jest.fn().mockReturnValue(SERVER_NAME),
getResourceTypes: jest.fn().mockReturnValue({ DATA_ELEMENT: 'dataElement' }),
Expand All @@ -43,24 +48,26 @@ export const stubDhisApi = ({
return dhisApi;
};

export const createModelsStub = () => ({
dataElement: createDataElementModelsStub(),
dataGroup: createDataGroupModelsStub(),
});

export const createDataElementModelsStub = () => ({
find: async specs =>
Object.values(DATA_SOURCES).filter(
({ code, type }) => specs.code.includes(code) && specs.type === type,
),
getTypes: () => ({ DATA_ELEMENT: 'dataElement', DATA_GROUP: 'dataGroup' }),
getDhisDataTypes: () => ({ DATA_ELEMENT: 'DataElement', INDICATOR: 'Indicator' }),
});

export const createDataGroupModelsStub = () => ({
find: async specs => Object.values(DATA_GROUPS).filter(({ code }) => specs.code.includes(code)),
getDataElementsInDataGroup: async groupCode => DATA_ELEMENTS_BY_GROUP[groupCode],
});
export const createModelsStub = () => {
return baseCreateModelsStub({
dataElement: {
records: Object.values(DATA_SOURCES),
extraMethods: {
getTypes: () => ({ DATA_ELEMENT: 'dataElement', DATA_GROUP: 'dataGroup' }),
getDhisDataTypes: () => ({ DATA_ELEMENT: 'DataElement', INDICATOR: 'Indicator' }),
},
},
dataGroup: {
records: Object.values(DATA_GROUPS),
extraMethods: {
getDataElementsInDataGroup: async groupCode => DATA_ELEMENTS_BY_GROUP[groupCode],
},
},
entity: {
records: Object.values(ENTITIES),
},
});
};

/**
* Reverse engineers the DHIS2 aggregate data response given the expected analytics
Expand All @@ -76,7 +83,7 @@ export const buildDhisAnalyticsResponse = analytics => {
const items = dataElementsInAnalytics
.map(dataElement => {
const { dataElementCode: dhisCode } = DATA_SOURCES[dataElement];
return DATA_ELEMENTS[dhisCode];
return DHIS_RESPONSE_DATA_ELEMENTS[dhisCode];
})
.reduce((itemAgg, { uid, code, name }) => {
const newItem = { uid, code, name, dimensionItemType: 'DATA_ELEMENT' };
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd
*/

export const EVENT_ANALYTICS_WITH_DATA_VALUES = {
const EVENT_ANALYTICS_WITH_DATA_VALUES = {
headers: [
{ name: 'psi', column: 'Event', valueType: 'TEXT' },
{ name: 'eventdate', column: 'Event date', valueType: 'DATE' },
Expand Down Expand Up @@ -52,7 +52,7 @@ export const EVENT_ANALYTICS_WITH_DATA_VALUES = {
],
};

export const EVENT_ANALYTICS_NO_DATA_VALUES = {
const EVENT_ANALYTICS_NO_DATA_VALUES = {
headers: [
{ name: 'psi', column: 'Event', valueType: 'TEXT' },
{ name: 'eventdate', column: 'Event date', valueType: 'DATE' },
Expand All @@ -79,7 +79,7 @@ export const EVENT_ANALYTICS_NO_DATA_VALUES = {
],
};

export const EVENT_ANALYTICS_EMPTY_ROWS = {
const EVENT_ANALYTICS_EMPTY_ROWS = {
headers: [
{ name: 'psi', column: 'Event', valueType: 'TEXT' },
{ name: 'eventdate', column: 'Event date', valueType: 'DATE' },
Expand Down Expand Up @@ -109,8 +109,51 @@ export const EVENT_ANALYTICS_EMPTY_ROWS = {
rows: [],
};

const EVENT_ANALYTICS_TRACKED_ENTITY_ID = {
headers: [
{ name: 'psi', column: 'Event', valueType: 'TEXT' },
{ name: 'eventdate', column: 'Event date', valueType: 'DATE' },
{ name: 'ouname', column: 'Organisation unit name', valueType: 'TEXT' },
{ name: 'oucode', column: 'Organisation unit code', valueType: 'TEXT' },
{ name: 'ou', column: 'Organisation unit', valueType: 'TEXT' },
{ name: 'tei', column: 'Tracked entity instance', valueType: 'TEXT' },
],
metaData: {
items: {
tonga_dhisId: { name: 'Tonga' },
ou: { name: 'Organisation unit' },
program_dhisId: { name: 'BCD1 Survey' },
},
dimensions: {
pe: [],
ou: ['tonga_dhisId'],
},
},
width: 7,
height: 0,
rows: [
[
'event1_dhisId',
'2020-02-06T10:18:00.000',
'Nukunuku',
'TO_Nukuhc',
'nukunuku_dhisId',
'tracked_entity_id_dl_household_1',
],
[
'event2_dhisId',
'2020-02-07T14:33:00.000',
'Haveluloto',
'TO_HvlMCH',
'houma_dhisId',
'tracked_entity_id_dl_household_2',
],
],
};

export const EVENT_ANALYTICS = {
withDataValues: EVENT_ANALYTICS_WITH_DATA_VALUES,
noDataValues: EVENT_ANALYTICS_NO_DATA_VALUES,
emptyRows: EVENT_ANALYTICS_EMPTY_ROWS,
withTrackedEntityIds: EVENT_ANALYTICS_TRACKED_ENTITY_ID,
};
Loading