-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
ActionTest.scala
424 lines (379 loc) · 13 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package com.typesafe.slick.testkit.tests
import com.typesafe.slick.testkit.util.{AsyncTest, RelationalTestDB, StandardTestDBs}
import slick.lifted.ProvenShape
import scala.concurrent.Future
class ActionTest extends AsyncTest[RelationalTestDB] {
import tdb.profile.api._
def testSimpleActionAsFuture = {
class T(tag: Tag) extends Table[Int](tag, "t".withUniquePostFix) {
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, "t".withUniquePostFix) {
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, "t".withUniquePostFix) {
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 _)
val a6 = DBIO.fold((1 to 50000).toSeq.map(i => LiteralColumn(i).result), 0)(_ + _)
val a7 = (1 to 10000).map(_ => DBIO.successful("a7")).reduceLeft((a, b) => a flatMap (_ => b) andThen b)
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"),
a6.map(_ shouldBe (1 to 50000).sum),
a7.map(_ shouldBe "a7")
)
} else DBIO.successful(())
def testOptionSequence = {
class T(tag: Tag) extends Table[Option[Int]](tag, "t".withUniquePostFix) {
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, "t".withUniquePostFix) {
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, "t".withUniquePostFix) {
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, "t".withUniquePostFix) {
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 ()
}
def testCreateIfNotExistsDropIfExists = {
import scala.util.{Success, Failure}
class T(_tag: Tag) extends Table[Int](_tag , "ddl_test"){
def a = column[Int]("a")
def * = a
}
class S(_tag: Tag) extends Table[String](_tag, "ddl_test2"){
def a = column[String]("a2")
def * = a
}
class A(tag: Tag) extends Table[(Int, Int, String)](tag, "a") {
def k1 = column[Int]("k1")
def k2 = column[Int]("k2")
def s = column[String]("s")
def * = (k1, k2, s)
def pk = primaryKey("pk_a", (k1, k2))
}
val as = TableQuery[A]
val ts = TableQuery[T]
val ts2 = TableQuery[S]
val batch = (ts.schema ++ ts2.schema)
for{
_ <- ts.schema.create
_ <- ts2.schema.create
_ <- ts.schema.createIfNotExists
_ <- ts2.schema.createIfNotExists
initial1 <- ts.result
_ = assert(initial1.toSet == Set())
initial2 <- ts2.result
_ = assert(initial2.toSet == Set())
res <- (ts ++= Seq(2, 3, 1, 5, 4)) >> ts.result
_ = assert(res.toSet == Set(2, 3, 1, 5, 4))
res2 <- (ts2 ++= Seq("2", "3", "1", "5", "4")) >> ts2.result
_ = assert(res2.toSet == Set("2", "3", "1", "5", "4"))
_ <- ts.schema.drop
_ <- ts2.schema.drop
_ <- ts.schema.dropIfExists
_ <- ts2.schema.dropIfExists
_ <- ts.schema.createIfNotExists
_ <- ts.schema.create.asTry.map{
case Failure(e:java.sql.SQLException) => ()
case Failure(e: slick.SlickException) if tdb.profile == slick.memory.MemoryProfile => ()
case Failure(e) => throw e
case Success(_) => throw new Exception("Should have failed to create new table. Table exists")
}
_ <- ts2.schema.createIfNotExists
_ <- ts2.schema.create.asTry.map{
case Failure(e:java.sql.SQLException) => ()
case Failure(e: slick.SlickException) if tdb.profile == slick.memory.MemoryProfile => ()
case Failure(e) => throw e
case Success(_) => throw new Exception("Should have failed to create new table. Table exists")
}
initial3 <- ts.result
_ = assert(initial3.toSet == Set())
initial4 <- ts2.result
_ = assert(initial4.toSet == Set())
_ <- ts.schema.dropIfExists
_ <- ts.schema.drop.asTry.map{
case Failure(e:java.sql.SQLException) => ()
case Failure(e: slick.SlickException) if tdb.profile == slick.memory.MemoryProfile => ()
case Failure(e) => throw e
case Success(_) => throw new Exception("Should have failed to drop table. Table already dropped")
}
_ <- ts2.schema.dropIfExists
_ <- ts2.schema.drop.asTry.map{
case Failure(e:java.sql.SQLException) => ()
case Failure(e: slick.SlickException) if tdb.profile == slick.memory.MemoryProfile => ()
case Failure(e) => throw e
case Success(_) => throw new Exception("Should have failed to drop table. Table already dropped")
}
//test batch create/drop
_ <- batch.create
_ <- batch.createIfNotExists
res <- (ts ++= Seq(2, 3, 1, 5, 4)) >> ts.result
_ = assert(res.toSet == Set(2, 3, 1, 5, 4))
res2 <- (ts2 ++= Seq("2", "3", "1", "5", "4")) >> ts2.result
_ = assert(res2.toSet == Set("2", "3", "1", "5", "4"))
_ <- batch.drop
_ <- batch.dropIfExists
_ <- batch.createIfNotExists
_ <- batch.create.asTry.map{
case Failure(e:java.sql.SQLException) => ()
case Failure(e: slick.SlickException) if tdb.profile == slick.memory.MemoryProfile => ()
case Failure(e) => throw e
case Success(_) => throw new Exception("Should have failed to create new table. Table exists")
}
_ <- batch.dropIfExists
_ <- batch.drop.asTry.map{
case Failure(e:java.sql.SQLException) => ()
case Failure(e: slick.SlickException) if tdb.profile == slick.memory.MemoryProfile => ()
case Failure(e) => throw e
case Success(_) => throw new Exception("Should have failed to drop table. Table already dropped")
}
//test create/drop with constraints
_ <- as.schema.createIfNotExists
_ <- as ++= Seq((1, 1, "a11"), (1, 2, "a12"), (2, 1, "a21"), (2, 2, "a22") )
_ <- (as += (1, 1, "a11-conflict")).failed
_ <- as.schema.drop
} yield ()
}
/** If an original table already exists and we add an FK to the schema and try to dropIfExists()
* it shouldn't fail. */
def testDropIfExistsWithFK = {
// a parent table
class T(_tag: Tag) extends Table[Int](_tag , "ddl_test_fk_drop_t"){
def t = column[Int]("a")
def * = t
}
val ts = TableQuery[T]
// a child table---current version
class S1(_tag: Tag) extends Table[Int](_tag, "ddl_test_fk_drop_s"){
def s1 = column[Int]("s1")
def * = s1
}
val s1s = TableQuery[S1]
// a child table---new version with a FK added
class S2(_tag: Tag) extends Table[(Int, Int)](_tag, "ddl_test_fk_drop_s"){
def s1 = column[Int]("s1")
def ref = column[Int]("ref")
def * = (s1, ref)
def fk = foreignKey("FK_S2_T", ref, ts)(_.t)
}
val s2s = TableQuery[S2]
for {
_ <- s1s.schema.create
_ <- s2s.schema.dropIfExists
} yield ()
}
def testDropIfExistsWithPK = {
case class D(a: Int, b: Int)
class T1(tag: Tag) extends Table[D](tag, "t1") {
def a: Rep[Int] = column[Int]("a")
def b: Rep[Int] = column[Int]("b")
def pk = primaryKey("books_pk", a)
override def * : ProvenShape[D] =
(a, b) <> ((D.apply _).tupled, D.unapply)
}
class T2(tag: Tag) extends Table[D](tag, "t2") {
def a: Rep[Int] = column[Int]("a", O.PrimaryKey)
def b: Rep[Int] = column[Int]("b")
override def * : ProvenShape[D] =
(a, b) <> ((D.apply _).tupled, D.unapply)
}
val t1 = TableQuery[T1]
val t2 = TableQuery[T2]
for {
_ <- t1.schema.create
_ <- t1.schema.dropIfExists
_ <- t2.schema.create
_ <- t2.schema.dropIfExists
} yield ()
}
def testDropIfExistsWithoutTableCreation = {
case class D(a: Int, b: Int)
class T1(tag: Tag) extends Table[D](tag, "t1") {
def a: Rep[Int] = column[Int]("a")
def b: Rep[Int] = column[Int]("b")
def pk = primaryKey("books_pk", a)
override def * : ProvenShape[D] =
(a, b) <> ((D.apply _).tupled, D.unapply)
}
class T2(tag: Tag) extends Table[D](tag, "t2") {
def a: Rep[Int] = column[Int]("a", O.PrimaryKey)
def b: Rep[Int] = column[Int]("b")
override def * : ProvenShape[D] =
(a, b) <> ((D.apply _).tupled, D.unapply)
}
class T3(tag: Tag) extends Table[D](tag, "t3") {
def a: Rep[Int] = column[Int]("a")
def b: Rep[Int] = column[Int]("b")
def pk = primaryKey("pk", (a, b))
override def * : ProvenShape[D] =
(a, b) <> ((D.apply _).tupled, D.unapply)
}
val t1 = TableQuery[T1]
val t2 = TableQuery[T2]
val t3 = TableQuery[T3]
for {
_ <- t1.schema.dropIfExists
_ <- t2.schema.dropIfExists
_ <- t3.schema.dropIfExists
} yield ()
}
}