Skip to content

Commit

Permalink
Upgrade testdatamanager and ensures test data follows correct modelin… (
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton authored May 25, 2024
1 parent 6e9ed6c commit 23929fb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.api.ConceptService;
import org.openmrs.api.PatientService;
import org.openmrs.contrib.testdata.TestDataManager;
import org.openmrs.contrib.testdata.builder.ObsBuilder;
import org.openmrs.module.emrapi.EmrApiConstants;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.adt.reporting.query.AwaitingAdmissionVisitQuery;
Expand Down Expand Up @@ -72,6 +72,12 @@ public void setup() throws Exception {
patient = testDataManager.randomPatient().birthdate("2010-01-01").save();
}

private Obs createDispositionObs(Encounter encounter, Concept disposition) {
ObsBuilder groupBuilder = testDataManager.obs().encounter(encounter).concept(dispositionDescriptor.getDispositionSetConcept());
groupBuilder.member(testDataManager.obs().encounter(encounter).concept(dispositionDescriptor.getDispositionConcept()).value(disposition).get());
return groupBuilder.save();
}

@Test
public void shouldFindVisitAwaitingAdmission() throws Exception {

Expand All @@ -88,12 +94,7 @@ public void shouldFindVisitAwaitingAdmission() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
Obs obs = testDataManager.obs()
.person(patient)
.encounter(encounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(encounter, admitToHospital);

VisitQueryResult result = visitQueryService.evaluate(query, null);
assertThat(result.getMemberIds().size(), is(1));
Expand All @@ -120,12 +121,7 @@ public void shouldNotCountDispositionOnVoidedEncounter() throws Exception {
.dateVoided(new Date())
.voidReason("test")
.save();
Obs obs = testDataManager.obs()
.person(patient)
.encounter(encounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(encounter, admitToHospital);

VisitQueryResult result = visitQueryService.evaluate(query, null);
assertThat(result.getMemberIds().size(), is(0));
Expand All @@ -151,12 +147,7 @@ public void shouldNotFindVisitIfPatientAdmitted() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter admissionEncounter = testDataManager.encounter()
.patient(patient)
.encounterDatetime(admitDatetime)
Expand Down Expand Up @@ -187,12 +178,7 @@ public void shouldNotConsiderVoidedAdmissionEncounter() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter admissionEncounter = testDataManager.encounter()
.patient(patient)
.encounterDatetime(admitDatetime)
Expand Down Expand Up @@ -227,12 +213,7 @@ public void shouldFindVisitEvenIfPatientHasMoreRecentVisitNoteWithoutAdmissionDi
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter secondVisitNoteEncounter = testDataManager.encounter()
.patient(patient)
.encounterDatetime(secondVisitNoteDatetime)
Expand Down Expand Up @@ -269,12 +250,7 @@ public void shouldNotFindVisitIfNoAdmitDisposition() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(emrConceptService.getConcept("org.openmrs.module.emrapi:Death"))
.save();
createDispositionObs(visitNoteEncounter, emrConceptService.getConcept("org.openmrs.module.emrapi:Death"));

VisitQueryResult result = visitQueryService.evaluate(query, null);
assertThat(result.getMemberIds().size(), is(0));
Expand All @@ -301,12 +277,7 @@ public void shouldNotFindVisitIfAtAnotherLocation() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);

query.setLocation(queryLocation);
VisitQueryResult result = visitQueryService.evaluate(query, null);
Expand All @@ -333,12 +304,7 @@ public void shouldFindVisitIfAtSameLocation() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);

query.setLocation(queryLocation);
VisitQueryResult result = visitQueryService.evaluate(query, null);
Expand All @@ -361,12 +327,7 @@ public void shouldNotReturnSameVisitTwice() throws Exception {
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new Date())
Expand Down Expand Up @@ -401,12 +362,7 @@ public void shouldNotFindVisitAwaitingAdmissionIfPatientNotInContext() throws Ex
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);

EvaluationContext context = new EvaluationContext();
context.setBaseCohort(new Cohort(Collections.singleton(2)));
Expand All @@ -430,12 +386,7 @@ public void shouldNotFindVisitAwaitingAdmissionIfVisitNotInContext() throws Exce
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);

VisitEvaluationContext context = new VisitEvaluationContext();
context.setBaseVisits(new VisitIdSet(10101)); // random visit id
Expand All @@ -459,12 +410,7 @@ public void shouldNotFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDenialO
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,10,11,0,0).toDate())
Expand Down Expand Up @@ -497,12 +443,7 @@ public void shouldFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDenialObsF
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,10,11,0,0).toDate())
Expand All @@ -521,12 +462,7 @@ public void shouldFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDenialObsF
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter3)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter3, admitToHospital);

VisitQueryResult result = visitQueryService.evaluate(query, null);
assertThat(result.getMemberIds().size(), is(1));
Expand All @@ -548,12 +484,7 @@ public void shouldNotFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDenialO
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,10,11,0,0).toDate())
Expand All @@ -572,12 +503,7 @@ public void shouldNotFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDenialO
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter3)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter3, admitToHospital);
Encounter visitNoteEncounter4 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,10,13,0,0).toDate())
Expand Down Expand Up @@ -610,12 +536,7 @@ public void shouldFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDecisionTh
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,10,11,0,0).toDate())
Expand Down Expand Up @@ -649,12 +570,7 @@ public void shouldFindVisitWithDispositionOfAdmitIfPrecededByAdmissionDenialObs(
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,9,10,0,0).toDate())
Expand Down Expand Up @@ -688,12 +604,7 @@ public void shouldFindVisitWithDispositionOfAdmitIfFollowedByAdmissionDenialObsT
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);
Encounter visitNoteEncounter2 = testDataManager.encounter()
.patient(patient)
.encounterDatetime(new DateTime(2014,10,11,10,0,0).toDate())
Expand Down Expand Up @@ -736,12 +647,7 @@ public void shouldNotFindVisitAwaitingAdmissionIfPatientIsDead() throws Exceptio
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.visit(visit)
.save();
testDataManager.obs()
.person(patient)
.encounter(visitNoteEncounter)
.concept(dispositionDescriptor.getDispositionConcept())
.value(admitToHospital)
.save();
createDispositionObs(visitNoteEncounter, admitToHospital);

VisitQueryResult result = visitQueryService.evaluate(query, null);
assertThat(result.getMemberIds().size(), is(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.EncounterType;
import org.openmrs.Location;
Expand All @@ -14,6 +15,7 @@
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
import org.openmrs.contrib.testdata.TestDataManager;
import org.openmrs.contrib.testdata.builder.ObsBuilder;
import org.openmrs.module.emrapi.EmrApiConstants;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.concept.EmrConceptService;
Expand Down Expand Up @@ -76,6 +78,12 @@ public void setup() throws Exception {
Context.flushSession();
}

private Obs createDispositionObs(Encounter encounter, Concept disposition) {
ObsBuilder groupBuilder = testDataManager.obs().encounter(encounter).concept(dispositionDescriptor.getDispositionSetConcept());
groupBuilder.member(testDataManager.obs().encounter(encounter).concept(dispositionDescriptor.getDispositionConcept()).value(disposition).get());
return groupBuilder.save();
}

@Test
public void testThatBeanCanHavePropertiesAutowired() throws Exception {
VisitDomainWrapper visitDomainWrapper = factory.newVisitDomainWrapper();
Expand All @@ -102,11 +110,8 @@ public void isAwaitingAdmission_shouldReturnTrueIfVisitAwaitingAdmission() throw
.visit(visit)
.encounterDatetime(new Date())
.encounterType(emrApiProperties.getVisitNoteEncounterType())
.obs(testDataManager.obs()
.concept(dispositionDescriptor.getDispositionConcept())
.value(emrConceptService.getConcept("org.openmrs.module.emrapi:Admit to hospital"))
.get())
.save();
createDispositionObs(encounter, emrConceptService.getConcept("org.openmrs.module.emrapi:Admit to hospital"));

VisitDomainWrapper visitDomainWrapper = factory.newVisitDomainWrapper(visit);
assertThat(visitDomainWrapper.isAwaitingAdmission(), is(true));
Expand Down Expand Up @@ -135,11 +140,8 @@ public void isAwaitingAdmission_shouldReturnFalseIfVisitNotAwaitingAdmission() t
.voided(true)
.dateVoided(new Date())
.voidReason("test")
.obs(testDataManager.obs()
.concept(dispositionDescriptor.getDispositionConcept())
.value(emrConceptService.getConcept("org.openmrs.module.emrapi:Admit to hospital"))
.get())
.save();
createDispositionObs(encounter, emrConceptService.getConcept("org.openmrs.module.emrapi:Admit to hospital"));

VisitDomainWrapper visitDomainWrapper = factory.newVisitDomainWrapper(visit);
assertThat(visitDomainWrapper.isAwaitingAdmission(), is(false));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openMRSVersion>2.2.1</openMRSVersion>
<legacyuiVersion>1.16.0</legacyuiVersion>
<openmrsTestutilsVersion>1.7.0</openmrsTestutilsVersion>
<openmrsTestutilsVersion>1.8.0-SNAPSHOT</openmrsTestutilsVersion>
<reportingVersion>1.25.0</reportingVersion>
<serialization.xstreamVersion>0.2.16</serialization.xstreamVersion>
<calculationVersion>1.3.0</calculationVersion>
Expand Down

0 comments on commit 23929fb

Please sign in to comment.