Skip to content

Commit

Permalink
Added ability to look up values in other tables in the target schema
Browse files Browse the repository at this point in the history
when defining tests.
  • Loading branch information
schuemie committed Jul 19, 2016
1 parent 1f28066 commit ac207fb
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/org/ohdsi/rabbitInAHat/ETLTestFrameWorkGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private static List<String> generateRScript(ETL etl) {
createExpectFunctions(r, DEFAULT, etl.getTargetDatabase());
createExpectFunctions(r, NEGATE, etl.getTargetDatabase());
createExpectFunctions(r, COUNT, etl.getTargetDatabase());
createLookupFunctions(r, etl.getTargetDatabase());
return r;
}

Expand Down Expand Up @@ -107,6 +108,8 @@ private static void createExpectFunctions(List<String> r, int type, Database dat
testDefs.add(" }");
testDefs.add(" if (is.null(" + rFieldName + ")) {");
testDefs.add(" statement <- paste0(statement, \" " + sqlFieldName + " IS NULL\")");
testDefs.add(" } else if (is(" + rFieldName + ", \"subQuery\")){");
testDefs.add(" statement <- paste0(statement, \" " + sqlFieldName + " = (\", as.character(" + rFieldName + "), \")\")");
testDefs.add(" } else {");
testDefs.add(" statement <- paste0(statement, \" " + sqlFieldName + " = '\", " + rFieldName + ",\"'\")");
testDefs.add(" }");
Expand Down Expand Up @@ -150,6 +153,50 @@ else if (type == NEGATE)
r.add("");
}
}

private static void createLookupFunctions(List<String> r, Database database) {
for (Table table : database.getTables()) {
StringBuilder line = new StringBuilder();
String rTableName = convertToRName(table.getName());
String sqlTableName = convertToSqlName(table.getName());
List<String> argDefs = new ArrayList<String>();
List<String> testDefs = new ArrayList<String>();
for (Field field : table.getFields()) {
String rFieldName = convertToRName(field.getName());
String sqlFieldName = convertToSqlName(field.getName());
argDefs.add(rFieldName);
testDefs.add(" if (!missing(" + rFieldName + ")) {");
testDefs.add(" if (first) {");
testDefs.add(" first <- FALSE");
testDefs.add(" } else {");
testDefs.add(" statement <- paste0(statement, \" AND\")");
testDefs.add(" }");
testDefs.add(" if (is.null(" + rFieldName + ")) {");
testDefs.add(" statement <- paste0(statement, \" " + sqlFieldName + " IS NULL\")");
testDefs.add(" } else {");
testDefs.add(" statement <- paste0(statement, \" " + sqlFieldName + " = '\", " + rFieldName + ",\"'\")");
testDefs.add(" }");
testDefs.add(" }");
testDefs.add("");
}
line.append("lookup_" + rTableName + " <- function(fetchField, ");
line.append(StringUtilities.join(argDefs, ", "));
line.append(") {");
r.add(line.toString());

line = new StringBuilder();
line.append(" statement <- paste0(\"SELECT \", fetchField , \" FROM ");
line.append(sqlTableName);
line.append(" WHERE\")");
r.add(line.toString());
r.add(" first <- TRUE");
r.addAll(testDefs);
r.add(" class(statement) <- \"subQuery\"");
r.add(" return(statement)");
r.add("}");
r.add("");
}
}

private static String convertToSqlName(String name) {
if (name.contains(" ") || name.contains(".") || keywordSet.contains(name.toUpperCase()))
Expand Down

0 comments on commit ac207fb

Please sign in to comment.