-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a default searchRepository implementation
- Loading branch information
1 parent
19f6c4d
commit 963053b
Showing
7 changed files
with
164 additions
and
4 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
108 changes: 108 additions & 0 deletions
108
src/test/kotlin/no/liflig/documentstore/SearchRepositoryTest.kt
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,108 @@ | ||
package no.liflig.documentstore | ||
|
||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import kotlinx.coroutines.runBlocking | ||
import no.liflig.documentstore.dao.CrudDaoJdbi | ||
import no.liflig.documentstore.dao.SearchRepositoryJdbi | ||
import no.liflig.documentstore.examples.ExampleEntity | ||
import no.liflig.documentstore.examples.ExampleId | ||
import no.liflig.documentstore.examples.ExampleSerializationAdapter | ||
import no.liflig.documentstore.examples.ExampleTextQuery | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class SearchRepositoryTest { | ||
val jdbi = createTestDatabase() | ||
val serializationAdapter = ExampleSerializationAdapter() | ||
val dao = CrudDaoJdbi(jdbi, "example", serializationAdapter) | ||
|
||
val searchRepository = | ||
SearchRepositoryJdbi<ExampleId, ExampleEntity, ExampleTextQuery>(jdbi, "example", serializationAdapter) | ||
|
||
@BeforeEach | ||
fun clearDatabase() { | ||
searchRepository.search(ExampleTextQuery()).forEach { | ||
dao.delete(it.item.id, it.version) | ||
} | ||
} | ||
|
||
@Test | ||
fun testWhere() { | ||
runBlocking { | ||
dao.create(ExampleEntity.create("hello world")) | ||
dao.create(ExampleEntity.create("world")) | ||
|
||
val result = searchRepository.search(ExampleTextQuery(text = "hello world")) | ||
|
||
result shouldHaveSize 1 | ||
} | ||
} | ||
|
||
@Test | ||
fun testLimit() { | ||
runBlocking { | ||
dao.create(ExampleEntity.create("1")) | ||
dao.create(ExampleEntity.create("2")) | ||
dao.create(ExampleEntity.create("3")) | ||
|
||
val result = searchRepository.search(ExampleTextQuery(limit = 2)) | ||
|
||
result shouldHaveSize 2 | ||
} | ||
} | ||
|
||
@Test | ||
fun testOffsett() { | ||
runBlocking { | ||
dao.create(ExampleEntity.create("hello world")) | ||
dao.create(ExampleEntity.create("world")) | ||
dao.create(ExampleEntity.create("world")) | ||
|
||
val result = searchRepository.search(ExampleTextQuery(offset = 1)) | ||
|
||
result shouldHaveSize 2 | ||
} | ||
} | ||
|
||
@Test | ||
fun emptySearchReturnsAllElements() { | ||
runBlocking { | ||
dao.create(ExampleEntity.create("A")) | ||
dao.create(ExampleEntity.create("B")) | ||
dao.create(ExampleEntity.create("C")) | ||
|
||
val result = searchRepository.search(ExampleTextQuery()) | ||
|
||
result shouldHaveSize 3 | ||
} | ||
} | ||
|
||
@Test | ||
fun orderAscWorks() { | ||
runBlocking { | ||
dao.create(ExampleEntity.create("A")) | ||
dao.create(ExampleEntity.create("B")) | ||
dao.create(ExampleEntity.create("C")) | ||
|
||
val result = searchRepository.search(ExampleTextQuery(orderBy = "data->>'text'", orderDesc = false)).map { it.item.text } | ||
|
||
result shouldBeEqual listOf("A", "B", "C") | ||
} | ||
} | ||
@Test | ||
fun orderDescWorksBothWays() { | ||
runBlocking { | ||
dao.create(ExampleEntity.create("A")) | ||
dao.create(ExampleEntity.create("B")) | ||
dao.create(ExampleEntity.create("C")) | ||
|
||
val result = searchRepository.search(ExampleTextQuery(orderBy = "data->>'text'", orderDesc = false)).map { it.item.id } | ||
val resul2 = searchRepository.search(ExampleTextQuery(orderBy = "data->>'text'", orderDesc = true)).map { it.item.id } | ||
|
||
result shouldBeEqual resul2.asReversed() | ||
} | ||
} | ||
} |
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
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
3 changes: 2 additions & 1 deletion
3
.../documentstore/ExampleSearchRepository.kt → ...store/examples/ExampleSearchRepository.kt
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
2 changes: 1 addition & 1 deletion
2
...umentstore/ExampleSerializationAdapter.kt → ...e/examples/ExampleSerializationAdapter.kt
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
15 changes: 15 additions & 0 deletions
15
src/test/kotlin/no/liflig/documentstore/examples/ExampleTextQuery.kt
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,15 @@ | ||
package no.liflig.documentstore.examples | ||
|
||
import no.liflig.documentstore.dao.QueryObject | ||
import org.jdbi.v3.core.statement.Query | ||
|
||
class ExampleTextQuery( | ||
val text: String? = null, | ||
override val limit: Int? = null, | ||
override val offset: Int? = null, | ||
override val orderBy: String? = null, | ||
override val orderDesc: Boolean = false, | ||
) : QueryObject() { | ||
override val sqlWhere: String = ":wildcardQuery IS NULL OR data->>'text' ILIKE :wildcardQuery " | ||
override val bindSqlParameters: Query.() -> Query = { bind("wildcardQuery", text?.let { "%$it%" }) } | ||
} |