All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.lightningdb.TextConditionTest.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
@file:UseContextualSerialization(UUID::class)
package com.lightningkite.lightningdb

import com.lightningkite.lightningdb.test.*
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseContextualSerialization
import org.junit.Test
import java.util.*
import kotlin.test.assertContains
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@Serializable
@GenerateDataClassPaths
@TextIndex(["string"])
data class ModelWithTextIndex(
    override val _id: UUID = UUID.randomUUID(),
    val string: String
): HasId

class TextConditionTest: MongoTest() {

    @Test
    fun testTextSearch() = runBlocking {
        val collection = defaultMongo.collection() as MongoFieldCollection
        val value1 = ModelWithTextIndex(string = "One Two Three")
        val value2 = ModelWithTextIndex(string = "Five Six Seven")
        collection.insertOne(value1)
        collection.insertOne(value2)

        var query = "One"
        var condition = path().fullTextSearch(query, false)
        var results = collection.find(condition).toList()
        assertContains(results, value1)

        query = "one"
        condition = path().fullTextSearch(query, false)
        results = collection.find(condition).toList()
        assertFalse(results.contains(value1))

        query = "one"
        condition = path().fullTextSearch(query, true)
        results = collection.find(condition).toList()
        assertContains(results, value1)

        query = "four two"
        condition = path().fullTextSearch(query, true)
        results = collection.find(condition).toList()
        assertContains(results, value1)

        query = "four five"
        condition = path().fullTextSearch(query, true)
        results = collection.find(condition).toList()
        assertContains(results, value2)

        query = "three five"
        condition = path().fullTextSearch(query, true)
        results = collection.find(condition).toList()
        assertContains(results, value1)
        assertContains(results, value2)
    }

    @Test
    fun testContains() = runBlocking {
        val collection = defaultMongo.collection("TextConditionSearchTest") as MongoFieldCollection
        val value1 = ModelWithTextIndex(string = "One Two Three")
        val value2 = ModelWithTextIndex(string = "Five Six Seven")
        collection.insertOne(value1)
        collection.insertOne(value2)

        var query = "One"
        var condition = path().string.contains(query, false)
        var results = collection.find(condition).toList()
        assertContains(results, value1)

        query = "one"
        condition = path().string.contains(query, false)
        results = collection.find(condition).toList()
        assertFalse(results.contains(value1))

        query = "one"
        condition = path().string.contains(query, true)
        results = collection.find(condition).toList()
        assertContains(results, value1)

        query = "four two"
        condition = path().string.contains(query, true)
        results = collection.find(condition).toList()
        assertTrue(results.isEmpty())

        query = "six seven"
        condition = path().string.contains(query, true)
        results = collection.find(condition).toList()
        assertContains(results, value2)

        query = "seven six"
        condition = path().string.contains(query, true)
        results = collection.find(condition).toList()
        assertTrue(results.isEmpty())

        query = "four five"
        condition = path().string.contains(query, true)
        results = collection.find(condition).toList()
        assertTrue(results.isEmpty())

        query = "three five"
        condition = path().string.contains(query, true)
        results = collection.find(condition).toList()
        assertTrue(results.isEmpty())
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy