Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion bifunctor-tagless/src/test/scala/leaderboard/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ import leaderboard.model.*
import leaderboard.repo.{Ladder, Profiles}
import leaderboard.services.Ranks
import leaderboard.zioenv.*
import zio.{IO, ZIO}
import zio.test.{Annotations, Live, TestClock}
import zio.{IO, ZIO, ZLayer}

abstract class LeaderboardTest extends SpecZIO with AssertZIO {
override def config = super.config.copy(
pluginConfig = PluginConfig.cached(packagesEnabled = Seq("leaderboard.plugins")),
moduleOverrides = super.config.moduleOverrides ++ new ModuleDef {
make[Rnd[IO]].from[Rnd.Impl[IO]]

make[Live].fromZLayerEnv(zio.test.liveEnvironment >>> Live.default)
make[Annotations].fromZLayerEnv(Annotations.live)
// construct using Live and Annotations above
make[zio.Clock].fromZLayerEnv(TestClock.default)

// Unfortunately, to override the magical `DefaultServices.currentServices`
// it seems the layers have to be defined here, it's not enough to just stick zio.Clock into env,
// because ZIO is not using its own env.
make[List[ZLayer[Any, Nothing, Any]]].named("zio-runtime-configuration").from {
List(zio.test.testEnvironment)
}
},
// For testing, set up a docker container with postgres,
// instead of trying to connect to an external database
Expand Down Expand Up @@ -52,6 +65,20 @@ final class RanksTestPostgres extends RanksTest with ProdTest

abstract class LadderTest extends LeaderboardTest {

"ZIOEnv" should {
"test" in {
for {
// this seems to require overriding `make[List[ZLayer[Any, Nothing, Any]]].named("zio-runtime-configuration")` to work
clockMagicalFiberRef <- ZIO.clock
_ <- assertIO(clockMagicalFiberRef.isInstanceOf[TestClock])

// this works with just make[zio.Clock]
clockEnv <- ZIO.service[zio.Clock]
_ <- assertIO(clockEnv.isInstanceOf[TestClock])
} yield ()
}
}

"Ladder" should {

/** this test gets dependencies injected through function arguments */
Expand Down
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ val Deps = new {
val kindProjector = "org.typelevel" % "kind-projector" % V.kindProjector cross CrossVersion.full

val zio = "dev.zio" %% "zio" % V.zio
val zioTest = "dev.zio" %% "zio-test" % V.zio
val zioCats = "dev.zio" %% "zio-interop-cats" % V.zioCats

val catsCore = "org.typelevel" %% "cats-core" % V.catsCore
Expand All @@ -66,6 +67,7 @@ val Deps = new {
doobieHikari,
catsCore,
graalMetadata,
zioTest,
)
}

Expand Down