Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases for uncovered methods in the opendatakit.data class #296

Open
wants to merge 3 commits into
base: upgrade-jdk11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
3 changes: 3 additions & 0 deletions androidlibrary_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ dependencies {
// Testing dependencies
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.annotation:annotation:1.7.1'
testImplementation 'org.mockito:mockito-core:4.0.0'
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:core:1.5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.TreeMap;
import org.opendatakit.logging.WebLogger;
import org.opendatakit.aggregate.odktables.rest.ElementDataType;
import org.opendatakit.database.data.TypedRow;
import static org.powermock.api.mockito.PowerMockito.*;
import org.opendatakit.logging.desktop.WebLoggerDesktopFactoryImpl;
import org.opendatakit.utilities.StaticStateManipulator;

Expand Down Expand Up @@ -92,4 +96,47 @@ public void testColorRule() {
Assert.assertTrue(cr1.equalsWithoutId(cr2));
}

@Test
public void testGetJsonRepresentation() {
ColorRule cr = new ColorRule("myElement", ColorRule.RuleType.EQUAL, "5", 0, 0);
TreeMap<String, Object> jsonMap = cr.getJsonRepresentation();

Assert.assertEquals("5", jsonMap.get("mValue"));
Assert.assertEquals("myElement", jsonMap.get("mElementKey"));
Assert.assertEquals(ColorRule.RuleType.EQUAL.name(), jsonMap.get("mOperator"));
Assert.assertNotNull(jsonMap.get("mId"));
Assert.assertEquals(0, jsonMap.get("mForeground"));
Assert.assertEquals(0, jsonMap.get("mBackground"));
}

@Test
public void testCheckMatch() {
// Create a mock TypedRow and ElementDataType
TypedRow mockRow = mock(TypedRow.class);
ElementDataType mockType = ElementDataType.number;

// Define behavior of the mock to return a specific value when getRawStringByKey is called
when(mockRow.getRawStringByKey("myElement")).thenReturn("10");

// Create a ColorRule for testing
ColorRule cr1 = new ColorRule("myElement", ColorRule.RuleType.LESS_THAN, "15", 0, 0);

// Verify that the match logic works as expected (10 is less than 15)
Assert.assertTrue(cr1.checkMatch(mockType, mockRow));

// Modify behavior to return a higher value that shouldn't match
when(mockRow.getRawStringByKey("myElement")).thenReturn("20");

// Verify the match fails since 20 is not less than 15
Assert.assertFalse(cr1.checkMatch(mockType, mockRow));

// Now change the rule to GREATER_THAN and test again
cr1.setOperator(ColorRule.RuleType.GREATER_THAN);
Assert.assertTrue(cr1.checkMatch(mockType, mockRow));

// Test for a case where the value is equal, but the rule is GREATER_THAN
when(mockRow.getRawStringByKey("myElement")).thenReturn("15");
Assert.assertFalse(cr1.checkMatch(mockType, mockRow));
}

}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 4 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Fri Feb 02 09:34:46 WAT 2024
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading