-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathPostgreSQLSampleSpec.scala
407 lines (382 loc) · 12.5 KB
/
PostgreSQLSampleSpec.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
package sample
import java.time.Instant
import java.time.temporal.ChronoUnit
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scalikejdbc._, async._
import unit._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class PostgreSQLSampleSpec
extends AnyFlatSpec
with Matchers
with DBSettings
with Logging {
val column = AsyncLover.column
val createdTime = Instant.now.plusMillis(123).truncatedTo(ChronoUnit.MICROS)
val al = AsyncLover.syntax("al")
it should "count" in {
val countFuture: Future[Long] = AsyncDB.withPool { implicit s =>
withSQL { select(sqls.count).from(AsyncLover as al) }
.map(_.long(1))
.single
.future()
.map(_.get)
}
val c = Await.result(countFuture, 5.seconds)
c should be > 0L
}
it should "select a single value" in {
val resultFuture: Future[Option[AsyncLover]] = AsyncDB.withPool {
implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 1) }
.map(AsyncLover(al))
.single
.future()
}
val result = Await.result(resultFuture, 5.seconds)
result.isDefined should be(true)
}
it should "repeat selecting a single value" in {
def resultFuture: Future[Option[AsyncLover]] = AsyncDB.withPool {
implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 1) }
.map(AsyncLover(al))
.single
.future()
}
val futures = Future.sequence(
(1 to (asyncConnectionPoolSettings.maxPoolSize + 1)).map(_ =>
resultFuture
)
)
val results = Await.result(futures, 5.seconds)
results.foreach(result => result.isDefined should be(true))
}
it should "select values as a Iterable" in {
val resultsFuture: Future[Iterable[AsyncLover]] = AsyncDB.withPool {
implicit s =>
withSQL { select.from(AsyncLover as al).limit(2) }
.map(AsyncLover(al))
.iterable
.future()
}
Await.result(resultsFuture, 5.seconds)
val results = resultsFuture.value.get.get
results.size should equal(2)
}
it should "select values as a List" in {
val resultsFuture: Future[List[AsyncLover]] = AsyncDB.withPool {
implicit s =>
withSQL { select.from(AsyncLover as al).limit(2) }
.map(AsyncLover(al))
.list
.future()
}
val results = Await.result(resultsFuture, 5.seconds)
results.size should equal(2)
}
it should "read values with convenience methods" in {
val generatedIdFuture: Future[Long] = AsyncDB.withPool { implicit s =>
withSQL {
insert
.into(AsyncLover)
.namedValues(
column.name -> "Eric",
column.rating -> 2,
column.isReactive -> false,
column.createdAt -> createdTime
)
.returningId
}.updateAndReturnGeneratedKey.future()
}
// in AsyncLover#apply we are using get with typebinders, specialized getters should work
val generatedId = Await.result(generatedIdFuture, 5.seconds)
val created = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, generatedId) }
.map((rs: WrappedResultSet) => {
AsyncLover(
id = rs.get[Long](al.resultName.id),
name = rs.get[String](al.resultName.name),
rating = rs.get[Int](al.resultName.rating),
isReactive = rs.get[Boolean](al.resultName.isReactive),
lunchtime = rs.get[Option[java.sql.Time]](al.resultName.lunchtime),
birthday = rs.get[Option[Instant]](al.resultName.lunchtime),
createdAt = rs.get[Instant](al.resultName.createdAt)
)
})
.single
.apply()
}.get
created.id should equal(generatedId)
created.name should equal("Eric")
created.rating should equal(2)
created.isReactive should be(false)
created.createdAt should equal(createdTime)
}
it should "return generated key" in {
val generatedIdFuture: Future[Long] = AsyncDB.withPool { implicit s =>
withSQL {
insert
.into(AsyncLover)
.namedValues(
column.name -> "Eric",
column.rating -> 2,
column.isReactive -> false,
column.createdAt -> createdTime
)
.returningId
}.updateAndReturnGeneratedKey.future()
}
// the generated key should be found
val generatedId = Await.result(generatedIdFuture, 5.seconds)
val created = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, generatedId) }
.map(AsyncLover(al))
.single
.apply()
}.get
created.id should equal(generatedId)
created.name should equal("Eric")
created.rating should equal(2)
created.isReactive should be(false)
created.createdAt should equal(createdTime)
}
it should "update" in {
// updating queries should be successful
DB autoCommit { implicit s =>
withSQL { delete.from(AsyncLover).where.eq(column.id, 1004) }.update
.apply()
withSQL {
insert
.into(AsyncLover)
.namedValues(
column.id -> 1004,
column.name -> "Chris",
column.rating -> 5,
column.isReactive -> true,
column.createdAt -> createdTime
)
}.update.apply()
}
val deletion: Future[Int] = AsyncDB.withPool { implicit s =>
withSQL { delete.from(AsyncLover).where.eq(column.id, 1004) }.update
.future()
}
Await.result(deletion, 5.seconds)
// should be committed
val deleted = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 1004) }
.map(AsyncLover(al))
.single
.apply()
}
deleted.isDefined should be(false)
}
it should "execute" in {
// execution should be successful
val id = DB.autoCommit { implicit s =>
withSQL {
insert
.into(AsyncLover)
.namedValues(
column.name -> "Chris",
column.rating -> 5,
column.isReactive -> true,
column.createdAt -> createdTime
)
}.updateAndReturnGeneratedKey.apply()
}
val deletion: Future[Boolean] = AsyncDB.withPool { implicit s =>
withSQL { delete.from(AsyncLover).where.eq(column.id, id) }.execute
.future()
}
Await.result(deletion, 5.seconds)
// should be committed
val deleted = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, id) }
.map(AsyncLover(al))
.single
.apply()
}
deleted.isDefined should be(false)
}
it should "update in a local transaction" in {
(1 to 10).foreach { _ =>
val generatedIdFuture: Future[Long] = AsyncDB.localTx { implicit tx =>
withSQL(
insert
.into(AsyncLover)
.namedValues(
column.name -> "Patric",
column.rating -> 2,
column.isReactive -> false,
column.createdAt -> createdTime
)
.returningId
).updateAndReturnGeneratedKey.future()
}
val generatedId = Await.result(generatedIdFuture, 5.seconds)
val created = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, generatedId) }
.map(AsyncLover(al))
.single
.apply()
}.get
created.id should equal(generatedId)
created.name should equal("Patric")
created.rating should equal(2)
created.isReactive should be(false)
created.createdAt should equal(createdTime)
}
}
it should "rollback in a local transaction" in {
(1 to 10).foreach { _ =>
DB.autoCommit { implicit s =>
withSQL { delete.from(AsyncLover).where.eq(column.id, 1003) }.update
.apply()
}
val failureFuture: Future[Unit] = AsyncDB.localTx { implicit tx =>
import FutureImplicits._
for {
_ <- insert
.into(AsyncLover)
.namedValues(
column.id -> 1003,
column.name -> "Patric",
column.rating -> 2,
column.isReactive -> false,
column.createdAt -> createdTime
)
_ <- sql"invalid_query".execute // failure
} yield ()
}
// exception should be thrown
try {
Await.result(failureFuture, 5.seconds)
fail("Exception expected")
} catch {
case e: Exception => log.debug("expected", e)
}
failureFuture.value.get.isFailure should be(true)
// should be rolled back
val notCreated = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 1003) }
.map(AsyncLover(al))
.single
.apply()
}
notCreated.isDefined should be(false)
}
}
it should "provide transaction by AsyncTx.withSQLBuilders" in {
pending // TODO
(1 to 10).foreach { _ =>
val deletionAndCreation: Future[Seq[AsyncQueryResult]] =
AsyncDB.withPool { implicit s =>
AsyncTx
.withSQLBuilders(
delete.from(AsyncLover).where.eq(column.id, 997),
insert
.into(AsyncLover)
.namedValues(
column.id -> 997,
column.name -> "Eric",
column.rating -> 2,
column.isReactive -> false,
column.createdAt -> createdTime
)
)
.future()
}
Await.result(deletionAndCreation, 5.seconds)
deletionAndCreation.value.get.isSuccess should be(true)
deletionAndCreation.value.get.get.size should be(2)
// should be found
val created = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 997) }
.map(AsyncLover(al))
.single
.apply()
}.get
created.id should equal(997)
created.name should equal("Eric")
created.rating should equal(2)
created.isReactive should be(false)
created.createdAt should equal(createdTime)
}
}
it should "provide transactional deletion by AsyncTx.withSQLBuilders" in {
(1 to 10).foreach { _ =>
DB.autoCommit { implicit s =>
withSQL { delete.from(AsyncLover).where.eq(column.id, 998) }.update
.apply()
}
val creationAndDeletion: Future[Seq[AsyncQueryResult]] =
AsyncDB.withPool { implicit s =>
AsyncTx
.withSQLBuilders(
insert
.into(AsyncLover)
.namedValues(
column.id -> 998,
column.name -> "Fred",
column.rating -> 5,
column.isReactive -> true,
column.createdAt -> new java.util.Date
),
delete.from(AsyncLover).where.eq(column.id, 998)
)
.future()
}
Await.result(creationAndDeletion, 5.seconds)
creationAndDeletion.value.get.isSuccess should be(true)
creationAndDeletion.value.get.get.size should be(2)
// should be committed
val deleted = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 998) }
.map(AsyncLover(al))
.single
.apply()
}
deleted.isDefined should be(false)
}
}
it should "rollback in a transaction when using AsyncTx.withSQLs" in {
(1 to 10).foreach { _ =>
val failure: Future[Seq[AsyncQueryResult]] = AsyncDB.withPool {
implicit s =>
AsyncTx
.withSQLs(
insert
.into(AsyncLover)
.namedValues(
column.id -> 999,
column.name -> "George",
column.rating -> 1,
column.isReactive -> false,
column.createdAt -> Instant.now
)
.toSQL,
sql"invalid_query" // failure
)
.future()
}
try {
Await.result(failure, 5.seconds)
fail("Exception expected")
} catch {
case e: Exception => log.debug("expected", e)
}
failure.value.get.isSuccess should be(false)
val notFound = DB.readOnly { implicit s =>
withSQL { select.from(AsyncLover as al).where.eq(al.id, 999) }
.map(AsyncLover(al))
.single
.apply()
}
notFound.isDefined should be(false)
}
}
}