Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ElasticRequestSpec and add missing unit tests #222

Merged
merged 7 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ object ElasticRequest {

def routing(value: Routing): CreateRequest =
self.copy(routing = Some(value))

def toJson: Json =
document.json
}

sealed trait CreateWithIdRequest
Expand All @@ -459,14 +462,19 @@ object ElasticRequest {

def routing(value: Routing): CreateWithIdRequest =
self.copy(routing = Some(value))

def toJson: Json =
document.json
}

sealed trait CreateIndexRequest extends ElasticRequest[CreationOutcome]

private[elasticsearch] final case class CreateIndex(
name: IndexName,
definition: Option[String]
) extends CreateIndexRequest
) extends CreateIndexRequest {
def toJson: String = definition.getOrElse("")
}

sealed trait CreateOrUpdateRequest
extends BulkableRequest[Unit]
Expand All @@ -485,6 +493,9 @@ object ElasticRequest {

def routing(value: Routing): CreateOrUpdateRequest =
self.copy(routing = Some(value))

def toJson: Json =
document.json
}

sealed trait DeleteByIdRequest
Expand Down Expand Up @@ -649,6 +660,7 @@ object ElasticRequest {
includes merge excludes
})
}

Obj("query" -> query.toJson(fieldPath = None)) merge
fromJson merge
sizeJson merge
Expand Down Expand Up @@ -734,6 +746,7 @@ object ElasticRequest {
includes merge excludes
})
}

Obj("query" -> query.toJson(fieldPath = None)) merge
Obj("aggs" -> aggregation.toJson) merge
fromJson merge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
baseRequest
.post(uri)
.contentType(ApplicationJson)
.body(r.document.json)
.body(r.toJson)
.response(asJson[CreateResponse])
).flatMap { response =>
response.code match {
Expand All @@ -206,7 +206,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
baseRequest
.post(uri)
.contentType(ApplicationJson)
.body(r.document.json)
.body(r.toJson)
).flatMap { response =>
response.code match {
case HttpCreated => ZIO.succeed(CreationOutcome.Created)
Expand All @@ -216,12 +216,12 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
}
}

private def executeCreateIndex(createIndex: CreateIndex): Task[CreationOutcome] =
private def executeCreateIndex(r: CreateIndex): Task[CreationOutcome] =
sendRequest(
baseRequest
.put(uri"${esConfig.uri}/${createIndex.name}")
.put(uri"${esConfig.uri}/${r.name}")
.contentType(ApplicationJson)
.body(createIndex.definition.getOrElse(""))
.body(r.toJson)
).flatMap { response =>
response.code match {
case HttpOk => ZIO.succeed(CreationOutcome.Created)
Expand All @@ -234,7 +234,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
val uri = uri"${esConfig.uri}/${r.index}/$Doc/${r.id}"
.withParams(getQueryParams(Chunk(("refresh", r.refresh), ("routing", r.routing))))

sendRequest(baseRequest.put(uri).contentType(ApplicationJson).body(r.document.json)).flatMap { response =>
sendRequest(baseRequest.put(uri).contentType(ApplicationJson).body(r.toJson)).flatMap { response =>
response.code match {
case HttpOk | HttpCreated => ZIO.unit
case _ => ZIO.fail(handleFailures(response))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.ElasticRequest.Bulk
import zio.elasticsearch.domain._
import zio.elasticsearch.query.DistanceType.Plane
import zio.elasticsearch.query.DistanceUnit.Kilometers
import zio.elasticsearch.query.ValidationMethod.IgnoreMalformed
import zio.elasticsearch.query._
import zio.elasticsearch.utils._
import zio.prelude.Validation
import zio.test.Assertion.equalTo
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assert}

Expand Down Expand Up @@ -1533,46 +1531,6 @@ object ElasticQuerySpec extends ZIOSpecDefault {
assert(queryWithAllParams.toJson(fieldPath = None))(equalTo(expectedWithAllParams.toJson))
}
),
test("bulk") {
val query = IndexName.make("users").map { index =>
val nestedField = TestNestedField("NestedField", 1)
val subDoc = TestSubDocument(
stringField = "StringField",
nestedField = nestedField,
intField = 100,
intFieldList = Nil
)
val req1 =
ElasticRequest
.create[TestSubDocument](index, DocumentId("ETux1srpww2ObCx"), subDoc.copy(intField = 65))
.routing(unsafeWrap(subDoc.stringField)(Routing))
val req2 =
ElasticRequest.create[TestSubDocument](index, subDoc).routing(unsafeWrap(subDoc.stringField)(Routing))
val req3 = ElasticRequest
.upsert[TestSubDocument](index, DocumentId("yMyEG8iFL5qx"), subDoc.copy(stringField = "StringField2"))
.routing(unsafeWrap(subDoc.stringField)(Routing))
val req4 =
ElasticRequest
.deleteById(index, DocumentId("1VNzFt2XUFZfXZheDc"))
.routing(unsafeWrap(subDoc.stringField)(Routing))
ElasticRequest.bulk(req1, req2, req3, req4) match {
case r: Bulk => Some(r.body)
case _ => None
}
}

val expected =
"""|{ "create" : { "_index" : "users", "_id" : "ETux1srpww2ObCx", "routing" : "StringField" } }
|{"stringField":"StringField","nestedField":{"stringField":"NestedField","longField":1},"intField":65,"intFieldList":[]}
|{ "create" : { "_index" : "users", "routing" : "StringField" } }
|{"stringField":"StringField","nestedField":{"stringField":"NestedField","longField":1},"intField":100,"intFieldList":[]}
|{ "index" : { "_index" : "users", "_id" : "yMyEG8iFL5qx", "routing" : "StringField" } }
|{"stringField":"StringField2","nestedField":{"stringField":"NestedField","longField":1},"intField":100,"intFieldList":[]}
|{ "delete" : { "_index" : "users", "_id" : "1VNzFt2XUFZfXZheDc", "routing" : "StringField" } }
|""".stripMargin

assert(query)(equalTo(Validation.succeed(Some(expected))))
},
test("contains") {
val query = contains(TestDocument.stringField, "test")
val queryWithBoost = contains(TestDocument.stringField, "test").boost(3.14)
Expand Down
Loading