Skip to content

Commit

Permalink
Use cats effect assertEquals for munit
Browse files Browse the repository at this point in the history
  • Loading branch information
floriancs committed Nov 27, 2024
1 parent 472eb3e commit 3be8bb0
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 271 deletions.
12 changes: 6 additions & 6 deletions modules/core/src/test/scala/doobie/issue/262.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
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)
}

}
103 changes: 41 additions & 62 deletions modules/core/src/test/scala/doobie/util/CatchSqlSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}

}
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)
}

}
6 changes: 3 additions & 3 deletions modules/core/src/test/scala/doobie/util/FragmentsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}

}
30 changes: 15 additions & 15 deletions modules/core/src/test/scala/doobie/util/GetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand 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")))
}

}
Loading

0 comments on commit 3be8bb0

Please sign in to comment.