-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(test): Setup test environment for executor (#20)
- Loading branch information
Showing
7 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: '3.8' | ||
|
||
services: | ||
elasticsearch: | ||
image: elasticsearch:7.17.6 | ||
container_name: zio-elasticsearch-test | ||
ports: | ||
- "9200:9200" | ||
environment: | ||
discovery.type: "single-node" | ||
xpack.security.enabled: "false" | ||
ES_JAVA_OPTS: "-Xms512m -Xmx512m" |
42 changes: 42 additions & 0 deletions
42
modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package zio.elasticsearch | ||
|
||
import zio.elasticsearch.ElasticError.DocumentRetrievingError.{DecoderError, DocumentNotFound} | ||
import zio.test.Assertion.equalTo | ||
import zio.test.TestAspect.nondeterministic | ||
import zio.test._ | ||
|
||
object HttpExecutorSpec extends IntegrationSpec { | ||
|
||
override def spec: Spec[TestEnvironment, Any] = | ||
suite("HTTP Executor")( | ||
suite("retrieving document by ID")( | ||
test("successfully return document") { | ||
checkOnce(genDocumentId, genCustomer) { (documentId, customer) => | ||
val result = for { | ||
_ <- ElasticRequest.upsert[CustomerDocument](index, documentId, customer).execute | ||
document <- ElasticRequest.getById[CustomerDocument](index, documentId).execute | ||
} yield document | ||
|
||
assertZIO(result)(Assertion.isRight(equalTo(customer))) | ||
} | ||
}, | ||
test("return DocumentNotFound if the document does not exist") { | ||
checkOnce(genDocumentId) { documentId => | ||
assertZIO(ElasticRequest.getById[CustomerDocument](index, documentId).execute)( | ||
Assertion.isLeft(equalTo(DocumentNotFound)) | ||
) | ||
} | ||
}, | ||
test("fail with decoding error") { | ||
checkOnce(genDocumentId, genEmployee) { (documentId, employee) => | ||
val result = for { | ||
_ <- ElasticRequest.upsert[EmployeeDocument](index, documentId, employee).execute | ||
document <- ElasticRequest.getById[CustomerDocument](index, documentId).execute | ||
} yield document | ||
|
||
assertZIO(result)(Assertion.isLeft(equalTo(DecoderError(".address(missing)")))) | ||
} | ||
} | ||
) @@ nondeterministic | ||
).provideShared(elasticsearchLayer) | ||
} |
30 changes: 30 additions & 0 deletions
30
modules/library/src/it/scala/zio/elasticsearch/IntegrationSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package zio.elasticsearch | ||
|
||
import sttp.client3.httpclient.zio.HttpClientZioBackend | ||
import zio.ZLayer | ||
import zio.test.CheckVariants.CheckN | ||
import zio.test.{Gen, ZIOSpecDefault, checkN} | ||
|
||
trait IntegrationSpec extends ZIOSpecDefault { | ||
val elasticsearchLayer: ZLayer[Any, Throwable, ElasticExecutor] = | ||
HttpClientZioBackend.layer() >>> ElasticExecutor.local | ||
|
||
val index: IndexName = IndexName("users") | ||
|
||
def genDocumentId: Gen[Any, DocumentId] = Gen.stringBounded(10, 40)(Gen.alphaNumericChar).map(DocumentId(_)) | ||
|
||
def genCustomer: Gen[Any, CustomerDocument] = for { | ||
id <- Gen.stringBounded(5, 10)(Gen.alphaNumericChar) | ||
name <- Gen.stringBounded(5, 10)(Gen.alphaChar) | ||
address <- Gen.stringBounded(5, 10)(Gen.alphaNumericChar) | ||
balance <- Gen.bigDecimal(100, 10000) | ||
} yield CustomerDocument(id = id, name = name, address = address, balance = balance) | ||
|
||
def genEmployee: Gen[Any, EmployeeDocument] = for { | ||
id <- Gen.stringBounded(5, 10)(Gen.alphaNumericChar) | ||
name <- Gen.stringBounded(5, 10)(Gen.alphaChar) | ||
degree <- Gen.stringBounded(5, 10)(Gen.alphaChar) | ||
} yield EmployeeDocument(id = id, name = name, degree = degree) | ||
|
||
def checkOnce: CheckN = checkN(1) | ||
} |
15 changes: 15 additions & 0 deletions
15
modules/library/src/it/scala/zio/elasticsearch/UserDocument.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package zio.elasticsearch | ||
|
||
import zio.schema.{DeriveSchema, Schema} | ||
|
||
final case class CustomerDocument(id: String, name: String, address: String, balance: BigDecimal) | ||
|
||
final case class EmployeeDocument(id: String, name: String, degree: String) | ||
|
||
object CustomerDocument { | ||
implicit val schema: Schema[CustomerDocument] = DeriveSchema.gen[CustomerDocument] | ||
} | ||
|
||
object EmployeeDocument { | ||
implicit val schema: Schema[EmployeeDocument] = DeriveSchema.gen[EmployeeDocument] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters