-
Notifications
You must be signed in to change notification settings - Fork 226
/
SQLInterpolationSpec.scala
118 lines (100 loc) · 3.98 KB
/
SQLInterpolationSpec.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package scalikejdbc
import org.scalatest._
import org.scalatest.matchers._
import org.joda.time._
class SQLInterpolationSpec extends FlatSpec with ShouldMatchers {
import scalikejdbc.interpolation._
import scalikejdbc.interpolation.Implicits._
behavior of "SQLInterpolation"
val props = new java.util.Properties
using(new java.io.FileInputStream("scalikejdbc-library/src/test/resources/jdbc.properties")) { in => props.load(in) }
val driverClassName = props.getProperty("driverClassName")
val url = props.getProperty("url")
val user = props.getProperty("user")
val password = props.getProperty("password")
Class.forName(driverClassName)
val poolSettings = new ConnectionPoolSettings(initialSize = 50, maxSize = 50)
ConnectionPool.singleton(url, user, password, poolSettings)
case class Group(id: Int, websiteUrl: Option[String], members: Seq[User] = Nil)
case class User(id: Int, name: Option[String], groupId: Option[Int] = None, group: Option[Group] = None)
it should "be available with here document values" in {
DB localTx {
implicit s =>
try {
sql"""create table users (id int, name varchar(256))""".execute.apply()
Seq((1, "foo"), (2, "bar"), (3, "baz")) foreach {
case (id, name) =>
sql"""insert into users values (${id}, ${name})""".update.apply()
}
val id = 3
val user = sql"""select * from users where id = ${id}""".map {
rs => User(id = rs.int("id"), name = rs.stringOpt("name"))
}.single.apply()
user.isDefined should equal(true)
} finally {
sql"""drop table users""".execute.apply()
}
}
}
it should "be available with option values" in {
DB localTx {
implicit s =>
try {
sql"create table users (id int not null, name varchar(256))".execute.apply()
Seq((1, Some("foo")), (2, None)) foreach {
case (id, name) =>
sql"insert into users values (${id}, ${name})".update.apply()
}
val id = 2
val user: User = sql"select * from users where id = ${id}".map {
rs => User(id = rs.int("id"), name = rs.stringOpt("name"))
}.single.apply().get
user.id should equal(2)
user.name should be(None)
} finally {
sql"drop table users".execute.apply()
}
}
}
it should "be available with the IN statement" in {
DB localTx {
implicit s =>
try {
sql"create table users (id int not null, name varchar(256))".execute.apply()
Seq((1, "foo"), (2, "bar"), (3, "baz")) foreach {
case (id, name) =>
sql"insert into users values (${id}, ${name})".update.apply()
}
val ids = List(1, 2, 4) ::: (100 until 200).toList
val users = sql"select * from users where id in (${ids})".map {
rs => User(id = rs.int("id"), name = rs.stringOpt("name"))
}.list.apply()
users.size should equal(2)
users.map(_.name) should equal(Seq(Some("foo"), Some("bar")))
} finally {
sql"drop table users".execute.apply()
}
}
}
it should "be available with sql syntax" in {
DB localTx {
implicit s =>
try {
sql"create table users (id int not null, name varchar(256))".execute.apply()
Seq((1, "foo"), (2, "bar"), (3, "baz")) foreach {
case (id, name) =>
sql"insert into users values (${id}, ${name})".update.apply()
}
val ids = List(1, 2, 4) ::: (100 until 200).toList
val sorting = sqls"desc"
val users = sql"select * from users where id in (${ids}) order by id ${sorting}".map {
rs => User(id = rs.int("id"), name = rs.stringOpt("name"))
}.list.apply()
users.size should equal(2)
users.map(_.name) should equal(Seq(Some("bar"), Some("foo")))
} finally {
sql"drop table users".execute.apply()
}
}
}
}