Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(test): Setup test environment for executor #20

Merged
merged 29 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
06e7912
Set up integration test configuration in build.sbt
mvelimir Dec 2, 2022
44535e9
Set up a Docker test container
mvelimir Dec 2, 2022
1968fbc
Add LiveSpec
mvelimir Dec 2, 2022
c337e96
Add integration tests to CI workflow
mvelimir Dec 2, 2022
50b7432
Add tests for getting by id
dbulaja98 Dec 2, 2022
9965bf1
Set up integration test configuration in build.sbt
mvelimir Dec 2, 2022
8f32f0e
Set up a Docker test container
mvelimir Dec 2, 2022
51b8d85
Add LiveSpec
mvelimir Dec 2, 2022
78e8ddc
Add integration tests to CI workflow
mvelimir Dec 2, 2022
8af319c
Add tests for getting by id
dbulaja98 Dec 2, 2022
42469c9
Generate document ID for every test
mvelimir Dec 2, 2022
408858f
Correctly specify location in docker compose
mvelimir Dec 2, 2022
e88f1d4
Merge changes
mvelimir Dec 2, 2022
f92913c
Transfer docker-compose.yml to root
dbulaja98 Dec 2, 2022
e071286
Fix code remarks
dbulaja98 Dec 5, 2022
e4c032d
Fix ci.yml
dbulaja98 Dec 5, 2022
10de585
Fix IndexName validation
dbulaja98 Dec 5, 2022
89605eb
Fix IndexName validation
dbulaja98 Dec 5, 2022
cbcf979
Fix IndexName validation
dbulaja98 Dec 5, 2022
044ec5b
Revert changes on IndexName validation
dbulaja98 Dec 5, 2022
110a38b
Add environment to es in docker-compose.yml
dbulaja98 Dec 5, 2022
4e62eb6
Fix code remarks
dbulaja98 Dec 5, 2022
ebe10bc
Fix IndexName validation
dbulaja98 Dec 5, 2022
89e794f
Fix IndexName validation
dbulaja98 Dec 5, 2022
75fcedd
Refactor tests
dbulaja98 Dec 6, 2022
90f3297
Fix code remarks
dbulaja98 Dec 6, 2022
5174e25
Refactor tests
dbulaja98 Dec 6, 2022
cdb6b3f
Fix code remarks
dbulaja98 Dec 7, 2022
56df2f3
Refactor IntegrationSpec
dbulaja98 Dec 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how it's organized, is there any chance to run test container only along with integration tests?

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 =
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
params.exists(StringUtils.contains(name, _))

}