Guest User

Untitled

a guest
Mar 6th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import org.specs2._
  2. import org.specs2.specification.core.{ Fragment, Fragments }
  3. import org.specs2.specification.create.{ FormattingFragments => Format }
  4. import org.specs2.specification.dsl.Online._
  5. import cats.implicits._
  6. import cats.effect._
  7. import doobie._
  8. import doobie.util.testing._
  9. import org.specs2.mutable.Specification
  10. import scala.reflect.runtime.universe.TypeTag
  11.  
  12. trait DoobieChecker[F[_]]{
  13. def checkQuery[A: TypeTag, B: TypeTag](q: Query[A, B]) : F[Fragments]
  14. def checkQuery0[A: TypeTag](q: Query0[A]): F[Fragments]
  15. def checkUpdate[A: TypeTag](q: Update[A]): F[Fragments]
  16. def checkUpdate0(q: Update0): F[Fragments]
  17. }
  18.  
  19. object DoobieChecker {
  20. def apply[F[_]](implicit ev: DoobieChecker[F]): DoobieChecker[F] = ev
  21.  
  22.  
  23. implicit def effectUniqueImpl[F[_]: Effect](
  24. spec: Specification,
  25. driverName: String,
  26. jdbcUrl: String,
  27. dbUserName: String,
  28. dbPassword: String,
  29. setup: F[Unit]
  30. ): DoobieChecker[F] = new DoobieChecker[F]{
  31. def checkQuery[A: TypeTag, B: TypeTag](q: Query[A, B]) : F[Fragments] = for {
  32. _ <- setup
  33. transactor <- Sync[F].delay(Transactor.fromDriverManager[F](
  34. driverName,
  35. jdbcUrl,
  36. dbUserName,
  37. dbPassword
  38. ))
  39. fragments <- checkImpl(spec, transactor, Analyzable.unpack(q))
  40. } yield fragments
  41.  
  42. override def checkQuery0[A: TypeTag](q: Query0[A]): F[Fragments] = for {
  43. _ <- setup
  44. transactor <- Sync[F].delay(Transactor.fromDriverManager[F](
  45. driverName,
  46. jdbcUrl,
  47. dbUserName,
  48. dbPassword
  49. ))
  50. fragments <- checkImpl(spec,transactor, Analyzable.unpack(q))
  51. } yield fragments
  52.  
  53. def checkUpdate[A: TypeTag](q: Update[A]): F[Fragments] = for {
  54. _ <- setup
  55. transactor <- Sync[F].delay(Transactor.fromDriverManager[F](
  56. driverName,
  57. jdbcUrl,
  58. dbUserName,
  59. dbPassword
  60. ))
  61. fragments <- checkImpl(spec,transactor, Analyzable.unpack(q))
  62. } yield fragments
  63. def checkUpdate0(q: Update0): F[Fragments]= for {
  64. _ <- setup
  65. transactor <- Sync[F].delay(Transactor.fromDriverManager[F](
  66. driverName,
  67. jdbcUrl,
  68. dbUserName,
  69. dbPassword
  70. ))
  71. fragments <- checkImpl(spec, transactor, Analyzable.unpack(q))
  72. } yield fragments
  73.  
  74. }
  75.  
  76.  
  77.  
  78. private def checkImpl[F[_]: Effect](spec: Specification, transactor: Transactor[F], arguments: AnalysisArgs): F[Fragments] = {
  79. import spec._
  80. Sync[F].delay(
  81. // continuesWith is necessary to make sure the query doesn't run too early
  82. s"${arguments.header}\n\n${arguments.cleanedSql.padLeft(" ")}\n" >> ok.continueWith {
  83. analyzeIO[F](arguments, transactor).map{report =>
  84. indentBlock(
  85. report.items.map { item =>
  86. item.description ! item.error.fold(ok) {
  87. err => ko(err.wrap(70).toString)
  88. }
  89. }
  90. )
  91. }.unsafeRunSync
  92. }
  93. )
  94. }
  95.  
  96. private def indentBlock(fs: Seq[Fragment]): Fragments =
  97. // intersperse fragments with newlines, and indent them.
  98. // This differs from standard version (FragmentsDsl.fragmentsBlock()) in
  99. // that any failure gets properly indented, too.
  100. Fragments.empty
  101. .append(Format.t)
  102. .append(fs.flatMap(Seq(Format.br, _)))
  103. .append(Format.bt)
  104.  
  105. }
Add Comment
Please, Sign In to add comment