Skip to content

Instantly share code, notes, and snippets.

@ChristopherDavenport
Created November 14, 2018 13:16
Show Gist options
  • Save ChristopherDavenport/c4fa7d331e18fa473830b12c420531d4 to your computer and use it in GitHub Desktop.
Save ChristopherDavenport/c4fa7d331e18fa473830b12c420531d4 to your computer and use it in GitHub Desktop.
Test Containers Specs2 Setup
import doobie._
import cats.effect._
import cats.effect.IO
import doobie.specs2._
import io.chrisdavenport.testcontainersspecs2._
import org.specs2.mutable.Specification
import com.dimafeng.testcontainers.GenericContainer
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy
import java.time.Duration
import java.time.temporal.ChronoUnit.SECONDS
class IODoobieQueriesSpec extends QueriesSpec[IO] {
// Using this instead of IOAnalysisMatchers to avoid uninitialized field error
override implicit val M: Effect[IO] = IO.ioConcurrentEffect
}
trait QueriesSpec[F[_]] extends Specification with Checker[F] with ForAllTestContainer {
sequential
// IMPORTANT: MUST BE LAZY VAL
override lazy val container = GenericContainer(
"christopherdavenport/postgres-db-with-role:latest",
List(5432),
Map(
"POSTGRES_DB" -> dbName,
"POSTGRES_USER" -> dbUserName,
"POSTGRES_PASSWORD" -> dbPassword,
"POSTGRES_ROLE" -> dbName
),
waitStrategy = new LogMessageWaitStrategy()
.withRegEx(".*database system is ready to accept connections.*\\s")
.withTimes(2)
.withStartupTimeout(Duration.of(60, SECONDS))
)
lazy val driverName = "org.postgresql.Driver"
lazy val jdbcUrl = s"jdbc:postgresql://${container.container.getContainerIpAddress()}:${container.container.getMappedPort(5432)}/${dbName}"
lazy val dbUserName = "dbUser"
lazy val dbPassword = "password"
lazy val dbName = "dbName"
lazy val transactor = Transactor.fromDriverManager[F](
driverName,
jdbcUrl,
dbUserName,
dbPassword
)
override def afterStart : Unit =
Migrations.makeMigrations[IO](jdbcUrl, dbUserName, dbPassword).unsafeRunSync() // My Migration Utility
check("select 1".query[Int])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment