Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Dec 17, 2024
1 parent 9006bdb commit 8e9c25f
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}

override fun query(list: String, query: Query?): List<Entity.Saved> {
if (!listExists(list)) {
return emptyList()
}

return queryWithAttachedRowId(list, query?.copyWithMappedColumns { columnName ->
when (columnName) {
EntityItemElement.ID -> EntitiesTable.COLUMN_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.hamcrest.Matchers.containsInAnyOrder
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.odk.collect.android.entities.support.EntitySameAsMatcher.Companion.sameEntityAs
import org.odk.collect.db.sqlite.Query
import org.odk.collect.entities.storage.EntitiesRepository
import org.odk.collect.entities.storage.Entity

Expand Down Expand Up @@ -769,4 +770,119 @@ abstract class EntitiesRepositoryTest {
assertThat(savedEntities[0].properties.size, equalTo(1))
assertThat(savedEntities[0].properties[0].first, equalTo("prop"))
}

@Test
fun `#query returns matching entities`() {
val repository = buildSubject()

val leoville = Entity.New(
"1",
"Léoville Barton 2008",
version = 1,
properties = listOf("vintage" to "2008")
)

val canet = Entity.New(
"2",
"Pontet-Canet 2014",
version = 2,
properties = listOf("vintage" to "2009")
)

val aultmore = Entity.New(
"3",
"Aultmore 12",
version = 1,
properties = listOf("vintage" to "2009")
)

repository.save("wines", leoville, canet, aultmore)

val wines = repository.query(
"wines",
Query.Or(
Query.Eq("name", "1"),
Query.And(
Query.Eq("__version", "2"),
Query.Eq("vintage", "2009")
)
)
)
assertThat(wines, containsInAnyOrder(sameEntityAs(leoville), sameEntityAs(canet)))
}

@Test
fun `#query returns empty list when there are no matches`() {
val repository = buildSubject()

val leoville = Entity.New(
"1",
"Léoville Barton 2008",
version = 1,
properties = listOf("vintage" to "2008")
)

val canet = Entity.New(
"2",
"Pontet-Canet 2014",
version = 2,
properties = listOf("vintage" to "2009")
)

val aultmore = Entity.New(
"3",
"Aultmore 12",
version = 1,
properties = listOf("vintage" to "2009")
)

repository.save("wines", leoville, canet, aultmore)

val wines = repository.query(
"wines",
Query.And(
Query.Eq("name", "1"),
Query.Or(
Query.Eq("__version", "4"),
Query.Eq("vintage", "2010")
)
)
)
assertThat(wines, equalTo(emptyList()))
}

@Test
fun `#query returns empty list when there is a match in a different list`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val ardbeg = Entity.New("2", "Ardbeg 10",)

repository.save("wines", leoville)
repository.save("whisky", ardbeg)

assertThat(repository.query("wines", Query.Eq("label", "Ardbeg 10")), equalTo(emptyList()))
}

@Test
fun `#query returns empty list where there are no entities in the list`() {
val repository = buildSubject()
assertThat(repository.query("wines", Query.Eq("label", "Léoville Barton 2008")), equalTo(emptyList()))
}

@Test
fun `#query supports list names with dots and dashes`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val canet = Entity.New("2", "Pontet-Canet 2014")
repository.save("favourite-wines", leoville)
repository.save("other.favourite.wines", canet)

val queriedLeoville = repository.query("favourite-wines", Query.Eq("label", "Léoville Barton 2008"))
assertThat(queriedLeoville, containsInAnyOrder(sameEntityAs(leoville)))

val queriedCanet = repository.query("other.favourite.wines", Query.Eq("label", "Pontet-Canet 2014"))
assertThat(queriedCanet, containsInAnyOrder(sameEntityAs(canet)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,64 @@ class LocalEntitiesFilterStrategyTest {
val choices = scenario.choicesOf("/data/question").map { it.value }
assertThat(choices, containsInAnyOrder("thing1"))
}

@Test
fun `works correctly with complex expressions`() {
entitiesRepository.save(
"things",
Entity.New(
"thing1",
"Thing1",
version = 1,
properties = listOf("property" to "value1")
),
Entity.New(
"thing2",
"Thing2",
version = 2,
properties = listOf("property" to "value2")
),
Entity.New(
"thing3",
"Thing3",
version = 2,
properties = listOf("property" to "value3")
),
)

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/question",
"instance('things')/root/item[label='Thing1' or (__version='2' and property='value3')]",
"name",
"label"
)
)
),
controllerSupplier
)

val choices = scenario.choicesOf("/data/question").map { it.value }
assertThat(choices, containsInAnyOrder("thing1", "thing3"))
assertThat(fallthroughFilterStrategy.fellThrough, equalTo(false))
assertThat(instanceProvider.fullParsePerformed, equalTo(false))
}
}

private class FallthroughFilterStrategy : FilterStrategy {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ danlewAndroidJoda = { group = "net.danlew", name = "android.joda", version = "2.
rarepebbleColorpicker = { group = "com.github.martin-stone", name = "hsv-alpha-color-picker-android", version = "3.1.0" }
commonsIo = { group = "commons-io", name = "commons-io", version = "2.5" } # Commons 2.6+ introduce java.nio usage that we can't access until our minSdkVersion >= 26 (https://developer.android.com/reference/java/io/File#toPath())
opencsv = { group = "com.opencsv", name = "opencsv", version = "5.9" }
javarosa = { group = "org.getodk", name = "javarosa", version = "5.0.0" } # Online
javarosa = { group = "org.getodk", name = "javarosa", version = "5.1.0-SNAPSHOT-ab0e8f4" } # Online
# javarosa = { group = "org.getodk", name = "javarosa", version = "local" } # Local
karumiDexter = { group = "com.karumi", name = "dexter", version = "6.2.3" }
zxingAndroidEmbedded = { group = "com.journeyapps", name = "zxing-android-embedded", version = "4.3.0" }
Expand Down

0 comments on commit 8e9c25f

Please sign in to comment.