Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Configure postgres docker host dynamically as well
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-kai committed Dec 1, 2019
1 parent 7e9507f commit d15721b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/main/resources/common-reference.conf
Expand Up @@ -5,7 +5,9 @@ logger {

postgres {
jdbcDriver = "org.postgresql.Driver"
url = "jdbc:postgresql://localhost:${port}/postgres"
url = "jdbc:postgresql://{host}:{port}/postgres"
user = "postgres"
password = "postgres"
host = "localhost"
port = 5432
}
15 changes: 10 additions & 5 deletions src/main/scala/livecode/code.scala
Expand Up @@ -153,7 +153,7 @@ object code {
HikariTransactor
.newHikariTransactor(
driverClassName = cfg.jdbcDriver,
url = cfg.url.replace("${port}", portCfg.postgresPort.toString),
url = portCfg.substitute(cfg.url),
user = cfg.user,
pass = cfg.password,
connectEC = blocker.blockingContext,
Expand All @@ -169,19 +169,24 @@ object code {
)

final case class PostgresPortCfg(
postgresPort: Int,
)
host: String,
port: Int,
) {
def substitute(s: String): String = {
s.replace("{host}", host).replace("{port}", port.toString)
}
}

final class PgIntegrationCheck(
portCheck: PortCheck,
cfg: PostgresCfg @ConfPath("postgres"),
portCfg: PostgresPortCfg,
) extends IntegrationCheck {
override def resourcesAvailable(): ResourceCheck = {
val str = cfg.url.stripPrefix("jdbc:").replace("${port}", "")
val str = portCfg.substitute(cfg.url.stripPrefix("jdbc:"))
val uri = URI.create(str)

portCheck.checkUri(uri, portCfg.postgresPort, s"Couldn't connect to postgres at uri=$uri defaultPort=${portCfg.postgresPort}")
portCheck.checkUri(uri, portCfg.port, s"Couldn't connect to postgres at uri=$uri defaultPort=${portCfg.port}")
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/livecode/plugins/LivecodePlugin.scala
Expand Up @@ -3,6 +3,7 @@ package livecode.plugins
import distage.plugins.PluginDef
import distage.{TagK, TagKK}
import doobie.util.transactor.Transactor
import izumi.distage.config.annotations.ConfPath
import izumi.distage.model.definition.ModuleDef
import izumi.distage.model.definition.StandardAxis.Repo
import izumi.fundamentals.platform.integration.PortCheck
Expand Down Expand Up @@ -39,7 +40,10 @@ object LivecodePlugin extends PluginDef {
make[Transactor[F[Throwable, ?]]].fromResource(Postgres.resource[F[Throwable, ?]] _)
make[PgIntegrationCheck]
make[PortCheck].from(new PortCheck(3))
make[PostgresPortCfg].from(PostgresPortCfg(5432))
make[PostgresPortCfg].from {
conf: PostgresPortCfg @ConfPath("postgres") =>
conf
}
}

def repoDummy[F[+_, +_]: TagKK](implicit ev: TagK[F[Throwable, ?]]): ModuleDef = new ModuleDef {
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/livecode/PostgresDockerModule.scala
Expand Up @@ -20,8 +20,8 @@ object PostgresDockerModule extends ModuleDef {
// integration check is performed
make[PostgresPortCfg].from {
docker: PostgresDocker.Container =>
println(docker)
PostgresPortCfg(docker.availablePorts(DockerPort.TCP(5432)).head.port)
val knownAddress = docker.availablePorts(DockerPort.TCP(5432)).head
PostgresPortCfg(knownAddress.hostV4, knownAddress.port)
}

// add docker dependencies and override default configuration
Expand Down
34 changes: 15 additions & 19 deletions src/test/scala/livecode/tests.scala
Expand Up @@ -2,13 +2,11 @@ package livecode

import distage.{DIKey, ModuleDef}
import doobie.util.transactor.Transactor
import izumi.distage.constructors.ConcreteConstructor
import izumi.distage.model.definition.StandardAxis
import izumi.distage.testkit.integration.docker.examples.{PostgresDocker, PostgresDockerModule}
import izumi.distage.testkit.integration.docker.examples.PostgresDocker
import izumi.distage.testkit.services.DISyntaxZIOEnv
import izumi.distage.testkit.services.st.dtest.TestConfig
import izumi.distage.testkit.st.specs.DistageBIOSpecScalatest
import livecode.code.Postgres.PgIntegrationCheck
import livecode.code._
import livecode.zioenv._
import zio.{IO, Task, ZIO}
Expand Down Expand Up @@ -43,19 +41,20 @@ final class RanksTestDummy extends RanksTest with DummyTest
class LadderTest extends LivecodeTest with DummyTest {

"Ladder" should {
// this test gets dependencies through arguments
"submit & get" in {
// (rnd: Rnd[IO], ladder: Ladder[IO]) =>
for {
user <- rnd[UserId]
score <- rnd[Score]
_ <- ladder.submitScore(user, score)
res <- ladder.getScores.map(_.find(_._1 == user).map(_._2))
_ = assert(res contains score)
} yield ()
(rnd: Rnd[IO], ladder: Ladder[IO]) =>
for {
user <- rnd[UserId]
score <- rnd[Score]
_ <- ladder.submitScore(user, score)
res <- ladder.getScores.map(_.find(_._1 == user).map(_._2))
_ = assert(res contains score)
} yield ()
}

// other tests get dependencies via ZIO Env:
"return higher score higher in the list" in {
// (rnd: Rnd[IO], ladder: Ladder[IO]) =>
for {
user1 <- rnd[UserId]
score1 <- rnd[Score]
Expand All @@ -82,9 +81,9 @@ class LadderTest extends LivecodeTest with DummyTest {

class ProfilesTest extends LivecodeTest {
"Profiles" should {
// that's what the env signature looks like for ZIO Env injection
"set & get" in {
// (rnd: Rnd[IO], profiles: Profiles[IO]) =>
for {
val zioValue: ZIO[ProfilesEnv with RndEnv, QueryFailure, Unit] = for {
user <- rnd[UserId]
name <- rnd[String]
desc <- rnd[String]
Expand All @@ -93,15 +92,15 @@ class ProfilesTest extends LivecodeTest {
res <- profiles.getProfile(user)
_ = assert(res contains profile)
} yield ()
zioValue
}
}
}

class RanksTest extends LivecodeTest {
"Ranks" should {
"return None for a user with no score" in {
// (rnd: Rnd[IO], ranks: Ranks[IO], profiles: Profiles[IO]) =>
val value: ZIO[RanksEnv with ProfilesEnv with RndEnv, QueryFailure, Unit] = for {
for {
user <- rnd[UserId]
name <- rnd[String]
desc <- rnd[String]
Expand All @@ -110,11 +109,9 @@ class RanksTest extends LivecodeTest {
res1 <- ranks.getRank(user)
_ = assert(res1.isEmpty)
} yield ()
value
}

"return None for a user with no profile" in {
// (rnd: Rnd[IO], ranks: Ranks[IO], ladder: Ladder[IO]) =>
for {
user <- rnd[UserId]
score <- rnd[Score]
Expand All @@ -125,7 +122,6 @@ class RanksTest extends LivecodeTest {
}

"assign a higher rank to a user with more score" in {
// (rnd: Rnd[IO], ranks: Ranks[IO], ladder: Ladder[IO], profiles: Profiles[IO]) =>
for {
user1 <- rnd[UserId]
name1 <- rnd[String]
Expand Down

0 comments on commit d15721b

Please sign in to comment.