Skip to content

Commit

Permalink
test: add KProperty field test
Browse files Browse the repository at this point in the history
  • Loading branch information
masonJS committed Sep 30, 2023
1 parent 08d6a0a commit 302163a
Showing 1 changed file with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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" - {
Expand All @@ -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")
Expand All @@ -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" - {
Expand Down Expand Up @@ -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")
Expand All @@ -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" - {
Expand Down Expand Up @@ -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\")"
}
}
})

0 comments on commit 302163a

Please sign in to comment.