-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9edad27
Add doobie-munit package to integrate Doobie with the shiny new MUnit
ff5e0ce
fix: header check requires the copyright to have expired to years ago
b11538c
Merge branch 'master' into munit
alejandrohdezma d641056
Update MUnit version
alejandrohdezma 1d96dfe
Avoid `Auto-application to `()` is deprecated` error
alejandrohdezma 662a4dc
Pass `Location` around so failure occurs at the `check` call
alejandrohdezma 32f2de3
Update `setup-scala` Github Action to latest version to fix build fail
alejandrohdezma 357e6d4
Re-create headers
alejandrohdezma 0ecf3b5
Merge branch 'master' into munit
alejandrohdezma 229e833
Conform `doobie-munit` with `doobie-specs2`
alejandrohdezma c50c210
Merge branch 'master' into munit
alejandrohdezma 5693893
Update MUnit version so it works in Scala 3
alejandrohdezma 991b1fd
`docs` should depend on `munit`
alejandrohdezma f89be9b
`munit` package has to be imported as `_root_.munit`
alejandrohdezma 13ea0c0
Merge branch 'master' into munit
alejandrohdezma 539e1da
Add failing assertion and conform with specs2 tests
alejandrohdezma 6a3d5a3
Merge branch 'master' into munit
alejandrohdezma a16dda7
Merge branch 'master' into munit
alejandrohdezma b161a60
Merge branch 'master' into munit
jatcwang e426df8
Merge branch 'master' into munit
jatcwang 936f03e
Merge branch 'master' into munit
alejandrohdezma 1a281b8
Merge branch 'master' into munit
jatcwang 4ec4730
Merge branch 'master' into munit
jatcwang 5b488a2
Merge branch 'master' into munit
alejandrohdezma 9955bee
Merge branch 'master' into munit
jatcwang 703211c
Merge branch 'master' into munit
Kazark 4b3b84b
Update build.sbt
Kazark 176a5a1
Merge branch 'master' into munit
jatcwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
## Unit Testing | ||
|
||
The YOLO-mode query checking feature demonstated in an earlier chapter is also available as a trait you can mix into your [Specs2](http://etorreborre.github.io/specs2/) or [ScalaTest](http://www.scalatest.org/) unit tests. | ||
The YOLO-mode query checking feature demonstated in an earlier chapter is also available as a trait you can mix into your [Specs2](http://etorreborre.github.io/specs2/), [ScalaTest](http://www.scalatest.org/) or [MUnit](https://scalameta.org/munit) unit tests. | ||
|
||
### Setting Up | ||
|
||
|
@@ -129,3 +129,25 @@ Details are shown for failing tests. | |
// Run a test programmatically. Usually you would do this from sbt, bloop, etc. | ||
(new AnalysisTestScalaCheck).execute(color = false) | ||
``` | ||
|
||
### The MUnit Package | ||
|
||
The `doobie-munit` add-on provides a mix-in trait that we can add to any `Assertions` implementation (like `FunSuite`) much like the ScalaTest package above. | ||
|
||
```scala mdoc:silent | ||
import munit._ | ||
|
||
class AnalysisTestScalaCheck extends FunSuite with doobie.munit.IOChecker { | ||
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. Does 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. Yes. |
||
|
||
override val colors = doobie.util.Colors.None // just for docs | ||
|
||
val transactor = Transactor.fromDriverManager[IO]( | ||
"org.postgresql.Driver", "jdbc:postgresql:world", "postgres", "" | ||
) | ||
|
||
test("trivial") { check(trivial) } | ||
test("biggerThan") { check(biggerThan(0)) } | ||
test("update") { check(update("", "")) } | ||
|
||
} | ||
``` |
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,68 @@ | ||
// Copyright (c) 2013-2018 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 munit.Assertions | ||
import scala.reflect.runtime.universe.TypeTag | ||
|
||
/** | ||
* Mix-in trait for specifications that enables checking of doobie `Query` and `Update` values. | ||
* Users must provide an effect type `M` as well as a `Transactor[M]` and instances. As a | ||
* convenience doobie provides specializations for common effect types (see other types in this | ||
* package). | ||
* | ||
* {{{ | ||
* class ExampleSpec 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) } | ||
* | ||
* } | ||
* }}} | ||
*/ | ||
trait Checker[M[_]] extends CheckerBase[M] { self: Assertions => | ||
|
||
def check[A: Analyzable](a: A) = checkImpl(Analyzable.unpack(a)) | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.Overloading")) | ||
def checkOutput[A: TypeTag](q: Query0[A]) = | ||
checkImpl(AnalysisArgs( | ||
s"Query0[${typeName[A]}]", q.pos, q.sql, q.outputAnalysis | ||
)) | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.Overloading")) | ||
def checkOutput[A: TypeTag, B: TypeTag](q: Query[A, B]) = | ||
checkImpl(AnalysisArgs( | ||
s"Query[${typeName[A]}, ${typeName[B]}]", q.pos, q.sql, q.outputAnalysis | ||
)) | ||
|
||
private def checkImpl(args: AnalysisArgs) = { | ||
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 | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
// Copyright (c) 2013-2018 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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 :)