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

Make improvements in tests #49

Merged
merged 4 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package zio.elasticsearch

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.{aResponse, delete, get, head, post, put, urlEqualTo}
import sttp.model.StatusCode
import zio.ZIO
import zio.elasticsearch.ElasticQuery.matchAll
import zio.test.Assertion._
import zio.test.TestAspect.{afterAll, beforeAll}
import zio.test.{Spec, TestEnvironment, assertZIO}

object HttpElasticExecutorSpec extends WiremockSpec {
object HttpElasticExecutorSpec extends WireMockSpec {

override def spec: Spec[TestEnvironment, Any] =
suite("HttpElasticExecutor")(
suite("bulk request") {
test("successfully execute bulk request") {
server.addStubMapping(
test("bulk request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
post(urlEqualTo("/_bulk?refresh=true"))
.willReturn(
aResponse
Expand Down Expand Up @@ -48,13 +48,15 @@ object HttpElasticExecutorSpec extends WiremockSpec {
)
.build
)
)

assertZIO(ElasticRequest.bulk(ElasticRequest.create(index, repo)).refreshTrue.execute)(isUnit)
}
assertZIO(addStubMapping *> ElasticRequest.bulk(ElasticRequest.create(index, repo)).refreshTrue.execute)(
isUnit
)
},
suite("creating document request") {
test("return document ID") {
server.addStubMapping(
test("creating document request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
post(urlEqualTo("/repositories/_doc?refresh=true&routing=routing"))
.willReturn(
aResponse
Expand All @@ -68,114 +70,125 @@ object HttpElasticExecutorSpec extends WiremockSpec {
)
.build
)
)

assertZIO(
ElasticRequest.create[GitHubRepo](index = index, doc = repo).routing(Routing("routing")).refreshTrue.execute
)(equalTo(DocumentId("V4x8q4UB3agN0z75fv5r")))
}
assertZIO(
addStubMapping *> ElasticRequest
.create[GitHubRepo](index = index, doc = repo)
.routing(Routing("routing"))
.refreshTrue
.execute
)(equalTo(DocumentId("V4x8q4UB3agN0z75fv5r")))
},
suite("creating request with given ID") {
test("return Created outcome") {
server.addStubMapping(
test("creating request with given ID") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
post(urlEqualTo("/repositories/_create/V4x8q4UB3agN0z75fv5r?refresh=true&routing=routing"))
.willReturn(aResponse.withStatus(StatusCode.Created.code))
.build
)
)

assertZIO(
ElasticRequest
.create[GitHubRepo](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = repo)
.routing(Routing("routing"))
.refreshTrue
.execute
)(equalTo(CreationOutcome.Created))
}
assertZIO(
addStubMapping *> ElasticRequest
.create[GitHubRepo](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = repo)
.routing(Routing("routing"))
.refreshTrue
.execute
)(equalTo(CreationOutcome.Created))
},
suite("creating index request") {
test("return Created outcome") {
server.addStubMapping(
test("creating index request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
put(urlEqualTo("/repositories")).willReturn(aResponse.withStatus(StatusCode.Ok.code)).build
)
)

assertZIO(ElasticRequest.createIndex(name = index, definition = None).execute)(
equalTo(CreationOutcome.Created)
)
}
assertZIO(addStubMapping *> ElasticRequest.createIndex(name = index, definition = None).execute)(
equalTo(CreationOutcome.Created)
)
},
suite("creating or updating request") {
test("successfully create or update document") {
server.addStubMapping(
test("creating or updating request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
put(urlEqualTo("/repositories/_doc/V4x8q4UB3agN0z75fv5r?refresh=true&routing=routing"))
.willReturn(aResponse.withStatus(StatusCode.Created.code))
.build
)
)

assertZIO(
ElasticRequest
.upsert[GitHubRepo](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = repo)
.routing(Routing("routing"))
.refreshTrue
.execute
)(isUnit)
}
assertZIO(
addStubMapping *> ElasticRequest
.upsert[GitHubRepo](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), doc = repo)
.routing(Routing("routing"))
.refreshTrue
.execute
)(isUnit)
},
suite("deleting by ID request") {
test("return Deleted outcome") {
server.addStubMapping(
test("deleting by ID request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
delete(urlEqualTo("/repositories/_doc/V4x8q4UB3agN0z75fv5r?refresh=true&routing=routing"))
.willReturn(aResponse.withStatus(StatusCode.Ok.code))
.build
)
)

assertZIO(
ElasticRequest
.deleteById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"))
.routing(Routing("routing"))
.refreshTrue
.execute
)(equalTo(DeletionOutcome.Deleted))
}
assertZIO(
addStubMapping *> ElasticRequest
.deleteById(index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"))
.routing(Routing("routing"))
.refreshTrue
.execute
)(equalTo(DeletionOutcome.Deleted))
},
suite("deleting by query request") {
test("return Deleted outcome") {
server.addStubMapping(
test("deleting by query request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
post(urlEqualTo("/repositories/_delete_by_query?refresh=true"))
.willReturn(aResponse.withStatus(StatusCode.Ok.code))
.build
)
)

assertZIO(ElasticRequest.deleteByQuery(index = index, query = matchAll()).refreshTrue.execute)(
equalTo(DeletionOutcome.Deleted)
)
}
assertZIO(
addStubMapping *> ElasticRequest.deleteByQuery(index = index, query = matchAll()).refreshTrue.execute
)(
equalTo(DeletionOutcome.Deleted)
)
},
suite("deleting index request") {
test("return Deleted outcome") {
server.addStubMapping(
test("deleting index request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
delete(urlEqualTo("/repositories"))
.willReturn(aResponse.withStatus(StatusCode.Ok.code))
.build
)
)

assertZIO(ElasticRequest.deleteIndex(name = index).execute)(equalTo(DeletionOutcome.Deleted))
}
assertZIO(addStubMapping *> ElasticRequest.deleteIndex(name = index).execute)(
equalTo(DeletionOutcome.Deleted)
)
},
suite("exists request") {
test("return true") {
server.addStubMapping(
test("exists request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
head(urlEqualTo("/repositories/_doc/example-id?routing=routing"))
.willReturn(aResponse.withStatus(StatusCode.Ok.code))
.build
)
)

assertZIO(
ElasticRequest.exists(index = index, id = DocumentId("example-id")).routing(Routing("routing")).execute
)(isTrue)
}
assertZIO(
addStubMapping *> ElasticRequest
.exists(index = index, id = DocumentId("example-id"))
.routing(Routing("routing"))
.execute
)(isTrue)
},
suite("getting by ID request") {
test("successfully return document") {
server.addStubMapping(
test("getting by ID request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
get(urlEqualTo("/repositories/_doc/V4x8q4UB3agN0z75fv5r?routing=routing"))
.willReturn(
aResponse
Expand All @@ -195,20 +208,18 @@ object HttpElasticExecutorSpec extends WiremockSpec {
)
.build
)
)

assertZIO(
ElasticRequest
.getById[GitHubRepo](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"))
.routing(Routing("routing"))
.execute
)(
isSome(equalTo(repo))
)
}
assertZIO(
addStubMapping *> ElasticRequest
.getById[GitHubRepo](index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"))
.routing(Routing("routing"))
.execute
)(isSome(equalTo(repo)))
},
suite("getting by query request") {
test("successfully return documents") {
server.addStubMapping(
test("getting by query request") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
post(urlEqualTo("/repositories/_search"))
.willReturn(
aResponse
Expand Down Expand Up @@ -251,13 +262,11 @@ object HttpElasticExecutorSpec extends WiremockSpec {
)
.build
)
)

assertZIO(ElasticRequest.search[GitHubRepo](index = index, query = matchAll()).execute)(
equalTo(List(repo))
)
}
assertZIO(addStubMapping *> ElasticRequest.search[GitHubRepo](index = index, query = matchAll()).execute)(
equalTo(List(repo))
)
}
).provideShared(elasticsearchWireMockLayer) @@
beforeAll(ZIO.attempt(server.start())) @@
afterAll(ZIO.attempt(server.stop()).orDie)
).provideShared(elasticsearchWireMockLayer, wireMockServerLayer)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ object IndexNameSpec extends ZIOSpecDefault {
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing charachter '*'") {
test("fail for string containing character '*'") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"$part1*$part2"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing charachter ':'") {
test("fail for string containing character ':'") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"$part1:$part2"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
Expand All @@ -36,7 +36,7 @@ object IndexNameSpec extends ZIOSpecDefault {
val name = ""
assert(IndexName.make(name))(equalTo(Validation.succeed(unsafeWrap(IndexName)(name))))
},
test("fail for string starting with charachter '-'") {
test("fail for string starting with character '-'") {
check(genString(1, 255)) { name =>
val invalidName = s"-$name"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
MatchQuery(field = "day_of_week", value = "Monday"),
MatchQuery(field = "customer_gender", value = "MALE")
),
should = List.empty
should = Nil
)
)
)
Expand All @@ -72,7 +72,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(query)(
equalTo(
BoolQuery(
must = List.empty,
must = Nil,
should = List(
MatchQuery(field = "day_of_week", value = "Monday"),
MatchQuery(field = "customer_gender", value = "MALE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@ import com.github.tomakehurst.wiremock.WireMockServer
import sttp.client3.httpclient.zio.HttpClientZioBackend
import zio.prelude.Newtype.unsafeWrap
import zio.test.ZIOSpecDefault
import zio.{TaskLayer, ZLayer}
import zio.{TaskLayer, ZIO, ZLayer}

trait WiremockSpec extends ZIOSpecDefault {
trait WireMockSpec extends ZIOSpecDefault {
val index: IndexName = unsafeWrap(IndexName)("repositories")

val repo: GitHubRepo =
GitHubRepo(id = Some("123"), organization = "lambdaworks.io", name = "LambdaWorks", stars = 10, forks = 10)

val port: Int = 9300

val elasticsearchWireMockLayer: TaskLayer[ElasticExecutor] =
HttpClientZioBackend
.layer() >>> (ZLayer.succeed(ElasticConfig.apply("localhost", port)) >>> ElasticExecutor.live)
val server: WireMockServer = new WireMockServer(port)

val wireMockServerLayer: TaskLayer[WireMockServer] = {
val server = ZIO.acquireRelease(
ZIO.attemptBlocking {
val server = new WireMockServer(port)

server.start()
server
}
)(server => ZIO.succeedBlocking(server.stop()))

ZLayer.scoped(server)
}
}