Skip to content

Commit

Permalink
increase test coverage for Color Rule to maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
BukunmiOla committed Oct 14, 2024
1 parent cd99c81 commit 30b813a
Showing 1 changed file with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.opendatakit.data.ColorRule.RuleType.getValues;

import android.graphics.Color;

Expand Down Expand Up @@ -111,13 +112,39 @@ public void testColorRule() {
}

//Test to show if the returned Json representation for ColorRule is correct
@Test
public void getSymbol() {
assertEquals("=", ColorRule.RuleType.EQUAL.getSymbol());
}
@Test
public void testGetEnumFromString() {
CharSequence [] ruleTypeStrings = getValues();
ColorRule.RuleType expected = ColorRule.RuleType.LESS_THAN;
assertEquals(expected, ColorRule.RuleType.getEnumFromString(ruleTypeStrings[0].toString()));
expected = ColorRule.RuleType.LESS_THAN_OR_EQUAL;
assertEquals(expected, ColorRule.RuleType.getEnumFromString(ruleTypeStrings[1].toString()));
expected = ColorRule.RuleType.EQUAL;
assertEquals(expected, ColorRule.RuleType.getEnumFromString(ruleTypeStrings[2].toString()));
expected = ColorRule.RuleType.GREATER_THAN_OR_EQUAL;
assertEquals(expected, ColorRule.RuleType.getEnumFromString(ruleTypeStrings[3].toString()));
expected = ColorRule.RuleType.GREATER_THAN;
assertEquals(expected, ColorRule.RuleType.getEnumFromString(ruleTypeStrings[4].toString()));
expected = ColorRule.RuleType.NO_OP;
assertEquals(expected, ColorRule.RuleType.getEnumFromString(""));
try {
ColorRule.RuleType.getEnumFromString("odk");
}catch (IllegalArgumentException e) {
assertEquals("unrecognized rule operator: odk", e.getMessage());
}
}

@Test
public void testGetJsonRepresentation(){
String ruleId = generateId();
ColorRule cr = new ColorRule(ruleId, "myElement1", ColorRule.RuleType.NO_OP, "-1", Color.YELLOW, Color.BLACK);
ColorRule cr = new ColorRule(ruleId, MY_ELEMENT, ColorRule.RuleType.NO_OP, "-1", Color.YELLOW, Color.BLACK);
TreeMap<String,Object> expected = new TreeMap<>();
expected.put("mValue", "-1");
expected.put("mElementKey", "myElement1");
expected.put("mElementKey", MY_ELEMENT);
expected.put("mOperator", "NO_OP");
expected.put("mId", ruleId);
expected.put("mForeground", Color.YELLOW);
Expand All @@ -128,51 +155,62 @@ public void testGetJsonRepresentation(){

@Test
public void testToString() {
ColorRule cr = new ColorRule("uuid13", "myElement", ColorRule.RuleType.EQUAL, "1", Color.BLUE, Color.WHITE);
ColorRule cr = new ColorRule("uuid13", MY_ELEMENT, ColorRule.RuleType.EQUAL, "1", Color.BLUE, Color.WHITE);
String expected = "[id=uuid13, elementKey=myElement, operator=EQUAL, value=1, background=-1, foreground=-16776961]";

assertEquals(expected, cr.toString());
}

private static final String APP_NAME = "colorRuleTest";
private static final String TABLE_ID_1 = "myTableId_1";
private static final String COLOR_COL = "Color_Col";


@Test
public void testCheckMatch() {
ColorRule cr1 = new ColorRule(generateId(), "myElement1", ColorRule.RuleType.EQUAL, "1", Color.BLUE, Color.WHITE);
ColorRule cr2 = new ColorRule(generateId(), "myElement2", ColorRule.RuleType.LESS_THAN, "2", Color.BLUE, Color.WHITE);
ColorRule cr3 = new ColorRule(generateId(), "myElement3", ColorRule.RuleType.LESS_THAN_OR_EQUAL, "3", Color.BLUE, Color.WHITE);
ColorRule cr4 = new ColorRule(generateId(), "myElement4", ColorRule.RuleType.GREATER_THAN, "4", Color.BLUE, Color.WHITE);
ColorRule cr5 = new ColorRule(generateId(), "myElement5", ColorRule.RuleType.GREATER_THAN_OR_EQUAL, "5", Color.BLUE, Color.WHITE);

//Create test values
ColorRule cr1 = new ColorRule(generateId(), MY_ELEMENT_1, ColorRule.RuleType.EQUAL, "1", Color.BLUE, Color.WHITE);
ColorRule cr2 = new ColorRule(generateId(), MY_ELEMENT_2, ColorRule.RuleType.LESS_THAN, "2", Color.BLUE, Color.WHITE);
ColorRule cr3 = new ColorRule(generateId(), MY_ELEMENT_3, ColorRule.RuleType.LESS_THAN_OR_EQUAL, "3", Color.BLUE, Color.WHITE);
ColorRule cr4 = new ColorRule(generateId(), MY_ELEMENT_4, ColorRule.RuleType.GREATER_THAN, "4", Color.BLUE, Color.WHITE);
ColorRule cr5 = new ColorRule(generateId(), MY_ELEMENT_5, ColorRule.RuleType.GREATER_THAN_OR_EQUAL, "5", Color.BLUE, Color.WHITE);
ColorRule cr6 = new ColorRule(generateId(), MY_ELEMENT_6, ColorRule.RuleType.GREATER_THAN_OR_EQUAL, "5", Color.BLUE, Color.WHITE);
ColorRule cr = new ColorRule(generateId(), MY_ELEMENT, ColorRule.RuleType.NO_OP, "5", Color.BLUE, Color.WHITE);

//Setup Color table
String[] primaryKeys = {cr1.getRuleId(), cr2.getRuleId(), cr3.getRuleId(), cr4.getRuleId(), cr5.getRuleId()};
String[] elementKeys = {MY_ELEMENT_1, MY_ELEMENT_2, MY_ELEMENT_3, MY_ELEMENT_4, MY_ELEMENT_5, MY_ELEMENT, MY_ELEMENT_6};
BaseTable table = new BaseTable(primaryKeys, elementKeys, generateElementKeyToIndex(elementKeys), 5);
//Define the table's column
List<Column> columns = new ArrayList<>();
columns.add(new Column(COLOR_COL, COLOR_COL, ElementDataType.integer.name(), null));
OrderedColumns orderedColumns = new OrderedColumns(APP_NAME, TABLE_ID_1, columns);
String[] primaryKeys = {cr1.getRuleId(), cr2.getRuleId(), cr3.getRuleId(), cr4.getRuleId(), cr5.getRuleId()};
String[] elementKeys = {"myElement1", "myElement2", "myElement3", "myElement4", "myElement5", "myElement"};
BaseTable table = new BaseTable(primaryKeys, elementKeys, generateElementKeyToIndex(elementKeys), 5);
//Define the table's rows
Row row;
TypedRow rowToMatch;
row= new Row(new String[]{"1","1","3","5","5","0"}, table);
row= new Row(new String[]{"1","1","3","5","5",null}, table);
table.addRow(row);
rowToMatch = new TypedRow(table.getRowAtIndex(0),orderedColumns);

//Check that all RuleTypes work with integer or number type
assertTrue(cr1.checkMatch(ElementDataType.integer, rowToMatch));
assertTrue(cr2.checkMatch(ElementDataType.integer, rowToMatch));
assertTrue(cr3.checkMatch(ElementDataType.integer, rowToMatch));
assertTrue(cr4.checkMatch(ElementDataType.integer, rowToMatch));
assertTrue(cr5.checkMatch(ElementDataType.integer, rowToMatch));
assertTrue(cr5.checkMatch(ElementDataType.number, rowToMatch));
assertFalse(cr.checkMatch(ElementDataType.integer, rowToMatch));

row= new Row(new String[]{"0","2","4","4","3"}, table);
row= new Row(new String[]{"","false","[44,67]","4","3","5","odk"}, table);
table.addRow(row);
rowToMatch = new TypedRow(table.getRowAtIndex(1),orderedColumns);

assertFalse(cr1.checkMatch(ElementDataType.integer, rowToMatch));
assertFalse(cr2.checkMatch(ElementDataType.integer, rowToMatch));
assertFalse(cr3.checkMatch(ElementDataType.integer, rowToMatch));
assertFalse(cr4.checkMatch(ElementDataType.integer, rowToMatch));
assertFalse(cr5.checkMatch(ElementDataType.integer, rowToMatch));
//Check that RuleTypes work with non-numeric types
assertFalse(cr1.checkMatch(ElementDataType.string, rowToMatch));
cr2.setVal("true");
assertTrue(cr2.checkMatch(ElementDataType.bool, rowToMatch));
cr3.setVal("[2,3,4]");
assertFalse(cr3.checkMatch(ElementDataType.array, rowToMatch));
assertFalse(cr4.checkMatch(ElementDataType.string, rowToMatch));
assertFalse(cr5.checkMatch(ElementDataType.string, rowToMatch));
try {
cr.checkMatch(ElementDataType.integer, rowToMatch);
}catch (IllegalArgumentException e) {
assertEquals("unrecognized op passed to checkMatch: NO_OP", e.getMessage());
}
cr6.checkMatch(ElementDataType.integer, rowToMatch);
}

private String generateId(){
Expand All @@ -186,4 +224,14 @@ private Map<String, Integer> generateElementKeyToIndex(String[] elementKeys) {
}
return elementKeyToIndex;
}
private static final String APP_NAME = "colorRuleTest";
private static final String TABLE_ID_1 = "myTableId_1";
private static final String COLOR_COL = "Color_Col";
private static final String MY_ELEMENT = "myElement";
private static final String MY_ELEMENT_1 = "myElement1";
private static final String MY_ELEMENT_2 = "myElement2";
private static final String MY_ELEMENT_3 = "myElement3";
private static final String MY_ELEMENT_4 = "myElement4";
private static final String MY_ELEMENT_5 = "myElement5";
private static final String MY_ELEMENT_6 = "myElement6";
}

0 comments on commit 30b813a

Please sign in to comment.