-
Notifications
You must be signed in to change notification settings - Fork 348
/
Checker.scala
69 lines (61 loc) · 2.18 KB
/
Checker.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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.scalatest
import cats.effect.{ Effect, IO }
import doobie.util.query.{Query, Query0}
import doobie.util.testing._
import org.scalatest.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).
*
* {{{
* // An example specification, taken from the examples project.
* 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)
.padLeft(" ")
.toString
)
}
}
}
/** Implementation of Checker[IO] */
trait IOChecker extends Checker[IO] {
self: Assertions =>
val M: Effect[IO] = implicitly
}