-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add doobie-munit package to integrate Doobie with the shiny new MUnit #1233
Changes from 14 commits
9edad27
ff5e0ce
b11538c
d641056
1d96dfe
662a4dc
32f2de3
357e6d4
0ecf3b5
229e833
c50c210
5693893
991b1fd
f89be9b
13ea0c0
539e1da
6a3d5a3
a16dda7
b161a60
e426df8
936f03e
1a281b8
4ec4730
5b488a2
9955bee
703211c
4b3b84b
176a5a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+51
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't levering https://github.com/typelevel/munit-cats-effect be a better choice?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have implemented the munit-cats-effect integration at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like a good idea. It'll also make CE3 migration easier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation also changes the semantics of check; it is no longer eager, is this desired? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If execution thread gets to munit (and all other test frameworks that I know) all use call-by-name to allow skipping tests, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the evaluation of the check effect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah I think the other integrations (scalatest/specs) run it eagerly. Perhaps it is worth keeping that behaviour to make it easier for people to migrate between test libs. (I know that in my codebase I have a few So I guess cats-effect munit won't help us here. Sorry! |
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/** Implementation of Checker[IO] */ | ||||||||||||||||||||||||||||||||||||||
trait IOChecker extends Checker[IO] { | ||||||||||||||||||||||||||||||||||||||
self: Assertions => | ||||||||||||||||||||||||||||||||||||||
val M: Effect[IO] = implicitly | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} |
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 | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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]) } | ||
jatcwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
|
||
class IOCheckerCheck extends CheckerChecks[IO] with IOChecker { | ||
def contextShift: ContextShift[IO] = | ||
IO.contextShift(ExecutionContext.global) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take or leave it: might a bulleted list be easier to view?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a reasonable idea; the general principle of the PR was to try to stick closely with what's there. We'll see what @tpolecat says.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idm what's currently here, since it's featured prominantly in 13-Unit-Testing.md :)