From 3be8bb0cc58854b8c0aeb1df6f8c5cbd4584ad81 Mon Sep 17 00:00:00 2001 From: Florian Emile Date: Wed, 27 Nov 2024 18:58:59 +0000 Subject: [PATCH 1/6] Use cats effect assertEquals for munit --- .../src/test/scala/doobie/issue/262.scala | 12 +- .../scala/doobie/util/CatchSqlSuite.scala | 103 +++++++--------- .../scala/doobie/util/ConnectionIOSuite.scala | 7 +- .../scala/doobie/util/FragmentSuite.scala | 17 ++- .../scala/doobie/util/FragmentsSuite.scala | 6 +- .../src/test/scala/doobie/util/GetSuite.scala | 30 ++--- .../test/scala/doobie/util/ProcessSuite.scala | 12 +- .../test/scala/doobie/util/QuerySuite.scala | 68 +++++------ .../test/scala/doobie/util/ReadSuite.scala | 30 ++--- .../scala/doobie/util/StrategySuite.scala | 115 ++++++++---------- .../scala/doobie/util/TransactorSuite.scala | 22 ++-- .../test/scala/doobie/util/WriteSuite.scala | 14 +-- .../scala/doobie/util/meta/MetaSuite.scala | 9 +- .../h2/src/test/scala/doobie/h2/h2types.scala | 56 +++++---- 14 files changed, 230 insertions(+), 271 deletions(-) diff --git a/modules/core/src/test/scala/doobie/issue/262.scala b/modules/core/src/test/scala/doobie/issue/262.scala index 579cac1ab..fd7855cd9 100644 --- a/modules/core/src/test/scala/doobie/issue/262.scala +++ b/modules/core/src/test/scala/doobie/issue/262.scala @@ -5,12 +5,12 @@ package doobie.issue import cats.effect.IO -import doobie.*, doobie.implicits.* - -class `262` extends munit.FunSuite { - - import cats.effect.unsafe.implicits.global +import doobie.* +import doobie.implicits.* +import munit.CatsEffectSuite +class `262` extends CatsEffectSuite { + // an interpreter that returns null when we ask for statement metadata object Interp extends KleisliInterpreter[IO](LogHandler.noop) { override lazy val PreparedStatementInterpreter: PreparedStatementInterpreter = @@ -34,7 +34,7 @@ class `262` extends munit.FunSuite { test("getColumnJdbcMeta should handle null metadata") { val prog = HC.prepareStatementPrimitive("select 1")(HPS.getColumnJdbcMeta) - assertEquals(prog.transact(xa).unsafeRunSync(), Nil) + prog.transact(xa).assertEquals(Nil) } } diff --git a/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala b/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala index 55c8f6a58..7b3ec023c 100644 --- a/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala +++ b/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala @@ -4,151 +4,130 @@ package doobie.util -import cats.effect.{IO} -import doobie.*, doobie.implicits.* -import java.sql.SQLException +import cats.effect.IO +import doobie.* +import doobie.implicits.* +import munit.CatsEffectSuite -class CatchSqlSuite extends munit.FunSuite { +import java.sql.SQLException - import cats.effect.unsafe.implicits.global +class CatchSqlSuite extends CatsEffectSuite { val SQLSTATE_FOO = SqlState("Foo") val SQLSTATE_BAR = SqlState("Bar") test("attemptSql should do nothing on success") { - assertEquals(IO.delay(3).attemptSql.unsafeRunSync(), Right(3)) + IO.delay(3).attemptSql.assertEquals(Right(3)) } test("attemptSql should catch SQLException") { val e = new SQLException - assertEquals(IO.raiseError(e).attemptSql.unsafeRunSync(), Left(e)) + IO.raiseError(e).attemptSql.assertEquals(Left(e)) } test("attemptSql should ignore non-SQLException") { val e = new IllegalArgumentException - intercept[IllegalArgumentException] { - IO.raiseError(e).attemptSql.unsafeRunSync() - } + IO.raiseError(e).attemptSql.intercept[IllegalArgumentException] } test("attemptSqlState shuold do nothing on success") { - assertEquals(IO.delay(3).attemptSqlState.unsafeRunSync(), Right(3)) + IO.delay(3).attemptSqlState.assertEquals(Right(3)) } test("attemptSqlState shuold catch SQLException") { val e = new SQLException("", SQLSTATE_FOO.value) - assertEquals(IO.raiseError(e).attemptSqlState.unsafeRunSync(), Left(SQLSTATE_FOO)) + IO.raiseError(e).attemptSqlState.assertEquals(Left(SQLSTATE_FOO)) } test("attemptSqlState shuold ignore non-SQLException") { val e = new IllegalArgumentException - intercept[IllegalArgumentException] { - IO.raiseError(e).attemptSqlState.unsafeRunSync() - } + IO.raiseError(e).attemptSqlState.intercept[IllegalArgumentException] } test("attemptSomeSqlState should do nothing on success") { - assertEquals( - IO.delay(3).attemptSomeSqlState { - case SQLSTATE_FOO => 42 - case SQLSTATE_BAR => 66 - }.unsafeRunSync(), - Right(3)) + IO.delay(3).attemptSomeSqlState { + case SQLSTATE_FOO => 42 + case SQLSTATE_BAR => 66 + }.assertEquals(Right(3)) } test("attemptSomeSqlState should catch SQLException with matching state (1)") { val e = new SQLException("", SQLSTATE_FOO.value) - assertEquals( - IO.raiseError(e).attemptSomeSqlState { - case SQLSTATE_FOO => 42 - case SQLSTATE_BAR => 66 - }.unsafeRunSync(), - Left(42)) + IO.raiseError(e).attemptSomeSqlState { + case SQLSTATE_FOO => 42 + case SQLSTATE_BAR => 66 + }.assertEquals(Left(42)) } test("attemptSomeSqlState should catch SQLException with matching state (2)") { val e = new SQLException("", SQLSTATE_BAR.value) - assertEquals( - IO.raiseError(e).attemptSomeSqlState { - case SQLSTATE_FOO => 42 - case SQLSTATE_BAR => 66 - }.unsafeRunSync(), - Left(66)) + IO.raiseError(e).attemptSomeSqlState { + case SQLSTATE_FOO => 42 + case SQLSTATE_BAR => 66 + }.assertEquals(Left(66)) } test("attemptSomeSqlState should ignore SQLException with non-matching state") { val e = new SQLException("", SQLSTATE_BAR.value) - intercept[SQLException] { - IO.raiseError(e).attemptSomeSqlState { - case SQLSTATE_FOO => 42 - }.unsafeRunSync() - } + IO.raiseError(e).attemptSomeSqlState { + case SQLSTATE_FOO => 42 + }.intercept[SQLException] } test("attemptSomeSqlState should ignore non-SQLException") { val e = new IllegalArgumentException - intercept[IllegalArgumentException] { - IO.raiseError(e).attemptSomeSqlState { - case SQLSTATE_FOO => 42 - }.unsafeRunSync() - } + IO.raiseError(e).attemptSomeSqlState { + case SQLSTATE_FOO => 42 + }.intercept[IllegalArgumentException] } lazy val rescue = IO.delay(4) test("exceptSql should do nothing on success") { - assertEquals(IO.delay(3).exceptSql(_ => rescue).unsafeRunSync(), 3) + IO.delay(3).exceptSql(_ => rescue).assertEquals(3) } test("exceptSql should catch SQLException") { val e = new SQLException("", SQLSTATE_FOO.value) - assertEquals(IO.raiseError[Int](e).exceptSql(_ => rescue).unsafeRunSync(), 4) + IO.raiseError[Int](e).exceptSql(_ => rescue).assertEquals(4) } test("exceptSql should ignore non-SQLException") { val e = new IllegalArgumentException - intercept[IllegalArgumentException] { - IO.raiseError[Int](e).exceptSql(_ => rescue).unsafeRunSync() - } + IO.raiseError[Int](e).exceptSql(_ => rescue).intercept[IllegalArgumentException] } test("exceptSqlState should do nothing on success") { - assertEquals(IO.delay(3).exceptSqlState(_ => rescue).unsafeRunSync(), 3) + IO.delay(3).exceptSqlState(_ => rescue).assertEquals(3) } test("exceptSqlState should catch SQLException") { val e = new SQLException("", SQLSTATE_FOO.value) - assertEquals(IO.raiseError[Int](e).exceptSqlState(_ => rescue).unsafeRunSync(), 4) + IO.raiseError[Int](e).exceptSqlState(_ => rescue).assertEquals(4) } test("exceptSqlState should ignore non-SQLException") { val e = new IllegalArgumentException - intercept[IllegalArgumentException] { - IO.raiseError[Int](e).exceptSqlState(_ => rescue).unsafeRunSync() - } + IO.raiseError[Int](e).exceptSqlState(_ => rescue).intercept[IllegalArgumentException] } test("exceptSomeSqlState should do nothing on success") { - assertEquals(IO.delay(3).exceptSomeSqlState { case _ => rescue }.unsafeRunSync(), 3) + IO.delay(3).exceptSomeSqlState { case _ => rescue }.assertEquals(3) } test("exceptSomeSqlState should catch SQLException with some state") { val e = new SQLException("", SQLSTATE_FOO.value) - assertEquals(IO.raiseError[Int](e).exceptSomeSqlState { case SQLSTATE_FOO => rescue }.unsafeRunSync(), 4) + IO.raiseError[Int](e).exceptSomeSqlState { case SQLSTATE_FOO => rescue }.assertEquals(4) } test("exceptSomeSqlState should ignore SQLException with other state") { val e = new SQLException("", SQLSTATE_FOO.value) - intercept[SQLException] { - IO.raiseError[Int](e).exceptSomeSqlState { case SQLSTATE_BAR => rescue }.unsafeRunSync() - } + IO.raiseError[Int](e).exceptSomeSqlState { case SQLSTATE_BAR => rescue }.intercept[SQLException] } test("exceptSomeSqlState should ignore non-SQLException") { val e = new IllegalArgumentException - intercept[IllegalArgumentException] { - IO.raiseError[Int](e).exceptSomeSqlState { case _ => rescue }.unsafeRunSync() - } + IO.raiseError[Int](e).exceptSomeSqlState { case _ => rescue }.intercept[IllegalArgumentException] } test("onSqlException should do nothing on success") { @@ -167,7 +146,7 @@ class CatchSqlSuite extends munit.FunSuite { test("onSqlException should ignore its effect on non-SQLException") { var a = 1 val e = new IllegalArgumentException - assertEquals(IO.raiseError[Int](e).onSqlException(IO.delay(a += 1)).attempt.unsafeRunSync(), Left(e)) + IO.raiseError[Int](e).onSqlException(IO.delay(a += 1)).attempt.assertEquals(Left(e)) assertEquals(a, 1) } diff --git a/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala b/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala index a824f38e4..368d855c4 100644 --- a/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala @@ -8,10 +8,11 @@ import cats.Applicative import cats.implicits.* import cats.effect.IO import cats.kernel.Monoid +import munit.* import doobie.* import doobie.implicits.* -class ConnectionIOSuite extends munit.FunSuite { +class ConnectionIOSuite extends munit.CatsEffectSuite { import cats.effect.unsafe.implicits.global @@ -25,11 +26,11 @@ class ConnectionIOSuite extends munit.FunSuite { test("Semigroup ConnectionIO") { val prg = Applicative[ConnectionIO].pure(List(1, 2, 3)) `combine` Applicative[ConnectionIO].pure(List(4, 5, 6)) - assertEquals(prg.transact(xa).unsafeRunSync(), List(1, 2, 3, 4, 5, 6)) + prg.transact(xa).assertEquals(List(1, 2, 3, 4, 5, 6)) } test("Monoid ConnectionIO") { - assertEquals(Monoid[ConnectionIO[List[Int]]].empty.transact(xa).unsafeRunSync(), Nil) + Monoid[ConnectionIO[List[Int]]].empty.transact(xa) assertEquals (Nil) } } diff --git a/modules/core/src/test/scala/doobie/util/FragmentSuite.scala b/modules/core/src/test/scala/doobie/util/FragmentSuite.scala index 1a71f3621..deba20877 100644 --- a/modules/core/src/test/scala/doobie/util/FragmentSuite.scala +++ b/modules/core/src/test/scala/doobie/util/FragmentSuite.scala @@ -9,10 +9,9 @@ import cats.effect.IO import doobie.* import doobie.implicits.* import doobie.testutils.VoidExtensions +import munit.CatsEffectSuite -class FragmentSuite extends munit.FunSuite { - - import cats.effect.unsafe.implicits.global +class FragmentSuite extends CatsEffectSuite { val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", @@ -51,22 +50,22 @@ class FragmentSuite extends munit.FunSuite { test("Fragment must maintain parameter indexing (in-order)") { val s = fr"select" ++ List(fra, frb, frc).intercalate(fr",") - assertEquals(s.query[(Int, String, Boolean)].unique.transact(xa).unsafeRunSync(), ((a, b, c))) + s.query[(Int, String, Boolean)].unique.transact(xa).assertEquals((a, b, c)) } test("Fragment must maintain parameter indexing (out-of-order)") { val s = fr"select" ++ List(frb, frc, fra).intercalate(fr",") - assertEquals(s.query[(String, Boolean, Int)].unique.transact(xa).unsafeRunSync(), ((b, c, a))) + s.query[(String, Boolean, Int)].unique.transact(xa).assertEquals((b, c, a)) } test("Fragment must maintain associativity (left)") { val s = fr"select" ++ List(fra, fr",", frb, fr",", frc).foldLeft(Fragment.empty)(_ ++ _) - assertEquals(s.query[(Int, String, Boolean)].unique.transact(xa).unsafeRunSync(), ((a, b, c))) + s.query[(Int, String, Boolean)].unique.transact(xa).assertEquals((a, b, c)) } test("Fragment must maintain associativity (right)") { val s = fr"select" ++ List(fra, fr",", frb, fr",", frc).foldRight(Fragment.empty)(_ ++ _) - assertEquals(s.query[(Int, String, Boolean)].unique.transact(xa).unsafeRunSync(), ((a, b, c))) + s.query[(Int, String, Boolean)].unique.transact(xa).assertEquals((a, b, c)) } test("Fragment must Add a trailing space when constructed with .const") { @@ -112,7 +111,7 @@ class FragmentSuite extends munit.FunSuite { fr0"SELECT 1 WHERE 1 IN (" ++ List.fill(STACK_UNSAFE_SIZE)(1).foldLeft(Fragment.empty)((f, n) => f ++ fr"$n,") ++ fr0"1)" - assertEquals(frag.query[Int].unique.transact(xa).unsafeRunSync(), 1) + frag.query[Int].unique.transact(xa).assertEquals(1) } test("Fragment must be stacksafe (right-associative)") { @@ -120,7 +119,7 @@ class FragmentSuite extends munit.FunSuite { fr0"SELECT 1 WHERE 1 IN (" ++ List.fill(STACK_UNSAFE_SIZE)(1).foldRight(Fragment.empty)((n, f) => f ++ fr"$n,") ++ fr0"1)" - assertEquals(frag.query[Int].unique.transact(xa).unsafeRunSync(), 1) + frag.query[Int].unique.transact(xa).assertEquals(1) } } diff --git a/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala b/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala index e3f2626b6..325e9d59d 100644 --- a/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala +++ b/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala @@ -9,7 +9,7 @@ import cats.effect.IO import doobie.* import doobie.implicits.* -class FragmentsSuite extends munit.FunSuite { +class FragmentsSuite extends munit.CatsEffectSuite { import Fragments.* import cats.effect.unsafe.implicits.global @@ -389,13 +389,13 @@ class FragmentsSuite extends munit.FunSuite { test("values (1)") { val c = Contact(Person("Bob", 42), Some("addr")) val f = sql"select" ++ Fragments.values(c) - assertEquals(f.query[Contact].unique.transact(xa).unsafeRunSync(), c) + f.query[Contact].unique.transact(xa)assertEquals(c) } test("values (2)") { val c = Contact(Person("Bob", 42), None) val f = sql"select" ++ Fragments.values(c) - assertEquals(f.query[Contact].unique.transact(xa).unsafeRunSync(), c) + f.query[Contact].unique.transact(xa).assertEquals( c) } } diff --git a/modules/core/src/test/scala/doobie/util/GetSuite.scala b/modules/core/src/test/scala/doobie/util/GetSuite.scala index fa9b74b2e..1f7248a70 100644 --- a/modules/core/src/test/scala/doobie/util/GetSuite.scala +++ b/modules/core/src/test/scala/doobie/util/GetSuite.scala @@ -50,7 +50,7 @@ class GetSuite extends munit.FunSuite with GetSuitePlatform { final case class Foo(s: String) final case class Bar(n: Int) -class GetDBSuite extends munit.FunSuite { +class GetDBSuite extends munit.CatsEffectSuite { import cats.effect.unsafe.implicits.global import doobie.syntax.all.* @@ -68,38 +68,38 @@ class GetDBSuite extends munit.FunSuite { implicit def barMeta: Get[Bar] = Get[Int].temap(n => if (n == 0) Left("cannot be 0") else Right(Bar(n))) test("Get should not allow map to observe null on the read side (AnyRef)") { - val x = sql"select null".query[Option[Foo]].unique.transact(xa).unsafeRunSync() - assertEquals(x, None) + val x = sql"select null".query[Option[Foo]].unique.transact(xa) + x.assertEquals(None) } test("Get should read non-null value (AnyRef)") { - val x = sql"select 'abc'".query[Foo].unique.transact(xa).unsafeRunSync() - assertEquals(x, Foo("ABC")) + val x = sql"select 'abc'".query[Foo].unique.transact(xa) + x.assertEquals(Foo("ABC")) } test("Get should error when reading a NULL into an unlifted Scala type (AnyRef)") { - def x = sql"select null".query[Foo].unique.transact(xa).attempt.unsafeRunSync() - assertEquals(x, Left(doobie.util.invariant.NonNullableColumnRead(1, JdbcType.Char))) + def x = sql"select null".query[Foo].unique.transact(xa).attempt + x.assertEquals(Left(doobie.util.invariant.NonNullableColumnRead(1, JdbcType.Char))) } test("Get should not allow map to observe null on the read side (AnyVal)") { - val x = sql"select null".query[Option[Bar]].unique.transact(xa).unsafeRunSync() - assertEquals(x, None) + val x = sql"select null".query[Option[Bar]].unique.transact(xa) + x.assertEquals(None) } test("Get should read non-null value (AnyVal)") { - val x = sql"select 1".query[Bar].unique.transact(xa).unsafeRunSync() - assertEquals(x, Bar(1)) + val x = sql"select 1".query[Bar].unique.transact(xa) + x.assertEquals(Bar(1)) } test("Get should error when reading a NULL into an unlifted Scala type (AnyVal)") { - def x = sql"select null".query[Bar].unique.transact(xa).attempt.unsafeRunSync() - assertEquals(x, Left(doobie.util.invariant.NonNullableColumnRead(1, JdbcType.Integer))) + def x = sql"select null".query[Bar].unique.transact(xa).attempt + x.assertEquals(Left(doobie.util.invariant.NonNullableColumnRead(1, JdbcType.Integer))) } test("Get should error when reading an incorrect value") { - def x = sql"select 0".query[Bar].unique.transact(xa).attempt.unsafeRunSync() - assertEquals(x, Left(doobie.util.invariant.InvalidValue[Int, Bar](0, "cannot be 0"))) + def x = sql"select 0".query[Bar].unique.transact(xa).attempt + x.assertEquals(Left(doobie.util.invariant.InvalidValue[Int, Bar](0, "cannot be 0"))) } } diff --git a/modules/core/src/test/scala/doobie/util/ProcessSuite.scala b/modules/core/src/test/scala/doobie/util/ProcessSuite.scala index a750afae4..06c14a45b 100644 --- a/modules/core/src/test/scala/doobie/util/ProcessSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ProcessSuite.scala @@ -7,16 +7,17 @@ package util import cats.effect.IO import doobie.util.stream.repeatEvalChunks +import munit.CatsEffectAssertions.MUnitCatsAssertionsForIOOps import org.scalacheck.Prop.forAll +import org.scalacheck.effect.PropF + import scala.Predef.* import scala.util.Random class ProcessSuite extends munit.ScalaCheckSuite { - - import cats.effect.unsafe.implicits.global - + test("repeatEvalChunks must yield the same result irrespective of chunk size") { - forAll { (n0: Int) => + PropF.forAllF { (n0: Int) => val dataSize = 1000 val chunkSize = (n0 % dataSize).abs max 1 val data = Seq.fill(dataSize)(Random.nextInt()) @@ -28,8 +29,7 @@ class ProcessSuite extends munit.ScalaCheckSuite { h } } - val result = repeatEvalChunks(fa).compile.toVector.unsafeRunSync() - assertEquals(result, data.toVector) + repeatEvalChunks(fa).compile.toVector.assertEquals(data.toVector) } } diff --git a/modules/core/src/test/scala/doobie/util/QuerySuite.scala b/modules/core/src/test/scala/doobie/util/QuerySuite.scala index 242792b5c..47f4c1668 100644 --- a/modules/core/src/test/scala/doobie/util/QuerySuite.scala +++ b/modules/core/src/test/scala/doobie/util/QuerySuite.scala @@ -8,7 +8,7 @@ import cats.effect.IO import doobie.*, doobie.implicits.* import scala.Predef.* -class QuerySuite extends munit.FunSuite { +class QuerySuite extends munit.CatsEffectSuite { import cats.effect.unsafe.implicits.global @@ -24,117 +24,117 @@ class QuerySuite extends munit.FunSuite { val pairQuery = Query[String, (String, Int)]("select 'xxx', 123 where ? = 'foo'", None) test("Query (non-empty) to") { - assertEquals(q.to[List]("foo").transact(xa).unsafeRunSync(), List(123)) + q.to[List]("foo").transact(xa)assertEquals( List(123)) } test("Query (non-empty) toMap") { - assertEquals(pairQuery.toMap[String, Int]("foo").transact(xa).unsafeRunSync(), Map("xxx" -> 123)) + pairQuery.toMap[String, Int]("foo").transact(xa)assertEquals(Map("xxx" -> 123)) } test("Query (non-empty) unique") { - assertEquals(q.unique("foo").transact(xa).unsafeRunSync(), 123) + q.unique("foo").transact(xa).assertEquals( 123) } test("Query (non-empty) option") { - assertEquals(q.option("foo").transact(xa).unsafeRunSync(), Some(123)) + q.option("foo").transact(xa)assertEquals( Some(123)) } test("Query (non-empty) map") { - assertEquals(q.map("x" * _).to[List]("foo").transact(xa).unsafeRunSync(), List("x" * 123)) + q.map("x" * _).to[List]("foo").transact(xa)assertEquals(List("x" * 123)) } test("Query (non-empty) contramap") { - assertEquals(q.contramap[Int](n => "foo" * n).to[List](1).transact(xa).unsafeRunSync(), List(123)) + q.contramap[Int](n => "foo" * n).to[List](1).transact(xa).assertEquals(List(123)) } test("Query (empty) to") { - assertEquals(q.to[List]("bar").transact(xa).unsafeRunSync(), Nil) + q.to[List]("bar").transact(xa).assertEquals( Nil) } test("Query (empty) toMap") { - assertEquals(pairQuery.toMap[String, Int]("bar").transact(xa).unsafeRunSync(), Map.empty[String, Int]) + pairQuery.toMap[String, Int]("bar").transact(xa).assertEquals(Map.empty[String, Int]) } test("Query (empty) unique") { - assertEquals(q.unique("bar").transact(xa).attempt.unsafeRunSync(), Left(invariant.UnexpectedEnd)) + q.unique("bar").transact(xa).attempt.assertEquals(Left(invariant.UnexpectedEnd)) } test("Query (empty) option") { - assertEquals(q.option("bar").transact(xa).unsafeRunSync(), None) + q.option("bar").transact(xa).assertEquals(None) } test("Query (empty) map") { - assertEquals(q.map("x" * _).to[List]("bar").transact(xa).unsafeRunSync(), Nil) + q.map("x" * _).to[List]("bar").transact(xa).assertEquals( Nil) } test("Query (empty) contramap") { - assertEquals(q.contramap[Int](n => "bar" * n).to[List](1).transact(xa).unsafeRunSync(), Nil) + q.contramap[Int](n => "bar" * n).to[List](1).transact(xa).assertEquals(Nil) } test("Query0 from Query (non-empty) to") { - assertEquals(q.toQuery0("foo").to[List].transact(xa).unsafeRunSync(), List(123)) + q.toQuery0("foo").to[List].transact(xa).assertEquals( List(123)) } test("Query0 from Query (non-empty) toMap") { - assertEquals(pairQuery.toQuery0("foo").toMap[String, Int].transact(xa).unsafeRunSync(), Map("xxx" -> 123)) + pairQuery.toQuery0("foo").toMap[String, Int].transact(xa).assertEquals(Map("xxx" -> 123)) } test("Query0 from Query (non-empty) unique") { - assertEquals(q.toQuery0("foo").unique.transact(xa).unsafeRunSync(), 123) + q.toQuery0("foo").unique.transact(xa).assertEquals( 123) } test("Query0 from Query (non-empty) option") { - assertEquals(q.toQuery0("foo").option.transact(xa).unsafeRunSync(), Some(123)) + q.toQuery0("foo").option.transact(xa).assertEquals( Some(123)) } test("Query0 from Query (non-empty) map") { - assertEquals(q.toQuery0("foo").map(_ * 2).to[List].transact(xa).unsafeRunSync(), List(246)) + q.toQuery0("foo").map(_ * 2).to[List].transact(xa).assertEquals( List(246)) } test("Query0 from Query (empty) to") { - assertEquals(q.toQuery0("bar").to[List].transact(xa).unsafeRunSync(), Nil) + q.toQuery0("bar").to[List].transact(xa).assertEquals( Nil) } test("Query0 from Query (empty) toMap") { - assertEquals(pairQuery.toQuery0("bar").toMap[String, Int].transact(xa).unsafeRunSync(), Map.empty[String, Int]) + pairQuery.toQuery0("bar").toMap[String, Int].transact(xa).assertEquals( Map.empty[String, Int]) } test("Query0 from Query (empty) unique") { - assertEquals(q.toQuery0("bar").unique.transact(xa).attempt.unsafeRunSync(), Left(invariant.UnexpectedEnd)) + q.toQuery0("bar").unique.transact(xa).attempt.assertEquals( Left(invariant.UnexpectedEnd)) } test("Query0 from Query (empty) option") { - assertEquals(q.toQuery0("bar").option.transact(xa).unsafeRunSync(), None) + q.toQuery0("bar").option.transact(xa).assertEquals( None) } test("Query0 from Query (empty) map") { - assertEquals(q.toQuery0("bar").map(_ * 2).to[List].transact(xa).unsafeRunSync(), Nil) + q.toQuery0("bar").map(_ * 2).to[List].transact(xa).assertEquals( Nil) } val q0n = Query0[Int]("select 123 where 'foo' = 'foo'", None) val pairQ0n = Query0[(String, Int)]("select 'xxx', 123 where 'foo' = 'foo'", None) test("Query0 via constructor (non-empty) to") { - assertEquals(q0n.to[List].transact(xa).unsafeRunSync(), List(123)) + q0n.to[List].transact(xa).assertEquals( List(123)) } test("Query0 via constructor (non-empty) toMap") { - assertEquals(pairQ0n.toMap[String, Int].transact(xa).unsafeRunSync(), Map("xxx" -> 123)) + pairQ0n.toMap[String, Int].transact(xa).assertEquals(Map("xxx" -> 123)) } test("Query0 via constructor (non-empty) unique") { - assertEquals(q0n.unique.transact(xa).unsafeRunSync(), 123) + q0n.unique.transact(xa).assertEquals( 123) } test("Query0 via constructor (non-empty) option") { - assertEquals(q0n.option.transact(xa).unsafeRunSync(), Some(123)) + q0n.option.transact(xa).assertEquals(Some(123)) } test("Query0 via constructor (non-empty) map") { - assertEquals(q0n.map(_ * 2).to[List].transact(xa).unsafeRunSync(), List(246)) + q0n.map(_ * 2).to[List].transact(xa).assertEquals(List(246)) } val q0e = Query0[Int]("select 123 where 'bar' = 'foo'", None) val pairQ0e = Query0[(String, Int)]("select 'xxx', 123 where 'bar' = 'foo'", None) test("Query0 via constructor (empty) to") { - assertEquals(q0e.to[List].transact(xa).unsafeRunSync(), Nil) + q0e.to[List].transact(xa).assertEquals( Nil) } test("Query0 via constructor (empty) toMap") { - assertEquals(pairQ0e.toMap[String, Int].transact(xa).unsafeRunSync(), Map.empty[String, Int]) + pairQ0e.toMap[String, Int].transact(xa).assertEquals( Map.empty[String, Int]) } test("Query0 via constructor (empty) unique") { - assertEquals(q0e.unique.transact(xa).attempt.unsafeRunSync(), Left(invariant.UnexpectedEnd)) + q0e.unique.transact(xa).attempt.assertEquals(Left(invariant.UnexpectedEnd)) } test("Query0 via constructor (empty) option") { - assertEquals(q0e.option.transact(xa).unsafeRunSync(), None) + q0e.option.transact(xa).assertEquals( None) } test("Query0 via constructor (empty) map") { - assertEquals(q0e.map(_ * 2).to[List].transact(xa).unsafeRunSync(), Nil) + q0e.map(_ * 2).to[List].transact(xa).assertEquals(Nil) } val qf = sql"select 'foo', ${1: Int}, ${Option.empty[Int]}, ${Option(42)}".query[String] // wrong! test("Query to Fragment and back") { val qfʹ = qf.toFragment.query[(String, Int, Option[Int], Option[Int])] - assertEquals(qfʹ.unique.transact(xa).unsafeRunSync(), (("foo", 1, None, Some(42)))) + qfʹ.unique.transact(xa).assertEquals(("foo", 1, None, Some(42))) } } diff --git a/modules/core/src/test/scala/doobie/util/ReadSuite.scala b/modules/core/src/test/scala/doobie/util/ReadSuite.scala index da8fd1b6e..5b945e56b 100644 --- a/modules/core/src/test/scala/doobie/util/ReadSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ReadSuite.scala @@ -8,11 +8,10 @@ import cats.effect.IO import doobie.util.TestTypes.* import doobie.util.transactor.Transactor import doobie.testutils.VoidExtensions +import munit.CatsEffectSuite -class ReadSuite extends munit.FunSuite with ReadSuitePlatform { - - import cats.effect.unsafe.implicits.global - +class ReadSuite extends CatsEffectSuite with ReadSuitePlatform { + val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", url = "jdbc:h2:mem:;DB_CLOSE_DELAY=-1", @@ -105,7 +104,7 @@ class ReadSuite extends munit.FunSuite with ReadSuitePlatform { val p = readInt.product(readString) - assertEquals(p.gets, (readInt.gets ++ readString.gets)) + assertEquals(p.gets, readInt.gets ++ readString.gets) } /* @@ -117,15 +116,13 @@ class ReadSuite extends munit.FunSuite with ReadSuitePlatform { val frag = sql"SELECT 1, NULL, 3, NULL" val q1 = frag.query[Option[(Int, Option[Int], Int, Option[Int])]].to[List] - val o1 = q1.transact(xa).unsafeRunSync() // This result doesn't seem ideal, because we should know that Int isn't // nullable, so the correct result is Some((1, None, 3, None)) // But with how things are wired at the moment this isn't possible - assertEquals(o1, List(None)) + q1.transact(xa).assertEquals(List(None)) val q2 = frag.query[Option[(Int, Int, Int, Int)]].to[List] - val o2 = q2.transact(xa).unsafeRunSync() - assertEquals(o2, List(None)) + q2.transact(xa).assertEquals(List(None)) } test("Read should read correct columns for instances with Option (Some)") { @@ -133,12 +130,10 @@ class ReadSuite extends munit.FunSuite with ReadSuitePlatform { val frag = sql"SELECT 1, 2, 3, 4" val q1 = frag.query[Option[(Int, Option[Int], Int, Option[Int])]].to[List] - val o1 = q1.transact(xa).unsafeRunSync() - assertEquals(o1, List(Some((1, Some(2), 3, Some(4))))) + q1.transact(xa).assertEquals(List(Some((1, Some(2), 3, Some(4))))) val q2 = frag.query[Option[(Int, Int, Int, Int)]].to[List] - val o2 = q2.transact(xa).unsafeRunSync() - assertEquals(o2, List(Some((1, 2, 3, 4)))) + q2.transact(xa).assertEquals(List(Some((1, 2, 3, 4)))) } test("Read should select correct columns when combined with `ap`") { @@ -150,10 +145,7 @@ class ReadSuite extends munit.FunSuite with ReadSuitePlatform { val c = (r, r, r, r, r).tupled val q = sql"SELECT 1, 2, 3, 4, 5".query(using c).to[List] - - val o = q.transact(xa).unsafeRunSync() - - assertEquals(o, List((1, 2, 3, 4, 5))) + q.transact(xa).assertEquals(List((1, 2, 3, 4, 5))) } test("Read should select correct columns when combined with `product`") { @@ -163,9 +155,7 @@ class ReadSuite extends munit.FunSuite with ReadSuitePlatform { val r = Read[Int].product(Read[Int].product(Read[Int])) val q = sql"SELECT 1, 2, 3".query(using r).to[List] - val o = q.transact(xa).unsafeRunSync() - - assertEquals(o, List((1, (2, 3)))) + q.transact(xa).assertEquals(List((1, (2, 3)))) } } diff --git a/modules/core/src/test/scala/doobie/util/StrategySuite.scala b/modules/core/src/test/scala/doobie/util/StrategySuite.scala index abd49128b..5a75236cf 100644 --- a/modules/core/src/test/scala/doobie/util/StrategySuite.scala +++ b/modules/core/src/test/scala/doobie/util/StrategySuite.scala @@ -12,13 +12,12 @@ import doobie.* import doobie.free.{connection, preparedstatement, resultset} import doobie.implicits.* import doobie.util.transactor.Transactor.Aux +import munit.CatsEffectSuite import java.sql.{Connection, PreparedStatement, ResultSet} import java.util -class StrategySuite extends munit.FunSuite { - - import cats.effect.unsafe.implicits.global +class StrategySuite extends CatsEffectSuite { val baseXa: Aux[IO, Unit] = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", @@ -75,115 +74,109 @@ class StrategySuite extends munit.FunSuite { Transactor.interpret.set(baseXa, i.ConnectionInterpreter) test("Connection.autoCommit should be set to false") { - val i = new Interp - val _ = sql"select 1".query[Int].unique.transact(xa(i)).unsafeRunSync() - assertEquals(i.Connection.autoCommit, Some(false)) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].unique.transact(xa(i))).map(_.Connection.autoCommit) + .assertEquals(Some(false)) } test("Connection.commit should be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].unique.transact(xa(i)).unsafeRunSync() - assertEquals(i.Connection.commit, Some(())) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].unique.transact(xa(i))).map(_.Connection.commit) + .assertEquals(Some(())) } test("Connection.commit should NOT be called on failure") { - val i = new Interp - assertEquals(sql"abc".query[Int].unique.transact(xa(i)).attempt.unsafeRunSync().toOption, None) - assertEquals(i.Connection.commit, None) + IO.pure(new Interp).flatTap(i => + sql"abc".query[Int].unique.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.Connection.commit) + .assertEquals(None) } test("Connection.rollback should NOT be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].unique.transact(xa(i)).unsafeRunSync() - assertEquals(i.Connection.rollback, None) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].unique.transact(xa(i))).map(_.Connection.rollback) + .assertEquals(None) } test("Connection.rollback should be called on failure") { - val i = new Interp - assertEquals(sql"abc".query[Int].unique.transact(xa(i)).attempt.unsafeRunSync().toOption, None) - assertEquals(i.Connection.rollback, Some(())) + IO.pure(new Interp).flatTap(i => + sql"abc".query[Int].unique.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.Connection.rollback) + .assertEquals(Some(())) } test("[Streaming] Connection.autoCommit should be set to false") { - val i = new Interp - val _ = sql"select 1".query[Int].stream.compile.toList.transact(xa(i)).unsafeRunSync() - assertEquals(i.Connection.autoCommit, Some(false)) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].stream.compile.toList.transact(xa(i))).map( + _.Connection.autoCommit).assertEquals(Some(false)) } test("[Streaming] Connection.commit should be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].stream.compile.toList.transact(xa(i)).unsafeRunSync() - assertEquals(i.Connection.commit, Some(())) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].stream.compile.toList.transact(xa(i))).map( + _.Connection.commit).assertEquals(Some(())) } test("[Streaming] Connection.commit should NOT be called on failure") { - val i = new Interp - assertEquals(sql"abc".query[Int].stream.compile.toList.transact(xa(i)).attempt.unsafeRunSync().toOption, None) - assertEquals(i.Connection.commit, None) + IO.pure(new Interp).flatTap(i => + sql"abc".query[Int].stream.compile.toList.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.Connection.commit) + .assertEquals(None) } test("[Streaming] Connection.rollback should NOT be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].stream.compile.toList.transact(xa(i)).unsafeRunSync() - assertEquals(i.Connection.rollback, None) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].stream.compile.toList.transact(xa(i))).map( + _.Connection.rollback).assertEquals(None) } test("[Streaming] Connection.rollback should be called on failure") { - val i = new Interp - assertEquals(sql"abc".query[Int].stream.compile.toList.transact(xa(i)).attempt.unsafeRunSync().toOption, None) - assertEquals(i.Connection.rollback, Some(())) + IO.pure(new Interp).flatTap(i => + sql"abc".query[Int].stream.compile.toList.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.Connection.rollback) + .assertEquals(Some(())) } test("PreparedStatement.close should be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].unique.transact(xa(i)).unsafeRunSync() - assertEquals(i.PreparedStatement.close, Some(())) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].unique.transact(xa(i))).map( + _.PreparedStatement.close).assertEquals(Some(())) } test("PreparedStatement.close should be called on failure") { - val i = new Interp - assertEquals(sql"select 'x'".query[Int].unique.transact(xa(i)).attempt.unsafeRunSync().toOption, None) - assertEquals(i.PreparedStatement.close, Some(())) + IO.pure(new Interp).flatTap(i => + sql"select 'x'".query[Int].unique.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.PreparedStatement.close) + .assertEquals(Some(())) } test("[Streaming] PreparedStatement.close should be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].stream.compile.toList.transact(xa(i)).unsafeRunSync() - assertEquals(i.PreparedStatement.close, Some(())) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].stream.compile.toList.transact(xa(i))).map( + _.PreparedStatement.close).assertEquals(Some(())) } test("[Streaming] PreparedStatement.close should be called on failure") { - val i = new Interp - assertEquals( - sql"select 'x'".query[Int].stream.compile.toList.transact(xa(i)).attempt.unsafeRunSync().toOption, - None) - assertEquals(i.PreparedStatement.close, Some(())) + IO.pure(new Interp).flatTap(i => + sql"select 'x'".query[Int].stream.compile.toList.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.PreparedStatement.close) + .assertEquals(Some(())) } test("ResultSet.close should be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].unique.transact(xa(i)).unsafeRunSync() - assertEquals(i.ResultSet.close, Some(())) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].unique.transact(xa(i))).map( + _.ResultSet.close).assertEquals(Some(())) } test("ResultSet.close should be called on failure") { - val i = new Interp - assertEquals(sql"select 'x'".query[Int].unique.transact(xa(i)).attempt.unsafeRunSync().toOption, None) - assertEquals(i.ResultSet.close, Some(())) + IO.pure(new Interp).flatTap(i => + sql"select 'x'".query[Int].unique.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.ResultSet.close) + .assertEquals(Some(())) } test("[Streaming] ResultSet.close should be called on success") { - val i = new Interp - val _ = sql"select 1".query[Int].stream.compile.toList.transact(xa(i)).unsafeRunSync() - assertEquals(i.ResultSet.close, Some(())) + IO.pure(new Interp).flatTap(i => sql"select 1".query[Int].stream.compile.toList.transact(xa(i))).map( + _.ResultSet.close).assertEquals(Some(())) } test("[Streaming] ResultSet.close should be called on failure") { - val i = new Interp - assertEquals( - sql"select 'x'".query[Int].stream.compile.toList.transact(xa(i)).attempt.unsafeRunSync().toOption, - None) - assertEquals(i.ResultSet.close, Some(())) + IO.pure(new Interp).flatTap(i => + sql"select 'x'".query[Int].stream.compile.toList.transact(xa(i)).attempt.map(_.isLeft).assert).map( + _.ResultSet.close) + .assertEquals(Some(())) } } diff --git a/modules/core/src/test/scala/doobie/util/TransactorSuite.scala b/modules/core/src/test/scala/doobie/util/TransactorSuite.scala index 2f496627e..2ae275444 100644 --- a/modules/core/src/test/scala/doobie/util/TransactorSuite.scala +++ b/modules/core/src/test/scala/doobie/util/TransactorSuite.scala @@ -5,9 +5,11 @@ package doobie.util import cats.effect.{Async, IO} -import doobie.*, doobie.implicits.* +import cats.implicits.catsSyntaxApplicativeId +import doobie.* +import doobie.implicits.* -class TransactorSuite extends munit.FunSuite { +class TransactorSuite extends munit.CatsEffectSuite { import cats.effect.unsafe.implicits.global @@ -22,7 +24,7 @@ class TransactorSuite extends munit.FunSuite { ) test("Transactor should support cats.effect.IO") { - assertEquals(q.transact(xa[IO]).unsafeRunSync(), 42) + q.transact(xa[IO])assertEquals(42) } class ConnectionTracker { @@ -47,29 +49,27 @@ class TransactorSuite extends munit.FunSuite { test("Connection.close should be called on success") { val tracker = new ConnectionTracker val transactor = tracker.track(xa[IO]) - val _ = sql"select 1".query[Int].unique.transact(transactor).unsafeRunSync() - assertEquals(tracker.connections.map(_.isClosed), List(true)) + val _ = sql"select 1".query[Int].unique.transact(transactor).map(_ => + tracker.connections.map(_.isClosed)).assertEquals(List(true)) } test("Connection.close should be called on failure") { val tracker = new ConnectionTracker val transactor = tracker.track(xa[IO]) - assertEquals(sql"abc".query[Int].unique.transact(transactor).attempt.unsafeRunSync().toOption, None) - assertEquals(tracker.connections.map(_.isClosed), List(true)) + sql"abc".query[Int].unique.transact(transactor).attempt.map(_.isLeft).assertEquals(true) } test("[Streaming] Connection.close should be called on success") { val tracker = new ConnectionTracker val transactor = tracker.track(xa[IO]) - val _ = sql"select 1".query[Int].stream.compile.toList.transact(transactor).unsafeRunSync() - assertEquals(tracker.connections.map(_.isClosed), List(true)) + sql"select 1".query[Int].stream.compile.toList.transact(transactor).map(_ => + tracker.connections.map(_.isClosed)).assertEquals(List(true)) } test("[Streaming] Connection.close should be called on failure") { val tracker = new ConnectionTracker val transactor = tracker.track(xa[IO]) - assertEquals(sql"abc".query[Int].stream.compile.toList.transact(transactor).attempt.unsafeRunSync().toOption, None) - assertEquals(tracker.connections.map(_.isClosed), List(true)) + sql"abc".query[Int].stream.compile.toList.transact(transactor).attempt.map(_.isLeft).assertEquals(true) } } diff --git a/modules/core/src/test/scala/doobie/util/WriteSuite.scala b/modules/core/src/test/scala/doobie/util/WriteSuite.scala index 33f756af6..dd28a82df 100644 --- a/modules/core/src/test/scala/doobie/util/WriteSuite.scala +++ b/modules/core/src/test/scala/doobie/util/WriteSuite.scala @@ -11,7 +11,7 @@ import cats.effect.IO import cats.effect.unsafe.implicits.global import doobie.testutils.VoidExtensions -class WriteSuite extends munit.FunSuite with WriteSuitePlatform { +class WriteSuite extends munit.CatsEffectSuite with WriteSuitePlatform { val xa: Transactor[IO] = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", @@ -119,22 +119,17 @@ class WriteSuite extends munit.FunSuite with WriteSuitePlatform { )) }) .transact(xa) - .unsafeRunSync() } test("Write should yield correct error when Some(null) inserted") { - interceptMessage[RuntimeException]("Expected non-nullable param at 2. Use Option to describe nullable values.") { - testNullPut(("a", Some(null))) - } + testNullPut((null, Some("b"))).interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") } test("Write should yield correct error when null inserted into non-nullable field") { - interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") { - testNullPut((null, Some("b"))) - } + testNullPut((null, Some("b"))).interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") } - private def testNullPut(input: (String, Option[String])): Int = { + private def testNullPut(input: (String, Option[String])): IO[Int] = { import doobie.implicits.* (for { @@ -142,7 +137,6 @@ class WriteSuite extends munit.FunSuite with WriteSuitePlatform { n <- Update[(String, Option[String])]("insert into t0 (a, b) values (?, ?)").run(input) } yield n) .transact(xa) - .unsafeRunSync() } } diff --git a/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala b/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala index f4eeec45a..44235820d 100644 --- a/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala +++ b/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala @@ -7,6 +7,7 @@ package doobie.util.meta import cats.effect.IO import doobie.util.transactor.Transactor import doobie.util.{Get, Put} +import munit.CatsEffectAssertions.MUnitCatsAssertionsForIOOps import scala.annotation.nowarn @@ -45,13 +46,13 @@ class MetaDBSuite extends munit.FunSuite { implicit def FooMeta: Meta[Foo] = Meta[String].tiemap(s => Either.cond(!s.isEmpty, Foo(s), "may not be empty"))(_.str) test("Meta.tiemap should accept valid values") { - val x = sql"select 'bar'".query[Foo].unique.transact(xa).unsafeRunSync() - assertEquals(x, Foo("bar")) + val x = sql"select 'bar'".query[Foo].unique.transact(xa) + x.assertEquals(Foo("bar")) } test("Meta.tiemap should reject invalid values") { - val x = sql"select ''".query[Foo].unique.transact(xa).attempt.unsafeRunSync() - assertEquals(x, Left(doobie.util.invariant.InvalidValue[String, Foo]("", "may not be empty"))) + val x = sql"select ''".query[Foo].unique.transact(xa).attempt + x.assertEquals(Left(doobie.util.invariant.InvalidValue[String, Foo]("", "may not be empty"))) } } diff --git a/modules/h2/src/test/scala/doobie/h2/h2types.scala b/modules/h2/src/test/scala/doobie/h2/h2types.scala index 91f881a17..7436d776c 100644 --- a/modules/h2/src/test/scala/doobie/h2/h2types.scala +++ b/modules/h2/src/test/scala/doobie/h2/h2types.scala @@ -15,6 +15,7 @@ import doobie.util.arbitraries.SQLArbitraries.* import doobie.util.arbitraries.StringArbitraries.* import org.scalacheck.Prop.forAll import org.scalacheck.{Arbitrary, Gen} +import munit.CatsEffectAssertions.MUnitCatsAssertionsForIOOps // Establish that we can read various types. It's not very comprehensive as a test, bit it's a start. class h2typesspec extends munit.ScalaCheckSuite { @@ -49,15 +50,15 @@ class h2typesspec extends munit.ScalaCheckSuite { def testInOutWithCustomGen[A](col: String, gen: Gen[A])(implicit m: Get[A], p: Put[A]) = { test(s"Mapping for $col as ${m.typeStack} - write+read $col as ${m.typeStack}") { - forAll(gen) { (t: A) => assertEquals(inOut(col, t).transact(xa).attempt.unsafeRunSync(), Right(t)) } + forAll(gen) { (t: A) => inOut(col, t).transact(xa).attempt.assertEquals(Right(t)) } } test(s"Mapping for $col as ${m.typeStack} - write+read $col as Option[${m.typeStack}] (Some)") { forAll(gen) { (t: A) => - assertEquals(inOutOpt[A](col, Some(t)).transact(xa).attempt.unsafeRunSync(), Right(Some(t))) + inOutOpt[A](col, Some(t)).transact(xa).attempt.assertEquals(Right(Some(t))) } } test(s"Mapping for $col as ${m.typeStack} - write+read $col as Option[${m.typeStack}] (None)") { - assertEquals(inOutOpt[A](col, None).transact(xa).attempt.unsafeRunSync(), Right(None)) + inOutOpt[A](col, None).transact(xa).attempt.assertEquals(Right(None)) } } @@ -115,30 +116,30 @@ class h2typesspec extends munit.ScalaCheckSuite { skip("GEOMETRY") test("Mapping for Boolean should pass query analysis for unascribed 'true'") { - val a = sql"select true".query[Boolean].analysis.transact(xa).unsafeRunSync() - assertEquals(a.alignmentErrors, Nil) + val a = sql"select true".query[Boolean].analysis.transact(xa) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for Boolean should pass query analysis for ascribed BIT") { - val a = sql"select true::BIT".query[Boolean].analysis.transact(xa).unsafeRunSync() - assertEquals(a.alignmentErrors, Nil) + val a = sql"select true::BIT".query[Boolean].analysis.transact(xa) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for Boolean should pass query analysis for ascribed BOOLEAN") { - val a = sql"select true::BOOLEAN".query[Boolean].analysis.transact(xa).unsafeRunSync() - assertEquals(a.alignmentErrors, Nil) + val a = sql"select true::BOOLEAN".query[Boolean].analysis.transact(xa) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for UUID should pass query analysis for unascribed UUID") { - val a = sql"select random_uuid()".query[UUID].analysis.transact(xa).unsafeRunSync() - assertEquals(a.alignmentErrors, Nil) + val a = sql"select random_uuid()".query[UUID].analysis.transact(xa) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for UUID should pass query analysis for ascribed UUID") { - val a = sql"select random_uuid()::UUID".query[UUID].analysis.transact(xa).unsafeRunSync() - assertEquals(a.alignmentErrors, Nil) + val a = sql"select random_uuid()::UUID".query[UUID].analysis.transact(xa) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for LocalDate should pass query analysis for DATE") { val a = analyzeDate[java.time.LocalDate] - assertEquals(a.alignmentErrors, Nil) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for LocalDate should fail query analysis for TIMESTAMP") { @@ -148,7 +149,7 @@ class h2typesspec extends munit.ScalaCheckSuite { test("Mapping for LocalTime should pass query analysis for TIME") { val a = analyzeTime[java.time.LocalTime] - assertEquals(a.alignmentErrors, Nil) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for LocalTime should fail query analysis for TIME WITH TIME ZONE") { @@ -158,7 +159,7 @@ class h2typesspec extends munit.ScalaCheckSuite { test("Mapping for OffsetTime should pass query analysis for TIME WITH TIME ZONE") { val a = analyzeTimeWithTimeZone[java.time.OffsetTime] - assertEquals(a.alignmentErrors, Nil) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for OffsetTime should fail query analysis for TIME") { @@ -168,7 +169,7 @@ class h2typesspec extends munit.ScalaCheckSuite { test("Mapping for LocalDateTime should pass query analysis for TIMESTAMP") { val a = analyzeTimestamp[java.time.LocalDateTime] - assertEquals(a.alignmentErrors, Nil) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for LocalDateTime should fail query analysis for DATE") { @@ -188,7 +189,7 @@ class h2typesspec extends munit.ScalaCheckSuite { test("Mapping for OffsetDateTime should pass query analysis for TIMESTAMP WITH TIME ZONE") { val a = analyzeTimestampWithTimeZone[java.time.OffsetDateTime] - assertEquals(a.alignmentErrors, Nil) + a.map(_.alignmentErrors).assertEquals(Nil) } test("Mapping for OffsetDateTime should fail query analysis for TIME WITH TIME ZONE") { @@ -201,18 +202,19 @@ class h2typesspec extends munit.ScalaCheckSuite { assertAnalyzeColumnError(a) } - private def analyzeDate[R: Read] = analyze(sql"select '2000-01-02'::DATE".query[R]) - private def analyzeTime[R: Read] = analyze(sql"select '01:02:03'::TIME".query[R]) - private def analyzeTimeWithTimeZone[R: Read] = analyze(sql"select '01:02:03+04:05'::TIME WITH TIME ZONE".query[R]) - private def analyzeTimestamp[R: Read] = analyze(sql"select '2000-01-02T01:02:03'::TIMESTAMP".query[R]) - private def analyzeTimestampWithTimeZone[R: Read] = + private def analyzeDate[R: Read]: IO[Analysis] = analyze(sql"select '2000-01-02'::DATE".query[R]) + private def analyzeTime[R: Read]: IO[Analysis] = analyze(sql"select '01:02:03'::TIME".query[R]) + private def analyzeTimeWithTimeZone[R: Read]: IO[Analysis] = + analyze(sql"select '01:02:03+04:05'::TIME WITH TIME ZONE".query[R]) + private def analyzeTimestamp[R: Read]: IO[Analysis] = analyze(sql"select '2000-01-02T01:02:03'::TIMESTAMP".query[R]) + private def analyzeTimestampWithTimeZone[R: Read]: IO[Analysis] = analyze(sql"select '2000-01-02T01:02:03+04:05'::TIMESTAMP WITH TIME ZONE".query[R]) - private def analyze[R](q: Query0[R]) = q.analysis.transact(xa).unsafeRunSync() + private def analyze[R](q: Query0[R]): IO[Analysis] = q.analysis.transact(xa) - private def assertAnalyzeColumnError(result: Analysis): Unit = { - val errorClasses = result.alignmentErrors.map(_.getClass) - assertEquals(errorClasses, List(classOf[ColumnTypeError])) + private def assertAnalyzeColumnError(result: IO[Analysis]): Unit = { + val errorClasses = result.map(_.alignmentErrors.map(_.getClass)) + errorClasses.assertEquals(List(classOf[ColumnTypeError])) } } From 2e66c8815a4f80d728c4440d26a02d1a3044869b Mon Sep 17 00:00:00 2001 From: Florian Emile Date: Wed, 27 Nov 2024 20:23:13 +0000 Subject: [PATCH 2/6] Format test files --- .../src/test/scala/doobie/issue/262.scala | 2 +- .../scala/doobie/util/FragmentsSuite.scala | 4 +- .../test/scala/doobie/util/ProcessSuite.scala | 2 +- .../test/scala/doobie/util/QuerySuite.scala | 42 +++++++++---------- .../test/scala/doobie/util/ReadSuite.scala | 2 +- .../scala/doobie/util/TransactorSuite.scala | 2 +- .../test/scala/doobie/util/WriteSuite.scala | 6 ++- 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/modules/core/src/test/scala/doobie/issue/262.scala b/modules/core/src/test/scala/doobie/issue/262.scala index fd7855cd9..599dfe53b 100644 --- a/modules/core/src/test/scala/doobie/issue/262.scala +++ b/modules/core/src/test/scala/doobie/issue/262.scala @@ -10,7 +10,7 @@ import doobie.implicits.* import munit.CatsEffectSuite class `262` extends CatsEffectSuite { - + // an interpreter that returns null when we ask for statement metadata object Interp extends KleisliInterpreter[IO](LogHandler.noop) { override lazy val PreparedStatementInterpreter: PreparedStatementInterpreter = diff --git a/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala b/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala index 325e9d59d..6ae616edb 100644 --- a/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala +++ b/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala @@ -389,13 +389,13 @@ class FragmentsSuite extends munit.CatsEffectSuite { test("values (1)") { val c = Contact(Person("Bob", 42), Some("addr")) val f = sql"select" ++ Fragments.values(c) - f.query[Contact].unique.transact(xa)assertEquals(c) + f.query[Contact].unique.transact(xa) assertEquals (c) } test("values (2)") { val c = Contact(Person("Bob", 42), None) val f = sql"select" ++ Fragments.values(c) - f.query[Contact].unique.transact(xa).assertEquals( c) + f.query[Contact].unique.transact(xa).assertEquals(c) } } diff --git a/modules/core/src/test/scala/doobie/util/ProcessSuite.scala b/modules/core/src/test/scala/doobie/util/ProcessSuite.scala index 06c14a45b..c23fe2782 100644 --- a/modules/core/src/test/scala/doobie/util/ProcessSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ProcessSuite.scala @@ -15,7 +15,7 @@ import scala.Predef.* import scala.util.Random class ProcessSuite extends munit.ScalaCheckSuite { - + test("repeatEvalChunks must yield the same result irrespective of chunk size") { PropF.forAllF { (n0: Int) => val dataSize = 1000 diff --git a/modules/core/src/test/scala/doobie/util/QuerySuite.scala b/modules/core/src/test/scala/doobie/util/QuerySuite.scala index 47f4c1668..29f4e7f66 100644 --- a/modules/core/src/test/scala/doobie/util/QuerySuite.scala +++ b/modules/core/src/test/scala/doobie/util/QuerySuite.scala @@ -24,26 +24,26 @@ class QuerySuite extends munit.CatsEffectSuite { val pairQuery = Query[String, (String, Int)]("select 'xxx', 123 where ? = 'foo'", None) test("Query (non-empty) to") { - q.to[List]("foo").transact(xa)assertEquals( List(123)) + q.to[List]("foo").transact(xa) assertEquals (List(123)) } test("Query (non-empty) toMap") { - pairQuery.toMap[String, Int]("foo").transact(xa)assertEquals(Map("xxx" -> 123)) + pairQuery.toMap[String, Int]("foo").transact(xa) assertEquals (Map("xxx" -> 123)) } test("Query (non-empty) unique") { - q.unique("foo").transact(xa).assertEquals( 123) + q.unique("foo").transact(xa).assertEquals(123) } test("Query (non-empty) option") { - q.option("foo").transact(xa)assertEquals( Some(123)) + q.option("foo").transact(xa) assertEquals (Some(123)) } test("Query (non-empty) map") { - q.map("x" * _).to[List]("foo").transact(xa)assertEquals(List("x" * 123)) + q.map("x" * _).to[List]("foo").transact(xa) assertEquals (List("x" * 123)) } test("Query (non-empty) contramap") { q.contramap[Int](n => "foo" * n).to[List](1).transact(xa).assertEquals(List(123)) } test("Query (empty) to") { - q.to[List]("bar").transact(xa).assertEquals( Nil) + q.to[List]("bar").transact(xa).assertEquals(Nil) } test("Query (empty) toMap") { pairQuery.toMap[String, Int]("bar").transact(xa).assertEquals(Map.empty[String, Int]) @@ -55,55 +55,55 @@ class QuerySuite extends munit.CatsEffectSuite { q.option("bar").transact(xa).assertEquals(None) } test("Query (empty) map") { - q.map("x" * _).to[List]("bar").transact(xa).assertEquals( Nil) + q.map("x" * _).to[List]("bar").transact(xa).assertEquals(Nil) } test("Query (empty) contramap") { q.contramap[Int](n => "bar" * n).to[List](1).transact(xa).assertEquals(Nil) } test("Query0 from Query (non-empty) to") { - q.toQuery0("foo").to[List].transact(xa).assertEquals( List(123)) + q.toQuery0("foo").to[List].transact(xa).assertEquals(List(123)) } test("Query0 from Query (non-empty) toMap") { pairQuery.toQuery0("foo").toMap[String, Int].transact(xa).assertEquals(Map("xxx" -> 123)) } test("Query0 from Query (non-empty) unique") { - q.toQuery0("foo").unique.transact(xa).assertEquals( 123) + q.toQuery0("foo").unique.transact(xa).assertEquals(123) } test("Query0 from Query (non-empty) option") { - q.toQuery0("foo").option.transact(xa).assertEquals( Some(123)) + q.toQuery0("foo").option.transact(xa).assertEquals(Some(123)) } test("Query0 from Query (non-empty) map") { - q.toQuery0("foo").map(_ * 2).to[List].transact(xa).assertEquals( List(246)) + q.toQuery0("foo").map(_ * 2).to[List].transact(xa).assertEquals(List(246)) } test("Query0 from Query (empty) to") { - q.toQuery0("bar").to[List].transact(xa).assertEquals( Nil) + q.toQuery0("bar").to[List].transact(xa).assertEquals(Nil) } test("Query0 from Query (empty) toMap") { - pairQuery.toQuery0("bar").toMap[String, Int].transact(xa).assertEquals( Map.empty[String, Int]) + pairQuery.toQuery0("bar").toMap[String, Int].transact(xa).assertEquals(Map.empty[String, Int]) } test("Query0 from Query (empty) unique") { - q.toQuery0("bar").unique.transact(xa).attempt.assertEquals( Left(invariant.UnexpectedEnd)) + q.toQuery0("bar").unique.transact(xa).attempt.assertEquals(Left(invariant.UnexpectedEnd)) } test("Query0 from Query (empty) option") { - q.toQuery0("bar").option.transact(xa).assertEquals( None) + q.toQuery0("bar").option.transact(xa).assertEquals(None) } test("Query0 from Query (empty) map") { - q.toQuery0("bar").map(_ * 2).to[List].transact(xa).assertEquals( Nil) + q.toQuery0("bar").map(_ * 2).to[List].transact(xa).assertEquals(Nil) } val q0n = Query0[Int]("select 123 where 'foo' = 'foo'", None) val pairQ0n = Query0[(String, Int)]("select 'xxx', 123 where 'foo' = 'foo'", None) test("Query0 via constructor (non-empty) to") { - q0n.to[List].transact(xa).assertEquals( List(123)) + q0n.to[List].transact(xa).assertEquals(List(123)) } test("Query0 via constructor (non-empty) toMap") { pairQ0n.toMap[String, Int].transact(xa).assertEquals(Map("xxx" -> 123)) } test("Query0 via constructor (non-empty) unique") { - q0n.unique.transact(xa).assertEquals( 123) + q0n.unique.transact(xa).assertEquals(123) } test("Query0 via constructor (non-empty) option") { q0n.option.transact(xa).assertEquals(Some(123)) @@ -116,16 +116,16 @@ class QuerySuite extends munit.CatsEffectSuite { val pairQ0e = Query0[(String, Int)]("select 'xxx', 123 where 'bar' = 'foo'", None) test("Query0 via constructor (empty) to") { - q0e.to[List].transact(xa).assertEquals( Nil) + q0e.to[List].transact(xa).assertEquals(Nil) } test("Query0 via constructor (empty) toMap") { - pairQ0e.toMap[String, Int].transact(xa).assertEquals( Map.empty[String, Int]) + pairQ0e.toMap[String, Int].transact(xa).assertEquals(Map.empty[String, Int]) } test("Query0 via constructor (empty) unique") { q0e.unique.transact(xa).attempt.assertEquals(Left(invariant.UnexpectedEnd)) } test("Query0 via constructor (empty) option") { - q0e.option.transact(xa).assertEquals( None) + q0e.option.transact(xa).assertEquals(None) } test("Query0 via constructor (empty) map") { q0e.map(_ * 2).to[List].transact(xa).assertEquals(Nil) diff --git a/modules/core/src/test/scala/doobie/util/ReadSuite.scala b/modules/core/src/test/scala/doobie/util/ReadSuite.scala index 5b945e56b..69d76edc5 100644 --- a/modules/core/src/test/scala/doobie/util/ReadSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ReadSuite.scala @@ -11,7 +11,7 @@ import doobie.testutils.VoidExtensions import munit.CatsEffectSuite class ReadSuite extends CatsEffectSuite with ReadSuitePlatform { - + val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", url = "jdbc:h2:mem:;DB_CLOSE_DELAY=-1", diff --git a/modules/core/src/test/scala/doobie/util/TransactorSuite.scala b/modules/core/src/test/scala/doobie/util/TransactorSuite.scala index 2ae275444..de14a27d5 100644 --- a/modules/core/src/test/scala/doobie/util/TransactorSuite.scala +++ b/modules/core/src/test/scala/doobie/util/TransactorSuite.scala @@ -24,7 +24,7 @@ class TransactorSuite extends munit.CatsEffectSuite { ) test("Transactor should support cats.effect.IO") { - q.transact(xa[IO])assertEquals(42) + q.transact(xa[IO]) assertEquals (42) } class ConnectionTracker { diff --git a/modules/core/src/test/scala/doobie/util/WriteSuite.scala b/modules/core/src/test/scala/doobie/util/WriteSuite.scala index dd28a82df..7974c728d 100644 --- a/modules/core/src/test/scala/doobie/util/WriteSuite.scala +++ b/modules/core/src/test/scala/doobie/util/WriteSuite.scala @@ -122,11 +122,13 @@ class WriteSuite extends munit.CatsEffectSuite with WriteSuitePlatform { } test("Write should yield correct error when Some(null) inserted") { - testNullPut((null, Some("b"))).interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") + testNullPut((null, Some("b"))).interceptMessage[RuntimeException]( + "Expected non-nullable param at 1. Use Option to describe nullable values.") } test("Write should yield correct error when null inserted into non-nullable field") { - testNullPut((null, Some("b"))).interceptMessage[RuntimeException]("Expected non-nullable param at 1. Use Option to describe nullable values.") + testNullPut((null, Some("b"))).interceptMessage[RuntimeException]( + "Expected non-nullable param at 1. Use Option to describe nullable values.") } private def testNullPut(input: (String, Option[String])): IO[Int] = { From 5b9604b7f5657f6a41fd73c0404ebb89b838d98e Mon Sep 17 00:00:00 2001 From: lewisc2303 Date: Wed, 27 Nov 2024 20:36:22 +0000 Subject: [PATCH 3/6] use cats effect assertEquals for munit --- .../scala/doobie/util/CatchSqlSuite.scala | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala b/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala index 7b3ec023c..77259c2d2 100644 --- a/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala +++ b/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala @@ -4,7 +4,7 @@ package doobie.util -import cats.effect.IO +import cats.effect.{IO, Ref} import doobie.* import doobie.implicits.* import munit.CatsEffectSuite @@ -131,23 +131,33 @@ class CatchSqlSuite extends CatsEffectSuite { } test("onSqlException should do nothing on success") { - var a = 1 - val _ = IO.delay(3).onSqlException(IO.delay(a += 1)).attempt.unsafeRunSync() - assertEquals(a, 1) + for { + a <- Ref.of[IO, Int](1) + _ <- IO.delay(3).onSqlException(IO.delay(a.set(2))).attempt + b <- a.get + } yield + assertEquals(b, 1) } test("onSqlException should perform its effect on SQLException") { - var a = 1 - val e = new SQLException("", SQLSTATE_FOO.value) - assertEquals(IO.raiseError[Int](e).onSqlException(IO.delay(a += 1)).attempt.unsafeRunSync(), Left(e)) - assertEquals(a, 2) + for { + a <- Ref.of[IO, Int](1) + e = new SQLException("foo") + _ <- IO.raiseError[Int](e).onSqlException(a.set(2)).attempt.assertEquals(Left(e)) + b <- a.get + } yield + assertEquals(b, 2) } test("onSqlException should ignore its effect on non-SQLException") { - var a = 1 - val e = new IllegalArgumentException - IO.raiseError[Int](e).onSqlException(IO.delay(a += 1)).attempt.assertEquals(Left(e)) - assertEquals(a, 1) + for { + a <- Ref.of[IO, Int](1) + e = new RuntimeException("foo") + _ <- IO.raiseError[Int](e).onSqlException(a.set(2)).attempt.assertEquals(Left(e)) + b <- a.get + } yield { + assertEquals(b, 1) + } } } From b80a1d36ae29ae95429e5000230a86b7c43593bf Mon Sep 17 00:00:00 2001 From: Florian Emile Date: Wed, 27 Nov 2024 20:40:05 +0000 Subject: [PATCH 4/6] Use munit cats effect to other classes --- .../scala/doobie/util/CatchSqlSuite.scala | 6 ++-- .../scala/doobie/h2/circe/H2JsonSuite.scala | 28 ++++++++----------- .../h2/src/test/scala/doobie/h2/h2types.scala | 6 ++-- .../test/scala/doobie/mysql/CheckSuite.scala | 16 ++++------- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala b/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala index 77259c2d2..f4e408ab7 100644 --- a/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala +++ b/modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala @@ -135,8 +135,7 @@ class CatchSqlSuite extends CatsEffectSuite { a <- Ref.of[IO, Int](1) _ <- IO.delay(3).onSqlException(IO.delay(a.set(2))).attempt b <- a.get - } yield - assertEquals(b, 1) + } yield assertEquals(b, 1) } test("onSqlException should perform its effect on SQLException") { @@ -145,8 +144,7 @@ class CatchSqlSuite extends CatsEffectSuite { e = new SQLException("foo") _ <- IO.raiseError[Int](e).onSqlException(a.set(2)).attempt.assertEquals(Left(e)) b <- a.get - } yield - assertEquals(b, 2) + } yield assertEquals(b, 2) } test("onSqlException should ignore its effect on non-SQLException") { diff --git a/modules/h2-circe/src/test/scala/doobie/h2/circe/H2JsonSuite.scala b/modules/h2-circe/src/test/scala/doobie/h2/circe/H2JsonSuite.scala index 0c2891a6a..ba2257c61 100644 --- a/modules/h2-circe/src/test/scala/doobie/h2/circe/H2JsonSuite.scala +++ b/modules/h2-circe/src/test/scala/doobie/h2/circe/H2JsonSuite.scala @@ -8,10 +8,9 @@ import cats.effect.IO import doobie.* import doobie.implicits.* import io.circe.{Decoder, Encoder, Json} +import munit.CatsEffectSuite -class H2JsonSuite extends munit.FunSuite { - - import cats.effect.unsafe.implicits.global +class H2JsonSuite extends CatsEffectSuite { val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", @@ -29,13 +28,13 @@ class H2JsonSuite extends munit.FunSuite { def testInOut[A](col: String, a: A, t: Transactor[IO])(implicit m: Get[A], p: Put[A]) = { test(s"Mapping for $col as ${m.typeStack} - write+read $col as ${m.typeStack}") { - assertEquals(inOut(col, a).transact(t).attempt.unsafeRunSync(), Right(a)) + inOut(col, a).transact(t).attempt.assertEquals(Right(a)) } test(s"Mapping for $col as ${m.typeStack} - write+read $col as Option[${m.typeStack}] (Some)") { - assertEquals(inOut[Option[A]](col, Some(a)).transact(t).attempt.unsafeRunSync(), Right(Some(a))) + inOut[Option[A]](col, Some(a)).transact(t).attempt.assertEquals(Right(Some(a))) } test(s"Mapping for $col as ${m.typeStack} - write+read $col as Option[${m.typeStack}] (None)") { - assertEquals(inOut[Option[A]](col, None).transact(t).attempt.unsafeRunSync(), Right(None)) + inOut[Option[A]](col, None).transact(t).attempt.assertEquals(Right(None)) } } @@ -48,15 +47,12 @@ class H2JsonSuite extends munit.FunSuite { test("json should check ok for read") { import doobie.h2.circe.json.implicits.* - - val a = sql"SELECT '{}' FORMAT JSON".query[Json].analysis.transact(xa).unsafeRunSync() - assertEquals(a.columnTypeErrors, Nil) + sql"SELECT '{}' FORMAT JSON".query[Json].analysis.transact(xa).map(_.columnTypeErrors).assertEquals(Nil) } + test("json should check ok for write") { import doobie.h2.circe.json.implicits.* - - val a = sql"SELECT ${Json.obj()} FORMAT JSON".query[Json].analysis.transact(xa).unsafeRunSync() - assertEquals(a.parameterTypeErrors, Nil) + sql"SELECT ${Json.obj()} FORMAT JSON".query[Json].analysis.transact(xa).map(_.parameterTypeErrors).assertEquals(Nil) } // Encoder / Decoders @@ -70,12 +66,12 @@ class H2JsonSuite extends munit.FunSuite { } test("fooGet should check ok for read") { - val a = sql"SELECT '{}' FORMAT JSON".query[Foo].analysis.transact(xa).unsafeRunSync() - assertEquals(a.columnTypeErrors, Nil) + sql"SELECT '{}' FORMAT JSON".query[Foo].analysis.transact(xa).map(_.columnTypeErrors).assertEquals(Nil) } + test("fooPut check ok for write") { - val a = sql"SELECT ${Foo(Json.obj())} FORMAT JSON".query[Foo].analysis.transact(xa).unsafeRunSync() - assertEquals(a.parameterTypeErrors, Nil) + sql"SELECT ${Foo(Json.obj())} FORMAT JSON".query[Foo].analysis.transact(xa).map(_.parameterTypeErrors).assertEquals( + Nil) } } diff --git a/modules/h2/src/test/scala/doobie/h2/h2types.scala b/modules/h2/src/test/scala/doobie/h2/h2types.scala index 7436d776c..133f9549d 100644 --- a/modules/h2/src/test/scala/doobie/h2/h2types.scala +++ b/modules/h2/src/test/scala/doobie/h2/h2types.scala @@ -5,7 +5,6 @@ package doobie.h2 import java.util.UUID - import cats.effect.IO import doobie.* import doobie.implicits.* @@ -16,6 +15,7 @@ import doobie.util.arbitraries.StringArbitraries.* import org.scalacheck.Prop.forAll import org.scalacheck.{Arbitrary, Gen} import munit.CatsEffectAssertions.MUnitCatsAssertionsForIOOps +import org.scalacheck.effect.PropF // Establish that we can read various types. It's not very comprehensive as a test, bit it's a start. class h2typesspec extends munit.ScalaCheckSuite { @@ -50,10 +50,10 @@ class h2typesspec extends munit.ScalaCheckSuite { def testInOutWithCustomGen[A](col: String, gen: Gen[A])(implicit m: Get[A], p: Put[A]) = { test(s"Mapping for $col as ${m.typeStack} - write+read $col as ${m.typeStack}") { - forAll(gen) { (t: A) => inOut(col, t).transact(xa).attempt.assertEquals(Right(t)) } + PropF.forAllF(gen) { (t: A) => inOut(col, t).transact(xa).attempt.assertEquals(Right(t)) } } test(s"Mapping for $col as ${m.typeStack} - write+read $col as Option[${m.typeStack}] (Some)") { - forAll(gen) { (t: A) => + PropF.forAllF(gen) { (t: A) => inOutOpt[A](col, Some(t)).transact(xa).attempt.assertEquals(Right(Some(t))) } } diff --git a/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala b/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala index b1c15af4a..e5ee6c99f 100644 --- a/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala +++ b/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala @@ -5,13 +5,13 @@ package doobie.mysql import java.time.{LocalDate, LocalDateTime, LocalTime, OffsetDateTime} - import doobie.* import doobie.implicits.* import doobie.mysql.implicits.* import doobie.util.analysis.ColumnTypeError +import munit.CatsEffectSuite -class CheckSuite extends munit.FunSuite { +class CheckSuite extends CatsEffectSuite { import cats.effect.unsafe.implicits.global import MySQLTestTransactor.xa @@ -65,17 +65,13 @@ class CheckSuite extends munit.FunSuite { } private def successRead[A: Read](frag: Fragment): Unit = { - val analysisResult = frag.query[A].analysis.transact(xa).unsafeRunSync() - assertEquals(analysisResult.columnAlignmentErrors, Nil) - - val result = frag.query[A].unique.transact(xa).attempt.unsafeRunSync() - assert(result.isRight) + frag.query[A].analysis.transact(xa).map(_.columnAlignmentErrors).assertEquals(Nil) + frag.query[A].unique.transact(xa).attempt.map(_.isRight).assert } private def failedRead[A: Read](frag: Fragment): Unit = { - val analysisResult = frag.query[A].analysis.transact(xa).unsafeRunSync() - val errorClasses = analysisResult.columnAlignmentErrors.map(_.getClass) - assertEquals(errorClasses, List(classOf[ColumnTypeError])) + frag.query[A].analysis.transact(xa).map(_.columnAlignmentErrors.map(_.getClass)).assertEquals(List( + classOf[ColumnTypeError])) } // private def failedWrite[A: Put](value: A, dbType: String): Unit = { From df8fc1a56c97579b96fbc263914a8a6deab2cca6 Mon Sep 17 00:00:00 2001 From: Florian Emile Date: Wed, 27 Nov 2024 20:50:15 +0000 Subject: [PATCH 5/6] Resolve unused imports --- .../core/src/test/scala/doobie/util/ConnectionIOSuite.scala | 3 --- modules/core/src/test/scala/doobie/util/FragmentsSuite.scala | 1 - modules/core/src/test/scala/doobie/util/GetSuite.scala | 1 - modules/core/src/test/scala/doobie/util/ProcessSuite.scala | 1 - modules/core/src/test/scala/doobie/util/QuerySuite.scala | 2 -- modules/core/src/test/scala/doobie/util/TransactorSuite.scala | 3 --- modules/core/src/test/scala/doobie/util/WriteSuite.scala | 1 - modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala | 1 - 8 files changed, 13 deletions(-) diff --git a/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala b/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala index 368d855c4..4ded2f089 100644 --- a/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ConnectionIOSuite.scala @@ -8,14 +8,11 @@ import cats.Applicative import cats.implicits.* import cats.effect.IO import cats.kernel.Monoid -import munit.* import doobie.* import doobie.implicits.* class ConnectionIOSuite extends munit.CatsEffectSuite { - import cats.effect.unsafe.implicits.global - val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", url = "jdbc:h2:mem:queryspec;DB_CLOSE_DELAY=-1", diff --git a/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala b/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala index 6ae616edb..e98bf096a 100644 --- a/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala +++ b/modules/core/src/test/scala/doobie/util/FragmentsSuite.scala @@ -11,7 +11,6 @@ import doobie.implicits.* class FragmentsSuite extends munit.CatsEffectSuite { import Fragments.* - import cats.effect.unsafe.implicits.global val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", diff --git a/modules/core/src/test/scala/doobie/util/GetSuite.scala b/modules/core/src/test/scala/doobie/util/GetSuite.scala index 1f7248a70..e5d6c862e 100644 --- a/modules/core/src/test/scala/doobie/util/GetSuite.scala +++ b/modules/core/src/test/scala/doobie/util/GetSuite.scala @@ -51,7 +51,6 @@ final case class Foo(s: String) final case class Bar(n: Int) class GetDBSuite extends munit.CatsEffectSuite { - import cats.effect.unsafe.implicits.global import doobie.syntax.all.* lazy val xa = Transactor.fromDriverManager[IO]( diff --git a/modules/core/src/test/scala/doobie/util/ProcessSuite.scala b/modules/core/src/test/scala/doobie/util/ProcessSuite.scala index c23fe2782..abad32c67 100644 --- a/modules/core/src/test/scala/doobie/util/ProcessSuite.scala +++ b/modules/core/src/test/scala/doobie/util/ProcessSuite.scala @@ -8,7 +8,6 @@ package util import cats.effect.IO import doobie.util.stream.repeatEvalChunks import munit.CatsEffectAssertions.MUnitCatsAssertionsForIOOps -import org.scalacheck.Prop.forAll import org.scalacheck.effect.PropF import scala.Predef.* diff --git a/modules/core/src/test/scala/doobie/util/QuerySuite.scala b/modules/core/src/test/scala/doobie/util/QuerySuite.scala index 29f4e7f66..265ffcd5f 100644 --- a/modules/core/src/test/scala/doobie/util/QuerySuite.scala +++ b/modules/core/src/test/scala/doobie/util/QuerySuite.scala @@ -10,8 +10,6 @@ import scala.Predef.* class QuerySuite extends munit.CatsEffectSuite { - import cats.effect.unsafe.implicits.global - val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", url = "jdbc:h2:mem:queryspec;DB_CLOSE_DELAY=-1", diff --git a/modules/core/src/test/scala/doobie/util/TransactorSuite.scala b/modules/core/src/test/scala/doobie/util/TransactorSuite.scala index de14a27d5..515670713 100644 --- a/modules/core/src/test/scala/doobie/util/TransactorSuite.scala +++ b/modules/core/src/test/scala/doobie/util/TransactorSuite.scala @@ -5,14 +5,11 @@ package doobie.util import cats.effect.{Async, IO} -import cats.implicits.catsSyntaxApplicativeId import doobie.* import doobie.implicits.* class TransactorSuite extends munit.CatsEffectSuite { - import cats.effect.unsafe.implicits.global - val q = sql"select 42".query[Int].unique def xa[A[_]: Async] = Transactor.fromDriverManager[A]( diff --git a/modules/core/src/test/scala/doobie/util/WriteSuite.scala b/modules/core/src/test/scala/doobie/util/WriteSuite.scala index 7974c728d..382e636b1 100644 --- a/modules/core/src/test/scala/doobie/util/WriteSuite.scala +++ b/modules/core/src/test/scala/doobie/util/WriteSuite.scala @@ -8,7 +8,6 @@ import doobie.Transactor import doobie.Update import doobie.util.TestTypes.* import cats.effect.IO -import cats.effect.unsafe.implicits.global import doobie.testutils.VoidExtensions class WriteSuite extends munit.CatsEffectSuite with WriteSuitePlatform { diff --git a/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala b/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala index 44235820d..48fb22db7 100644 --- a/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala +++ b/modules/core/src/test/scala/doobie/util/meta/MetaSuite.scala @@ -33,7 +33,6 @@ class MetaSuite extends munit.FunSuite { class MetaDBSuite extends munit.FunSuite { import doobie.implicits.* - import cats.effect.unsafe.implicits.global lazy val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", From a8184b6fa2ba5d16f7a163b0a87ae14cefb8b12b Mon Sep 17 00:00:00 2001 From: Florian Emile Date: Wed, 27 Nov 2024 20:56:11 +0000 Subject: [PATCH 6/6] Resolve unused imports --- modules/h2/src/test/scala/doobie/h2/h2types.scala | 5 +---- modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/modules/h2/src/test/scala/doobie/h2/h2types.scala b/modules/h2/src/test/scala/doobie/h2/h2types.scala index 133f9549d..3122fbf58 100644 --- a/modules/h2/src/test/scala/doobie/h2/h2types.scala +++ b/modules/h2/src/test/scala/doobie/h2/h2types.scala @@ -12,7 +12,6 @@ import doobie.h2.implicits.* import doobie.util.analysis.{Analysis, ColumnTypeError} import doobie.util.arbitraries.SQLArbitraries.* import doobie.util.arbitraries.StringArbitraries.* -import org.scalacheck.Prop.forAll import org.scalacheck.{Arbitrary, Gen} import munit.CatsEffectAssertions.MUnitCatsAssertionsForIOOps import org.scalacheck.effect.PropF @@ -20,8 +19,6 @@ import org.scalacheck.effect.PropF // Establish that we can read various types. It's not very comprehensive as a test, bit it's a start. class h2typesspec extends munit.ScalaCheckSuite { - import cats.effect.unsafe.implicits.global - val xa = Transactor.fromDriverManager[IO]( driver = "org.h2.Driver", url = "jdbc:h2:mem:ch3;DB_CLOSE_DELAY=-1", @@ -214,7 +211,7 @@ class h2typesspec extends munit.ScalaCheckSuite { private def assertAnalyzeColumnError(result: IO[Analysis]): Unit = { val errorClasses = result.map(_.alignmentErrors.map(_.getClass)) - errorClasses.assertEquals(List(classOf[ColumnTypeError])) + val _ = errorClasses.assertEquals(List(classOf[ColumnTypeError])) } } diff --git a/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala b/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala index e5ee6c99f..130735936 100644 --- a/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala +++ b/modules/mysql/src/test/scala/doobie/mysql/CheckSuite.scala @@ -65,12 +65,12 @@ class CheckSuite extends CatsEffectSuite { } private def successRead[A: Read](frag: Fragment): Unit = { - frag.query[A].analysis.transact(xa).map(_.columnAlignmentErrors).assertEquals(Nil) - frag.query[A].unique.transact(xa).attempt.map(_.isRight).assert + val _ = frag.query[A].analysis.transact(xa).map(_.columnAlignmentErrors).assertEquals(Nil) + val _ = frag.query[A].unique.transact(xa).attempt.map(_.isRight).assert } private def failedRead[A: Read](frag: Fragment): Unit = { - frag.query[A].analysis.transact(xa).map(_.columnAlignmentErrors.map(_.getClass)).assertEquals(List( + val _ = frag.query[A].analysis.transact(xa).map(_.columnAlignmentErrors.map(_.getClass)).assertEquals(List( classOf[ColumnTypeError])) }