-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1233 from Kazark/munit
Add doobie-munit package to integrate Doobie with the shiny new MUnit
- Loading branch information
Showing
6 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
modules/munit/src/main/scala/doobie/munit/analysisspec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie.munit | ||
|
||
import cats.effect.{ Effect, IO } | ||
import doobie.util.query.{Query, Query0} | ||
import doobie.util.testing._ | ||
import org.tpolecat.typename._ | ||
import munit.Assertions | ||
import munit.Location | ||
|
||
/** | ||
* Module with a mix-in trait for specifications that enables checking of doobie `Query` and `Update` values. | ||
* | ||
* {{{ | ||
* class ExampleSuite extends FunSuite with IOChecker { | ||
* | ||
* // The transactor to use for the tests. | ||
* val transactor = Transactor.fromDriverManager[IO]( | ||
* "org.postgresql.Driver", | ||
* "jdbc:postgresql:world", | ||
* "postgres", "" | ||
* ) | ||
* | ||
* // Now just mention the queries. Arguments are not used. | ||
* test("findByNameAndAge") { check(MyDaoModule.findByNameAndAge(null, 0)) } | ||
* test("allWoozles") { check(MyDaoModule.allWoozles) } | ||
* | ||
* } | ||
* }}} | ||
*/ | ||
object analysisspec { | ||
|
||
trait Checker[M[_]] extends CheckerBase[M] { this: Assertions => | ||
|
||
def check[A: Analyzable](a: A)(implicit loc: Location) = checkImpl(Analyzable.unpack(a)) | ||
|
||
def checkOutput[A: TypeName](q: Query0[A])(implicit loc: Location) = | ||
checkImpl(AnalysisArgs( | ||
s"Query0[${typeName[A]}]", q.pos, q.sql, q.outputAnalysis | ||
)) | ||
|
||
def checkOutput[A: TypeName, B: TypeName](q: Query[A, B])(implicit loc: Location) = | ||
checkImpl(AnalysisArgs( | ||
s"Query[${typeName[A]}, ${typeName[B]}]", q.pos, q.sql, q.outputAnalysis | ||
)) | ||
|
||
private def checkImpl(args: AnalysisArgs)(implicit loc: Location) = { | ||
val report = analyzeIO(args, transactor).unsafeRunSync() | ||
if (!report.succeeded) { | ||
fail( | ||
formatReport(args, report, colors) | ||
.padLeft(" ") | ||
.toString | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** Implementation of Checker[IO] */ | ||
trait IOChecker extends Checker[IO] { | ||
self: Assertions => | ||
val M: Effect[IO] = implicitly | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie | ||
|
||
package object munit { | ||
|
||
type Checker[M[_]] = analysisspec.Checker[M] | ||
type IOChecker = analysisspec.IOChecker | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
modules/munit/src/test/scala/doobie/munit/CheckerTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie.munit | ||
|
||
import cats.effect.{ ContextShift, IO } | ||
import doobie.syntax.string._ | ||
import doobie.util.transactor.Transactor | ||
import munit._ | ||
import scala.concurrent.ExecutionContext | ||
|
||
trait CheckerChecks[M[_]] extends FunSuite with Checker[M] { | ||
|
||
implicit def contextShift: ContextShift[M] | ||
|
||
lazy val transactor = Transactor.fromDriverManager[M]( | ||
"org.h2.Driver", | ||
"jdbc:h2:mem:queryspec;DB_CLOSE_DELAY=-1", | ||
"sa", "" | ||
) | ||
|
||
test("trivial") { check(sql"select 1".query[Int]) } | ||
|
||
test("fail".fail) { check(sql"select 1".query[String]) } | ||
|
||
final case class Foo[F[_]](x: Int) | ||
|
||
test ("trivial case-class"){ check(sql"select 1".query[Foo[cats.Id]]) } | ||
|
||
} | ||
|
||
class IOCheckerCheck extends CheckerChecks[IO] with IOChecker { | ||
def contextShift: ContextShift[IO] = | ||
IO.contextShift(ExecutionContext.global) | ||
} |