-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
InsertTest.scala
326 lines (292 loc) · 12.3 KB
/
InsertTest.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
package com.typesafe.slick.testkit.tests
import slick.jdbc.{DerbyProfile, JdbcCapabilities}
import com.typesafe.slick.testkit.util.{AsyncTest, JdbcTestDB}
class InsertTest extends AsyncTest[JdbcTestDB] {
import tdb.profile.api._
def testSimple = {
class TestTable(tag: Tag, tname: String) extends Table[(Int, String)](tag, tname) {
def id = column[Int]("id")
def name = column[String]("name")
def * = (id, name)
def ins = (id, name)
}
val src1 = TableQuery(new TestTable(_, "src1_q"))
val dst1 = TableQuery(new TestTable(_, "dst1_q"))
val dst2 = TableQuery(new TestTable(_, "dst2_q"))
val dst3 = TableQuery(new TestTable(_, "dst3_q"))
val q2 = for(s <- src1 if s.id <= 2) yield s
println("Insert 2: "+dst2.forceInsertStatementFor(q2))
val q3 = (42, "X".bind)
println("Insert 3: "+dst2.forceInsertStatementFor(q3))
val q4comp = Compiled { dst2.filter(_.id < 10) }
val dst3comp = Compiled { dst3 }
DBIO.sequence(Seq(
(src1.schema ++ dst1.schema ++ dst2.schema ++ dst3.schema).create,
src1 += (1, "A"),
src1.map(_.ins) ++= Seq((2, "B"), (3, "C")),
dst1.forceInsertQuery(src1),
dst1.to[Set].result.map(_ shouldBe Set((1,"A"), (2,"B"), (3,"C"))),
dst2.forceInsertQuery(q2),
dst2.to[Set].result.map(_ shouldBe Set((1,"A"), (2,"B"))),
dst2.forceInsertExpr(q3),
dst2.to[Set].result.map(_ shouldBe Set((1,"A"), (2,"B"), (42,"X"))),
dst3comp.forceInsertQuery(q4comp),
dst3comp.result.map(v => v.toSet shouldBe Set((1,"A"), (2,"B")))
))
}
def testEmptyInsert = {
class A(tag: Tag) extends Table[Int](tag, "A_EMPTYINSERT") {
def id = column[Int]("id", O.AutoInc, O.PrimaryKey)
def * = id
}
val as = TableQuery[A]
DBIO.seq(
as.schema.create,
as += 42,
as.result.map(_ shouldBe Seq(1))
)
}
def testUniqueInsert = {
case class ARow(email: String , id: Int = 0)
class A(tag: Tag) extends Table[ARow](tag , "A_UNIQUEINSERT"){
def id = column[Int]("id" , O.AutoInc , O.PrimaryKey)
def email = column[String]("email" , O.Unique , O.Length(254))
def * = (email , id)<>(ARow.tupled , ARow.unapply )
}
val atq = TableQuery[A]
import scala.util.{Failure, Success}
DBIO.seq(
atq.schema.create,
atq ++= Seq( ARow("unique@site.com") , ARow("user@site.com") ),
( atq += ARow("unique@site.com") ).asTry.map{
case Failure(e:java.sql.SQLException) if e.getMessage.toLowerCase.contains("unique") => ()
case Failure(e:java.sql.BatchUpdateException) if e.getMessage.toLowerCase.contains("unique") => ()
case Failure( e ) => throw e
case Success(_) => throw new Exception("Should have failed with UNIQUE constraint violation")
},
atq.result.map( _.size shouldBe 2 )
)
}
def testReturning = ifCap(jcap.returnInsertKey) {
class A(tag: Tag) extends Table[(Int, String, String)](tag, "A") {
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def s1 = column[String]("S1")
def s2 = column[String]("S2")
def * = (id, s1, s2)
}
val as = TableQuery[A]
def ins1 = as.map(a => (a.s1, a.s2)) returning as.map(_.id)
def ins2 = as.map(a => (a.s1, a.s2)) returning as.map(a => (a.id, a.s1))
def ins3 = as.map(a => (a.s1, a.s2)) returning as.map(_.id) into ((v, i) => (i, v._1, v._2))
def ins4 = as.map(a => (a.s1, a.s2)) returning as.map(a => a)
(for {
_ <- as.schema.create
_ <- (ins1 += ("a", "b")) map { (id1: Int) => id1 shouldBe 1 }
_ <- ifCap(jcap.returnInsertOther) {
(ins2 += ("c", "d")) map { (id2: (Int, String)) => id2 shouldBe (2, "c") }
}
_ <- ifNotCap(jcap.returnInsertOther) {
(ins1 += ("c", "d")) map { (id2: Int) => id2 shouldBe 2 }
}
_ <- (ins1 ++= Seq(("e", "f"), ("g", "h"))) map (_ shouldBe Seq(3, 4))
_ <- (ins3 += ("i", "j")) map (_ shouldBe (5, "i", "j"))
_ <- ifCap(jcap.returnInsertOther) {
(ins4 += ("k", "l")) map { (id5: (Int, String, String)) => id5 shouldBe (6, "k", "l") }
}
} yield ()).withPinnedSession // Some database servers (e.g. DB2) preallocate ID blocks per session
}
def testForced = {
class T(tname: String)(tag: Tag) extends Table[(Int, String, Int, Boolean, String, String, Int)](tag, tname) {
def id = column[Int]("id", O.AutoInc, O.PrimaryKey)
def name = column[String]("name")
def i1 = column[Int]("i1")
def b = column[Boolean]("b")
def s1 = column[String]("s1", O.Length(10,varying=true))
def s2 = column[String]("s2", O.Length(10,varying=true))
def i2 = column[Int]("i2")
def * = (id, name, i1, b, s1, s2, i2)
def ins = (id, name, i1, b, s1, s2, i2)
}
val ts = TableQuery(new T("t_forced")(_))
val src = TableQuery(new T("src_forced")(_))
seq(
(ts.schema ++ src.schema).create,
ts += (101, "A", 1, false, "S1", "S2", 0),
ts.map(_.ins) ++= Seq((102, "B", 1, false, "S1", "S2", 0), (103, "C", 1, false, "S1", "S2", 0)),
ts.filter(_.id > 100).length.result.map(_ shouldBe 0),
ifCap(jcap.forceInsert)(seq(
ts.forceInsert(104, "A", 1, false, "S1", "S2", 0),
ts.map(_.ins).forceInsertAll(Seq((105, "B", 1, false, "S1", "S2", 0), (106, "C", 1, false, "S1", "S2", 0))),
ts.filter(_.id > 100).length.result.map(_ shouldBe 3),
ts.map(_.ins).forceInsertAll(Seq((111, "D", 1, false, "S1", "S2", 0))),
ts.filter(_.id > 100).length.result.map(_ shouldBe 4),
src.forceInsert(90, "X", 1, false, "S1", "S2", 0),
mark("forceInsertQuery", ts.forceInsertQuery(src)).map(_ shouldBe 1),
ts.filter(_.id.between(90, 99)).result.headOption.map(_ shouldBe Some((90, "X", 1, false, "S1", "S2", 0)))
))
)
}
def testInsertOrUpdatePlain = {
class T(tag: Tag) extends Table[(Int, String)](tag, "t_merge") {
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name")
def * = (id, name)
def ins = (id, name)
}
val ts = TableQuery[T]
for {
_ <- ts.schema.create
_ <- ts ++= Seq((1, "a"), (2, "b"))
_ <- ts.insertOrUpdate((3, "c")).map(_ shouldBe 1)
_ <- ts.insertOrUpdate((1, "d")).map(_ shouldBe 1)
_ <- ts.sortBy(_.id).result.map(_ shouldBe Seq((1, "d"), (2, "b"), (3, "c")))
} yield ()
}
def testInsertOrUpdateNoPK = {
class T(tag: Tag) extends Table[(Int, String)](tag, "t_merge_no_pk") {
def id = column[Int]("id")
def name = column[String]("name")
def * = (id, name)
def ins = (id, name)
}
val ts = TableQuery[T]
val failed = try {
ts.insertOrUpdate((3, "c"))
false
}
catch {
case _: SlickException => true
}
if (!failed) throw new RuntimeException("Should fail since insertOrUpdate is not supported on a table without PK.")
DBIO.seq()
}
def testInsertOrUpdatePlainWithFuncDefinedPK: DBIOAction[Unit, NoStream, Effect.All] = {
//FIXME remove this after fixed checkInsert issue
if (tdb.profile.isInstanceOf[DerbyProfile]) return DBIO.successful(())
class T(tag: Tag) extends Table[(Int, String)](tag, "t_merge3") {
def id = column[Int]("id")
def name = column[String]("name")
def * = (id, name)
def ins = (id, name)
def pk = primaryKey("t_merge_pk_a", id)
}
val ts = TableQuery[T]
for {
_ <- ts.schema.create
_ <- ts ++= Seq((1, "a"), (2, "b"))
_ <- ts.insertOrUpdate((3, "c")).map(_ shouldBe 1)
_ <- ts.insertOrUpdate((1, "d")).map(_ shouldBe 1)
_ <- ts.sortBy(_.id).result.map(_ shouldBe Seq((1, "d"), (2, "b"), (3, "c")))
} yield ()
}
def testInsertOrUpdateAutoInc: DBIOAction[Unit, NoStream, Effect.All] = {
class T(tag: Tag) extends Table[(Int, String)](tag, "T_MERGE2") {
def id = column[Int]("ID", O.AutoInc, O.PrimaryKey)
def name = column[String]("NAME")
def * = (id, name)
def ins = (id, name)
}
val ts = TableQuery[T]
(for {
_ <- ts.schema.create
_ <- ts ++= Seq((1, "a"), (2, "b"))
_ <- ts.insertOrUpdate((0, "c")).map(_ shouldBe 1)
_ <- ts.insertOrUpdate((1, "d")).map(_ shouldBe 1)
_ <- ts.sortBy(_.id).result.map(_ shouldBe Seq((1, "d"), (2, "b"), (3, "c")))
_ <- ifCap(jcap.returnInsertKey) {
val q = ts returning ts.map(_.id)
for {
_ <- q.insertOrUpdate((0, "e")).map(_ shouldBe Some(4))
_ <- q.insertOrUpdate((1, "f")).map(_ shouldBe None)
_ <- ts.sortBy(_.id).result.map(_ shouldBe Seq((1, "f"), (2, "b"), (3, "c"), (4, "e")))
} yield ()
}
} yield ()).withPinnedSession
}
// Regression test for https://github.com/slick/slick/issues/1627
def testInsertOrUpdateWithPrimaryKeyOnly: DBIOAction[Unit, NoStream, Effect.All] =
if (!tdb.profile.capabilities.contains(JdbcCapabilities.insertOrUpdateWithPrimaryKeyOnly))
DBIO.successful(())
else {
class T(tag: Tag) extends Table[Int](tag, "mytable") {
def id = column[Int]("id", O.PrimaryKey)
def * = id
}
case class V(id: Int, value: String)
class T2(tag: Tag) extends Table[V](tag, "mytable2") {
def id = column[Int]("id")
def value = column[String]("value", O.Length(20))
def * = (id, value) <> ((V.apply _).tupled, V.unapply)
def pk = primaryKey("pk", (id, value))
}
val ts1 = TableQuery[T]
val ts2 = TableQuery[T2]
(for {
_ <- ts1.schema.create
_ <- ts1 ++= Seq((1), (2))
_ <- ts1.insertOrUpdate((0)).map(_ shouldBe 1)
_ <- ts1.insertOrUpdate((1)).map(_ shouldBe 1)
_ <- ts2.schema.create
_ <- ts2 ++= Seq(V(1, "a"), V(2, "b"))
_ <- ts2.insertOrUpdate(V(0, "c")).map(_ shouldBe 1)
_ <- ts2.insertOrUpdate(V(1, "a")).map(_ shouldBe 1)
_ <- ts2.insertOrUpdate(V(2, "d")).map(_ shouldBe 1)
} yield ()).withPinnedSession
}
// Regression test for https://github.com/slick/slick/issues/2045
def testInsertOrUpdateWithIntegrityError: DBIOAction[Unit, NoStream, Effect.All] = {
case class Book(id: Long, title: String, code: Option[String] = None)
class BooksTable(tag: Tag) extends Table[Book](tag, "books") {
def id = column[Long]("id", O.PrimaryKey)
def title = column[String]("title", O.Length(20))
def code = column[Option[String]]("code", O.Length(20))
override def * = (id, title, code) <> ((Book.apply _).tupled, Book.unapply)
}
val books = TableQuery[BooksTable]
case class BookMeta(id: Long, bookId: Long, tag: Long)
class BookMetaTable(tag: Tag) extends Table[BookMeta](tag, "book_meta") {
def id = column[Long]("id")
def bookId = column[Long]("book_id")
def bookTag = column[Long]("tag")
def pk = primaryKey("book_meta_pk", (bookId, id))
def book = foreignKey("book_fk", bookId, books)(_.id, onUpdate=ForeignKeyAction.Restrict, onDelete=ForeignKeyAction.Cascade)
override def * = (bookId, id, bookTag) <> ((BookMeta.apply _).tupled, BookMeta.unapply)
}
val meta = TableQuery[BookMetaTable]
import scala.util.Success
val bookMeta = BookMeta(-1, -1, 0) // fail, because there are no books.
DBIO.seq(
(meta.insertOrUpdate(bookMeta)).asTry.map {
case Success(_) => throw new Exception("Insertion should be failed.")
case _ => ()
}
)
}
// sqlite donesn't care this case
def testInsertAndUpdateShouldNotTruncateData = if (!tdb.confName.contains("sqlite")) {
case class Test(id: Int, name: String)
class TestTable(tag: Tag) extends Table[Test](tag, "test") {
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name", O.Length(2))
def * = (id, name) <> (Test.tupled, Test.unapply)
}
val ts = TableQuery[TestTable]
import scala.util.Success
DBIO.seq(
ts.schema.create,
ts ++= Seq(Test(1, "a")),
(ts ++= Seq(Test(2, "123"))).asTry.map {
case Success(_) => throw new Exception("Data is truncated. It shouldn't be succeeded.")
case _ => ()
},
ts.filter(_.id === 1).map(_.name).update("123").asTry.map {
case Success(_) => throw new Exception("Data is truncated. It shouldn't be succeeded.")
case _ => ()
},
ts.insertOrUpdate(Test(1, "123")).asTry.map {
case Success(_) => throw new Exception("Data is truncated. It shouldn't be succeeded.")
case _ => ()
}
)
} else DBIO.seq()
}