Skip to content

Commit

Permalink
Merge pull request #408 from gemini-hlsw/topic/munit
Browse files Browse the repository at this point in the history
Move to MUnit and whale-tail
  • Loading branch information
milessabin authored Jun 6, 2023
2 parents 104549b + 401819e commit b0d5096
Show file tree
Hide file tree
Showing 68 changed files with 1,292 additions and 1,596 deletions.
113 changes: 60 additions & 53 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import nl.zolotko.sbt.jfr.{JfrRecording, JfrRecorderOptions}
import scala.concurrent.duration.DurationInt

val catsVersion = "2.9.0"
val catsParseVersion = "0.3.9"
val catsEffectVersion = "3.1.1"
val catsTestkitScalaTestVersion = "2.1.5"
val circeVersion = "0.14.5"
val doobieVersion = "1.0.0-RC2"
val flywayVersion = "9.19.1"
val fs2Version = "3.7.0"
val http4sVersion = "0.23.19"
val http4sBlazeVersion = "0.23.15"
val kindProjectorVersion = "0.13.2"
val literallyVersion = "1.1.0"
val logbackVersion = "1.4.7"
val log4catsVersion = "2.6.0"
val skunkVersion = "0.6.0"
val shapeless2Version = "2.3.10"
val shapeless3Version = "3.1.0"
val sourcePosVersion = "1.1.0"
val testContainersVersion = "0.40.16"
val typenameVersion = "1.1.0"
val catsVersion = "2.9.0"
val catsParseVersion = "0.3.9"
val catsEffectVersion = "3.1.1"
val circeVersion = "0.14.5"
val disciplineMunitVersion = "2.0.0-M3"
val doobieVersion = "1.0.0-RC2"
val flywayVersion = "9.19.1"
val fs2Version = "3.7.0"
val http4sVersion = "0.23.19"
val http4sBlazeVersion = "0.23.15"
val jnrUnixsocketVersion = "0.38.19"
val kindProjectorVersion = "0.13.2"
val literallyVersion = "1.1.0"
val logbackVersion = "1.4.7"
val log4catsVersion = "2.6.0"
val munitVersion = "1.0.0-M7"
val munitCatsEffectVersion = "2.0.0-M3"
val skunkVersion = "0.6.0"
val shapeless2Version = "2.3.10"
val shapeless3Version = "3.1.0"
val sourcePosVersion = "1.1.0"
val typenameVersion = "1.1.0"
val whaleTailVersion = "0.0.10"

val Scala2 = "2.13.10"
val Scala3 = "3.3.0"
Expand Down Expand Up @@ -49,18 +52,20 @@ ThisBuild / githubWorkflowJavaVersions := Seq(JavaSpec.temurin("11"))

lazy val commonSettings = Seq(
//scalacOptions --= Seq("-Wunused:params", "-Wunused:imports", "-Wunused:patvars", "-Wdead-code", "-Wunused:locals", "-Wunused:privates", "-Wunused:implicits"),
scalacOptions += "-Wconf:msg=unused value of type org.scalatest.Assertion:silent",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-testkit" % catsVersion % "test",
"org.typelevel" %% "cats-testkit-scalatest" % catsTestkitScalaTestVersion % "test"
"org.scalameta" %% "munit" % munitVersion % "test",
"org.scalameta" %% "munit-scalacheck" % munitVersion % "test",
"org.typelevel" %% "cats-laws" % catsVersion % "test",
"org.typelevel" %% "discipline-munit" % disciplineMunitVersion % "test",
"org.typelevel" %% "munit-cats-effect" % munitCatsEffectVersion % "test"
) ++ Seq(
compilerPlugin("org.typelevel" %% "kind-projector" % kindProjectorVersion cross CrossVersion.full),
).filterNot(_ => tlIsScala3.value),
headerMappings := headerMappings.value + (HeaderFileType.scala -> HeaderCommentStyle.cppStyleLineComment),
headerLicense := Some(HeaderLicense.Custom(
"""|Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA)
|For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause
|""".stripMargin
|For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause
|""".stripMargin
)),
// Temporary workaround for https://github.com/lampepfl/dotty/issues/15927
Compile / doc / sources := {
Expand Down Expand Up @@ -102,14 +107,14 @@ lazy val core = project
name := "gsp-graphql-core",
libraryDependencies ++=
Seq(
"org.typelevel" %% "cats-parse" % catsParseVersion,
"org.typelevel" %% "cats-core" % catsVersion,
"org.typelevel" %% "literally" % literallyVersion,
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"org.tpolecat" %% "typename" % typenameVersion,
"org.tpolecat" %% "sourcepos" % sourcePosVersion,
"co.fs2" %% "fs2-core" % fs2Version,
"org.typelevel" %% "cats-parse" % catsParseVersion,
"org.typelevel" %% "cats-core" % catsVersion,
"org.typelevel" %% "literally" % literallyVersion,
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"org.tpolecat" %% "typename" % typenameVersion,
"org.tpolecat" %% "sourcepos" % sourcePosVersion,
"co.fs2" %% "fs2-core" % fs2Version,
)
)

Expand All @@ -132,10 +137,10 @@ lazy val sql = project
.settings(
name := "gsp-graphql-sql",
libraryDependencies ++= Seq(
"io.circe" %% "circe-generic" % circeVersion % "test",
"ch.qos.logback" % "logback-classic" % logbackVersion % "test",
"com.dimafeng" %% "testcontainers-scala-scalatest" % testContainersVersion % "test",
"com.dimafeng" %% "testcontainers-scala-postgresql" % testContainersVersion % "test",
"io.circe" %% "circe-generic" % circeVersion % "test",
"co.fs2" %% "fs2-io" % fs2Version % "test",
"io.chrisdavenport" %% "whale-tail-manager" % whaleTailVersion % "test",
"com.github.jnr" % "jnr-unixsocket" % jnrUnixsocketVersion % "test"
)
)

Expand All @@ -150,9 +155,10 @@ lazy val doobie = project
Test / fork := true,
Test / parallelExecution := false,
libraryDependencies ++= Seq(
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres-circe" % doobieVersion,
"org.typelevel" %% "log4cats-core" % log4catsVersion
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres-circe" % doobieVersion,
"org.typelevel" %% "log4cats-core" % log4catsVersion,
"ch.qos.logback" % "logback-classic" % logbackVersion % "test"
)
)

Expand All @@ -167,8 +173,8 @@ lazy val skunk = project
Test / fork := true,
Test / parallelExecution := false,
libraryDependencies ++= Seq(
"org.tpolecat" %% "skunk-core" % skunkVersion,
"org.tpolecat" %% "skunk-circe" % skunkVersion,
"org.tpolecat" %% "skunk-core" % skunkVersion,
"org.tpolecat" %% "skunk-circe" % skunkVersion,
)
)

Expand All @@ -195,17 +201,18 @@ lazy val demo = project
.settings(
name := "gsp-graphql-demo",
libraryDependencies ++= Seq(
"org.typelevel" %% "log4cats-slf4j" % log4catsVersion,
"ch.qos.logback" % "logback-classic" % logbackVersion,
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres" % doobieVersion,
"org.tpolecat" %% "doobie-hikari" % doobieVersion,
"org.http4s" %% "http4s-blaze-server" % http4sBlazeVersion,
"org.http4s" %% "http4s-blaze-client" % http4sBlazeVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"com.dimafeng" %% "testcontainers-scala-postgresql" % testContainersVersion,
"org.flywaydb" % "flyway-core" % flywayVersion
"org.typelevel" %% "log4cats-slf4j" % log4catsVersion,
"ch.qos.logback" % "logback-classic" % logbackVersion,
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres" % doobieVersion,
"org.tpolecat" %% "doobie-hikari" % doobieVersion,
"org.http4s" %% "http4s-blaze-server" % http4sBlazeVersion,
"org.http4s" %% "http4s-blaze-client" % http4sBlazeVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.flywaydb" % "flyway-core" % flywayVersion,
"io.chrisdavenport" %% "whale-tail-manager" % whaleTailVersion,
"com.github.jnr" % "jnr-unixsocket" % jnrUnixsocketVersion
)
)

Expand Down
99 changes: 63 additions & 36 deletions demo/src/main/scala/demo/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@

package demo

import java.util.concurrent.Executors

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

import cats.effect.{ExitCode, IO, IOApp, Resource}
import cats.implicits._
import com.dimafeng.testcontainers.PostgreSQLContainer
import demo.starwars.{StarWarsData, StarWarsMapping}
import demo.world.WorldMapping
import doobie.hikari.HikariTransactor
import io.chrisdavenport.whaletail.Docker
import io.chrisdavenport.whaletail.manager._
import org.flywaydb.core.Flyway
import org.testcontainers.containers.{PostgreSQLContainer => JavaPostgreSQLContainer}
import org.testcontainers.utility.DockerImageName

import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext

// #main
object Main extends IOApp {

def run(args: List[String]): IO[ExitCode] = {
container.use { container =>
container.use { connInfo =>
for {
_ <- dbMigration(container)
_ <- transactor(container).use { xa =>
_ <- dbMigration(connInfo)
_ <- transactor(connInfo).use { xa =>
val worldGraphQLRoutes = GraphQLService.routes(
"world",
GraphQLService.fromMapping(WorldMapping.mkMappingFromTransactor(xa))
Expand All @@ -38,38 +39,64 @@ object Main extends IOApp {
}.as(ExitCode.Success)
}

private val dbName = "test"
private val dbUser = "test"
private val dbPassword = "test"
private val container: Resource[IO, PostgreSQLContainer] = Resource.make(
IO {
val containerDef = PostgreSQLContainer.Def(
DockerImageName.parse(s"${JavaPostgreSQLContainer.IMAGE}:14.5"),
databaseName = dbName,
username = dbUser,
password = dbPassword
)
containerDef.start()
}
)(c => IO(c.stop()))
case class PostgresConnectionInfo(host: String, port: Int) {
val driverClassName = "org.postgresql.Driver"
val databaseName = "test"
val jdbcUrl = s"jdbc:postgresql://$host:$port/$databaseName"
val username = "test"
val password = "test"
}
object PostgresConnectionInfo {
val DefaultPort = 5432
}

private def dbUri(container: PostgreSQLContainer) =
s"jdbc:postgresql://${container.host}:${container.mappedPort(JavaPostgreSQLContainer.POSTGRESQL_PORT)}/$dbName"
val container: Resource[IO, PostgresConnectionInfo] = Docker.default[IO].flatMap(client =>
WhaleTailContainer.build(
client,
image = "postgres",
tag = "11.8".some,
ports = Map(PostgresConnectionInfo.DefaultPort -> None),
env = Map(
"POSTGRES_USER" -> "test",
"POSTGRES_PASSWORD" -> "test",
"POSTGRES_DB" -> "test"
),
labels = Map.empty
).evalTap(
ReadinessStrategy.checkReadiness(
client,
_,
ReadinessStrategy.LogRegex(".*database system is ready to accept connections.*".r, 2),
30.seconds
)
)
).flatMap(container =>
Resource.eval(
container.ports.get(PostgresConnectionInfo.DefaultPort).liftTo[IO](new Throwable("Missing Port"))
)
).map {
case (host, port) => PostgresConnectionInfo(host, port)
}

private def transactor(container: PostgreSQLContainer): Resource[IO, HikariTransactor[IO]] =
def transactor(connInfo: PostgresConnectionInfo): Resource[IO, HikariTransactor[IO]] = {
import connInfo._
HikariTransactor.newHikariTransactor[IO](
"org.postgresql.Driver",
dbUri(container),
dbUser,
dbPassword,
driverClassName,
jdbcUrl,
username,
password,
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(4))
)
}

private def dbMigration(container: PostgreSQLContainer): IO[Unit] = IO {
val flyway = Flyway
.configure()
.dataSource(dbUri(container), dbUser, dbPassword)
flyway.load().migrate()
}.void
def dbMigration(connInfo: PostgresConnectionInfo): IO[Unit] = {
import connInfo._
IO {
val flyway = Flyway
.configure()
.dataSource(jdbcUrl, username, password)
flyway.load().migrate()
}.void
}
}
// #main
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package edu.gemini.grackle
package circetests

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.tests.CatsSuite
import fs2.concurrent.SignallingRef
import io.circe.Json
import munit.CatsEffectSuite

import edu.gemini.grackle.syntax._

final class CirceEffectSpec extends CatsSuite {
final class CirceEffectSuite extends CatsEffectSuite {
test("circe effect (nested)") {
val query = """
query {
Expand Down Expand Up @@ -42,12 +41,7 @@ final class CirceEffectSpec extends CatsSuite {
eff <- ref.get
} yield (res, eff)

val (res, eff) = prg.unsafeRunSync()
//println(res)
//println(eff)

assert(res == expected)
assert(eff == 1)
assertIO(prg, (expected, 1))
}

test("circe effect (nested, aliased)") {
Expand Down Expand Up @@ -79,12 +73,7 @@ final class CirceEffectSpec extends CatsSuite {
eff <- ref.get
} yield (res, eff)

val (res, eff) = prg.unsafeRunSync()
//println(res)
//println(eff)

assert(res == expected)
assert(eff == 1)
assertIO(prg, (expected, 1))
}

test("circe effect (rooted)") {
Expand Down Expand Up @@ -116,12 +105,7 @@ final class CirceEffectSpec extends CatsSuite {
eff <- ref.get
} yield (res, eff)

val (res, eff) = prg.unsafeRunSync()
//println(res)
//println(eff)

assert(res == expected)
assert(eff == 1)
assertIO(prg, (expected, 1))
}

test("circe effect (rooted, aliased)") {
Expand Down Expand Up @@ -153,12 +137,7 @@ final class CirceEffectSpec extends CatsSuite {
eff <- ref.get
} yield (res, eff)

val (res, eff) = prg.unsafeRunSync()
//println(res)
//println(eff)

assert(res == expected)
assert(eff == 1)
assertIO(prg, (expected, 1))
}

test("circe effect (multiple)") {
Expand Down Expand Up @@ -206,11 +185,6 @@ final class CirceEffectSpec extends CatsSuite {
eff <- ref.get
} yield (res, eff)

val (res, eff) = prg.unsafeRunSync()
//println(res)
//println(eff)

assert(res == expected)
assert(eff == 3)
assertIO(prg, (expected, 3))
}
}
Loading

0 comments on commit b0d5096

Please sign in to comment.