From 52ea87daf31b8ba3d50c82cecf02cdb6cebc10d4 Mon Sep 17 00:00:00 2001 From: Suhas Vishwanath Date: Mon, 6 Nov 2023 16:33:00 +0530 Subject: [PATCH] avniproject/avni-client#1142 | Adding integration test to check mandatory fields --- .../integrationTest/RealmProxyTest.js | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/openchs-android/integrationTest/RealmProxyTest.js b/packages/openchs-android/integrationTest/RealmProxyTest.js index 7d1d61cb6..157cf782b 100644 --- a/packages/openchs-android/integrationTest/RealmProxyTest.js +++ b/packages/openchs-android/integrationTest/RealmProxyTest.js @@ -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) => { @@ -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); @@ -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;