Skip to content

Commit

Permalink
Add tests for type-safe query API
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir committed Jan 23, 2023
1 parent 5fea531 commit 54ac5cf
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package zio.elasticsearch

import zio.Scope
import zio.elasticsearch.Annotation.name
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.utils._
import zio.schema.{DeriveSchema, Schema}
import zio.test.Assertion.equalTo
import zio.test._

object QueryDSLSpec extends ZIOSpecDefault {

final case class Student(
name: String,
age: Int,
@name("is_employed")
isEmployed: Boolean
)

object Student {

implicit val schema: Schema.CaseClass3[String, Int, Boolean, Student] = DeriveSchema.gen[Student]

val (name, age, isEmployed) = schema.makeAccessors(ElasticQueryAccessorBuilder)
}

override def spec: Spec[Environment with TestEnvironment with Scope, Any] =
suite("Query DSL")(
suite("creating ElasticQuery")(
Expand All @@ -19,6 +36,20 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(queryBool)(equalTo(MatchQuery(field = "day_of_week", value = true)))
assert(queryLong)(equalTo(MatchQuery(field = "day_of_week", value = 1)))
},
test("successfully create type-safe Match query using `matches` method") {
val queryString = matches(field = Student.name, value = "James")
val queryInt = matches(field = Student.age, value = 20)
val queryBool = matches(field = Student.isEmployed, value = true)

assert(queryString)(equalTo(MatchQuery(field = "name", value = "Monday")))
assert(queryInt)(equalTo(MatchQuery(field = "age", value = 20)))
assert(queryBool)(equalTo(MatchQuery(field = "is_employed", value = true)))
},
test("successfully create type-safe Match query with multi-field using `matches` method") {
val query = matches(field = Student.name, multiField = Some("keyword"), value = "James")

assert(query)(equalTo(MatchQuery(field = "name.keyword", value = "James")))
},
test("successfully create `Must` query from two Match queries") {
val query = boolQuery()
.must(matches(field = "day_of_week", value = "Monday"), matches(field = "customer_gender", value = "MALE"))
Expand Down Expand Up @@ -96,6 +127,16 @@ object QueryDSLSpec extends ZIOSpecDefault {

assert(query)(equalTo(ExistsQuery(field = "day_of_week")))
},
test("successfully create Exists Query with accessor") {
val query = exists(field = Student.name)

assert(query)(equalTo(ExistsQuery(field = "name")))
},
test("successfully create Exists Query with accessor and multi-field") {
val query = exists(field = Student.name, multiField = Some("keyword"))

assert(query)(equalTo(ExistsQuery(field = "name.keyword")))
},
test("successfully create MatchAll Query") {
val query = matchAll()

Expand All @@ -111,6 +152,20 @@ object QueryDSLSpec extends ZIOSpecDefault {

assert(query)(equalTo(RangeQuery(field = "customer_age", lower = Unbounded, upper = Unbounded)))
},
test("successfully create empty type-safe Range Query") {
val queryString = range(field = Student.name)
val queryInt = range(field = Student.age)
val queryBool = range(field = Student.isEmployed)

assert(queryString)(equalTo(RangeQuery(field = "name", lower = Unbounded, upper = Unbounded)))
assert(queryInt)(equalTo(RangeQuery(field = "age", lower = Unbounded, upper = Unbounded)))
assert(queryBool)(equalTo(RangeQuery(field = "is_employed", lower = Unbounded, upper = Unbounded)))
},
test("successfully create empty type-safe Range Query with multi-field") {
val query = range(field = Student.name, multiField = Some("keyword"))

assert(query)(equalTo(RangeQuery(field = "name.keyword", lower = Unbounded, upper = Unbounded)))
},
test("successfully create Range Query with upper bound") {
val query = range(field = "customer_age").lt(23)

Expand Down Expand Up @@ -151,6 +206,20 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(queryBool)(equalTo(TermQuery(field = "day_of_week", value = true)))
assert(queryLong)(equalTo(TermQuery(field = "day_of_week", value = 1L)))
},
test("successfully create type-safe Term Query") {
val queryString = term(field = Student.name, value = "James")
val queryInt = term(field = Student.age, value = 20)
val queryBool = term(field = Student.isEmployed, value = true)

assert(queryString)(equalTo(TermQuery(field = "name", value = "Monday")))
assert(queryInt)(equalTo(TermQuery(field = "age", value = 20)))
assert(queryBool)(equalTo(TermQuery(field = "is_employed", value = true)))
},
test("successfully create type-safe Term Query with multi-field") {
val query = term(field = Student.name, multiField = Some("keyword"), value = "James")

assert(query)(equalTo(TermQuery(field = "name.keyword", value = "James")))
},
test("successfully create Term Query with boost") {
val queryInt = term(field = "day_of_week", value = 1).boost(1.0)
val queryString = term(field = "day_of_week", value = "Monday").boost(1.0)
Expand Down

0 comments on commit 54ac5cf

Please sign in to comment.