-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use shared postgres container for tests (#486)
- Loading branch information
abalias
authored
Mar 28, 2023
1 parent
9ede86f
commit 1d6aada
Showing
6 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ent/service/server/src/test/scala/io/iohk/atala/pollux/test/container/PostgresLayer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...-agent/service/wallet-api/src/test/scala/io/iohk/atala/test/container/PostgresLayer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters