Skip to content

Commit

Permalink
Support Elastic primitive for UUID (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya authored Apr 25, 2023
1 parent f29a440 commit c7bfd44
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package zio.elasticsearch
import zio.json.ast.Json
import zio.json.ast.Json.{Num, Str}

import java.util.UUID

object ElasticPrimitive {
sealed trait ElasticPrimitive[A] {
def toJson(value: A): Json
Expand Down Expand Up @@ -48,6 +50,10 @@ object ElasticPrimitive {
def toJson(value: String): Json = Str(value)
}

implicit object ElasticUUID extends ElasticPrimitive[UUID] {
def toJson(value: UUID): Json = Str(value.toString)
}

final implicit class ElasticPrimitiveOps[A](private val value: A) extends AnyVal {
def toJson(implicit EP: ElasticPrimitive[A]): Json = EP.toJson(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import zio.test.Assertion.equalTo
import zio.test._

object AggregationSpec extends ZIOSpecDefault {

def spec: Spec[Environment with TestEnvironment with Scope, Any] =
suite("Aggregations")(
suite("creating ElasticAggregation")(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package zio.elasticsearch

import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.query.Match
import zio.test.Assertion.equalTo
import zio.test._

import java.util.UUID

object ElasticPrimitiveSpec extends ZIOSpecDefault {
override def spec: Spec[TestEnvironment, Any] =
suite("Elastic Primitives")(
test("successfully create matches query with String") {
assert(matches(FieldName, "string"))(equalTo(Match[Any, String](FieldName, "string", boost = None)))
},
test("successfully create matches query with BigDecimal") {
assert(matches(FieldName, BigDecimal(1)))(
equalTo(Match[Any, BigDecimal](FieldName, BigDecimal(1), boost = None))
)
},
test("successfully create matches query with Boolean") {
assert(matches(FieldName, true))(equalTo(Match[Any, Boolean](FieldName, true, boost = None)))
},
test("successfully create matches query with Double") {
assert(matches(FieldName, 1.00))(equalTo(Match[Any, Double](FieldName, 1.00, boost = None)))
},
test("successfully create matches query with Int") {
assert(matches(FieldName, 1))(equalTo(Match[Any, Int](FieldName, 1, boost = None)))
},
test("successfully create matches query with Long") {
assert(matches(FieldName, 1L))(equalTo(Match[Any, Long](FieldName, 1L, boost = None)))
},
test("successfully create matches query with UUID") {
val uuid = UUID.randomUUID()
assert(matches(FieldName, uuid))(equalTo(Match[Any, UUID](FieldName, uuid, boost = None)))
}
)

private val FieldName = "fieldName"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ import zio.test._
import java.time.LocalDate

object ElasticRequestDSLSpec extends ZIOSpecDefault {

private val query = ElasticQuery.range(TestDocument.intField).gte(10)
private val index = IndexName("index")
private val docId = DocumentId("documentid")

override def spec: Spec[TestEnvironment, Any] =
suite("Elastic Requests JSON encoding")(
test("successfully encode search request to JSON") {
val jsonRequest: Json = search(index, query) match {
val jsonRequest: Json = search(Index, Query) match {
case r: ElasticRequest.Search => r.toJson
}
val expected =
Expand All @@ -44,7 +39,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
assert(jsonRequest)(equalTo(expected.toJson))
},
test("successfully encode search request to JSON with search after parameter") {
val jsonRequest: Json = search(index, query).searchAfter(Arr(Str("12345"))) match {
val jsonRequest: Json = search(Index, Query).searchAfter(Arr(Str("12345"))) match {
case r: ElasticRequest.Search => r.toJson
}
val expected =
Expand All @@ -66,7 +61,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
assert(jsonRequest)(equalTo(expected.toJson))
},
test("successfully encode search request to JSON with size parameter") {
val jsonRequest: Json = search(index, query).size(20) match {
val jsonRequest: Json = search(Index, Query).size(20) match {
case r: ElasticRequest.Search => r.toJson
}
val expected =
Expand All @@ -86,7 +81,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
assert(jsonRequest)(equalTo(expected.toJson))
},
test("successfully encode search request to JSON with multiple parameters") {
val jsonRequest = search(index, query)
val jsonRequest = search(Index, Query)
.size(20)
.sort(sortBy(TestDocument.intField).missing(First))
.from(10) match {
Expand Down Expand Up @@ -117,7 +112,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
assert(jsonRequest)(equalTo(expected.toJson))
},
test("successfully encode search request to JSON with all parameters") {
val jsonRequest = search(index, query)
val jsonRequest = search(Index, Query)
.size(20)
.highlights(highlight(TestDocument.intField))
.sort(sortBy(TestDocument.intField).missing(First))
Expand Down Expand Up @@ -154,7 +149,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
assert(jsonRequest)(equalTo(expected.toJson))
},
test("successfully encode search and aggregate request to JSON with all parameters") {
val jsonRequest = search(index, query)
val jsonRequest = search(Index, Query)
.aggregate(termsAggregation(name = "aggregation", field = "day_of_week"))
.size(20)
.highlights(highlight(TestDocument.intField))
Expand Down Expand Up @@ -200,7 +195,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
},
test("successfully encode update by query request to JSON") {
val jsonRequest = updateByQuery(
index = index,
index = Index,
query = term(TestDocument.stringField.keyword, "StringField"),
script = Script("ctx._source['intField']++")
) match { case r: UpdateByQuery => r.toJson }
Expand All @@ -225,8 +220,8 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
},
test("successfully encode update request to JSON with all parameters - script") {
val jsonRequest = updateByScript(
index = index,
id = docId,
index = Index,
id = DocId,
script = Script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
).orCreate[TestDocument](
TestDocument(
Expand Down Expand Up @@ -261,8 +256,8 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
},
test("successfully encode update request to JSON with all parameters - doc") {
val jsonRequest = update[TestDocument](
index = index,
id = docId,
index = Index,
id = DocId,
doc = TestDocument(
stringField = "stringField1",
subDocumentList = Nil,
Expand Down Expand Up @@ -303,4 +298,8 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
assert(jsonRequest)(equalTo(expected.toJson))
}
)

private val Query = ElasticQuery.range(TestDocument.intField).gte(10)
private val Index = IndexName("index")
private val DocId = DocumentId("documentid")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import zio.elasticsearch.domain.{TestNestedField, TestSubDocument}
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assertTrue}

object FieldDSLSpec extends ZIOSpecDefault {

def spec: Spec[TestEnvironment, Any] =
suite("Field DSL")(
test("properly encode single field path")(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assert}
import zio.{Chunk, Scope}

object HighlightsSpec extends ZIOSpecDefault {

def spec: Spec[Environment with TestEnvironment with Scope, Any] =
suite("Highlight")(
suite("creating Highlight")(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import zio.test.Assertion._
import zio.test.{Spec, TestEnvironment, TestResultZIOOps, assertZIO}

object HttpElasticExecutorSpec extends SttpBackendStubSpec {

def spec: Spec[TestEnvironment, Any] =
suite("HttpExecutor")(
test("aggregation request") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import zio.test.Assertion.equalTo
import zio.test._

object IndexNameSpec extends ZIOSpecDefault {

def spec: Spec[TestEnvironment, Any] =
suite("IndexName validation")(
test("succeed for valid string") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import zio.test.Assertion.equalTo
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assert}

object QueryDSLSpec extends ZIOSpecDefault {

def spec: Spec[Environment with TestEnvironment with Scope, Any] =
suite("Query DSL")(
suite("creating ElasticQuery")(
Expand Down

0 comments on commit c7bfd44

Please sign in to comment.