Skip to content

Commit

Permalink
(tests): Restructure HttpElasticExecutorSpec (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
mihajlo-ra92 authored Jul 6, 2023
1 parent 12c26d9 commit afeda67
Showing 1 changed file with 130 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,82 +33,82 @@ import zio.test.{Spec, TestEnvironment, TestResultZIOOps, assertZIO}

object HttpElasticExecutorSpec extends SttpBackendStubSpec {
def spec: Spec[TestEnvironment, Any] =
suite("HttpExecutor")(
test("aggregation request") {
assertZIO(
suite("HttpElasticExecutor")(
test("aggregation") {
val executorAgregate =
Executor
.execute(
ElasticRequest.aggregate(index, termsAggregation(name = "aggregation1", field = "name"))
)
.execute(ElasticRequest.aggregate(index, termsAggregation(name = "aggregation1", field = "name")))
.aggregations
)(
equalTo(
Map(
"aggregation1" -> TermsAggregationResult(
docErrorCount = 0,
sumOtherDocCount = 0,
buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty))
)

val expectedTermsAggregationResult =
Map(
"aggregation1" -> TermsAggregationResult(
docErrorCount = 0,
sumOtherDocCount = 0,
buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty))
)
)
)

assertZIO(executorAgregate)(equalTo(expectedTermsAggregationResult))
},
test("bulk request") {
assertZIO(
Executor.execute(ElasticRequest.bulk(ElasticRequest.create(index, doc)).refreshTrue)
)(
equalTo(
BulkResponse(
took = 3,
errors = false,
items = Chunk(
CreateBulkResponse(
index = "repositories",
id = "123",
version = Some(1),
result = Some("created"),
shards = Some(Shards(total = 1, successful = 1, failed = 0)),
status = Some(201),
error = None
)
test("bulk") {
val executorBulk =
Executor
.execute(
ElasticRequest
.bulk(ElasticRequest.create(index, doc))
.refreshTrue
)
val expectedBulkResponse =
BulkResponse(
took = 3,
errors = false,
items = Chunk(
CreateBulkResponse(
index = "repositories",
id = "123",
version = Some(1),
result = Some("created"),
shards = Some(Shards(total = 1, successful = 1, failed = 0)),
status = Some(201),
error = None
)
)
)
assertZIO(executorBulk)(
equalTo(expectedBulkResponse)
)
},
test("count request") {
assertZIO(Executor.execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))))(
equalTo(2)
)
test("count") {
val executorCount =
Executor
.execute(ElasticRequest.count(index, matchAll).routing(Routing("routing")))
assertZIO(executorCount)(equalTo(2))
},
test("creating document request") {
assertZIO(
Executor.execute(
ElasticRequest
.create[TestDocument](index = index, doc = doc)
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(DocumentId("V4x8q4UB3agN0z75fv5r")))
test("create") {
val executorCreate =
Executor
.execute(
ElasticRequest
.create[TestDocument](index = index, doc = doc)
.routing(Routing("routing"))
.refreshTrue
)
assertZIO(executorCreate)(equalTo(DocumentId("V4x8q4UB3agN0z75fv5r")))
},
test("creating request with given ID") {
assertZIO(
test("create with ID") {
val executorCreateDocumentId =
Executor.execute(
ElasticRequest
.create[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc)
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(Created))
assertZIO(executorCreateDocumentId)(equalTo(Created))
},
test("creating index request without mapping") {
assertZIO(
test("createIndex") {
val executorCreateIndex =
Executor.execute(ElasticRequest.createIndex(name = index))
)(
equalTo(Created)
)
},
test("creating index request with mapping") {
val mapping =
"""
|{
Expand All @@ -129,130 +129,105 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec {
| }
|}
|""".stripMargin

assertZIO(
val executorCreateIndexMapping =
Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping))
)(
equalTo(Created)
)
assertZIO(executorCreateIndex)(equalTo(Created)) &&
assertZIO(executorCreateIndexMapping)(equalTo(Created))
},
test("creating or updating request") {
assertZIO(
Executor.execute(
ElasticRequest
.upsert[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc)
.routing(Routing("routing"))
.refreshTrue
)
)(isUnit)
},
test("deleting by ID request") {
assertZIO(
test("deleteById") {
val executorDeleteById =
Executor.execute(
ElasticRequest
.deleteById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"))
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(Deleted))
assertZIO(executorDeleteById)(equalTo(Deleted))
},
test("deleting by query request") {
assertZIO(
test("deleteByQuery") {
val executorDeleteByQuery =
Executor.execute(
ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.routing(Routing("routing"))
)
)(
equalTo(Deleted)
)
assertZIO(executorDeleteByQuery)(equalTo(Deleted))
},
test("deleting index request") {
assertZIO(Executor.execute(ElasticRequest.deleteIndex(name = index)))(
equalTo(Deleted)
)
test("deleteIndex") {
val executorDeleteIndex =
Executor.execute(ElasticRequest.deleteIndex(name = index))
assertZIO(executorDeleteIndex)(equalTo(Deleted))
},
test("exists request") {
assertZIO(
test("exists") {
val executorExists =
Executor.execute(
ElasticRequest
.exists(index = index, id = DocumentId("example-id"))
.routing(Routing("routing"))
)
)(isTrue)
assertZIO(executorExists)(isTrue)
},
test("getting by ID request") {
assertZIO(
test("getById") {
val executorGetById =
Executor
.execute(
ElasticRequest
.getById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"))
.routing(Routing("routing"))
)
.documentAs[TestDocument]
)(isSome(equalTo(doc)))
assertZIO(executorGetById)(isSome(equalTo(doc)))
},
test("search request") {
assertZIO(
test("search") {
val executorSearch =
Executor
.execute(ElasticRequest.search(index = index, query = matchAll))
.documentAs[TestDocument]
)(equalTo(Chunk(doc)))
val terms = termsAggregation(name = "aggregation1", field = "name")
val executorSearchWithTerms = Executor
.execute(ElasticRequest.search(index = index, query = matchAll, terms))
.documentAs[TestDocument]
assertZIO(executorSearch)(equalTo(Chunk(doc))) && assertZIO(executorSearchWithTerms)(equalTo(Chunk(doc)))
},
test("search with aggregation request") {
test("search and aggergate") {
val terms = termsAggregation(name = "aggregation1", field = "name")
val req = Executor
val executorSearchAggregations = Executor
.execute(ElasticRequest.search(index = index, query = matchAll, terms))
assertZIO(req.documentAs[TestDocument])(equalTo(Chunk(doc))) &&
assertZIO(req.aggregations)(
equalTo(
Map(
"aggregation1" -> TermsAggregationResult(
docErrorCount = 0,
sumOtherDocCount = 0,
buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty))
)
.aggregations
val expectedTermsAggregationResult =
Map(
"aggregation1" -> TermsAggregationResult(
docErrorCount = 0,
sumOtherDocCount = 0,
buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty))
)
)
)
},
test("update request with script") {
assertZIO(
Executor.execute(
ElasticRequest
.updateByScript(
index = index,
id = DocumentId("V4x8q4UB3agN0z75fv5r"),
script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)
)
.orCreate(doc = secondDoc)
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(UpdateOutcome.Updated))
assertZIO(executorSearchAggregations)(equalTo(expectedTermsAggregationResult))
},
test("update request with doc") {
assertZIO(
test("update") {
val executorUpdate =
Executor.execute(
ElasticRequest
.update[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc)
.orCreate(doc = secondDoc)
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(UpdateOutcome.Updated))
assertZIO(executorUpdate)(equalTo(UpdateOutcome.Updated))
},
test("update all by query request") {
assertZIO(
test("updateAllByQuery") {
val executorUpdateAllByQuery =
Executor.execute(
ElasticRequest
.updateAllByQuery(index = index, script = Script("ctx._source['intField']++"))
.conflicts(Proceed)
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2)))
val expectedUpdateByQueryResult =
UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2)
assertZIO(executorUpdateAllByQuery)(equalTo(expectedUpdateByQueryResult))
},
test("update by query request") {
assertZIO(
test("updateByQuery") {
val executorUpdateByQuery =
Executor.execute(
ElasticRequest
.updateByQuery(
Expand All @@ -264,7 +239,34 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec {
.routing(Routing("routing"))
.refreshTrue
)
)(equalTo(UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2)))
val expectedUpdateByQueryResult =
UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2)
assertZIO(executorUpdateByQuery)(equalTo(expectedUpdateByQueryResult))
},
test("updateByScript") {
val executorUpdateByScript =
Executor.execute(
ElasticRequest
.updateByScript(
index = index,
id = DocumentId("V4x8q4UB3agN0z75fv5r"),
script = Script("ctx._source.intField += params['factor']").params("factor" -> 2)
)
.orCreate(doc = secondDoc)
.routing(Routing("routing"))
.refreshTrue
)
assertZIO(executorUpdateByScript)(equalTo(UpdateOutcome.Updated))
},
test("upsert") {
val executorUpsert =
Executor.execute(
ElasticRequest
.upsert[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc)
.routing(Routing("routing"))
.refreshTrue
)
assertZIO(executorUpsert)(isUnit)
}
).provideShared(elasticsearchSttpLayer)
}

0 comments on commit afeda67

Please sign in to comment.