Skip to content

Commit

Permalink
fix: use shared postgres container for tests (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
abalias authored Mar 28, 2023
1 parent 9ede86f commit 1d6aada
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 deletions.
8 changes: 5 additions & 3 deletions prism-agent/service/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ object Dependencies {
val zioMetrics = "2.0.6"
val akka = "2.6.20"
val akkaHttp = "10.2.9"
val castor = "0.8.2"
val pollux = "0.43.2"
val connect = "0.13.0"
val castor = "0.8.3"
val pollux = "0.43.3"
val shared = "0.3.0"
val connect = "0.13.1"
val bouncyCastle = "1.70"
val logback = "1.4.5"
val mercury = "0.22.0"
Expand Down Expand Up @@ -47,6 +48,7 @@ object Dependencies {

private lazy val polluxCore = "io.iohk.atala" %% "pollux-core" % Versions.pollux
private lazy val polluxSqlDoobie = "io.iohk.atala" %% "pollux-sql-doobie" % Versions.pollux
private lazy val shared = "io.iohk.atala" % "shared" % Versions.shared

private lazy val connectCore = "io.iohk.atala" %% "connect-core" % Versions.connect
private lazy val connectSqlDoobie = "io.iohk.atala" %% "connect-sql-doobie" % Versions.connect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.iohk.atala.pollux.credentialschema.http.{
}
import io.iohk.atala.pollux.sql.repository.JdbcCredentialSchemaRepository
import io.iohk.atala.pollux.test.container.MigrationAspects.*
import io.iohk.atala.pollux.test.container.PostgresTestContainer.*
import io.iohk.atala.pollux.test.container.PostgresLayer.*
import sttp.client3.testing.SttpBackendStub
import sttp.client3.ziojson.*
import sttp.client3.{DeserializationException, ResponseException, SttpBackend, UriContext, basicRequest, Response as R}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.iohk.atala.pollux.credentialschema.http.{
}
import io.iohk.atala.pollux.sql.repository.JdbcCredentialSchemaRepository
import io.iohk.atala.pollux.test.container.MigrationAspects.*
import io.iohk.atala.pollux.test.container.PostgresTestContainer.*
import io.iohk.atala.pollux.test.container.PostgresLayer.*
import sttp.client3.testing.SttpBackendStub
import sttp.client3.ziojson.*
import sttp.client3.{DeserializationException, Response, ResponseException, SttpBackend, UriContext, basicRequest}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.iohk.atala.pollux.test.container

import cats.Functor
import cats.effect.std.Dispatcher
import cats.effect.{Async, Resource}
import cats.syntax.functor.*
import com.dimafeng.testcontainers.{JdbcDatabaseContainer, PostgreSQLContainer}
import com.zaxxer.hikari.HikariConfig
import doobie.hikari.HikariTransactor
import doobie.util.ExecutionContexts
import doobie.util.transactor.Transactor
import io.iohk.atala.shared.test.containers.PostgresTestContainer.postgresContainer
import org.testcontainers.containers.output.OutputFrame
import org.testcontainers.utility.DockerImageName
import zio.*
import zio.ZIO.*
import zio.interop.catz.*

import java.util.function.Consumer
import scala.concurrent.ExecutionContext

object PostgresLayer {

def postgresLayer(
imageName: Option[String] = Some("postgres"),
verbose: Boolean = false
): ZLayer[Any, Nothing, PostgreSQLContainer] =
ZLayer.scoped {
acquireRelease(ZIO.attemptBlockingIO {
val container = postgresContainer(imageName, verbose)
container.start()
container
}.orDie)(container => attemptBlockingIO(container.stop()).orDie)
}

private def hikariConfig(container: PostgreSQLContainer): HikariConfig = {
val config = HikariConfig()
config.setJdbcUrl(container.jdbcUrl)
config.setUsername(container.username)
config.setPassword(container.password)
config
}

lazy val hikariConfigLayer: ZLayer[PostgreSQLContainer, Nothing, HikariConfig] =
ZLayer.fromZIO {
for {
container <- ZIO.service[PostgreSQLContainer]
} yield hikariConfig(container)
}

def transactor: ZLayer[HikariConfig, Throwable, Transactor[Task]] = ZLayer.fromZIO {
val hikariTransactorLayerZIO = for {
config <- ZIO.service[HikariConfig]
htxResource: Resource[Task, HikariTransactor[Task]] = for {
ec <- ExecutionContexts.cachedThreadPool[Task]
xa <- HikariTransactor.fromHikariConfig[Task](config, ec)
} yield xa
layer <- Dispatcher[Task].allocated.map {
case (dispatcher, _) => {
given Dispatcher[Task] = dispatcher
htxResource.toManaged.toLayer[Transactor[Task]]
}
}
} yield layer
hikariTransactorLayerZIO
}.flatten

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.iohk.atala.test.container

import cats.Functor
import cats.effect.std.Dispatcher
import cats.effect.{Async, Resource}
import cats.syntax.functor.*
import com.dimafeng.testcontainers.{JdbcDatabaseContainer, PostgreSQLContainer}
import com.zaxxer.hikari.HikariConfig
import doobie.hikari.HikariTransactor
import doobie.util.ExecutionContexts
import doobie.util.transactor.Transactor
import io.iohk.atala.shared.test.containers.PostgresTestContainer.postgresContainer
import org.testcontainers.containers.output.OutputFrame
import org.testcontainers.utility.DockerImageName
import zio.*
import zio.ZIO.*
import zio.interop.catz.*

import java.util.function.Consumer
import scala.concurrent.ExecutionContext
object PostgresLayer {

def postgresLayer(
imageName: Option[String] = Some("postgres"),
verbose: Boolean = false
): ZLayer[Any, Nothing, PostgreSQLContainer] =
ZLayer.scoped {
acquireRelease(ZIO.attemptBlockingIO {
val container = postgresContainer(imageName, verbose)
container.start()
container
}.orDie)(container => attemptBlockingIO(container.stop()).orDie)
}

private def hikariConfig(container: PostgreSQLContainer): HikariConfig = {
val config = HikariConfig()
config.setJdbcUrl(container.jdbcUrl)
config.setUsername(container.username)
config.setPassword(container.password)
config
}

lazy val hikariConfigLayer: ZLayer[PostgreSQLContainer, Nothing, HikariConfig] =
ZLayer.fromZIO {
for {
container <- ZIO.service[PostgreSQLContainer]
} yield hikariConfig(container)
}

def transactor: ZLayer[HikariConfig, Throwable, Transactor[Task]] = ZLayer.fromZIO {
val hikariTransactorLayerZIO = for {
config <- ZIO.service[HikariConfig]
htxResource: Resource[Task, HikariTransactor[Task]] = for {
ec <- ExecutionContexts.cachedThreadPool[Task]
xa <- HikariTransactor.fromHikariConfig[Task](config, ec)
} yield xa
layer <- Dispatcher[Task].allocated.map {
case (dispatcher, _) => {
given Dispatcher[Task] = dispatcher
htxResource.toManaged.toLayer[Transactor[Task]]
}
}
} yield layer
hikariTransactorLayerZIO
}.flatten

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import zio.*

trait PostgresTestContainerSupport {

protected val pgContainerLayer: ULayer[PostgreSQLContainer] = PostgresTestContainer.postgresLayer()
protected val pgContainerLayer: ULayer[PostgreSQLContainer] = PostgresLayer.postgresLayer()

protected val transactorLayer: TaskLayer[Transactor[Task]] =
pgContainerLayer >>> PostgresTestContainer.hikariConfigLayer >>> PostgresTestContainer.transactor
pgContainerLayer >>> PostgresLayer.hikariConfigLayer >>> PostgresLayer.transactor

}

0 comments on commit 1d6aada

Please sign in to comment.