From 302163a9bd807f1d042a2876777321ea088856a5 Mon Sep 17 00:00:00 2001 From: minwoo Date: Sun, 1 Oct 2023 00:48:29 +0900 Subject: [PATCH] test: add KProperty field test --- .../search/QueryStringQueryOptionDslTest.kt | 81 +++++++++++++++++-- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/core/src/test/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/search/QueryStringQueryOptionDslTest.kt b/core/src/test/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/search/QueryStringQueryOptionDslTest.kt index 4d9c87ca..69c3a917 100644 --- a/core/src/test/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/search/QueryStringQueryOptionDslTest.kt +++ b/core/src/test/kotlin/com/github/inflab/spring/data/mongodb/core/aggregation/search/QueryStringQueryOptionDslTest.kt @@ -4,6 +4,8 @@ import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe internal class QueryStringQueryOptionDslTest : FreeSpec({ + data class Sample(val field: String) + fun query(block: QueryStringQueryOptionDsl.() -> Unit) = QueryStringQueryOptionDsl().apply(block) @@ -21,7 +23,7 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ result shouldBe "\"search\"" } - "should add a text with field" { + "should add a text with string field" { // given val option = query { text("search", "field") @@ -34,6 +36,19 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ result shouldBe "field:\"search\"" } + "should add a text with property field" { + // given + val option = query { + text("search", Sample::field) + } + + // when + val result = option.build() + + // then + result shouldBe "field:\"search\"" + } + "should escape special characters" { // given val option = query { @@ -62,7 +77,7 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ result shouldBe "search*?" } - "should add a wildcard with field" { + "should add a wildcard with string field" { // given val option = query { wildcard("search", "field") @@ -74,6 +89,19 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ // then result shouldBe "field:search" } + + "should add a wildcard with property field" { + // given + val option = query { + wildcard("search", Sample::field) + } + + // when + val result = option.build() + + // then + result shouldBe "field:search" + } } "regex" - { @@ -90,7 +118,7 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ result shouldBe "/search/" } - "should add a regex with field" { + "should add a regex with string field" { // given val option = query { regex("search", "field") @@ -102,6 +130,19 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ // then result shouldBe "field:/search/" } + + "should add a regex with property field" { + // given + val option = query { + regex("search", Sample::field) + } + + // when + val result = option.build() + + // then + result shouldBe "field:/search/" + } } "range" - { @@ -172,7 +213,7 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ result shouldBe "search~2" } - "should add a fuzzy with field" { + "should add a fuzzy with string field" { // given val option = query { fuzzy("search", 3, "field") @@ -184,6 +225,19 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ // then result shouldBe "field:search~3" } + + "should add a fuzzy with property field" { + // given + val option = query { + fuzzy("search", 3, Sample::field) + } + + // when + val result = option.build() + + // then + result shouldBe "field:search~3" + } } "not" - { @@ -245,17 +299,30 @@ internal class QueryStringQueryOptionDslTest : FreeSpec({ result shouldBe "(\"a\" OR \"b\") AND \"c\"" } - "should add subquery block with field" { + "should add subquery block with string field" { + // given + val option = query { + sub(text("a") or text("b"), "field") + } + + // when + val result = option.build() + + // then + result shouldBe "field:(\"a\" OR \"b\")" + } + + "should add subquery block with property field" { // given val option = query { - sub(text("a") or text("b"), "c") + sub(text("a") or text("b"), Sample::field) } // when val result = option.build() // then - result shouldBe "c:(\"a\" OR \"b\")" + result shouldBe "field:(\"a\" OR \"b\")" } } })