Skip to content

Commit

Permalink
#1142 | Adding integration test to check mandatory fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhas Vishwanath committed Nov 7, 2023
1 parent 2e9db09 commit 52ea87d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion packages/openchs-android/integrationTest/RealmProxyTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import BaseIntegrationTest from "./BaseIntegrationTest";
import {Observation} from "openchs-models";
import {AddressLevel, Observation, Individual, SubjectType} from "openchs-models";
import {assert} from "chai";
import TestObsFactory from "../test/model/TestObsFactory";
import TestSubjectFactory from "../test/model/txn/TestSubjectFactory";
import TestAddressLevelFactory from "../test/model/TestAddressLevelFactory";
import moment from "moment/moment";
import TestSubjectTypeFactory from "../test/model/TestSubjectTypeFactory";

function shouldFail(baseIntegrationTest, obs, updateMode) {
baseIntegrationTest.executeInWrite((db) => {
Expand All @@ -13,6 +17,31 @@ function shouldFail(baseIntegrationTest, obs, updateMode) {
});
}

function shouldFailSubjectCreationWithoutSubjectType(baseIntegrationTest, sub, updateMode) {
baseIntegrationTest.executeInWrite((db) => {
try {
let savedAddressLevel = db.create(AddressLevel, TestAddressLevelFactory.createWithDefaults({level: 1}));
sub.lowestAddressLevel = savedAddressLevel;
db.create(Individual, sub, updateMode);
assert.fail("Subject without subjectType should have failed to save.");
} catch (error) {
console.error(error)
}
});
}
function shouldFailSubjectCreationWithoutAddress(baseIntegrationTest, sub, updateMode) {
baseIntegrationTest.executeInWrite((db) => {
try {
let newSubjectType = db.create(SubjectType, TestSubjectTypeFactory.createWithDefaults({type: SubjectType.types.Person, name: 'Beneficiary'}));
sub.subjectType = newSubjectType;
db.create(Individual, sub, updateMode);
assert.fail("Subject without addressLevel should have failed to save.");
} catch (error) {
console.error(error)
}
});
}

function shouldPass(baseIntegrationTest, obs, updateMode) {
baseIntegrationTest.executeInWrite((db) => {
db.create(Observation, obs, updateMode);
Expand All @@ -25,6 +54,18 @@ class RealmProxyTest extends BaseIntegrationTest {
shouldFail(this, TestObsFactory.create({concept: null, valueJSON: "{}"}), true);
shouldPass(this, TestObsFactory.create({valueJSON: "{}"}), true);
}

doNotAllowCreationOfIndividualWithoutSubjectType() {
shouldFailSubjectCreationWithoutSubjectType(this,
TestSubjectFactory.createWithDefaults({firstName:'foo', lastName:'bar', address:null, registrationDate: moment().toDate(), observations:[]}),
false)
}

doNotAllowCreationOfIndividualWithoutAddressLevel() {
shouldFailSubjectCreationWithoutAddress(this,
TestSubjectFactory.createWithDefaults({firstName:'foo', lastName:'bar', address:null, registrationDate: moment().toDate(), observations:[]}),
false)
}
}

export default RealmProxyTest;

0 comments on commit 52ea87d

Please sign in to comment.