diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 5bc36d604..86a25ed22 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -31,12 +31,10 @@ object HttpExecutorSpec extends IntegrationSpec { suite("creating document")( test("successfully create document") { checkOnce(genCustomer) { customer => - val result = for { + for { docId <- ElasticRequest.create[CustomerDocument](index, customer).execute res <- ElasticRequest.getById[CustomerDocument](index, docId).execute - } yield res - - assertZIO(result)(isSome(equalTo(customer))) + } yield assert(res)(isSome(equalTo(customer))) } }, test("successfully create document with ID given") { @@ -46,12 +44,10 @@ object HttpExecutorSpec extends IntegrationSpec { }, test("return 'AlreadyExists' if document with given ID already exists") { checkOnce(genDocumentId, genCustomer, genCustomer) { (documentId, customer1, customer2) => - val result = for { + for { _ <- ElasticRequest.upsert[CustomerDocument](index, documentId, customer1).execute res <- ElasticRequest.create[CustomerDocument](index, documentId, customer2).execute - } yield res - - assertZIO(result)(equalTo(AlreadyExists)) + } yield assert(res)(equalTo(AlreadyExists)) } } ), @@ -60,46 +56,38 @@ object HttpExecutorSpec extends IntegrationSpec { assertZIO(ElasticRequest.createIndex(createIndexTestName, None).execute)(equalTo(Created)) }, test("return 'AlreadyExists' if index already exists") { - val result = for { + for { _ <- ElasticRequest.createIndex(createIndexTestName, None).execute res <- ElasticRequest.createIndex(createIndexTestName, None).execute - } yield res - - assertZIO(result)(equalTo(AlreadyExists)) + } yield assert(res)(equalTo(AlreadyExists)) } ) @@ after(ElasticRequest.deleteIndex(createIndexTestName).execute.orDie), suite("creating or updating document")( test("successfully create document") { checkOnce(genDocumentId, genCustomer) { (documentId, customer) => - val result = for { + for { _ <- ElasticRequest.upsert[CustomerDocument](index, documentId, customer).execute doc <- ElasticRequest.getById[CustomerDocument](index, documentId).execute - } yield doc - - assertZIO(result)(isSome(equalTo(customer))) + } yield assert(doc)(isSome(equalTo(customer))) } }, test("successfully update document") { checkOnce(genDocumentId, genCustomer, genCustomer) { (documentId, firstCustomer, secondCustomer) => - val result = for { + for { _ <- ElasticRequest.create[CustomerDocument](index, documentId, firstCustomer).execute _ <- ElasticRequest.upsert[CustomerDocument](index, documentId, secondCustomer).execute doc <- ElasticRequest.getById[CustomerDocument](index, documentId).execute - } yield doc - - assertZIO(result)(isSome(equalTo(secondCustomer))) + } yield assert(doc)(isSome(equalTo(secondCustomer))) } } ), suite("deleting document by ID")( test("successfully delete existing document") { checkOnce(genDocumentId, genCustomer) { (documentId, customer) => - val result = for { + for { _ <- ElasticRequest.upsert[CustomerDocument](index, documentId, customer).execute res <- ElasticRequest.deleteById(index, documentId).execute - } yield res - - assertZIO(result)(equalTo(Deleted)) + } yield assert(res)(equalTo(Deleted)) } }, test("return 'NotFound' if the document does not exist") { @@ -111,12 +99,10 @@ object HttpExecutorSpec extends IntegrationSpec { suite("delete index")( test("successfully delete existing index") { checkOnce(genIndexName) { name => - val result = for { + for { _ <- ElasticRequest.createIndex(name, None).execute res <- ElasticRequest.deleteIndex(name).execute - } yield res - - assertZIO(result)(equalTo(Deleted)) + } yield assert(res)(equalTo(Deleted)) } }, test("return 'NotFound' if index does not exists") { @@ -128,12 +114,10 @@ object HttpExecutorSpec extends IntegrationSpec { suite("finding document")( test("return true if the document exists") { checkOnce(genDocumentId, genCustomer) { (documentId, customer) => - val result = for { + for { _ <- ElasticRequest.upsert[CustomerDocument](index, documentId, customer).execute res <- ElasticRequest.exists(index, documentId).execute - } yield res - - assertZIO(result)(isTrue) + } yield assert(res)(isTrue) } }, test("return false if the document does not exist") { @@ -145,12 +129,10 @@ object HttpExecutorSpec extends IntegrationSpec { suite("retrieving document by ID")( test("successfully return document") { checkOnce(genDocumentId, genCustomer) { (documentId, customer) => - val result = for { + for { _ <- ElasticRequest.upsert[CustomerDocument](index, documentId, customer).execute res <- ElasticRequest.getById[CustomerDocument](index, documentId).execute - } yield res - - assertZIO(result)(isSome(equalTo(customer))) + } yield assert(res)(isSome(equalTo(customer))) } }, test("return None if the document does not exist") { @@ -175,23 +157,21 @@ object HttpExecutorSpec extends IntegrationSpec { test("search for document using range query") { checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) { (firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) => - val result = - for { - _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute - _ <- - ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute - _ <- - ElasticRequest - .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) - .refreshTrue - .execute - query = range("balance").gte(100) - res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute - } yield res - - assertZIO(result)(isNonEmpty) + for { + _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute + _ <- + ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute + _ <- + ElasticRequest + .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) + .refreshTrue + .execute + query = range("balance").gte(100) + res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute + } yield assert(res)(isNonEmpty) } - } @@ before(ElasticRequest.createIndex(firstSearchIndex, None).execute) @@ after( + } @@ around( + ElasticRequest.createIndex(firstSearchIndex, None).execute, ElasticRequest.deleteIndex(firstSearchIndex).execute.orDie ), test("fail if any of results cannot be decoded") { @@ -218,74 +198,69 @@ object HttpExecutorSpec extends IntegrationSpec { ) ) } - } @@ before(ElasticRequest.createIndex(secondSearchIndex, None).execute) @@ after( + } @@ around( + ElasticRequest.createIndex(secondSearchIndex, None).execute, ElasticRequest.deleteIndex(secondSearchIndex).execute.orDie ), test("search for a document which contains a term using a wildcard query") { checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) { (firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) => - val result = - for { - _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute - _ <- - ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute - _ <- - ElasticRequest - .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) - .refreshTrue - .execute - query = ElasticQuery.contains("name.keyword", firstCustomer.name.take(3)) - res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute - } yield res - - assertZIO(result)(Assertion.contains(firstCustomer)) + for { + _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute + _ <- + ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute + _ <- + ElasticRequest + .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) + .refreshTrue + .execute + query = ElasticQuery.contains("name.keyword", firstCustomer.name.take(3)) + res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute + } yield assert(res)(Assertion.contains(firstCustomer)) } - } @@ before(ElasticRequest.createIndex(firstSearchIndex, None).execute) @@ after( + } @@ around( + ElasticRequest.createIndex(firstSearchIndex, None).execute, ElasticRequest.deleteIndex(firstSearchIndex).execute.orDie ), test("search for a document which starts with a term using a wildcard query") { checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) { (firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) => - val result = - for { - _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute - _ <- - ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute - _ <- - ElasticRequest - .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) - .refreshTrue - .execute - query = ElasticQuery.startsWith("name.keyword", firstCustomer.name.take(3)) - res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute - } yield res - - assertZIO(result)(Assertion.contains(firstCustomer)) + for { + _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute + _ <- + ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute + _ <- + ElasticRequest + .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) + .refreshTrue + .execute + query = ElasticQuery.startsWith("name.keyword", firstCustomer.name.take(3)) + res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute + } yield assert(res)(Assertion.contains(firstCustomer)) } - } @@ before(ElasticRequest.createIndex(firstSearchIndex, None).execute) @@ after( + } @@ around( + ElasticRequest.createIndex(firstSearchIndex, None).execute, ElasticRequest.deleteIndex(firstSearchIndex).execute.orDie ), test("search for a document which conforms to a pattern using a wildcard query") { checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) { (firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) => - val result = - for { - _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute - _ <- - ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute - _ <- - ElasticRequest - .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) - .refreshTrue - .execute - query = - wildcard("name.keyword", s"${firstCustomer.name.take(2)}*${firstCustomer.name.takeRight(2)}") - res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute - } yield res - - assertZIO(result)(Assertion.contains(firstCustomer)) + for { + _ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute + _ <- + ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute + _ <- + ElasticRequest + .upsert[CustomerDocument](firstSearchIndex, secondDocumentId, secondCustomer) + .refreshTrue + .execute + query = + wildcard("name.keyword", s"${firstCustomer.name.take(2)}*${firstCustomer.name.takeRight(2)}") + res <- ElasticRequest.search[CustomerDocument](firstSearchIndex, query).execute + } yield assert(res)(Assertion.contains(firstCustomer)) } - } @@ before(ElasticRequest.createIndex(firstSearchIndex, None).execute) @@ after( + } @@ around( + ElasticRequest.createIndex(firstSearchIndex, None).execute, ElasticRequest.deleteIndex(firstSearchIndex).execute.orDie ) ) @@ shrinks(0), @@ -293,40 +268,38 @@ object HttpExecutorSpec extends IntegrationSpec { test("successfully delete all matched documents") { checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer, genDocumentId, genCustomer) { (firstDocumentId, firstCustomer, secondDocumentId, secondCustomer, thirdDocumentId, thirdCustomer) => - val result = - for { - _ <- ElasticRequest - .upsert[CustomerDocument]( - deleteByQueryIndex, - firstDocumentId, - firstCustomer.copy(balance = 150) - ) - .execute - _ <- - ElasticRequest - .upsert[CustomerDocument]( - deleteByQueryIndex, - secondDocumentId, - secondCustomer.copy(balance = 350) - ) - .execute - _ <- - ElasticRequest - .upsert[CustomerDocument]( - deleteByQueryIndex, - thirdDocumentId, - thirdCustomer.copy(balance = 400) - ) - .refreshTrue - .execute - deleteQuery = range("balance").gte(300) - _ <- ElasticRequest.deleteByQuery(deleteByQueryIndex, deleteQuery).refreshTrue.execute - res <- ElasticRequest.search[CustomerDocument](deleteByQueryIndex, matchAll()).execute - } yield res - - assertZIO(result)(hasSameElements(List(firstCustomer.copy(balance = 150)))) + for { + _ <- ElasticRequest + .upsert[CustomerDocument]( + deleteByQueryIndex, + firstDocumentId, + firstCustomer.copy(balance = 150) + ) + .execute + _ <- + ElasticRequest + .upsert[CustomerDocument]( + deleteByQueryIndex, + secondDocumentId, + secondCustomer.copy(balance = 350) + ) + .execute + _ <- + ElasticRequest + .upsert[CustomerDocument]( + deleteByQueryIndex, + thirdDocumentId, + thirdCustomer.copy(balance = 400) + ) + .refreshTrue + .execute + deleteQuery = range("balance").gte(300) + _ <- ElasticRequest.deleteByQuery(deleteByQueryIndex, deleteQuery).refreshTrue.execute + res <- ElasticRequest.search[CustomerDocument](deleteByQueryIndex, matchAll()).execute + } yield assert(res)(hasSameElements(List(firstCustomer.copy(balance = 150)))) } - } @@ before(ElasticRequest.createIndex(deleteByQueryIndex, None).execute) @@ after( + } @@ around( + ElasticRequest.createIndex(deleteByQueryIndex, None).execute, ElasticRequest.deleteIndex(deleteByQueryIndex).execute.orDie ), test("returns NotFound when provided index is missing") { @@ -339,23 +312,20 @@ object HttpExecutorSpec extends IntegrationSpec { test("successfully execute bulk query") { checkOnce(genDocumentId, genDocumentId, genDocumentId, genCustomer) { (firstDocId, secondDocId, thirdDocId, customer) => - val result = - for { - _ <- ElasticRequest - .create[CustomerDocument](index, firstDocId, customer.copy(id = "randomIdString")) - .execute - _ <- ElasticRequest - .create[CustomerDocument](index, secondDocId, customer.copy(id = "randomIdString2")) - .refreshTrue - .execute - req1 = ElasticRequest.create[CustomerDocument](index, thirdDocId, customer) - req2 = ElasticRequest.create[CustomerDocument](index, customer.copy(id = "randomIdString3")) - req3 = ElasticRequest.upsert[CustomerDocument](index, firstDocId, customer.copy(balance = 3000)) - req4 = ElasticRequest.deleteById(index, secondDocId) - res <- ElasticRequest.bulk(req1, req2, req3, req4).execute - } yield res - - assertZIO(result)(isUnit) + for { + _ <- ElasticRequest + .create[CustomerDocument](index, firstDocId, customer.copy(id = "randomIdString")) + .execute + _ <- ElasticRequest + .create[CustomerDocument](index, secondDocId, customer.copy(id = "randomIdString2")) + .refreshTrue + .execute + req1 = ElasticRequest.create[CustomerDocument](index, thirdDocId, customer) + req2 = ElasticRequest.create[CustomerDocument](index, customer.copy(id = "randomIdString3")) + req3 = ElasticRequest.upsert[CustomerDocument](index, firstDocId, customer.copy(balance = 3000)) + req4 = ElasticRequest.deleteById(index, secondDocId) + res <- ElasticRequest.bulk(req1, req2, req3, req4).execute + } yield assert(res)(isUnit) } } )