Skip to content

Commit

Permalink
(test): Setup test environment for executor (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 authored Dec 7, 2022
1 parent 28bf574 commit fbbb5ed
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ jobs:
uses: coursier/cache-action@v6
- name: Run tests
run: ./sbt ++${{ matrix.scala }}! test
- name: Run test container
run: docker-compose -f docker-compose.yml up -d
- name: Run integration tests
run: ./sbt ++${{ matrix.scala }}! it:test

website:
runs-on: ubuntu-20.04
Expand Down
9 changes: 7 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ lazy val library =
.in(file("modules/library"))
.settings(stdSettings("zio-elasticsearch"))
.settings(scalacOptions += "-language:higherKinds")
.configs(IntegrationTest)
.settings(
Defaults.itSettings,
libraryDependencies ++= List(
"com.softwaremill.sttp.client3" %% "zio" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.3",
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-prelude" % "1.0.0-RC16",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1",
"org.apache.commons" % "commons-lang3" % "3.12.0"
)
"org.apache.commons" % "commons-lang3" % "3.12.0",
"dev.zio" %% "zio-test" % "2.0.4" % IntegrationTest,
"dev.zio" %% "zio-test-sbt" % "2.0.4" % IntegrationTest
),
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework"))
)

lazy val example =
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
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"
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)
}
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 modules/library/src/it/scala/zio/elasticsearch/UserDocument.scala
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]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package zio

import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.StringUtils._
import zio.prelude.Assertion._
import zio.prelude.Assertion.isEmptyString
import zio.prelude.AssertionError.failure
import zio.prelude.Newtype

Expand All @@ -19,7 +20,7 @@ package object elasticsearch {
if (
name.toLowerCase != name ||
startsWithAny(name, "+", "-", "_") ||
containsAny(name, '\\', '/', '*', '?', '"', '/', '<', '>', '|', ' ', ',', '#', ':') ||
containsAny(name, List("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) ||
equalsAny(name, ".", "..") ||
name.getBytes().length > 255
)
Expand All @@ -42,4 +43,7 @@ package object elasticsearch {
}
type IndexName = IndexName.Type

def containsAny(name: String, params: List[String]): Boolean =
params.exists(StringUtils.contains(name, _))

}

0 comments on commit fbbb5ed

Please sign in to comment.