From d047362f281565b48e9c42d6c4d4539882a56d66 Mon Sep 17 00:00:00 2001 From: BukunmiOla Date: Mon, 14 Oct 2024 21:40:27 +0100 Subject: [PATCH] increase test coverage for Color Rule to maximum --- .../org/opendatakit/data/ColorRuleTest.java | 102 +++++++++++++----- 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/androidlibrary_lib/src/test/java/org/opendatakit/data/ColorRuleTest.java b/androidlibrary_lib/src/test/java/org/opendatakit/data/ColorRuleTest.java index 530a83ae..c79e3808 100644 --- a/androidlibrary_lib/src/test/java/org/opendatakit/data/ColorRuleTest.java +++ b/androidlibrary_lib/src/test/java/org/opendatakit/data/ColorRuleTest.java @@ -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; @@ -111,13 +112,39 @@ public void testColorRule() { } //Test to show if the returned Json representation for ColorRule is correct + @Test + public void testGetSymbol() { + 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 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); @@ -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 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(){ @@ -186,4 +224,14 @@ private Map 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"; }