Skip to content

Commit

Permalink
#1079 - writing test for registration launched from add member. creat…
Browse files Browse the repository at this point in the history
…ed worklist factory to capture the creation of worklists to be used in test also.
  • Loading branch information
petmongrels committed Sep 7, 2023
1 parent 4945c2d commit be283c6
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/openchs-android/index.android.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* @flow */
import {AppRegistry} from 'react-native';
// import App from "./integrationTest/IntegrationTestApp";
import App from "./src/Avni";
AppRegistry.registerComponent('Avni', () => App);
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class BaseIntegrationTest {
this.getService(RuleService).init();
this.getService(PrivilegeService).deleteRevokedEntities();
}

setup() {
GlobalContext.getInstance().db.realmDb.deleteAll();
return this;
}
}

class TestDb {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class IntegrationTestApp extends Component {

if (this.state.isInitialisationDone) {
return <View style={{flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: "black"}}>
<Button title="Run Test" onPress={() => new PersonRegisterActionsIntegrationTest().last_page_of_registration_should_show_worklist_correctly(IntegrationTestContext)}/>
<Button title="Run Test" onPress={() => {
const personRegisterActionsIntegrationTest = new PersonRegisterActionsIntegrationTest();
// personRegisterActionsIntegrationTest.setup().person_registration_should_show_worklist_correctly(IntegrationTestContext);
personRegisterActionsIntegrationTest.setup().person_registration_via_add_member_should_show_worklist_correctly(IntegrationTestContext);
}}/>
</View>;
}
return <View style={{flex: 1, alignItems: 'center', justifyContent: 'center', color: "white", backgroundColor: "black"}}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TestConceptFactory from "../test/model/TestConceptFactory";
import {Gender, OrganisationConfig, AddressLevel, Concept, Form, SubjectType, WorkList, WorkLists, FormMapping} from "openchs-models";
import {Gender, Individual, OrganisationConfig, AddressLevel, Concept, Form, SubjectType, WorkList, WorkLists, FormMapping} from "openchs-models";
import BaseIntegrationTest from "./BaseIntegrationTest";
import TestFormFactory from "../test/model/form/TestFormFactory";
import TestFormElementGroupFactory from "../test/model/form/TestFormElementGroupFactory";
Expand All @@ -11,19 +11,20 @@ import TestGenderFactory from "../test/model/TestGenderFactory";
import TestFormMappingFactory from "../test/model/form/TestFormMappingFactory";
import Reducers from "../src/reducer";
import TestOrganisationConfigFactory from "../test/model/TestOrganisationConfigFactory";
import TestSubjectFactory from "../test/model/txn/TestSubjectFactory";
import WorklistsFactory from "../src/model/WorklistsFactory";

const rule = `({params, imports}) => {
console.log("Run started");
const workLists = params.workLists;
const context = params.context;
const WorkItem = imports.models.WorkItem;
const currentWorkItem = workLists.getCurrentWorkItem();
const currentWorkList = workLists.currentWorkList;
const age = _.get(context, 'entity.individual.age');
console.log("In the rule");
if (_.get(context, 'entity.individual.subjectType.name') === 'Family Member') {
const uniqueID = (new Date()).toString();
const covidSurvey = new WorkItem(uniqueID,
WorkItem.type.ENCOUNTER,
const covidSurvey = new WorkItem(uniqueID, WorkItem.type.ENCOUNTER,
{
encounterType: 'Covid Survey',
subjectUUID: _.get(context, 'entity.individual.uuid')
Expand All @@ -35,19 +36,27 @@ const rule = `({params, imports}) => {
};`;

class PersonRegisterActionsIntegrationTest extends BaseIntegrationTest {
last_page_of_registration_should_show_worklist_correctly(context) {
concept; addressLevel; gender;

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"}));
});
return this;
}

person_registration_should_show_worklist_correctly(context) {
context.starting(arguments);
let subjectType, gender, addressLevel;
let subjectType;
this.executeInWrite((db) => {
const concept = db.create(Concept, TestConceptFactory.createWithDefaults({dataType: Concept.dataType.Text}));
subjectType = db.create(SubjectType, TestSubjectTypeFactory.createWithDefaults({type: SubjectType.types.Person, name: 'Family Member'}));
const form = db.create(Form, TestFormFactory.createWithDefaults({formType: Form.formTypes.IndividualProfile}));
const formElementGroup = TestFormElementGroupFactory.create({form: form});
TestFormElementFactory.create({concept: concept, displayOrder: 1, formElementGroup: formElementGroup});
db.create(FormMapping, TestFormMappingFactory.createWithDefaults({subjectType: subjectType, form: form}))

addressLevel = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1}));
gender = db.create(Gender, TestGenderFactory.createWithDefaults({name: "Male"}));
db.create(OrganisationConfig, TestOrganisationConfigFactory.createWithDefaults({worklistUpdationRule: rule}));
});

Expand All @@ -57,9 +66,39 @@ class PersonRegisterActionsIntegrationTest extends BaseIntegrationTest {
this.dispatch({type: Actions.ON_LOAD, isDraftEntity: false, workLists: workLists});
this.dispatch({type: Actions.REGISTRATION_ENTER_FIRST_NAME, value: "foo"});
this.dispatch({type: Actions.REGISTRATION_ENTER_LAST_NAME, value: "bar"});
this.dispatch({type: Actions.REGISTRATION_ENTER_GENDER, value: gender});
this.dispatch({type: Actions.REGISTRATION_ENTER_GENDER, value: this.gender});
this.dispatch({type: Actions.REGISTRATION_ENTER_DOB, value: new Date()});
this.dispatch({type: Actions.REGISTRATION_ENTER_ADDRESS_LEVEL, value: this.addressLevel});
this.dispatch({type: Actions.NEXT, completed: () => {}});
let state = this.getState(Reducers.reducerKeys.personRegister);
console.log(state.workListState.workLists.currentWorkList.workItems);
// this.dispatch({type: Actions.SAVE, cb: () => {}});
context.ending(arguments);
}

person_registration_via_add_member_should_show_worklist_correctly(context) {
context.starting(arguments);
let householdSubjectType, personSubjectType, household;
this.executeInWrite((db) => {
householdSubjectType = db.create(SubjectType, TestSubjectTypeFactory.createWithDefaults({type: SubjectType.types.Household, name: 'Family', isGroup: true}));
personSubjectType = db.create(SubjectType, TestSubjectTypeFactory.createWithDefaults({type: SubjectType.types.Person, name: 'Family Member', isGroup: false}));
const form = db.create(Form, TestFormFactory.createWithDefaults({formType: Form.formTypes.IndividualProfile}));
const formElementGroup = TestFormElementGroupFactory.create({form: form});
TestFormElementFactory.create({concept: concept, displayOrder: 1, formElementGroup: formElementGroup});
db.create(FormMapping, TestFormMappingFactory.createWithDefaults({subjectType: householdSubjectType, form: form}))
db.create(OrganisationConfig, TestOrganisationConfigFactory.createWithDefaults({worklistUpdationRule: rule}));
household = db.create(Individual, TestSubjectFactory.createWithDefaults({subjectType: householdSubjectType, firstName: "House1", address: this.addressLevel}));
});

this.initialDataSetupComplete();

const workLists = WorklistsFactory.createForAddMember(null, );
this.dispatch({type: Actions.ON_LOAD, isDraftEntity: false, workLists: workLists});
this.dispatch({type: Actions.REGISTRATION_ENTER_FIRST_NAME, value: "foo"});
this.dispatch({type: Actions.REGISTRATION_ENTER_LAST_NAME, value: "bar"});
this.dispatch({type: Actions.REGISTRATION_ENTER_GENDER, value: this.gender});
this.dispatch({type: Actions.REGISTRATION_ENTER_DOB, value: new Date()});
this.dispatch({type: Actions.REGISTRATION_ENTER_ADDRESS_LEVEL, value: addressLevel});
this.dispatch({type: Actions.REGISTRATION_ENTER_ADDRESS_LEVEL, value: this.addressLevel});
this.dispatch({type: Actions.NEXT, completed: () => {}});
let state = this.getState(Reducers.reducerKeys.personRegister);
console.log(state.workListState.workLists.currentWorkList.workItems);
Expand Down
8 changes: 0 additions & 8 deletions packages/openchs-android/src/Avni.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React, {Component} from 'react';
import {StatusBar, StyleSheet, View} from 'react-native';
import Playground from "./Playground";
import Config from './framework/Config';
import Colors from "./views/primitives/Colors";
import { LogBox } from 'react-native';
import General from "./utility/General";

export default class Avni extends Component {

static styles = StyleSheet.create({
container: {
flex: 1,
Expand All @@ -19,11 +16,6 @@ export default class Avni extends Component {

render() {
LogBox.ignoreAllLogs();

if (Config.PLAYGROUND) {
console.log("=====================>>>>>>>Rendering Playground app component");
return <Playground/>;
}
General.logDebug("Avni", "=====================>>>>>>>Rendering main app component");
const App = require('./App').default;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AbstractComponent extends Component {
dispatchAction(action, params) {
const type = action instanceof Function ? action.Id : action;
if (General.canLog(General.LogLevel.Debug)) {
General.logDebug(`${this.constructor.name}::AC`, `Dispatching action: ${JSON.stringify(type)}`);
General.logDebugTemp(`${this.constructor.name}::AC`, `Dispatching action: ${JSON.stringify(type)}`);
}
const dispatchResult = this.context.getStore().dispatch({type, ...params});
if (General.canLog(General.LogLevel.Debug)) {
Expand Down
22 changes: 22 additions & 0 deletions packages/openchs-android/src/model/WorklistsFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import General from "../utility/General";
import {WorkItem, WorkList, WorkLists} from 'openchs-models';

class WorklistsFactory {
static createForAddMember(memberSubject, member, individualRelative, isHeadOfHousehold, relativeGender) {
const workItem = new WorkItem(General.randomUUID(), WorkItem.type.ADD_MEMBER,
{
uuid: memberSubject.uuid,
subjectTypeName: memberSubject.subjectType.name,
member: member,
individualRelative: individualRelative,
headOfHousehold: isHeadOfHousehold,
relativeGender: relativeGender,
groupSubjectUUID: member.groupSubject.uuid
}
);
const workList = new WorkList(`${memberSubject.subjectType.name} `, [workItem]);
return new WorkLists(workList);
}
}

export default WorklistsFactory;
10 changes: 6 additions & 4 deletions packages/openchs-android/src/service/AuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import StubbedAuthService from "./StubbedAuthService";
import CognitoAuthService from "./CognitoAuthService";
import KeycloakAuthService from "./KeycloakAuthService";
import { IDP_PROVIDERS } from "../model/IdpProviders";
import General from "../utility/General";

@Service("authService")
class AuthService extends BaseService {
Expand Down Expand Up @@ -42,11 +43,12 @@ class AuthService extends BaseService {
const settings = this.settingsService.getSettings();
const serverURL = settings.serverURL;
const url = `${serverURL}/idp-details`;
return getJSON(url, true).then(( authDetails ) => {
return getJSON(url, true).then(( idpDetails ) => {
let newSettings = settings.clone();
newSettings.idpType = authDetails.idpType;
newSettings = _.merge(newSettings, this._updateCognitoSettings(authDetails.cognito));
newSettings = _.merge(newSettings, this._updateKeycloakSettings(authDetails.keycloak));
newSettings.idpType = idpDetails.idpType;
newSettings = _.merge(newSettings, this._updateCognitoSettings(idpDetails.cognito));
newSettings = _.merge(newSettings, this._updateKeycloakSettings(idpDetails.keycloak));
General.logDebug("AuthService", newSettings);
this.settingsService.saveOrUpdate(newSettings);
return newSettings;
});
Expand Down
1 change: 0 additions & 1 deletion packages/openchs-android/src/service/CognitoAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import AuthenticationError, {NO_USER} from "./AuthenticationError";
import General from "../utility/General";
import ErrorHandler from "../utility/ErrorHandler";
import UserInfoService from "./UserInfoService";
import AuthService from "./AuthService";
import BaseAuthProviderService from "./BaseAuthProviderService";

@Service("cognitoAuthService")
Expand Down
1 change: 0 additions & 1 deletion packages/openchs-android/src/service/EncryptionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {randomBytes} from "react-native-randombytes";
import GlobalContext from "../GlobalContext";
import {getGenericPassword} from "react-native-keychain";


const CREDENTIAL_USERNAME = "avni-user";

@Service("encryptionService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import GenericDashboardView from "../program/GenericDashboardView";
import AbstractDataEntryState from "../../state/AbstractDataEntryState";
import ValidationErrorMessage from "../form/ValidationErrorMessage";
import WorkListState from "../../state/WorkListState";
import WorklistsFactory from "../../model/WorklistsFactory";

@Path('/addNewMemberView')
class AddNewMemberView extends AbstractComponent {
Expand Down Expand Up @@ -117,20 +118,9 @@ class AddNewMemberView extends AbstractComponent {
} else {
const memberSubject = this.state.member.memberSubject;
if (!_.isEmpty(this.state.validationResults)) {
return
return;
}
CHSNavigator.navigateToRegisterView(this, {workLists: new WorkLists(new WorkList(`${memberSubject.subjectType.name} `,
[new WorkItem(General.randomUUID(), WorkItem.type.ADD_MEMBER,
{
uuid: memberSubject.uuid,
subjectTypeName: memberSubject.subjectType.name,
member: this.state.member,
individualRelative: this.state.individualRelative,
headOfHousehold: this.isHeadOfHousehold(),
relativeGender: this.state.relativeGender,
groupSubjectUUID: this.state.member.groupSubject.uuid,
}
)]))});
CHSNavigator.navigateToRegisterView(this, {workLists: WorklistsFactory.createForAddMember(memberSubject, this.state.member, this.state.individualRelative, this.isHeadOfHousehold(), this.state.relativeGender)});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {SubjectType} from "openchs-models";
import General from "../../src/utility/General";

class TestSubjectTypeFactory {
static createWithDefaults({type, name = General.randomUUID()}) {
static createWithDefaults({type, name = General.randomUUID(), isGroup = false}) {
const subjectType = new SubjectType();
subjectType.uuid = General.randomUUID();
subjectType.name = name;
subjectType.type = type;
subjectType.group = isGroup;
return subjectType;
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/openchs-android/test/model/txn/TestSubjectFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Individual} from 'openchs-models';
import General from "../../../src/utility/General";

class TestSubjectFactory {
static createWithDefaults({subjectType, firstName, lastName, address}) {
const individual = new Individual();
individual.uuid = General.randomUUID();
individual.subjectType = subjectType;
individual.firstName = firstName;
individual.lastName = lastName;
individual.lowestAddressLevel = address;
return individual;
}
}

export default TestSubjectFactory;

0 comments on commit be283c6

Please sign in to comment.