Skip to content

Commit

Permalink
Added unit tests for the changeset that has the precondition that che…
Browse files Browse the repository at this point in the history
…cks if there any unmapped drug order frequencies or units

Follow up to use DatabaseUtil.executeSQL when fetching unique non null column values - TRUNK-4187
  • Loading branch information
wluyima committed Jan 22, 2014
1 parent d2c480d commit be2de9c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 48 deletions.
32 changes: 8 additions & 24 deletions api/src/main/java/org/openmrs/util/DatabaseUtil.java
Expand Up @@ -245,34 +245,18 @@ public static Integer getOrderFrequencyIdForConceptId(Connection connection, Int
* @param tableName the table
* @param connection
* @return
* @throws SQLException
* @throws Exception
*/
public static <T> Set<T> getUniqueNonNullColumnValues(String columnName, String tableName, Class<T> type,
Connection connection) throws SQLException {
Connection connection) throws Exception {
Set<T> uniqueValues = new HashSet<T>();
PreparedStatement pstmt = null;
final String alias = "unique_values";

try {
pstmt = connection.prepareStatement("SELECT DISTINCT " + columnName + " AS " + alias + " FROM " + tableName
+ " WHERE " + columnName + " IS NOT NULL");
ResultSet resultSet = pstmt.executeQuery();
while (resultSet.next()) {
Object value = resultSet.getObject(alias);
if (value != null) {
uniqueValues.add((T) value);
}
}
}
finally {
if (pstmt != null) {
try {
pstmt.close();
}
catch (SQLException e) {
log.warn("Failed to close the PreparedStatement object");
}
}
String select = "SELECT DISTINCT " + columnName + " AS " + alias + " FROM " + tableName + " WHERE " + columnName
+ " IS NOT NULL";
List<List<Object>> rows = DatabaseUtil.executeSQL(connection, select, true);
for (List<Object> row : rows) {
//There can only be one column since we are selecting one
uniqueValues.add((T) row.get(0));
}

return uniqueValues;
Expand Down
Expand Up @@ -52,8 +52,8 @@ public void check(Database database) throws CustomPreconditionFailedException, C
Set<String> unmappedFrequencies = getUnMappedText(frequencies, connection);
if (unmappedFrequencies.size() > 0) {
throw new CustomPreconditionFailedException(
"Upgrade failed because of the following unmapped drug order frequencies units that were found: ["
+ StringUtils.join(unmappedDoseUnits, ", ")
"Upgrade failed because of the following unmapped drug order frequencies that were found: ["
+ StringUtils.join(unmappedFrequencies, ", ")
+ "]. Please make sure you have mapped all free text dose units and "
+ "frequencies via the global property named orderEntry.unitsToConceptsMappings"
+ " or use 1.10 upgrade helper module to map them");
Expand Down
Expand Up @@ -25,10 +25,7 @@

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

Expand All @@ -43,10 +40,7 @@ public void execute(Database database) throws CustomChangeException {
String.class, connection.getUnderlyingConnection());
insertUniqueFrequencies(connection, uniqueFrequencies);
}
catch (SQLException e) {
throw new CustomChangeException(e);
}
catch (DatabaseException e) {
catch (Exception e) {
throw new CustomChangeException(e);
}
}
Expand Down
Expand Up @@ -24,10 +24,7 @@
import org.openmrs.util.DatabaseUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class MigrateDrugOrderFrequencyToCodedOrderFrequencyChangeset implements CustomTaskChange {
Expand All @@ -41,10 +38,7 @@ public void execute(Database database) throws CustomChangeException {
String.class, connection.getUnderlyingConnection());
migrateFrequenciesToCodedValue(connection, uniqueFrequencies);
}
catch (SQLException e) {
throw new CustomChangeException(e);
}
catch (DatabaseException e) {
catch (Exception e) {
throw new CustomChangeException(e);
}
}
Expand Down
Expand Up @@ -26,10 +26,7 @@ public void execute(Database database) throws CustomChangeException {
connection.getUnderlyingConnection());
migrateUnitsToCodedValue(connection, uniqueUnits);
}
catch (SQLException e) {
throw new CustomChangeException(e);
}
catch (DatabaseException e) {
catch (Exception e) {
throw new CustomChangeException(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/resources/liquibase-update-to-latest.xml
Expand Up @@ -6411,7 +6411,7 @@
</changeSet>

<changeSet id="201401101645-TRUNK-4187" author="wyclif">
<preConditions>
<preConditions onFail="HALT">

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Jan 22, 2014

Member

Very good catch above. :)

<customPrecondition className="org.openmrs.util.databasechange.CheckDrugOrderUnitAndFrequencyTextNotMappedToConcepts" />
</preConditions>
<comment>
Expand Down
Expand Up @@ -18,15 +18,14 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.util.DatabaseUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Set;

/**
* Tests database upgrade from OpenMRS 1.9.7 to OpenMRS 1.10.
Expand Down Expand Up @@ -106,6 +105,56 @@ public void shouldMigrateDrugOrders() throws IOException, SQLException {
conceptsToFrequencies.get("113")), row("order_id", "5", "frequency", conceptsToFrequencies.get("114"))));
}

@Test(expected = Exception.class)
public void shouldFailIfAnyDrugOrderUnitsNotMappedToConceptsAreFound() throws IOException, Exception {
//sanity check that we have some unmapped drug order dose units
upgradeTestUtil.executeDataset("/org/openmrs/util/databasechange/standardTest-1.9.7-dataSet.xml");
Set<String> uniqueUnits = DatabaseUtil.getUniqueNonNullColumnValues("units", "drug_order", String.class,
upgradeTestUtil.getConnection());
Assert.assertTrue(uniqueUnits.size() > 0);

//map the frequencies only
upgradeTestUtil.insertGlobalProperty("orderEntry.unitsToConceptsMappings",
"1/day x 7 days/week:113,2/day x 7 days/week:114");

upgradeTestUtil.upgrade();
}

@Test(expected = Exception.class)
public void shouldFailIfAnyDrugOrderFrequenciesNotMappedToConceptsAreFound() throws IOException, Exception {
//sanity check that we have some unmapped drug order frequencies
upgradeTestUtil.executeDataset("/org/openmrs/util/databasechange/standardTest-1.9.7-dataSet.xml");
Set<String> uniqueFrequencies = DatabaseUtil.getUniqueNonNullColumnValues("frequency", "drug_order", String.class,
upgradeTestUtil.getConnection());
Assert.assertTrue(uniqueFrequencies.size() > 0);

//map the dose units only
upgradeTestUtil.insertGlobalProperty("orderEntry.unitsToConceptsMappings", "mg:111,tab(s):112");

upgradeTestUtil.upgrade();
}

@Test
public void shouldPassIfAllExistingDrugOrderUnitsAndFrequenciesAreMappedToConcepts() throws IOException, Exception {
//sanity check that we have some drug order dose units and frequencies in the test dataset
upgradeTestUtil.executeDataset("/org/openmrs/util/databasechange/standardTest-1.9.7-dataSet.xml");
Set<String> uniqueUnits = DatabaseUtil.getUniqueNonNullColumnValues("units", "drug_order", String.class,
upgradeTestUtil.getConnection());
Assert.assertTrue(uniqueUnits.size() > 0);

Set<String> uniqueFrequencies = DatabaseUtil.getUniqueNonNullColumnValues("frequency", "drug_order", String.class,
upgradeTestUtil.getConnection());
Assert.assertTrue(uniqueFrequencies.size() > 0);

upgradeTestUtil.executeDataset("/org/openmrs/util/databasechange/database1_9To1_10UpgradeTest-dataSet.xml");

//set the mappings for all existing frequencies and dose units
upgradeTestUtil.insertGlobalProperty("orderEntry.unitsToConceptsMappings",
"mg:111,tab(s):112,1/day x 7 days/week:113,2/day x 7 days/week:114");

upgradeTestUtil.upgrade();
}

private Map<String, String> row(String... values) {
Map<String, String> row = new HashMap<String, String>();
for (int i = 0; i < values.length; i += 2) {
Expand Down

0 comments on commit be2de9c

Please sign in to comment.