Skip to content

Commit

Permalink
#1142 - integration test for realm proxy changes to check for presenc…
Browse files Browse the repository at this point in the history
…e of mandatory object fields
  • Loading branch information
petmongrels committed Oct 26, 2023
1 parent 2d04d0b commit 00947bf
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class BaseIntegrationTest {
}

setup() {
this.log("Setup Called");
GlobalContext.getInstance().db.write(() => {
GlobalContext.getInstance().db.realmDb.deleteAll();
});
Expand Down
12 changes: 4 additions & 8 deletions packages/openchs-android/integrationTest/DatabaseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import TestCommentFactory from "../test/model/comment/TestCommentFactory";
class DatabaseTest extends BaseIntegrationTest {
shouldReturnFirstElementAsNilIfCollectionIsEmpty() {
const db = GlobalContext.getInstance().db;
assert.equal(null, db.objects(Encounter.schema.name)[0]);
assert.equal(null, db.objects(Encounter.schema.name).filtered("uuid = '1'")[0]);
assert.equal(db.objects(Encounter.schema.name).length, 0);
const objects = db.objects(Encounter.schema.name);
assert.equal(objects[0], null);
assert.equal(db.objects(Encounter.schema.name).filtered("uuid = '1'")[0], null);
}

save_plain_object_graph_causes_circular_saves_leading_to_error() {
Expand Down Expand Up @@ -90,12 +92,6 @@ class DatabaseTest extends BaseIntegrationTest {
});
assert.equal(this.getEntity(FormElement, formElement.uuid).uuid, formElement.uuid);
}

is_object_relationship_optional() {
this.executeInWrite((db) => {
db.create(Comment, TestCommentFactory.create({}));
});
}
}

export default DatabaseTest;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DatabaseTest from "./DatabaseTest";
import IntegrationTestRunner, {TestSuite} from "./IntegrationTestRunner";
import UtilTest from "./UtilTest";
import UserInfoServiceTest from "./UserInfoServiceTest";
import RealmProxyTest from "./RealmProxyTest";

const itemCommonStyle = {
padding: 10,
Expand Down Expand Up @@ -60,7 +61,7 @@ class IntegrationTestApp extends Component {
super(props, context);
FileSystem.init();
this.getBean = this.getBean.bind(this);
this.integrationTestRunner = new IntegrationTestRunner(UserInfoServiceTest, DatabaseTest, PersonRegisterActionsIntegrationTest, UtilTest);
this.integrationTestRunner = new IntegrationTestRunner(UserInfoServiceTest, DatabaseTest, PersonRegisterActionsIntegrationTest, UtilTest, RealmProxyTest);
this.state = {isInitialisationDone: false, integrationTests: this.integrationTestRunner.testSuite};
}

Expand Down
30 changes: 30 additions & 0 deletions packages/openchs-android/integrationTest/RealmProxyTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import BaseIntegrationTest from "./BaseIntegrationTest";
import {Observation} from "openchs-models";
import {assert} from "chai";
import TestObsFactory from "../test/model/TestObsFactory";

function shouldFail(baseIntegrationTest, obs, updateMode) {
baseIntegrationTest.executeInWrite((db) => {
try {
db.create(Observation, obs, updateMode);
assert.fail("Comment without subject and CommentThread should have failed to save.");
} catch (error) {
}
});
}

function shouldPass(baseIntegrationTest, obs, updateMode) {
baseIntegrationTest.executeInWrite((db) => {
db.create(Observation, obs, updateMode);
});
}

class RealmProxyTest extends BaseIntegrationTest {
doNotAllowCreateWithMandatoryObjectTypePropertyAsNull() {
shouldFail(this, TestObsFactory.create({valueJSON: "{}"}), false);
shouldFail(this, TestObsFactory.create({concept: null, valueJSON: "{}"}), true);
shouldPass(this, TestObsFactory.create({valueJSON: "{}"}), true);
}
}

export default RealmProxyTest;
14 changes: 7 additions & 7 deletions packages/openchs-android/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openchs-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"lodash": "4.17.21",
"moment": "2.29.4",
"native-base": "3.4.9",
"openchs-models": "1.30.79",
"openchs-models": "1.30.80",
"prop-types": "15.8.1",
"react": "18.2.0",
"react-native": "0.72.3",
Expand Down
4 changes: 3 additions & 1 deletion packages/openchs-android/test/model/TestObsFactory.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {Observation} from 'openchs-models';
import _ from 'lodash';

class TestObsFactory {
static create({concept, valueJSON}) {
const observation = new Observation();
observation.concept = concept;
if (!_.isNil(concept))
observation.concept = concept;
observation.valueJSON = valueJSON;
return observation;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from "lodash";
import General from "../../../src/utility/General";
import {Comment} from 'openchs-models';

Expand Down
12 changes: 12 additions & 0 deletions packages/openchs-android/test/model/txn/TestObservationFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Observation} from 'openchs-models';

class TestObservationFactory {
static create({concept, valueJson}) {
const observation = new Observation();
observation.concept = concept;
observation.valueJSON = valueJson;
return observation;
}
}

export default TestObservationFactory;

0 comments on commit 00947bf

Please sign in to comment.