Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruhanga committed Jan 23, 2024
1 parent d528f15 commit 16cb198
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ private Parameter getStartDateParameter() {
}

private Parameter getEndDateParameter() {
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
return new Parameter("endDate", "End Date", Date.class, null, DateUtil.parseDate(today, "yyyy-MM-dd"));
String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
return new Parameter("endDate", "End Date", Date.class, null, DateUtil.parseDate(endDate, "yyyy-MM-dd"));
}

@Override
Expand Down Expand Up @@ -122,9 +122,12 @@ private Map<String, String> getMetadataReplacements() {
map.put("ncdEncounterTypeUuid", inizService.getValueFromKey("report.disbursement.ncd.encounter.type.uuid"));
map.put("yesConceptUuid", inizService.getValueFromKey("report.disbursement.yes.concept.uuid"));
map.put("positiveConceptUuid", inizService.getValueFromKey("report.disbursement.positive.concept.uuid"));
map.put("viaDiagnosisQuestionConceptUuid", inizService.getValueFromKey("report.disbursement.via.diagnosis.question.concept.uuid"));
map.put("followupQuestionConceptUuid", inizService.getValueFromKey("report.disbursement.followup.question.concept.uuid"));
map.put("startedMedicationQuestionConceptUuid", inizService.getValueFromKey("report.disbursement.started.medication.question.concept.uuid"));
map.put("viaDiagnosisQuestionConceptUuid",
inizService.getValueFromKey("report.disbursement.via.diagnosis.question.concept.uuid"));
map.put("followupQuestionConceptUuid",
inizService.getValueFromKey("report.disbursement.followup.question.concept.uuid"));
map.put("startedMedicationQuestionConceptUuid",
inizService.getValueFromKey("report.disbursement.started.medication.question.concept.uuid"));

return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ FROM
INNER JOIN
person p ON e.patient_id = p.person_id
WHERE
e.encounter_datetime > ':startDate'
AND e.encounter_datetime < ':endDate'
e.encounter_datetime > :startDate
AND e.encounter_datetime < :endDate
AND e.encounter_type = (
SELECT encounter_type_id
FROM encounter_type s_et
WHERE s_et.`uuid` LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141'
WHERE s_et.uuid LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141'
)
AND ROUND(DATEDIFF(e.encounter_datetime, p.birthdate) / 365.25, 0) >= 40
AND e.patient_id NOT IN (
Expand All @@ -21,9 +21,9 @@ WHERE
WHERE s_e.encounter_type = (
SELECT encounter_type_id
FROM encounter_type ss_et
WHERE ss_et.`uuid` LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141'
WHERE ss_et.uuid LIKE '422b7e0c-b8f3-4748-8e60-d6684315f141'
)
AND s_e.encounter_datetime <= ':startDate'
AND s_e.encounter_datetime <= :startDate
)

UNION ALL
Expand All @@ -37,12 +37,12 @@ FROM
INNER JOIN
person p ON e.patient_id = p.person_id
WHERE
e.encounter_datetime > ':startDate'
AND e.encounter_datetime < ':endDate'
e.encounter_datetime > :startDate
AND e.encounter_datetime < :endDate
AND e.encounter_type = (
SELECT encounter_type_id
FROM encounter_type s_et
WHERE s_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
WHERE s_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
)
AND ROUND(DATEDIFF(e.encounter_datetime, p.birthdate) / 365.25, 0) BETWEEN 30 AND 49
AND e.patient_id NOT IN (
Expand All @@ -51,9 +51,9 @@ WHERE
WHERE s_e.encounter_type = (
SELECT encounter_type_id
FROM encounter_type ss_et
WHERE ss_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
WHERE ss_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
)
AND s_e.encounter_datetime <= ':startDate'
AND s_e.encounter_datetime <= :startDate
)

UNION ALL
Expand Down Expand Up @@ -101,12 +101,12 @@ LEFT OUTER JOIN
WHERE uuid LIKE '758b9dd8-b6d0-4ac2-b245-0e7bffb4693a'
)
WHERE
e.encounter_datetime > ':startDate'
AND e.encounter_datetime < ':endDate'
e.encounter_datetime > :startDate
AND e.encounter_datetime < :endDate
AND e.encounter_type = (
SELECT encounter_type_id
FROM encounter_type s_et
WHERE s_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
WHERE s_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
)
AND ROUND(DATEDIFF(e.encounter_datetime, p.birthdate) / 365.25, 0) BETWEEN 30 AND 49
AND e.patient_id NOT IN (
Expand All @@ -115,9 +115,9 @@ WHERE
WHERE s_e.encounter_type = (
SELECT encounter_type_id
FROM encounter_type ss_et
WHERE ss_et.`uuid` LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
WHERE ss_et.uuid LIKE '3fd606b6-4c9d-4077-a532-c1ac58644ad2'
)
AND s_e.encounter_datetime <= ':startDate'
AND s_e.encounter_datetime <= :startDate
)

UNION ALL
Expand Down Expand Up @@ -161,7 +161,7 @@ INNER JOIN
)
WHERE
(
(o_f.value_datetime >= ':startDate' AND o_f.value_datetime <= ':endDate')
AND (o_m.obs_datetime < DATE_SUB(DATE(':endDate'), INTERVAL 3 WEEK))
OR (o_f.value_datetime >= ':startDate' AND o_f.value_datetime <= ':endDate')
(o_f.value_datetime >= :startDate AND o_f.value_datetime <= :endDate)
AND (o_m.obs_datetime < DATE_SUB(:endDate, INTERVAL 3 WEEK))
OR (o_f.value_datetime >= :startDate AND o_f.value_datetime <= :endDate)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.openmrs.module.commonreports.reports;

import static java.math.BigDecimal.ONE;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.hibernate.cfg.Environment;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Cohort;
import org.openmrs.api.ConceptService;
import org.openmrs.module.initializer.Domain;
import org.openmrs.module.initializer.api.InitializerService;
import org.openmrs.module.initializer.api.loaders.Loader;
import org.openmrs.module.reporting.common.DateUtil;
import org.openmrs.module.reporting.dataset.DataSet;
import org.openmrs.module.reporting.dataset.DataSetRow;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.report.ReportData;
import org.openmrs.module.reporting.report.definition.ReportDefinition;
import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService;
import org.openmrs.module.reporting.report.manager.ReportManagerUtil;
import org.openmrs.module.reporting.report.service.ReportService;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class DisbursementReportManagerTest extends BaseModuleContextSensitiveMysqlBackedTest {

@Autowired
private InitializerService iniz;

@Autowired
private ReportService rs;

@Autowired
private ReportDefinitionService rds;

@Autowired
@Qualifier("conceptService")
private ConceptService cs;

@Autowired
private DisbursementReportManager manager;

@Before
public void setup() throws Exception {
executeDataSet("org/openmrs/module/reporting/include/ReportTestDataset-openmrs-2.0.xml");
executeDataSet("org/openmrs/module/commonreports/include/disbursementReportTestDataset.xml");

String path = getClass().getClassLoader().getResource("testAppDataDir").getPath() + File.separator;
System.setProperty("OPENMRS_APPLICATION_DATA_DIRECTORY", path);

for (Loader loader : iniz.getLoaders()) {
if (loader.getDomainName().equals(Domain.JSON_KEY_VALUES.getName())) {
loader.load();
}
}
}

@Test
public void setupReport_shouldSetupEmergencyReport() {

// replay
ReportManagerUtil.setupReport(manager);

// verify
Assert.assertNotNull(rs.getReportDesignByUuid("77b6c2cb-4b96-47d8-bcde-b1b7f16f5670"));

}

@Test
public void testReport() throws Exception {
// setup
EvaluationContext context = new EvaluationContext();
context.addParameterValue("startDate", DateUtil.parseDate("2024-01-01", "yyyy-MM-dd"));
context.addParameterValue("endDate", DateUtil.parseDate("2024-02-28", "yyyy-MM-dd"));

// replay
ReportDefinition rd = manager.constructReportDefinition();
ReportData data = rds.evaluate(rd, context);

// verify
boolean indicator1 = true;
boolean indicator2 = true;
boolean indicator3 = true;
boolean indicator4 = true;
for (DataSet ds : data.getDataSets().values()) {
for (Iterator<DataSetRow> itr = ds.iterator(); itr.hasNext();) {
DataSetRow row = itr.next();
System.out.println(row);

if (row.getColumnValue("Indicator").equals("Patients aged 40 and above with NCD form filled out for the first time")) {
assertEquals("0", row.getColumnValue("Value"));
indicator1 = true;
}
if (row.getColumnValue("Indicator").equals("Women aged 30 to 49 years with CCS form filled out for the first time")) {
assertEquals("5", row.getColumnValue("Value"));
indicator2 = true;
}
if (row.getColumnValue("Indicator").equals("80% (of women aged 30 to 49 years with CCS form filled out for the first time) had their first CCS screening, were VIA positive and referred")) {
assertEquals("No", row.getColumnValue("Value"));
indicator3 = true;
}
if (row.getColumnValue("Indicator").equals("80% have a Follow-up date and were given medication at least 3 weeks ago")) {
assertEquals("No", row.getColumnValue("Value"));
indicator4 = true;
}
}
assertTrue(indicator1 && indicator2 && indicator3 && indicator4);
}
}
}
Loading

0 comments on commit 16cb198

Please sign in to comment.