From eb66fa8cc21f2f6f21c6fd5209a672a78c8d2afb Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Wed, 19 Feb 2025 00:03:36 +0100 Subject: [PATCH] Improved tests --- .../entities/EntitiesRepositoryTest.kt | 9 ++ .../filter/LocalEntitiesFilterStrategyTest.kt | 118 ++++++++++++------ 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt b/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt index ebb6ae42b6f..476b45af357 100644 --- a/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt @@ -8,6 +8,7 @@ import org.junit.Test import org.odk.collect.android.entities.support.EntitySameAsMatcher.Companion.sameEntityAs import org.odk.collect.entities.storage.EntitiesRepository import org.odk.collect.entities.storage.Entity +import org.odk.collect.entities.storage.QueryException import org.odk.collect.shared.Query abstract class EntitiesRepositoryTest { @@ -872,4 +873,12 @@ abstract class EntitiesRepositoryTest { val queriedCanet = repository.query("other.favourite.wines", Query.Eq("label", "Pontet-Canet 2014")) assertThat(queriedCanet, containsInAnyOrder(sameEntityAs(canet))) } + + @Test(expected = QueryException::class) + fun `#query throws an exception when not existing property is used`() { + val repository = buildSubject() + repository.save("wines", Entity.New("1", "LĂ©oville Barton 2008",)) + + repository.query("wines", Query.Eq("score", "92")) + } } diff --git a/entities/src/test/java/org/odk/collect/entities/javarosa/filter/LocalEntitiesFilterStrategyTest.kt b/entities/src/test/java/org/odk/collect/entities/javarosa/filter/LocalEntitiesFilterStrategyTest.kt index 254526bd93c..36d39b68c99 100644 --- a/entities/src/test/java/org/odk/collect/entities/javarosa/filter/LocalEntitiesFilterStrategyTest.kt +++ b/entities/src/test/java/org/odk/collect/entities/javarosa/filter/LocalEntitiesFilterStrategyTest.kt @@ -543,7 +543,7 @@ class LocalEntitiesFilterStrategyTest { } @Test - fun `works correctly but not in the optimized way with question = expressions`() { + fun `works correctly but not in the optimized way with unanswered question = ''`() { entitiesRepository.save("things", Entity.New("thing1", "Thing1")) val scenario = Scenario.init( @@ -556,63 +556,78 @@ class LocalEntitiesFilterStrategyTest { t( "data id=\"create-entity-form\"", t("ref_question"), - t("question1"), - t("question2"), - t("question3"), + t("question") ) ), t("instance id=\"things\" src=\"jr://file-csv/things.csv\""), bind("/data/ref_question").type("string"), - bind("/data/question1").type("string"), - bind("/data/question2").type("string"), - bind("/data/question3").type("string") + bind("/data/question").type("string") ) ), body( input("/data/ref_question"), select1Dynamic( - "/data/question1", + "/data/question", "instance('things')/root/item[/data/ref_question='']", "name", "label" - ), - select1Dynamic( - "/data/question2", - "instance('things')/root/item[/data/ref_question=undefined]", - "name", - "label" - ), - select1Dynamic( - "/data/question3", - "instance('things')/root/item[/data/ref_question=null]", - "name", - "label" ) ) ), controllerSupplier ) - var choices = scenario.choicesOf("/data/question1").map { it.value } + val choices = scenario.choicesOf("/data/question").map { it.value } assertThat(choices, containsInAnyOrder("thing1")) - choices = scenario.choicesOf("/data/question2").map { it.value } - assertThat(choices, containsInAnyOrder("thing1")) + assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true)) + } - choices = scenario.choicesOf("/data/question3").map { it.value } - assertThat(choices, containsInAnyOrder("thing1")) + @Test + fun `works correctly but not in the optimized way with answered question = value`() { + entitiesRepository.save("things", Entity.New("thing1", "Thing1")) + val scenario = Scenario.init( + "Secondary instance form", + html( + head( + title("Secondary instance form"), + model( + mainInstance( + t( + "data id=\"create-entity-form\"", + t("ref_question"), + t("question") + ) + ), + t("instance id=\"things\" src=\"jr://file-csv/things.csv\""), + bind("/data/ref_question").type("string"), + bind("/data/question").type("string") + ) + ), + body( + input("/data/ref_question"), + select1Dynamic( + "/data/question", + "instance('things')/root/item[/data/ref_question='']", + "name", + "label" + ) + ) + ), + controllerSupplier + ) scenario.next() scenario.answer("blah") - choices = scenario.choicesOf("/data/question1").map { it.value } + val choices = scenario.choicesOf("/data/question").map { it.value } assertThat(choices.isEmpty(), equalTo(true)) assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true)) } @Test - fun `works correctly but not in the optimized way with non existing property = expressions`() { + fun `works correctly but not in the optimized way with non existing property = ''`() { entitiesRepository.save("things", Entity.New("thing1", "Thing1")) val scenario = Scenario.init( @@ -624,24 +639,54 @@ class LocalEntitiesFilterStrategyTest { mainInstance( t( "data id=\"create-entity-form\"", - t("question1"), - t("question2"), + t("question") ) ), t("instance id=\"things\" src=\"jr://file-csv/things.csv\""), - bind("/data/question1").type("string"), - bind("/data/question2").type("string") + bind("/data/question").type("string") ) ), body( select1Dynamic( - "/data/question1", + "/data/question", "instance('things')/root/item[not_existing_property='']", "name", "label" - ), + ) + ) + ), + controllerSupplier + ) + + val choices = scenario.choicesOf("/data/question").map { it.value } + assertThat(choices, containsInAnyOrder("thing1")) + + assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true)) + } + + @Test + fun `works correctly but not in the optimized way with non existing property = value`() { + entitiesRepository.save("things", Entity.New("thing1", "Thing1")) + + val scenario = Scenario.init( + "Secondary instance form", + html( + head( + title("Secondary instance form"), + model( + mainInstance( + t( + "data id=\"create-entity-form\"", + t("question") + ) + ), + t("instance id=\"things\" src=\"jr://file-csv/things.csv\""), + bind("/data/question").type("string") + ) + ), + body( select1Dynamic( - "/data/question2", + "/data/question", "instance('things')/root/item[not_existing_property='value']", "name", "label" @@ -651,10 +696,7 @@ class LocalEntitiesFilterStrategyTest { controllerSupplier ) - var choices = scenario.choicesOf("/data/question1").map { it.value } - assertThat(choices, containsInAnyOrder("thing1")) - - choices = scenario.choicesOf("/data/question2").map { it.value } + val choices = scenario.choicesOf("/data/question").map { it.value } assertThat(choices.isEmpty(), equalTo(true)) assertThat(fallthroughFilterStrategy.fellThrough, equalTo(true))