From 8c890125d29137ba95342c5a8ea77826a2e72a46 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 14 Nov 2023 20:23:25 +0530 Subject: [PATCH] #1185 - supporting classes for the integration test for report cards. --- .../integrationTest/IntegrationTestApp.js | 3 +- .../ReportCardServiceIntegrationTest.js | 77 +++++++++++++++++++ packages/openchs-android/package-lock.json | 14 ++-- .../src/model/DashboardReportRuleInput.js | 4 +- .../customDashboard/ReportCardService.js | 8 +- .../src/utility/JsonStringify.js | 2 +- .../model/TestEntityApprovalStatusFactory.js | 16 ++++ .../model/TestOrganisationConfigFactory.js | 2 +- ...stDashboardReportFilterRuleInputFactory.js | 16 ++++ .../TestDashboardReportRuleInputFactory.js | 11 +++ .../reportNDashboard/TestReportCardFactory.js | 15 ++++ .../TestStandardReportCardTypeFactory.js | 13 ++++ 12 files changed, 166 insertions(+), 15 deletions(-) create mode 100644 packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js create mode 100644 packages/openchs-android/test/model/TestEntityApprovalStatusFactory.js create mode 100644 packages/openchs-android/test/model/reportNDashboard/TestDashboardReportFilterRuleInputFactory.js create mode 100644 packages/openchs-android/test/model/reportNDashboard/TestDashboardReportRuleInputFactory.js create mode 100644 packages/openchs-android/test/model/reportNDashboard/TestReportCardFactory.js create mode 100644 packages/openchs-android/test/model/reportNDashboard/TestStandardReportCardTypeFactory.js diff --git a/packages/openchs-android/integrationTest/IntegrationTestApp.js b/packages/openchs-android/integrationTest/IntegrationTestApp.js index 154d342fd..38f5e7202 100644 --- a/packages/openchs-android/integrationTest/IntegrationTestApp.js +++ b/packages/openchs-android/integrationTest/IntegrationTestApp.js @@ -12,6 +12,7 @@ import IntegrationTestRunner, {TestSuite} from "./IntegrationTestRunner"; import UtilTest from "./UtilTest"; import UserInfoServiceTest from "./UserInfoServiceTest"; import RealmProxyTest from "./RealmProxyTest"; +import ReportCardServiceIntegrationTest from "./ReportCardServiceIntegrationTest"; const itemCommonStyle = { padding: 10, @@ -61,7 +62,7 @@ class IntegrationTestApp extends Component { super(props, context); FileSystem.init(); this.getBean = this.getBean.bind(this); - this.integrationTestRunner = new IntegrationTestRunner(UserInfoServiceTest, DatabaseTest, PersonRegisterActionsIntegrationTest, UtilTest, RealmProxyTest); + this.integrationTestRunner = new IntegrationTestRunner(ReportCardServiceIntegrationTest, UserInfoServiceTest, DatabaseTest, PersonRegisterActionsIntegrationTest, UtilTest, RealmProxyTest); this.state = {isInitialisationDone: false, integrationTests: this.integrationTestRunner.testSuite}; } diff --git a/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js b/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js new file mode 100644 index 000000000..ab721b645 --- /dev/null +++ b/packages/openchs-android/integrationTest/ReportCardServiceIntegrationTest.js @@ -0,0 +1,77 @@ +import BaseIntegrationTest from "./BaseIntegrationTest"; +import { + EntityApprovalStatus, + AddressLevel, + Concept, + Form, + StandardReportCardType, + FormElement, + FormElementGroup, + FormMapping, + Gender, + Individual, + OrganisationConfig, + Settings, + SubjectType, + CustomFilter, + ReportCard +} from "openchs-models"; +import TestConceptFactory from "../test/model/TestConceptFactory"; +import TestAddressLevelFactory from "../test/model/TestAddressLevelFactory"; +import TestGenderFactory from "../test/model/TestGenderFactory"; +import TestSettingsFactory from "../test/model/user/TestSettingsFactory"; +import TestSubjectTypeFactory from "../test/model/TestSubjectTypeFactory"; +import TestFormFactory from "../test/model/form/TestFormFactory"; +import TestFormElementGroupFactory from "../test/model/form/TestFormElementGroupFactory"; +import TestFormElementFactory from "../test/model/form/TestFormElementFactory"; +import TestKeyValueFactory from "../test/model/TestKeyValueFactory"; +import TestFormMappingFactory from "../test/model/form/TestFormMappingFactory"; +import TestOrganisationConfigFactory from "../test/model/TestOrganisationConfigFactory"; +import TestSubjectFactory from "../test/model/txn/TestSubjectFactory"; +import TestObsFactory from "../test/model/TestObsFactory"; +import TestEntityApprovalStatusFactory from "../test/model/TestEntityApprovalStatusFactory"; +import ReportCardService from "../src/service/customDashboard/ReportCardService"; +import TestStandardReportCardTypeFactory from "../test/model/reportNDashboard/TestStandardReportCardTypeFactory"; +import TestReportCardFactory from "../test/model/reportNDashboard/TestReportCardFactory"; +import TestDashboardReportRuleInputFactory from "../test/model/reportNDashboard/TestDashboardReportRuleInputFactory"; +import TestDashboardReportFilterRuleInputFactory from "../test/model/reportNDashboard/TestDashboardReportFilterRuleInputFactory"; + +class ReportCardServiceIntegrationTest extends BaseIntegrationTest { + approvedCard; subjectType; + + setup() { + super.setup(); + this.executeInWrite((db) => { + this.concept = db.create(Concept, TestConceptFactory.createWithDefaults({dataType: Concept.dataType.Text})); + this.addressLevel = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1})); + this.gender = db.create(Gender, TestGenderFactory.createWithDefaults({name: "Male"})); + db.create(Settings, TestSettingsFactory.createWithDefaults({})); + + this.subjectType = db.create(SubjectType, TestSubjectTypeFactory.createWithDefaults({type: SubjectType.types.Person, name: 'Beneficiary'})); + const form = db.create(Form, TestFormFactory.createWithDefaults({formType: Form.formTypes.IndividualProfile})); + const formElementGroup = db.create(FormElementGroup, TestFormElementGroupFactory.create({form: form})); + db.create(FormElement, TestFormElementFactory.create({ + uuid: "FOO", + concept: this.concept, + displayOrder: 1, + formElementGroup: formElementGroup, + mandatory: true, + keyValues: [TestKeyValueFactory.create({key: "unique", value: "true"})] + })); + db.create(FormMapping, TestFormMappingFactory.createWithDefaults({subjectType: this.subjectType, form: form})); + db.create(OrganisationConfig, TestOrganisationConfigFactory.createWithDefaults({})); + const subject = db.create(Individual, TestSubjectFactory.createWithDefaults({subjectType: this.subjectType, address: this.addressLevel, firstName: "foo", lastName: "bar", observations: [TestObsFactory.create({concept: this.concept, valueJSON: JSON.stringify(this.concept.getValueWrapperFor("ABC"))})]})); + db.create(EntityApprovalStatus, TestEntityApprovalStatusFactory.create({entityType: EntityApprovalStatus.entityType.Subject, entityUUID: subject.uuid, entityTypeUuid: this.subjectType.uuid})); + + const approvedCardType = db.create(StandardReportCardType, TestStandardReportCardTypeFactory.create({name: StandardReportCardType.type.Approved})); + this.approvedCard = db.create(ReportCard, TestReportCardFactory.create({name: "foo", standardReportCardType: approvedCardType})); + }); + } + + getResultForApprovalCardsType() { + const dashboardReportRuleInput = TestDashboardReportRuleInputFactory.create({filterValues: [TestDashboardReportFilterRuleInputFactory.create({type: CustomFilter.type.Concept, filterValue: [this.addressLevel]})]}); + this.getService(ReportCardService).getReportCardCount(this.approvedCard, dashboardReportRuleInput); + } +} + +export default ReportCardServiceIntegrationTest; diff --git a/packages/openchs-android/package-lock.json b/packages/openchs-android/package-lock.json index 0d249c272..a4be8d02f 100644 --- a/packages/openchs-android/package-lock.json +++ b/packages/openchs-android/package-lock.json @@ -19,7 +19,7 @@ "@react-native-firebase/analytics": "15.2.0", "@react-native-firebase/app": "15.2.0", "amazon-cognito-identity-js": "6.3.1", - "avni-health-modules": "^0.0.19", + "avni-health-modules": "^0.0.22", "base-64": "^1.0.0", "bugsnag-react-native": "2.23.10", "color": "4.2.3", @@ -5754,9 +5754,9 @@ } }, "node_modules/avni-health-modules": { - "version": "0.0.19", - "resolved": "https://registry.npmjs.org/avni-health-modules/-/avni-health-modules-0.0.19.tgz", - "integrity": "sha512-Q0JmKC/K5uIIWP9nmmKozF/EmoKG9bviVfUfYl2OFoh/zRqYZ4stRLwlGn70APBIOuyfiB1GGHsQpXmSxFXjPg==", + "version": "0.0.22", + "resolved": "https://registry.npmjs.org/avni-health-modules/-/avni-health-modules-0.0.22.tgz", + "integrity": "sha512-GzKDk8c22Eucx4gB9SdY8h4kSg/jV8T8tCOiVKnSbIyzcozJfWH7oEHNJ03p3O0kfasdKrsIZNOtQNwFG9wxcw==", "peerDependencies": { "lodash": "*", "moment": "*", @@ -26863,9 +26863,9 @@ "dev": true }, "avni-health-modules": { - "version": "0.0.19", - "resolved": "https://registry.npmjs.org/avni-health-modules/-/avni-health-modules-0.0.19.tgz", - "integrity": "sha512-Q0JmKC/K5uIIWP9nmmKozF/EmoKG9bviVfUfYl2OFoh/zRqYZ4stRLwlGn70APBIOuyfiB1GGHsQpXmSxFXjPg==" + "version": "0.0.22", + "resolved": "https://registry.npmjs.org/avni-health-modules/-/avni-health-modules-0.0.22.tgz", + "integrity": "sha512-GzKDk8c22Eucx4gB9SdY8h4kSg/jV8T8tCOiVKnSbIyzcozJfWH7oEHNJ03p3O0kfasdKrsIZNOtQNwFG9wxcw==" }, "aws-sdk": { "version": "2.1258.0", diff --git a/packages/openchs-android/src/model/DashboardReportRuleInput.js b/packages/openchs-android/src/model/DashboardReportRuleInput.js index cd9b160ec..e0a8d87b8 100644 --- a/packages/openchs-android/src/model/DashboardReportRuleInput.js +++ b/packages/openchs-android/src/model/DashboardReportRuleInput.js @@ -1,4 +1,4 @@ -// These classes are created to document the contract with the rules. Objects has been used in place of arrays to allow for flexibility in contract in the future. +// Objects has been used in place of arrays to allow for flexibility in contract in the future. export class DashboardReportFilterRuleInput { type; @@ -10,7 +10,7 @@ export class DashboardReportFilterRuleInput { } class DashboardReportRuleInput { - filterValues; + filterValues; // array of DashboardReportFilterRuleInput constructor(filterValues) { this.filterValues = filterValues; diff --git a/packages/openchs-android/src/service/customDashboard/ReportCardService.js b/packages/openchs-android/src/service/customDashboard/ReportCardService.js index 2b415ddb4..76d8d08da 100644 --- a/packages/openchs-android/src/service/customDashboard/ReportCardService.js +++ b/packages/openchs-android/src/service/customDashboard/ReportCardService.js @@ -8,6 +8,7 @@ import CommentService from "../comment/CommentService"; import _ from "lodash"; import TaskService from "../task/TaskService"; import General from "../../utility/General"; +import {JSONStringify} from "../../utility/JsonStringify"; function getApprovalStatusForType(type) { const typeToStatusMap = { @@ -29,8 +30,8 @@ class ReportCardService extends BaseService { return ReportCard.schema.name; } - getCountForApprovalCardsType(type, filterValuePairs) { - const {result} = this.getResultForApprovalCardsType(type, filterValuePairs); + getCountForApprovalCardsType(type) { + const {result} = this.getResultForApprovalCardsType(type); return { primaryValue: _.map(result, ({data}) => data.length).reduce((total, l) => total + l, 0), secondaryValue: null, @@ -38,7 +39,7 @@ class ReportCardService extends BaseService { }; } - getResultForApprovalCardsType(type, filterValuePairs) { + getResultForApprovalCardsType(type) { const approvalStatus = getApprovalStatusForType(type); return this.getService(EntityApprovalStatusService).getAllEntitiesWithStatus(approvalStatus); } @@ -107,6 +108,7 @@ class ReportCardService extends BaseService { getReportCardCount(reportCard, ruleInputArray) { General.logDebug("ReportCardService", `Executing report card: ${reportCard.name}`); + General.logDebugTemp("ReportCardService", JSONStringify(ruleInputArray, 5)); const standardReportCardType = reportCard.standardReportCardType; switch (true) { case _.isNil(standardReportCardType) : diff --git a/packages/openchs-android/src/utility/JsonStringify.js b/packages/openchs-android/src/utility/JsonStringify.js index 72ab34f10..17aee42da 100644 --- a/packages/openchs-android/src/utility/JsonStringify.js +++ b/packages/openchs-android/src/utility/JsonStringify.js @@ -42,7 +42,7 @@ const duckCheckNativeRealmCollection = function (obj) { const JSONStringifyInternal = function (obj, depth, objectMap: Map, arrayWidth, objectKey) { if (depth === 0) - return undefined; + return "BELOW_DEPTH"; // Boolean and Number behave in a same way and String we need to add extra quotes if (restOfDataTypes(obj)) { const passQuotes = isString(obj) ? `"` : ''; diff --git a/packages/openchs-android/test/model/TestEntityApprovalStatusFactory.js b/packages/openchs-android/test/model/TestEntityApprovalStatusFactory.js new file mode 100644 index 000000000..bdcb2334e --- /dev/null +++ b/packages/openchs-android/test/model/TestEntityApprovalStatusFactory.js @@ -0,0 +1,16 @@ +import {EntityApprovalStatus} from 'openchs-models'; +import General from "../../src/utility/General"; + +class TestEntityApprovalStatusFactory { + static create({uuid = General.randomUUID(), entityType, entityUUID, entityTypeUuid, statusDateTime = new Date()}) { + const entityApprovalStatus = new EntityApprovalStatus(); + entityApprovalStatus.uuid = uuid; + entityApprovalStatus.entityUUID = entityUUID; + entityApprovalStatus.entityType = entityType; + entityApprovalStatus.entityTypeUuid = entityTypeUuid; + entityApprovalStatus.statusDateTime = statusDateTime; + return entityApprovalStatus; + } +} + +export default TestEntityApprovalStatusFactory; diff --git a/packages/openchs-android/test/model/TestOrganisationConfigFactory.js b/packages/openchs-android/test/model/TestOrganisationConfigFactory.js index cb5917292..1feeb4596 100644 --- a/packages/openchs-android/test/model/TestOrganisationConfigFactory.js +++ b/packages/openchs-android/test/model/TestOrganisationConfigFactory.js @@ -2,7 +2,7 @@ import {OrganisationConfig} from 'openchs-models'; import General from "../../src/utility/General"; class TestOrganisationConfigFactory { - static createWithDefaults({worklistUpdationRule = null, settings = "{\"languages\": [\"en\"]}"}) { + static createWithDefaults({worklistUpdationRule = "", settings = "{\"languages\": [\"en\"]}"}) { const organisationConfig = new OrganisationConfig(); organisationConfig.uuid = General.randomUUID(); organisationConfig.settings = settings; diff --git a/packages/openchs-android/test/model/reportNDashboard/TestDashboardReportFilterRuleInputFactory.js b/packages/openchs-android/test/model/reportNDashboard/TestDashboardReportFilterRuleInputFactory.js new file mode 100644 index 000000000..3cbb53d93 --- /dev/null +++ b/packages/openchs-android/test/model/reportNDashboard/TestDashboardReportFilterRuleInputFactory.js @@ -0,0 +1,16 @@ +import {DashboardReportFilterRuleInput} from "../../../src/model/DashboardReportRuleInput"; + +class TestDashboardReportFilterRuleInputFactory { + static create({type, dataType, subjectType, groupSubjectTypeFilter, observationBasedFilter, filterValue}) { + const dashboardReportFilterRuleInput = new DashboardReportFilterRuleInput(); + dashboardReportFilterRuleInput.type = type; + dashboardReportFilterRuleInput.dataType = dataType; + dashboardReportFilterRuleInput.subjectType = subjectType; + dashboardReportFilterRuleInput.groupSubjectTypeFilter = groupSubjectTypeFilter; + dashboardReportFilterRuleInput.observationBasedFilter = observationBasedFilter; + dashboardReportFilterRuleInput.filterValue = filterValue; + return dashboardReportFilterRuleInput; + } +} + +export default TestDashboardReportFilterRuleInputFactory; diff --git a/packages/openchs-android/test/model/reportNDashboard/TestDashboardReportRuleInputFactory.js b/packages/openchs-android/test/model/reportNDashboard/TestDashboardReportRuleInputFactory.js new file mode 100644 index 000000000..dd20b63ee --- /dev/null +++ b/packages/openchs-android/test/model/reportNDashboard/TestDashboardReportRuleInputFactory.js @@ -0,0 +1,11 @@ +import DashboardReportRuleInput from "../../../src/model/DashboardReportRuleInput"; + +class TestDashboardReportRuleInputFactory { + static create({filterValues = []}) { + const dashboardReportRuleInput = new DashboardReportRuleInput(); + dashboardReportRuleInput.filterValues = filterValues; + return dashboardReportRuleInput; + } +} + +export default TestDashboardReportRuleInputFactory; diff --git a/packages/openchs-android/test/model/reportNDashboard/TestReportCardFactory.js b/packages/openchs-android/test/model/reportNDashboard/TestReportCardFactory.js new file mode 100644 index 000000000..fa58c94b1 --- /dev/null +++ b/packages/openchs-android/test/model/reportNDashboard/TestReportCardFactory.js @@ -0,0 +1,15 @@ +import {ReportCard} from 'openchs-models'; +import General from "../../../src/utility/General"; + +class TestReportCardFactory { + static create({uuid = General.randomUUID(), name, standardReportCardType, colour = "red"}) { + const reportCard = new ReportCard(); + reportCard.uuid = uuid; + reportCard.name = name; + reportCard.standardReportCardType = standardReportCardType; + reportCard.colour = colour; + return reportCard; + } +} + +export default TestReportCardFactory; diff --git a/packages/openchs-android/test/model/reportNDashboard/TestStandardReportCardTypeFactory.js b/packages/openchs-android/test/model/reportNDashboard/TestStandardReportCardTypeFactory.js new file mode 100644 index 000000000..b49b19e30 --- /dev/null +++ b/packages/openchs-android/test/model/reportNDashboard/TestStandardReportCardTypeFactory.js @@ -0,0 +1,13 @@ +import {StandardReportCardType} from 'openchs-models'; +import General from "../../../src/utility/General"; + +class TestStandardReportCardTypeFactory { + static create({name, uuid = General.randomUUID()}) { + const standardReportCardType = new StandardReportCardType(); + standardReportCardType.uuid = uuid; + standardReportCardType.name = name; + return standardReportCardType; + } +} + +export default TestStandardReportCardTypeFactory;