From 79e7962ecc87359fa4141434138fc00f86b7f527 Mon Sep 17 00:00:00 2001 From: mihajlo-ra92 Date: Sat, 24 Jun 2023 17:25:12 +0200 Subject: [PATCH 1/5] Refactor httpElasticExecutorSpec --- .../HttpElasticExecutorSpec.scala | 309 ++++++++++-------- 1 file changed, 180 insertions(+), 129 deletions(-) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 79c3b7ded..a0e3ec755 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -33,82 +33,43 @@ import zio.test.{Spec, TestEnvironment, TestResultZIOOps, assertZIO} object HttpElasticExecutorSpec extends SttpBackendStubSpec { def spec: Spec[TestEnvironment, Any] = - suite("HttpExecutor")( + suite("HttpElasticExecutor")( test("aggregation request") { - assertZIO( + + val executorAgregate = Executor .execute( - ElasticRequest.aggregate(index, termsAggregation(name = "aggregation1", field = "name")) + 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 executorBulk = + Executor + .execute( + ElasticRequest + .bulk(ElasticRequest.create(index, doc)) + .refreshTrue ) - ) - ) - }, - 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 - ) - ) + val executorCount = + Executor + .execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))) + val executorCreate = + Executor + .execute( + ElasticRequest + .create[TestDocument](index = index, doc = doc) + .routing(Routing("routing")) + .refreshTrue ) - ) - ) - }, - test("count request") { - assertZIO(Executor.execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))))( - equalTo(2) - ) - }, - test("creating document request") { - assertZIO( - Executor.execute( - ElasticRequest - .create[TestDocument](index = index, doc = doc) - .routing(Routing("routing")) - .refreshTrue - ) - )(equalTo(DocumentId("V4x8q4UB3agN0z75fv5r"))) - }, - test("creating request with given ID") { - assertZIO( + val executorCreateDocumentId = Executor.execute( ElasticRequest .create[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) .routing(Routing("routing")) .refreshTrue ) - )(equalTo(Created)) - }, - test("creating index request without mapping") { - assertZIO( + val executorCreateIndex = Executor.execute(ElasticRequest.createIndex(name = index)) - )( - equalTo(Created) - ) - }, - test("creating index request with mapping") { val mapping = """ |{ @@ -129,58 +90,35 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { | } |} |""".stripMargin - - assertZIO( + val executorCreateIndexMapping = Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping)) - )( - equalTo(Created) - ) - }, - test("creating or updating request") { - assertZIO( + val executorUpsert = Executor.execute( ElasticRequest .upsert[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) .routing(Routing("routing")) .refreshTrue ) - )(isUnit) - }, - test("deleting by ID request") { - assertZIO( + val executorDeleteById = Executor.execute( ElasticRequest .deleteById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r")) .routing(Routing("routing")) .refreshTrue ) - )(equalTo(Deleted)) - }, - test("deleting by query request") { - assertZIO( + val executorDeleteByQuery = Executor.execute( ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.routing(Routing("routing")) ) - )( - equalTo(Deleted) - ) - }, - test("deleting index request") { - assertZIO(Executor.execute(ElasticRequest.deleteIndex(name = index)))( - equalTo(Deleted) - ) - }, - test("exists request") { - assertZIO( + val executorDeleteIndex = + Executor.execute(ElasticRequest.deleteIndex(name = index)) + val executorExists = Executor.execute( ElasticRequest .exists(index = index, id = DocumentId("example-id")) .routing(Routing("routing")) ) - )(isTrue) - }, - test("getting by ID request") { - assertZIO( + val executorGetById = Executor .execute( ElasticRequest @@ -188,34 +126,18 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) ) .documentAs[TestDocument] - )(isSome(equalTo(doc))) - }, - test("search request") { - assertZIO( + val executorSearch = Executor .execute(ElasticRequest.search(index = index, query = matchAll)) .documentAs[TestDocument] - )(equalTo(Chunk(doc))) - }, - test("search with aggregation request") { val terms = termsAggregation(name = "aggregation1", field = "name") - val req = Executor + val executorSearchWithTerms = 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)) - ) - ) - ) - ) - }, - test("update request with script") { - assertZIO( + .documentAs[TestDocument] + val executorSearchAggregations = Executor + .execute(ElasticRequest.search(index = index, query = matchAll, terms)) + .aggregations + val executorUpdateByScript = Executor.execute( ElasticRequest .updateByScript( @@ -227,10 +149,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - )(equalTo(UpdateOutcome.Updated)) - }, - test("update request with doc") { - assertZIO( + val executorUpdate = Executor.execute( ElasticRequest .update[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) @@ -238,10 +157,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - )(equalTo(UpdateOutcome.Updated)) - }, - test("update all by query request") { - assertZIO( + val executorUpdateAllByQuery = Executor.execute( ElasticRequest .updateAllByQuery(index = index, script = Script("ctx._source['intField']++")) @@ -249,10 +165,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - )(equalTo(UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2))) - }, - test("update by query request") { - assertZIO( + val executorUpdateByQuery = Executor.execute( ElasticRequest .updateByQuery( @@ -264,7 +177,145 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - )(equalTo(UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2))) + + val expectedTermsAggregationResult = + Map( + "aggregation1" -> TermsAggregationResult( + docErrorCount = 0, + sumOtherDocCount = 0, + buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty)) + ) + ) + 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 + ) + ) + ) + val expectedUpdateByQueryResult = + UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2) + + assertZIO( + executorAgregate + )( + equalTo( + expectedTermsAggregationResult + ) + ) && assertZIO( + executorBulk + )( + equalTo( + expectedBulkResponse + ) + ) && assertZIO( + executorCount + )( + equalTo( + 2 + ) + ) && assertZIO( + executorCreate + )( + equalTo( + DocumentId("V4x8q4UB3agN0z75fv5r") + ) + ) && assertZIO( + executorCreateDocumentId + )( + equalTo( + Created + ) + ) && assertZIO( + executorCreateIndex + )( + equalTo( + Created + ) + ) && assertZIO( + executorCreateIndexMapping + )( + equalTo( + Created + ) + ) && assertZIO( + executorUpsert + )( + isUnit + ) && assertZIO( + executorDeleteById + )( + equalTo( + Deleted + ) + ) && assertZIO( + executorDeleteByQuery + )( + equalTo( + Deleted + ) + ) && assertZIO( + executorDeleteIndex + )( + equalTo( + Deleted + ) + ) && assertZIO( + executorExists + )(isTrue) && assertZIO( + executorGetById + )( + isSome(equalTo(doc)) + ) && assertZIO( + executorSearch + )( + equalTo( + Chunk(doc) + ) + ) && assertZIO( + executorSearchWithTerms + )( + equalTo(Chunk(doc)) + ) && assertZIO( + executorSearchAggregations + )( + equalTo( + expectedTermsAggregationResult + ) + ) && assertZIO( + executorUpdateByScript + )( + equalTo( + UpdateOutcome.Updated + ) + ) && assertZIO( + executorUpdate + )( + equalTo( + UpdateOutcome.Updated + ) + ) && assertZIO( + executorUpdateAllByQuery + )( + equalTo( + expectedUpdateByQueryResult + ) + ) && assertZIO( + executorUpdateByQuery + )( + equalTo( + expectedUpdateByQueryResult + ) + ) } ).provideShared(elasticsearchSttpLayer) } From b65e57195a7cfff5050ec4bac99b83b72d17d8ce Mon Sep 17 00:00:00 2001 From: mihajlo-ra92 Date: Tue, 4 Jul 2023 20:48:25 +0200 Subject: [PATCH 2/5] Refactor upto search --- .../HttpElasticExecutorSpec.scala | 363 ++++++++++-------- 1 file changed, 200 insertions(+), 163 deletions(-) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index a0e3ec755..08ca618e8 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -34,8 +34,7 @@ import zio.test.{Spec, TestEnvironment, TestResultZIOOps, assertZIO} object HttpElasticExecutorSpec extends SttpBackendStubSpec { def spec: Spec[TestEnvironment, Any] = suite("HttpElasticExecutor")( - test("aggregation request") { - + test("aggregation") { val executorAgregate = Executor .execute( @@ -43,89 +42,6 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .aggregate(index, termsAggregation(name = "aggregation1", field = "name")) ) .aggregations - val executorBulk = - Executor - .execute( - ElasticRequest - .bulk(ElasticRequest.create(index, doc)) - .refreshTrue - ) - val executorCount = - Executor - .execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))) - val executorCreate = - Executor - .execute( - ElasticRequest - .create[TestDocument](index = index, doc = doc) - .routing(Routing("routing")) - .refreshTrue - ) - val executorCreateDocumentId = - Executor.execute( - ElasticRequest - .create[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) - .routing(Routing("routing")) - .refreshTrue - ) - val executorCreateIndex = - Executor.execute(ElasticRequest.createIndex(name = index)) - val mapping = - """ - |{ - | "settings": { - | "index": { - | "number_of_shards": 1 - | } - | }, - | "mappings": { - | "_routing": { - | "required": true - | }, - | "properties": { - | "id": { - | "type": "keyword" - | } - | } - | } - |} - |""".stripMargin - val executorCreateIndexMapping = - Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping)) - val executorUpsert = - Executor.execute( - ElasticRequest - .upsert[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) - .routing(Routing("routing")) - .refreshTrue - ) - val executorDeleteById = - Executor.execute( - ElasticRequest - .deleteById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r")) - .routing(Routing("routing")) - .refreshTrue - ) - val executorDeleteByQuery = - Executor.execute( - ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.routing(Routing("routing")) - ) - val executorDeleteIndex = - Executor.execute(ElasticRequest.deleteIndex(name = index)) - val executorExists = - Executor.execute( - ElasticRequest - .exists(index = index, id = DocumentId("example-id")) - .routing(Routing("routing")) - ) - val executorGetById = - Executor - .execute( - ElasticRequest - .getById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r")) - .routing(Routing("routing")) - ) - .documentAs[TestDocument] val executorSearch = Executor .execute(ElasticRequest.search(index = index, query = matchAll)) @@ -186,22 +102,6 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty)) ) ) - 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 - ) - ) - ) val expectedUpdateByQueryResult = UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2) @@ -212,110 +112,247 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { expectedTermsAggregationResult ) ) && assertZIO( - executorBulk + executorSearch )( equalTo( - expectedBulkResponse + Chunk(doc) ) ) && assertZIO( - executorCount + executorSearchWithTerms )( - equalTo( - 2 - ) + equalTo(Chunk(doc)) ) && assertZIO( - executorCreate + executorSearchAggregations )( equalTo( - DocumentId("V4x8q4UB3agN0z75fv5r") + expectedTermsAggregationResult ) ) && assertZIO( - executorCreateDocumentId + executorUpdateByScript )( equalTo( - Created + UpdateOutcome.Updated ) ) && assertZIO( - executorCreateIndex + executorUpdate )( equalTo( - Created + UpdateOutcome.Updated ) ) && assertZIO( - executorCreateIndexMapping + executorUpdateAllByQuery )( equalTo( - Created + expectedUpdateByQueryResult ) ) && assertZIO( - executorUpsert - )( - isUnit - ) && assertZIO( - executorDeleteById + executorUpdateByQuery )( equalTo( - Deleted + expectedUpdateByQueryResult ) - ) && assertZIO( - executorDeleteByQuery - )( - equalTo( - Deleted + ) + }, + + 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( - executorDeleteIndex - )( - equalTo( - Deleted + assertZIO( + executorBulk + )( + equalTo( + expectedBulkResponse + ) ) - ) && assertZIO( - executorExists - )(isTrue) && assertZIO( - executorGetById - )( - isSome(equalTo(doc)) - ) && assertZIO( - executorSearch - )( + }, + test("count"){ + val executorCount = + Executor + .execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))) + assertZIO ( + executorCount + )( equalTo( - Chunk(doc) + 2 ) - ) && assertZIO( - executorSearchWithTerms - )( - equalTo(Chunk(doc)) - ) && assertZIO( - executorSearchAggregations - )( + ) + }, + test("create"){ + val executorCreate = + Executor + .execute( + ElasticRequest + .create[TestDocument](index = index, doc = doc) + .routing(Routing("routing")) + .refreshTrue + ) + assertZIO ( + executorCreate + )( equalTo( - expectedTermsAggregationResult + DocumentId("V4x8q4UB3agN0z75fv5r") ) - ) && assertZIO( - executorUpdateByScript - )( + ) + }, + test("create with ID"){ + val executorCreateDocumentId = + Executor.execute( + ElasticRequest + .create[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) + .routing(Routing("routing")) + .refreshTrue + ) + assertZIO( + executorCreateDocumentId + )( + equalTo( + Created + ) + ) + }, + test("createIndex"){ + val executorCreateIndex = + Executor.execute(ElasticRequest.createIndex(name = index)) + val mapping = + """ + |{ + | "settings": { + | "index": { + | "number_of_shards": 1 + | } + | }, + | "mappings": { + | "_routing": { + | "required": true + | }, + | "properties": { + | "id": { + | "type": "keyword" + | } + | } + | } + |} + |""".stripMargin + val executorCreateIndexMapping = + Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping)) + assertZIO ( + executorCreateIndex + )( equalTo( - UpdateOutcome.Updated + Created ) ) && assertZIO( - executorUpdate + executorCreateIndexMapping )( equalTo( - UpdateOutcome.Updated + Created ) - ) && assertZIO( - executorUpdateAllByQuery - )( + ) + }, + test("deleteById"){ + val executorDeleteById = + Executor.execute( + ElasticRequest + .deleteById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r")) + .routing(Routing("routing")) + .refreshTrue + ) + assertZIO( + executorDeleteById + )( + equalTo( + Deleted + ) + ) + }, + test("deleteByQuery"){ + val executorDeleteByQuery = + Executor.execute( + ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.routing(Routing("routing")) + ) + assertZIO ( + executorDeleteByQuery + )( equalTo( - expectedUpdateByQueryResult + Deleted ) - ) && assertZIO( - executorUpdateByQuery - )( + ) + }, + test("deleteIndex"){ + val executorDeleteIndex = + Executor.execute(ElasticRequest.deleteIndex(name = index)) + assertZIO ( + executorDeleteIndex + )( equalTo( - expectedUpdateByQueryResult + Deleted ) ) + }, + test("exists"){ + val executorExists = + Executor.execute( + ElasticRequest + .exists(index = index, id = DocumentId("example-id")) + .routing(Routing("routing")) + ) + assertZIO ( + executorExists + )( + isTrue + ) + }, + test("getById"){ + val executorGetById = + Executor + .execute( + ElasticRequest + .getById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r")) + .routing(Routing("routing")) + ) + .documentAs[TestDocument] + assertZIO ( + executorGetById + )( + isSome( + equalTo(doc) + ) + ) + }, + 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) } From 434bbd8e0cdb2cc8d90cbf6d311d574de71b2477 Mon Sep 17 00:00:00 2001 From: mihajlo-ra92 Date: Tue, 4 Jul 2023 21:07:10 +0200 Subject: [PATCH 3/5] Finish refactor httpElasticExecutorSpec --- .../HttpElasticExecutorSpec.scala | 300 ++++++++++-------- 1 file changed, 164 insertions(+), 136 deletions(-) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 08ca618e8..db82a191c 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -42,57 +42,6 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .aggregate(index, termsAggregation(name = "aggregation1", field = "name")) ) .aggregations - val executorSearch = - Executor - .execute(ElasticRequest.search(index = index, query = matchAll)) - .documentAs[TestDocument] - val terms = termsAggregation(name = "aggregation1", field = "name") - val executorSearchWithTerms = Executor - .execute(ElasticRequest.search(index = index, query = matchAll, terms)) - .documentAs[TestDocument] - val executorSearchAggregations = Executor - .execute(ElasticRequest.search(index = index, query = matchAll, terms)) - .aggregations - 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 - ) - val executorUpdate = - Executor.execute( - ElasticRequest - .update[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) - .orCreate(doc = secondDoc) - .routing(Routing("routing")) - .refreshTrue - ) - val executorUpdateAllByQuery = - Executor.execute( - ElasticRequest - .updateAllByQuery(index = index, script = Script("ctx._source['intField']++")) - .conflicts(Proceed) - .routing(Routing("routing")) - .refreshTrue - ) - val executorUpdateByQuery = - Executor.execute( - ElasticRequest - .updateByQuery( - index = index, - query = term(field = TestDocument.stringField.keyword, value = "StringField"), - script = Script("ctx._source['intField']++") - ) - .conflicts(Proceed) - .routing(Routing("routing")) - .refreshTrue - ) val expectedTermsAggregationResult = Map( @@ -102,8 +51,6 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty)) ) ) - val expectedUpdateByQueryResult = - UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2) assertZIO( executorAgregate @@ -111,50 +58,9 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { equalTo( expectedTermsAggregationResult ) - ) && assertZIO( - executorSearch - )( - equalTo( - Chunk(doc) - ) - ) && assertZIO( - executorSearchWithTerms - )( - equalTo(Chunk(doc)) - ) && assertZIO( - executorSearchAggregations - )( - equalTo( - expectedTermsAggregationResult - ) - ) && assertZIO( - executorUpdateByScript - )( - equalTo( - UpdateOutcome.Updated - ) - ) && assertZIO( - executorUpdate - )( - equalTo( - UpdateOutcome.Updated - ) - ) && assertZIO( - executorUpdateAllByQuery - )( - equalTo( - expectedUpdateByQueryResult - ) - ) && assertZIO( - executorUpdateByQuery - )( - equalTo( - expectedUpdateByQueryResult - ) ) }, - - test("bulk"){ + test("bulk") { val executorBulk = Executor .execute( @@ -179,26 +85,26 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { ) ) assertZIO( - executorBulk - )( - equalTo( - expectedBulkResponse - ) + executorBulk + )( + equalTo( + expectedBulkResponse ) + ) }, - test("count"){ + test("count") { val executorCount = Executor .execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))) - assertZIO ( + assertZIO( executorCount - )( + )( equalTo( 2 ) ) }, - test("create"){ + test("create") { val executorCreate = Executor .execute( @@ -207,15 +113,15 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO ( + assertZIO( executorCreate - )( + )( equalTo( DocumentId("V4x8q4UB3agN0z75fv5r") ) ) }, - test("create with ID"){ + test("create with ID") { val executorCreateDocumentId = Executor.execute( ElasticRequest @@ -224,14 +130,14 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .refreshTrue ) assertZIO( - executorCreateDocumentId - )( - equalTo( - Created - ) + executorCreateDocumentId + )( + equalTo( + Created ) + ) }, - test("createIndex"){ + test("createIndex") { val executorCreateIndex = Executor.execute(ElasticRequest.createIndex(name = index)) val mapping = @@ -256,9 +162,9 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { |""".stripMargin val executorCreateIndexMapping = Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping)) - assertZIO ( + assertZIO( executorCreateIndex - )( + )( equalTo( Created ) @@ -270,7 +176,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { ) ) }, - test("deleteById"){ + test("deleteById") { val executorDeleteById = Executor.execute( ElasticRequest @@ -279,51 +185,51 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .refreshTrue ) assertZIO( - executorDeleteById - )( - equalTo( - Deleted - ) + executorDeleteById + )( + equalTo( + Deleted ) + ) }, - test("deleteByQuery"){ + test("deleteByQuery") { val executorDeleteByQuery = Executor.execute( ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.routing(Routing("routing")) ) - assertZIO ( + assertZIO( executorDeleteByQuery - )( + )( equalTo( Deleted ) ) }, - test("deleteIndex"){ + test("deleteIndex") { val executorDeleteIndex = Executor.execute(ElasticRequest.deleteIndex(name = index)) - assertZIO ( + assertZIO( executorDeleteIndex - )( + )( equalTo( Deleted ) ) }, - test("exists"){ + test("exists") { val executorExists = Executor.execute( ElasticRequest .exists(index = index, id = DocumentId("example-id")) .routing(Routing("routing")) ) - assertZIO ( + assertZIO( executorExists - )( + )( isTrue ) }, - test("getById"){ + test("getById") { val executorGetById = Executor .execute( @@ -332,15 +238,137 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) ) .documentAs[TestDocument] - assertZIO ( + assertZIO( executorGetById - )( + )( isSome( equalTo(doc) ) ) }, - test("upsert"){ + test("search") { + val executorSearch = + Executor + .execute(ElasticRequest.search(index = index, query = matchAll)) + .documentAs[TestDocument] + 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 and aggergate") { + val terms = termsAggregation(name = "aggregation1", field = "name") + val executorSearchAggregations = Executor + .execute(ElasticRequest.search(index = index, query = matchAll, terms)) + .aggregations + val expectedTermsAggregationResult = + Map( + "aggregation1" -> TermsAggregationResult( + docErrorCount = 0, + sumOtherDocCount = 0, + buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty)) + ) + ) + assertZIO( + executorSearchAggregations + )( + equalTo( + expectedTermsAggregationResult + ) + ) + }, + test("update") { + val executorUpdate = + Executor.execute( + ElasticRequest + .update[TestDocument](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = doc) + .orCreate(doc = secondDoc) + .routing(Routing("routing")) + .refreshTrue + ) + assertZIO( + executorUpdate + )( + equalTo( + UpdateOutcome.Updated + ) + ) + }, + test("updateAllByQuery") { + val executorUpdateAllByQuery = + Executor.execute( + ElasticRequest + .updateAllByQuery(index = index, script = Script("ctx._source['intField']++")) + .conflicts(Proceed) + .routing(Routing("routing")) + .refreshTrue + ) + val expectedUpdateByQueryResult = + UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2) + assertZIO( + executorUpdateAllByQuery + )( + equalTo( + expectedUpdateByQueryResult + ) + ) + }, + test("updateByQuery") { + val executorUpdateByQuery = + Executor.execute( + ElasticRequest + .updateByQuery( + index = index, + query = term(field = TestDocument.stringField.keyword, value = "StringField"), + script = Script("ctx._source['intField']++") + ) + .conflicts(Proceed) + .routing(Routing("routing")) + .refreshTrue + ) + 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 @@ -348,9 +376,9 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO ( + assertZIO( executorUpsert - )( + )( isUnit ) } From 3e1dc8660f0004adf6745eebbb1fcad92133dc4b Mon Sep 17 00:00:00 2001 From: mihajlo-ra92 Date: Wed, 5 Jul 2023 13:54:57 +0200 Subject: [PATCH 4/5] Remove whitespace --- .../HttpElasticExecutorSpec.scala | 151 +++--------------- 1 file changed, 20 insertions(+), 131 deletions(-) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index db82a191c..72187f219 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -52,13 +52,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { ) ) - assertZIO( - executorAgregate - )( - equalTo( - expectedTermsAggregationResult - ) - ) + assertZIO(executorAgregate)(equalTo(expectedTermsAggregationResult)) }, test("bulk") { val executorBulk = @@ -84,25 +78,15 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { ) ) ) - assertZIO( - executorBulk - )( - equalTo( - expectedBulkResponse - ) + assertZIO(executorBulk)( + equalTo(expectedBulkResponse) ) }, test("count") { val executorCount = Executor .execute(ElasticRequest.count(index, matchAll).routing(Routing("routing"))) - assertZIO( - executorCount - )( - equalTo( - 2 - ) - ) + assertZIO(executorCount)(equalTo(2)) }, test("create") { val executorCreate = @@ -113,13 +97,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO( - executorCreate - )( - equalTo( - DocumentId("V4x8q4UB3agN0z75fv5r") - ) - ) + assertZIO(executorCreate)(equalTo(DocumentId("V4x8q4UB3agN0z75fv5r"))) }, test("create with ID") { val executorCreateDocumentId = @@ -129,13 +107,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO( - executorCreateDocumentId - )( - equalTo( - Created - ) - ) + assertZIO(executorCreateDocumentId)(equalTo(Created)) }, test("createIndex") { val executorCreateIndex = @@ -162,19 +134,8 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { |""".stripMargin val executorCreateIndexMapping = Executor.execute(ElasticRequest.createIndex(name = index, definition = mapping)) - assertZIO( - executorCreateIndex - )( - equalTo( - Created - ) - ) && assertZIO( - executorCreateIndexMapping - )( - equalTo( - Created - ) - ) + assertZIO(executorCreateIndex)(equalTo(Created)) && + assertZIO(executorCreateIndexMapping)(equalTo(Created)) }, test("deleteById") { val executorDeleteById = @@ -184,37 +145,19 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO( - executorDeleteById - )( - equalTo( - Deleted - ) - ) + assertZIO(executorDeleteById)(equalTo(Deleted)) }, test("deleteByQuery") { val executorDeleteByQuery = Executor.execute( ElasticRequest.deleteByQuery(index = index, query = matchAll).refreshTrue.routing(Routing("routing")) ) - assertZIO( - executorDeleteByQuery - )( - equalTo( - Deleted - ) - ) + assertZIO(executorDeleteByQuery)(equalTo(Deleted)) }, test("deleteIndex") { val executorDeleteIndex = Executor.execute(ElasticRequest.deleteIndex(name = index)) - assertZIO( - executorDeleteIndex - )( - equalTo( - Deleted - ) - ) + assertZIO(executorDeleteIndex)(equalTo(Deleted)) }, test("exists") { val executorExists = @@ -223,11 +166,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .exists(index = index, id = DocumentId("example-id")) .routing(Routing("routing")) ) - assertZIO( - executorExists - )( - isTrue - ) + assertZIO(executorExists)(isTrue) }, test("getById") { val executorGetById = @@ -238,13 +177,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) ) .documentAs[TestDocument] - assertZIO( - executorGetById - )( - isSome( - equalTo(doc) - ) - ) + assertZIO(executorGetById)(isSome(equalTo(doc))) }, test("search") { val executorSearch = @@ -255,17 +188,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { val executorSearchWithTerms = Executor .execute(ElasticRequest.search(index = index, query = matchAll, terms)) .documentAs[TestDocument] - assertZIO( - executorSearch - )( - equalTo( - Chunk(doc) - ) - ) && assertZIO( - executorSearchWithTerms - )( - equalTo(Chunk(doc)) - ) + assertZIO(executorSearch)(equalTo(Chunk(doc))) && assertZIO(executorSearchWithTerms)(equalTo(Chunk(doc))) }, test("search and aggergate") { val terms = termsAggregation(name = "aggregation1", field = "name") @@ -280,13 +203,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { buckets = Chunk(TermsAggregationBucketResult(docCount = 5, key = "name", subAggregations = Map.empty)) ) ) - assertZIO( - executorSearchAggregations - )( - equalTo( - expectedTermsAggregationResult - ) - ) + assertZIO(executorSearchAggregations)(equalTo(expectedTermsAggregationResult)) }, test("update") { val executorUpdate = @@ -297,13 +214,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO( - executorUpdate - )( - equalTo( - UpdateOutcome.Updated - ) - ) + assertZIO(executorUpdate)(equalTo(UpdateOutcome.Updated)) }, test("updateAllByQuery") { val executorUpdateAllByQuery = @@ -316,13 +227,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { ) val expectedUpdateByQueryResult = UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2) - assertZIO( - executorUpdateAllByQuery - )( - equalTo( - expectedUpdateByQueryResult - ) - ) + assertZIO(executorUpdateAllByQuery)(equalTo(expectedUpdateByQueryResult)) }, test("updateByQuery") { val executorUpdateByQuery = @@ -339,13 +244,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { ) val expectedUpdateByQueryResult = UpdateByQueryResult(took = 1, total = 10, updated = 8, deleted = 0, versionConflicts = 2) - assertZIO( - executorUpdateByQuery - )( - equalTo( - expectedUpdateByQueryResult - ) - ) + assertZIO(executorUpdateByQuery)(equalTo(expectedUpdateByQueryResult)) }, test("updateByScript") { val executorUpdateByScript = @@ -360,13 +259,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO( - executorUpdateByScript - )( - equalTo( - UpdateOutcome.Updated - ) - ) + assertZIO(executorUpdateByScript)(equalTo(UpdateOutcome.Updated)) }, test("upsert") { val executorUpsert = @@ -376,11 +269,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .routing(Routing("routing")) .refreshTrue ) - assertZIO( - executorUpsert - )( - isUnit - ) + assertZIO(executorUpsert)(isUnit) } ).provideShared(elasticsearchSttpLayer) } From e0006be6d101fceb84627b1bff722c0000ca4655 Mon Sep 17 00:00:00 2001 From: mihajlo-ra92 Date: Thu, 6 Jul 2023 12:15:16 +0200 Subject: [PATCH 5/5] Cleanup executorAgregate --- .../scala/zio/elasticsearch/HttpElasticExecutorSpec.scala | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 72187f219..d384abc9f 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -37,10 +37,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { test("aggregation") { val executorAgregate = Executor - .execute( - ElasticRequest - .aggregate(index, termsAggregation(name = "aggregation1", field = "name")) - ) + .execute(ElasticRequest.aggregate(index, termsAggregation(name = "aggregation1", field = "name"))) .aggregations val expectedTermsAggregationResult =