Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk committed Jul 18, 2024
1 parent 30f0398 commit d9ecd78
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,18 @@ lazy val cats_effect_2 = (project in file("elastic4s-effect-cats-2"))
.settings(libraryDependencies += cats2)

lazy val zio_1 = (project in file("elastic4s-effect-zio-1"))
.dependsOn(core, testkit % "test")
.dependsOn(core, clientsttp % "test", testkit % "test")
.settings(name := "elastic4s-effect-zio-1")
.settings(scala3Settings)
.settings(libraryDependencies ++= Dependencies.zio1)
.settings(libraryDependencies ++= Seq(Dependencies.sttpZio1Backend % "test"))

lazy val zio = (project in file("elastic4s-effect-zio"))
.dependsOn(core, testkit % "test")
.dependsOn(core, clientsttp % "test", testkit % "test")
.settings(name := "elastic4s-effect-zio")
.settings(scala3Settings)
.settings(libraryDependencies ++= Dependencies.zio)
.settings(libraryDependencies ++= Seq(Dependencies.sttpZioBackend % "test"))

lazy val scalaz = (project in file("elastic4s-effect-scalaz"))
.dependsOn(core)
Expand Down Expand Up @@ -302,7 +304,7 @@ lazy val clientsttp = (project in file("elastic4s-client-sttp"))
.dependsOn(core, testkit % "test")
.settings(name := "elastic4s-client-sttp")
.settings(scala3Settings)
.settings(libraryDependencies ++= Seq(sttp, asyncHttpClientBackendFuture))
.settings(libraryDependencies ++= Seq(sttp, sttpFutureBackend))

lazy val clientakka = (project in file("elastic4s-client-akka"))
.dependsOn(core, testkit % "test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class AkkaHttpClientTest extends AnyFlatSpec with Matchers with DockerTests with

private lazy val akkaClient = AkkaHttpClient(AkkaHttpClientSettings(List(s"$elasticHost:$elasticPort")))

override val client = ElasticClient(akkaClient)
def mkAkkaBasedClient(implicit executor: Executor[Future]): ElasticClient[Future] = ElasticClient(akkaClient)

override lazy val client = mkAkkaBasedClient

"AkkaHttpClient" should "support utf-8" in {

Expand Down Expand Up @@ -113,7 +115,7 @@ class AkkaHttpClientTest extends AnyFlatSpec with Matchers with DockerTests with
}
}

client.execute {
mkAkkaBasedClient.execute {
catHealth()
}.await.result.status shouldBe "401"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ class PekkoHttpClientTest extends AnyFlatSpec with Matchers with DockerTests wit

private lazy val pekkoClient = PekkoHttpClient(PekkoHttpClientSettings(List(s"$elasticHost:$elasticPort")))

override val client = ElasticClient(pekkoClient)
def mkPekkoBasedClient(implicit executor: Executor[Future]): ElasticClient[Future] =
ElasticClient(pekkoClient)

override lazy val client = mkPekkoBasedClient

"PekkoHttpClient" should "support utf-8" in {

Expand Down Expand Up @@ -113,7 +116,7 @@ class PekkoHttpClientTest extends AnyFlatSpec with Matchers with DockerTests wit
}
}

client.execute {
mkPekkoBasedClient.execute {
catHealth()
}.await.result.status shouldBe "401"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class SttpRequestHttpClientTest extends AnyFlatSpec with Matchers with DockerTests {
implicit val executor: Executor[Future] = new Executor[Future] {
override def exec(client: HttpClient[Future], request: ElasticRequest): Future[HttpResponse] = {
val cred = Base64.getEncoder.encodeToString("user123:pass123".getBytes(StandardCharsets.UTF_8))
Executor.FutureExecutor.exec(client, request.copy(headers = Map("Authorization" -> s"Basic $cred")))
}
}
private lazy val sttpClient = SttpRequestHttpClient(ElasticNodeEndpoint("http", elasticHost, elasticPort.toInt, None))
override val client = ElasticClient(sttpClient)
override lazy val client = ElasticClient(sttpClient)

"SttpRequestHttpClient" should "propagate headers if included" in {
implicit val executor: Executor[Future] = new Executor[Future] {
override def exec(client: HttpClient[Future], request: ElasticRequest): Future[HttpResponse] = {
val cred = Base64.getEncoder.encodeToString("user123:pass123".getBytes(StandardCharsets.UTF_8))
Executor.FutureExecutor.exec(client, request.copy(headers = Map("Authorization" -> s"Basic $cred")))
}
}

client.execute {
catHealth()
}.await.result.status shouldBe "401"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package com.sksamuel.elastic4s.zio

import com.sksamuel.elastic4s.{ElasticClient, ElasticNodeEndpoint}
import com.sksamuel.elastic4s.sttp.SttpRequestHttpClient
import com.sksamuel.elastic4s.testkit.DockerTests
import com.sksamuel.elastic4s.zio.instances._
import org.scalatest.BeforeAndAfterAll
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.client3.SttpBackend
import sttp.client3.httpclient.zio.HttpClientZioBackend
import sttp.client3.impl.zio.RIOMonadAsyncError
import sttp.monad.MonadError
import zio.Task

class ZIOTaskTest extends AnyFlatSpec with Matchers with DockerTests with BeforeAndAfterAll {
private implicit val sttpZioMonadError: MonadError[Task] = new RIOMonadAsyncError()
private implicit val sttpBackend: SttpBackend[Task, Any] =
HttpClientZioBackend().unsafeRun.fold(throw _, identity)

private lazy val sttpClient: SttpRequestHttpClient[Task] =
new SttpRequestHttpClient(ElasticNodeEndpoint("http", elasticHost, elasticPort.toInt, None))

val zioClient: ElasticClient[Task] = ElasticClient(sttpClient)

implicit class RichZIO[A](task: Task[A]) {
def unsafeRun: Either[Throwable, A] = {
Expand All @@ -16,19 +30,19 @@ class ZIOTaskTest extends AnyFlatSpec with Matchers with DockerTests with Before
}

override def beforeAll(): Unit = {
client.execute {
zioClient.execute {
deleteIndex("testindex")
}.unsafeRun
}

override def afterAll(): Unit = {
client.execute {
zioClient.execute {
deleteIndex("testindex")
}.unsafeRun
}

it should "index doc successfully" in {
val r = client.execute {
val r = zioClient.execute {
indexInto("testindex").doc("""{ "text":"Buna ziua!" }""")
}.unsafeRun
r shouldBe Symbol("right")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package com.sksamuel.elastic4s.zio

import com.sksamuel.elastic4s.{ElasticClient, ElasticNodeEndpoint}
import com.sksamuel.elastic4s.sttp.SttpRequestHttpClient
import com.sksamuel.elastic4s.testkit.DockerTests
import com.sksamuel.elastic4s.zio.instances._
import org.scalatest.BeforeAndAfterAll
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.client3.SttpBackend
import sttp.client3.httpclient.zio.HttpClientZioBackend
import sttp.client3.impl.zio.RIOMonadAsyncError
import sttp.monad.MonadError
import zio.{Task, Unsafe}

class ZIOTaskTest extends AnyFlatSpec with Matchers with DockerTests with BeforeAndAfterAll {
private implicit val sttpZioMonadError: MonadError[Task] = new RIOMonadAsyncError()
private implicit val sttpBackend: SttpBackend[Task, Any] =
HttpClientZioBackend().unsafeRun.fold(throw _, identity)

private lazy val sttpClient: SttpRequestHttpClient[Task] =
new SttpRequestHttpClient(ElasticNodeEndpoint("http", elasticHost, elasticPort.toInt, None))

val zioClient: ElasticClient[Task] = ElasticClient(sttpClient)

implicit class RichZIO[A](task: Task[A]) {
def unsafeRun: Either[Throwable, A] = {
Expand All @@ -18,19 +32,19 @@ class ZIOTaskTest extends AnyFlatSpec with Matchers with DockerTests with Before
}

override def beforeAll(): Unit = {
client.execute {
zioClient.execute {
deleteIndex("testindex")
}.unsafeRun
}

override def afterAll(): Unit = {
client.execute {
zioClient.execute {
deleteIndex("testindex")
}.unsafeRun
}

it should "index doc successfully" in {
val r = client.execute {
val r = zioClient.execute {
indexInto("testindex").doc("""{ "text":"Buna ziua!" }""")
}.unsafeRun
r shouldBe Symbol("right")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sksamuel.elastic4s.testkit

import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.{ElasticClient, ElasticDsl, ElasticProperties}
import com.sksamuel.elastic4s.{ElasticClient, ElasticDsl, ElasticProperties, Executor}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
Expand All @@ -14,7 +14,12 @@ trait DockerTests extends ElasticDsl with ClientProvider {
// use obscure ports for the tests to reduce the risk of interfering with existing elastic installations/containers
"39227"
)
val client: ElasticClient[Future] = ElasticClient(JavaClient(ElasticProperties(s"http://$elasticHost:$elasticPort")))

def mkJavaBasedClient(implicit executor: Executor[Future]): ElasticClient[Future] =
ElasticClient(JavaClient(ElasticProperties(s"http://$elasticHost:$elasticPort")))

// TODO: client is not closed, consider using beforeAll/afterAll to close it
lazy val client: ElasticClient[Future] = mkJavaBasedClient

protected def deleteIdx(indexName: String): Unit = {
Try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ElasticClientTests extends AnyFlatSpec with Matchers with DockerTests {
}
}

client.execute {
mkJavaBasedClient.execute {
catHealth()
}.await.result.status shouldBe "401"
}
Expand Down
4 changes: 3 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ object Dependencies {
lazy val akkaActor = "com.typesafe.akka" %% "akka-actor" % AkkaVersion
lazy val akkaHTTP = "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion
lazy val akkaStream = "com.typesafe.akka" %% "akka-stream" % AkkaVersion
lazy val asyncHttpClientBackendFuture = "com.softwaremill.sttp.client3" %% "async-http-client-backend-future" % SttpVersion
lazy val sttpFutureBackend = "com.softwaremill.sttp.client3" %% "async-http-client-backend-future" % SttpVersion
lazy val sttpZioBackend = "com.softwaremill.sttp.client3" %% "zio" % SttpVersion
lazy val sttpZio1Backend = "com.softwaremill.sttp.client3" %% "zio1" % SttpVersion
lazy val cats = "org.typelevel" %% "cats-effect" % CatsEffectVersion
lazy val cats2 = "org.typelevel" %% "cats-effect" % CatsEffect2Version
lazy val elasticsearchRestClient = "org.elasticsearch.client" % "elasticsearch-rest-client" % ElasticsearchVersion
Expand Down

0 comments on commit d9ecd78

Please sign in to comment.