Skip to content

Commit

Permalink
unit test for ColorRule.checkMatch function
Browse files Browse the repository at this point in the history
  • Loading branch information
BukunmiOla committed Oct 14, 2024
1 parent c2bc107 commit cd99c81
Showing 1 changed file with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@
package org.opendatakit.data;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.graphics.Color;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.opendatakit.aggregate.odktables.rest.ElementDataType;
import org.opendatakit.aggregate.odktables.rest.entity.Column;
import org.opendatakit.database.data.BaseTable;
import org.opendatakit.database.data.OrderedColumns;
import org.opendatakit.database.data.Row;
import org.opendatakit.database.data.TypedRow;
import org.opendatakit.logging.WebLogger;
import org.opendatakit.logging.desktop.WebLoggerDesktopFactoryImpl;
import org.opendatakit.utilities.StaticStateManipulator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

Expand Down Expand Up @@ -100,7 +113,7 @@ public void testColorRule() {
//Test to show if the returned Json representation for ColorRule is correct
@Test
public void testGetJsonRepresentation(){
String ruleId = UUID.randomUUID().toString();
String ruleId = generateId();
ColorRule cr = new ColorRule(ruleId, "myElement1", ColorRule.RuleType.NO_OP, "-1", Color.YELLOW, Color.BLACK);
TreeMap<String,Object> expected = new TreeMap<>();
expected.put("mValue", "-1");
Expand All @@ -121,4 +134,56 @@ public void testToString() {
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);

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);
Row row;
TypedRow rowToMatch;
row= new Row(new String[]{"1","1","3","5","5","0"}, table);
table.addRow(row);
rowToMatch = new TypedRow(table.getRowAtIndex(0),orderedColumns);

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));

row= new Row(new String[]{"0","2","4","4","3"}, 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));
}

private String generateId(){
return UUID.randomUUID().toString();
}

private Map<String, Integer> generateElementKeyToIndex(String[] elementKeys) {
Map<String, Integer> elementKeyToIndex = new HashMap<>();
for (int i = 0; i < elementKeys.length; i++) {
elementKeyToIndex.put(elementKeys[i], i);
}
return elementKeyToIndex;
}
}

0 comments on commit cd99c81

Please sign in to comment.