-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5e0194
commit d1c2405
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.odk.collect.db.sqlite | ||
|
||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
|
||
class QueryTest { | ||
@Test | ||
fun `Eq query generates correct selection and arguments`() { | ||
val query = Query.Eq("name", "John") | ||
|
||
assertThat(query.selection, equalTo("name = ?")) | ||
assertThat(query.selectionArgs, equalTo(arrayOf("John"))) | ||
} | ||
|
||
@Test | ||
fun `NotEq query generates correct selection and arguments`() { | ||
val query = Query.NotEq("age", "30") | ||
|
||
assertThat(query.selection, equalTo("age != ?")) | ||
assertThat(query.selectionArgs, equalTo(arrayOf("30"))) | ||
} | ||
|
||
@Test | ||
fun `And query generates correct selection and arguments`() { | ||
val queryA = Query.Eq("name", "John") | ||
val queryB = Query.NotEq("age", "30") | ||
val combinedQuery = Query.And(queryA, queryB) | ||
|
||
assertThat(combinedQuery.selection, equalTo("(name = ? AND age != ?)")) | ||
assertThat(combinedQuery.selectionArgs, equalTo(arrayOf("John", "30"))) | ||
} | ||
|
||
@Test | ||
fun `Or query generates correct selection and arguments`() { | ||
val queryA = Query.Eq("city", "New York") | ||
val queryB = Query.NotEq("country", "Canada") | ||
val combinedQuery = Query.Or(queryA, queryB) | ||
|
||
assertThat(combinedQuery.selection, equalTo("(city = ? OR country != ?)")) | ||
assertThat(combinedQuery.selectionArgs, equalTo(arrayOf("New York", "Canada"))) | ||
} | ||
|
||
@Test | ||
fun `nested And and Or queries generates correct selection and arguments`() { | ||
val queryA = Query.Eq("status", "active") | ||
val queryB = Query.NotEq("role", "admin") | ||
val queryC = Query.Eq("team", "engineering") | ||
|
||
val combinedQuery = Query.And(Query.Or(queryA, queryB), queryC) | ||
|
||
assertThat(combinedQuery.selection, equalTo("((status = ? OR role != ?) AND team = ?)")) | ||
assertThat(combinedQuery.selectionArgs, equalTo(arrayOf("active", "admin", "engineering"))) | ||
} | ||
} |