Skip to content

Commit

Permalink
Merge pull request #2181 from jf-botto/use_munit_cats_effect_jorge
Browse files Browse the repository at this point in the history
Using Munit Cats Effects for test suites
  • Loading branch information
jatcwang authored Jan 31, 2025
2 parents 0e2b9b6 + d02efc7 commit 00d98e4
Show file tree
Hide file tree
Showing 19 changed files with 509 additions and 610 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait QueryLogSuitePlatform { self: QueryLogSuite =>
test("[Query] n-arg success") {
val Sql = "select 1 where ? = ?"
val Arg = 1 :: 1 :: HNil
eventForUniqueQuery(Sql, Arg) match {
eventForUniqueQuery(Sql, Arg).map {
case Success(Sql, Parameters.NonBatch(List(1, 1)), _, _, _) => ()
case a => fail(s"no match: $a")
}
Expand All @@ -21,7 +21,7 @@ trait QueryLogSuitePlatform { self: QueryLogSuite =>
test("[Query] n-arg processing failure") {
val Sql = "select 1 where ? = ?"
val Arg = 1 :: 2 :: HNil
eventForUniqueQuery(Sql, Arg) match {
eventForUniqueQuery(Sql, Arg).map {
case ProcessingFailure(Sql, Parameters.NonBatch(List(1, 2)), _, _, _, _) => ()
case a => fail(s"no match: $a")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait QueryLogSuitePlatform { self: QueryLogSuite =>
test("[Query] n-arg success") {
val Sql = "select 1 where ? = ?"
val Arg = 1 *: 1 *: EmptyTuple
eventForUniqueQuery(Sql, Arg) match {
eventForUniqueQuery(Sql, Arg).map {
case Success(Sql, Parameters.NonBatch(List(1, 1)), _, _, _) => ()
case a => fail(s"no match: $a")
}
Expand All @@ -20,7 +20,7 @@ trait QueryLogSuitePlatform { self: QueryLogSuite =>
test("[Query] n-arg processing failure") {
val Sql = "select 1 where ? = ?"
val Arg = 1 *: 2 *: EmptyTuple
eventForUniqueQuery(Sql, Arg) match {
eventForUniqueQuery(Sql, Arg).map {
case ProcessingFailure(Sql, Parameters.NonBatch(List(1, 2)), _, _, _, _) => ()
case a => fail(s"no match: $a")
}
Expand Down
10 changes: 5 additions & 5 deletions modules/core/src/test/scala/doobie/issue/262.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package doobie.issue

import cats.effect.IO
import doobie.*, doobie.implicits.*
import doobie.*
import doobie.implicits.*
import munit.CatsEffectSuite

class `262` extends munit.FunSuite {

import cats.effect.unsafe.implicits.global
class `262` extends CatsEffectSuite {

// an interpreter that returns null when we ask for statement metadata
object Interp extends KleisliInterpreter[IO](LogHandler.noop) {
Expand All @@ -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)
}

}
131 changes: 59 additions & 72 deletions modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,171 +4,158 @@

package doobie.util

import cats.effect.{IO}
import doobie.*, doobie.implicits.*
import java.sql.SQLException
import cats.effect.{IO, Ref}
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") {
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
assertEquals(IO.raiseError[Int](e).onSqlException(IO.delay(a += 1)).attempt.unsafeRunSync(), 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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import cats.kernel.Monoid
import doobie.*
import doobie.implicits.*

class ConnectionIOSuite extends munit.FunSuite {

import cats.effect.unsafe.implicits.global
class ConnectionIOSuite extends munit.CatsEffectSuite {

val xa = Transactor.fromDriverManager[IO](
driver = "org.h2.Driver",
Expand All @@ -25,11 +23,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)
}

}
17 changes: 8 additions & 9 deletions modules/core/src/test/scala/doobie/util/FragmentSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -112,15 +111,15 @@ 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)") {
val frag =
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)
}

}
Loading

0 comments on commit 00d98e4

Please sign in to comment.