forked from slick/slick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionTest.scala
223 lines (196 loc) · 5.74 KB
/
ActionTest.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.typesafe.slick.testkit.tests
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Future
import com.typesafe.slick.testkit.util.AsyncTest
import com.typesafe.slick.testkit.util.RelationalTestDB
import com.typesafe.slick.testkit.util.StandardTestDBs
import slick.dbio.DBIOAction
import slick.dbio.Effect.Read
class ActionTest extends AsyncTest[RelationalTestDB] {
import tdb.profile.api._
def testSimpleActionAsFuture = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
for {
_ <- db.run {
ts.schema.create >>
(ts ++= Seq(2, 3, 1, 5, 4))
}
q1 = ts.sortBy(_.a).map(_.a)
f1 = db.run(q1.result)
r1 <- f1 : Future[Seq[Int]]
_ = r1 shouldBe List(1, 2, 3, 4, 5)
} yield ()
}
def testSessionPinning = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
val aSetup = ts.schema.create andThen (ts ++= Seq(2, 3, 1, 5, 4))
val aNotPinned = for {
p1 <- IsPinned
s1 <- GetSession
l <- ts.length.result
p2 <- IsPinned
s2 <- GetSession
_ = p1 shouldBe false
_ = p2 shouldBe false
_ = s1 shouldNotBe s2
} yield ()
val aFused = for {
((s1, l), s2) <- GetSession zip ts.length.result zip GetSession
_ = s1 shouldBe s2
} yield ()
val aPinned = for {
_ <- (for {
p1 <- IsPinned
s1 <- GetSession
l <- ts.length.result
p2 <- IsPinned
s2 <- GetSession
_ = p1 shouldBe true
_ = p2 shouldBe true
_ = s1 shouldBe s2
} yield ()).withPinnedSession
p3 <- IsPinned
_ = p3 shouldBe false
} yield ()
aSetup andThen aNotPinned andThen aFused andThen aPinned
}
def testStreaming = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
val q1 = ts.sortBy(_.a).map(_.a)
val p1 = db.stream {
ts.schema.create >>
(ts ++= Seq(2, 3, 1, 5, 4)) >>
q1.result
}
for {
r1 <- materialize(p1)
_ = r1 shouldBe Vector(1, 2, 3, 4, 5)
r2 <- db.run(q1.result.head)
_ = r2 shouldBe 1
r3 <- db.run(q1.result.headOption)
_ = r3 shouldBe Some(1)
} yield ()
}
def testDeepRecursion = if(tdb == StandardTestDBs.H2Disk) {
val a1 = DBIO.sequence((1 to 5000).toSeq.map(i => LiteralColumn(i).result))
val a2 = DBIO.sequence((1 to 20).toSeq.map(i => if(i%2 == 0) LiteralColumn(i).result else DBIO.from(Future.successful(i))))
val a3 = DBIO.sequence((1 to 20).toSeq.map(i => if((i/4)%2 == 0) LiteralColumn(i).result else DBIO.from(Future.successful(i))))
val a4 = DBIO.seq((1 to 50000).toSeq.map(i => DBIO.successful("a4")): _*)
val a5 = (1 to 50000).toSeq.map(i => DBIO.successful("a5")).reduceLeft(_ andThen _)
DBIO.seq(
a1.map(_ shouldBe (1 to 5000).toSeq),
a2.map(_ shouldBe (1 to 20).toSeq),
a3.map(_ shouldBe (1 to 20).toSeq),
a4.map(_ shouldBe (())),
a5.map(_ shouldBe "a5")
)
} else DBIO.successful(())
def testOptionSequence = {
class T(tag: Tag) extends Table[Option[Int]](tag, u"t") {
def a = column[Int]("a")
def * = a.?
}
val ts = TableQuery[T]
val aSetup = ts.schema.create
val a1 = LiteralColumn(Option(1)).result
val a2 = DBIO.sequenceOption(Option(LiteralColumn(1).result))
val a3 = DBIO.sequenceOption(Option.empty[DBIO[Int]])
for {
_ <- aSetup
b1 <- a1
b2 <- a2
b3 <- a3
} yield {
b1 shouldBe b2
b2 shouldNotBe b3
}
}
def testFlatten = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
for {
_ <- db.run {
ts.schema.create >>
(ts ++= Seq(2, 3, 1, 5, 4))
}
needFlatten = for (_ <- ts.result) yield ts.result
result <- db.run(needFlatten.flatten)
_ = result shouldBe Seq(2, 3, 1, 5, 4)
} yield ()
}
def testZipWith = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
for {
_ <- db.run {
ts.schema.create >>
(ts ++= Seq(2, 3, 1, 5, 4))
}
q1 = ts.sortBy(_.a).map(_.a).take(1)
result <- db.run(q1.result.head.zipWith(q1.result.head)({ case (a, b) => a + b }))
_ = result shouldBe 2
} yield ()
}
def testCollect = {
class T(tag: Tag) extends Table[Int](tag, u"t") {
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
for {
_ <- db.run {
ts.schema.create >>
(ts ++= Seq(2, 3, 1, 5, 4))
}
q1 = ts.sortBy(_.a).map(_.a).take(1)
result <- db.run(q1.result.headOption.collect {
case Some(a) => a
})
_ = result shouldBe 1
_ = result shouldFail { _ =>
val future = db.run(q1.result.headOption.collect {
case None => ()
})
import scala.concurrent.duration.Duration
import scala.concurrent.Await
Await.result(future, Duration.Inf)
}
} yield ()
}
def testTruncate = {
class T(_tag: Tag) extends Table[Int](_tag , "truncate_test"){
def a = column[Int]("a")
def * = a
}
val ts = TableQuery[T]
for{
_ <- ts.schema.create
initial <- ts.result
_ = assert(initial.toSet == Set())
res <- (ts ++= Seq(2, 3, 1, 5, 4)) >>
ts.result
_ = assert(res.toSet == Set(2, 3, 1, 5, 4))
newRes <- ts.schema.truncate >>
ts.result
_ = assert(newRes.toSet == Set())
} yield ()
}
}