From d1c2405d918832456f7a5b6f7a669cb556e2966e Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Mon, 16 Dec 2024 03:04:02 +0100 Subject: [PATCH] Added tests for Query class --- .../org/odk/collect/db/sqlite/QueryTest.kt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 db/src/test/java/org/odk/collect/db/sqlite/QueryTest.kt diff --git a/db/src/test/java/org/odk/collect/db/sqlite/QueryTest.kt b/db/src/test/java/org/odk/collect/db/sqlite/QueryTest.kt new file mode 100644 index 00000000000..ac80f29bca5 --- /dev/null +++ b/db/src/test/java/org/odk/collect/db/sqlite/QueryTest.kt @@ -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"))) + } +}