/*
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.reporting.data.encounter.evaluator;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.module.reporting.common.TestUtil;
import org.openmrs.module.reporting.data.encounter.EvaluatedEncounterData;
import org.openmrs.module.reporting.data.encounter.definition.SqlEncounterDataDefinition;
import org.openmrs.module.reporting.data.encounter.service.EncounterDataService;
import org.openmrs.module.reporting.evaluation.context.EncounterEvaluationContext;
import org.openmrs.module.reporting.query.encounter.EncounterIdSet;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
/**
*
*/
public class SqlEncounterDataEvaluatorTest extends BaseModuleContextSensitiveTest {
protected static final String XML_DATASET_PATH = "org/openmrs/module/reporting/include/";
protected static final String XML_REPORT_TEST_DATASET = "ReportTestDataset";
@Autowired
EncounterDataService encounterDataService;
/**
* Run this before each unit test in this class. The "@Before" method in
* {@link org.openmrs.test.BaseContextSensitiveTest} is run right before this method.
*
* @throws Exception
*/
@Before
public void setup() throws Exception {
executeDataSet(XML_DATASET_PATH + new TestUtil().getTestDatasetFilename(XML_REPORT_TEST_DATASET));
}
@Test
public void testEvaluate() throws Exception {
String sql = "select e.encounter_id, o.value_coded from encounter e inner join obs o on e.encounter_id = o.encounter_id where o.concept_id = 21 and e.encounter_id in (:encounterIds)";
SqlEncounterDataDefinition definition = new SqlEncounterDataDefinition();
definition.setSql(sql);
EncounterEvaluationContext context = new EncounterEvaluationContext();
context.setBaseEncounters(new EncounterIdSet(3, 4, 5));
EvaluatedEncounterData data = encounterDataService.evaluate(definition, context);
assertThat((Integer) data.getData().get(3), is(8));
assertThat((Integer) data.getData().get(4), is(7));
assertThat((Integer) data.getData().get(5), nullValue());
}
}