-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryStackSpec.scala
More file actions
63 lines (53 loc) · 1.94 KB
/
QueryStackSpec.scala
File metadata and controls
63 lines (53 loc) · 1.94 KB
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
package com.zengularity.querymonad.test.module.sql
import acolyte.jdbc.{
AcolyteDSL,
QueryExecution,
QueryResult => AcolyteQueryResult
}
import acolyte.jdbc.RowLists.rowList1
import anorm.{SQL, SqlParser}
import org.specs2.concurrent.ExecutionEnv
import org.specs2.mutable.Specification
import com.zengularity.querymonad.module.sql.{
SqlQuery,
SqlQueryRunner,
SqlQueryT
}
import com.zengularity.querymonad.module.future.implicits._
import com.zengularity.querymonad.module.sql.future.WithSqlConnectionF
import com.zengularity.querymonad.test.module.sql.utils.SqlConnectionFactory
class QueryStackSpec(implicit ee: ExecutionEnv) extends Specification {
"QueryStackSpec" should {
def error(divisor: Int)(nb: Int): String =
s"$nb cannot be divided by $divisor"
val (divideBy2, divideBy3) = {
def divideBy(divisor: Int)(nb: Int): SqlQuery[Either[String, Int]] =
SqlQuery { implicit c =>
SQL(s"select ($nb / $divisor)").as(
SqlParser.get[Int](1).singleOpt
)
}.map(_.toRight(error(divisor)(nb)))
(divideBy(2) _, divideBy(3) _)
}
def resultSet(result: Int): AcolyteQueryResult = {
val schema = rowList1(classOf[Int])
schema.append(result).asResult
}
"test 1" in {
import cats.instances.either._
val handler = AcolyteDSL.handleQuery {
case QueryExecution("select (6 / 2)", Nil) => resultSet(3)
case QueryExecution("select (3 / 3)", Nil) => resultSet(1)
case _ => AcolyteQueryResult.Nil
}
val withSqlConnection: WithSqlConnectionF =
SqlConnectionFactory.withSqlConnection(handler)
val runner = SqlQueryRunner(withSqlConnection)
val query = for {
three <- SqlQueryT.fromQuery(divideBy2(6))
one <- SqlQueryT.fromQuery(divideBy3(three))
} yield one
runner(query) aka "one" must beTypedEqualTo(Right(1): Either[String, Int]).await
}
}
}