forked from stephencelis/SQLite.swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueryTests.swift
584 lines (481 loc) · 23.8 KB
/
QueryTests.swift
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import XCTest
#if SQLITE_SWIFT_STANDALONE
import sqlite3
#elseif SQLITE_SWIFT_SQLCIPHER
import SQLCipher
#elseif os(Linux)
import CSQLite
#else
import SQLite3
#endif
@testable import SQLite
class QueryTests: XCTestCase {
let users = Table("users")
let id = Expression<Int64>("id")
let email = Expression<String>("email")
let age = Expression<Int?>("age")
let admin = Expression<Bool>("admin")
let optionalAdmin = Expression<Bool?>("admin")
let posts = Table("posts")
let userId = Expression<Int64>("user_id")
let categoryId = Expression<Int64>("category_id")
let published = Expression<Bool>("published")
let categories = Table("categories")
let tag = Expression<String>("tag")
func test_select_withExpression_compilesSelectClause() {
assertSQL("SELECT \"email\" FROM \"users\"", users.select(email))
}
func test_select_withStarExpression_compilesSelectClause() {
assertSQL("SELECT * FROM \"users\"", users.select(*))
}
func test_select_withNamespacedStarExpression_compilesSelectClause() {
assertSQL("SELECT \"users\".* FROM \"users\"", users.select(users[*]))
}
func test_select_withVariadicExpressions_compilesSelectClause() {
assertSQL("SELECT \"email\", count(*) FROM \"users\"", users.select(email, count(*)))
}
func test_select_withExpressions_compilesSelectClause() {
assertSQL("SELECT \"email\", count(*) FROM \"users\"", users.select([email, count(*)]))
}
func test_selectDistinct_withExpression_compilesSelectClause() {
assertSQL("SELECT DISTINCT \"age\" FROM \"users\"", users.select(distinct: age))
}
func test_selectDistinct_withExpressions_compilesSelectClause() {
assertSQL("SELECT DISTINCT \"age\", \"admin\" FROM \"users\"", users.select(distinct: [age, admin]))
}
func test_selectDistinct_withStar_compilesSelectClause() {
assertSQL("SELECT DISTINCT * FROM \"users\"", users.select(distinct: *))
}
func test_union_compilesUnionClause() {
assertSQL("SELECT * FROM \"users\" UNION SELECT * FROM \"posts\"", users.union(posts))
}
func test_union_compilesUnionAllClause() {
assertSQL("SELECT * FROM \"users\" UNION ALL SELECT * FROM \"posts\"", users.union(all: true, posts))
}
func test_join_compilesJoinClause() {
assertSQL(
"SELECT * FROM \"users\" INNER JOIN \"posts\" ON (\"posts\".\"user_id\" = \"users\".\"id\")",
users.join(posts, on: posts[userId] == users[id])
)
}
func test_join_withExplicitType_compilesJoinClauseWithType() {
assertSQL(
"SELECT * FROM \"users\" LEFT OUTER JOIN \"posts\" ON (\"posts\".\"user_id\" = \"users\".\"id\")",
users.join(.leftOuter, posts, on: posts[userId] == users[id])
)
assertSQL(
"SELECT * FROM \"users\" CROSS JOIN \"posts\" ON (\"posts\".\"user_id\" = \"users\".\"id\")",
users.join(.cross, posts, on: posts[userId] == users[id])
)
}
func test_join_withTableCondition_compilesJoinClauseWithTableCondition() {
assertSQL(
"SELECT * FROM \"users\" INNER JOIN \"posts\" ON ((\"posts\".\"user_id\" = \"users\".\"id\") AND \"published\")",
users.join(posts.filter(published), on: posts[userId] == users[id])
)
}
func test_join_whenChained_compilesAggregateJoinClause() {
assertSQL(
"SELECT * FROM \"users\" " +
"INNER JOIN \"posts\" ON (\"posts\".\"user_id\" = \"users\".\"id\") " +
"INNER JOIN \"categories\" ON (\"categories\".\"id\" = \"posts\".\"category_id\")",
users.join(posts, on: posts[userId] == users[id]).join(categories, on: categories[id] == posts[categoryId])
)
}
func test_filter_compilesWhereClause() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.filter(admin == true))
}
func test_filter_compilesWhereClause_false() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.filter(admin == false))
}
func test_filter_compilesWhereClause_optional() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.filter(optionalAdmin == true))
}
func test_filter_compilesWhereClause_optional_false() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.filter(optionalAdmin == false))
}
func test_where_compilesWhereClause() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.where(admin == true))
}
func test_where_compilesWhereClause_false() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.where(admin == false))
}
func test_where_compilesWhereClause_optional() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 1)", users.where(optionalAdmin == true))
}
func test_where_compilesWhereClause_optional_false() {
assertSQL("SELECT * FROM \"users\" WHERE (\"admin\" = 0)", users.where(optionalAdmin == false))
}
func test_filter_whenChained_compilesAggregateWhereClause() {
assertSQL(
"SELECT * FROM \"users\" WHERE ((\"age\" >= 35) AND \"admin\")",
users.filter(age >= 35).filter(admin)
)
}
func test_group_withSingleExpressionName_compilesGroupClause() {
assertSQL("SELECT * FROM \"users\" GROUP BY \"age\"",
users.group(age))
}
func test_group_withVariadicExpressionNames_compilesGroupClause() {
assertSQL("SELECT * FROM \"users\" GROUP BY \"age\", \"admin\"", users.group(age, admin))
}
func test_group_withExpressionNameAndHavingBindings_compilesGroupClause() {
assertSQL("SELECT * FROM \"users\" GROUP BY \"age\" HAVING \"admin\"", users.group(age, having: admin))
assertSQL("SELECT * FROM \"users\" GROUP BY \"age\" HAVING (\"age\" >= 30)", users.group(age, having: age >= 30))
}
func test_group_withExpressionNamesAndHavingBindings_compilesGroupClause() {
assertSQL(
"SELECT * FROM \"users\" GROUP BY \"age\", \"admin\" HAVING \"admin\"",
users.group([age, admin], having: admin)
)
assertSQL(
"SELECT * FROM \"users\" GROUP BY \"age\", \"admin\" HAVING (\"age\" >= 30)",
users.group([age, admin], having: age >= 30)
)
}
func test_order_withSingleExpressionName_compilesOrderClause() {
assertSQL("SELECT * FROM \"users\" ORDER BY \"age\"", users.order(age))
}
func test_order_withVariadicExpressionNames_compilesOrderClause() {
assertSQL("SELECT * FROM \"users\" ORDER BY \"age\", \"email\"", users.order(age, email))
}
func test_order_withArrayExpressionNames_compilesOrderClause() {
assertSQL("SELECT * FROM \"users\" ORDER BY \"age\", \"email\"", users.order([age, email]))
}
func test_order_withExpressionAndSortDirection_compilesOrderClause() {
// AssertSQL("SELECT * FROM \"users\" ORDER BY \"age\" DESC, \"email\" ASC", users.order(age.desc, email.asc))
}
func test_order_whenChained_resetsOrderClause() {
assertSQL("SELECT * FROM \"users\" ORDER BY \"age\"", users.order(email).order(age))
}
func test_reverse_withoutOrder_ordersByRowIdDescending() {
// AssertSQL("SELECT * FROM \"users\" ORDER BY \"ROWID\" DESC", users.reverse())
}
func test_reverse_withOrder_reversesOrder() {
// AssertSQL("SELECT * FROM \"users\" ORDER BY \"age\" DESC, \"email\" ASC", users.order(age, email.desc).reverse())
}
func test_limit_compilesLimitClause() {
assertSQL("SELECT * FROM \"users\" LIMIT 5", users.limit(5))
}
func test_limit_withOffset_compilesOffsetClause() {
assertSQL("SELECT * FROM \"users\" LIMIT 5 OFFSET 5", users.limit(5, offset: 5))
}
func test_limit_whenChained_overridesLimit() {
let query = users.limit(5)
assertSQL("SELECT * FROM \"users\" LIMIT 10", query.limit(10))
assertSQL("SELECT * FROM \"users\"", query.limit(nil))
}
func test_limit_whenChained_withOffset_overridesOffset() {
let query = users.limit(5, offset: 5)
assertSQL("SELECT * FROM \"users\" LIMIT 10 OFFSET 20", query.limit(10, offset: 20))
assertSQL("SELECT * FROM \"users\"", query.limit(nil))
}
func test_alias_aliasesTable() {
let managerId = Expression<Int64>("manager_id")
let managers = users.alias("managers")
assertSQL(
"SELECT * FROM \"users\" " +
"INNER JOIN \"users\" AS \"managers\" ON (\"managers\".\"id\" = \"users\".\"manager_id\")",
users.join(managers, on: managers[id] == users[managerId])
)
}
func test_with_compilesWithClause() {
let temp = Table("temp")
assertSQL("WITH \"temp\" AS (SELECT * FROM \"users\") SELECT * FROM \"temp\"",
temp.with(temp, as: users))
}
func test_with_compilesWithRecursiveClause() {
let temp = Table("temp")
assertSQL("WITH RECURSIVE \"temp\" AS (SELECT * FROM \"users\") SELECT * FROM \"temp\"",
temp.with(temp, recursive: true, as: users))
}
func test_with_compilesWithMaterializedClause() {
let temp = Table("temp")
assertSQL("WITH \"temp\" AS MATERIALIZED (SELECT * FROM \"users\") SELECT * FROM \"temp\"",
temp.with(temp, hint: .materialized, as: users))
}
func test_with_compilesWithNotMaterializedClause() {
let temp = Table("temp")
assertSQL("WITH \"temp\" AS NOT MATERIALIZED (SELECT * FROM \"users\") SELECT * FROM \"temp\"",
temp.with(temp, hint: .notMaterialized, as: users))
}
func test_with_columns_compilesWithClause() {
let temp = Table("temp")
assertSQL("WITH \"temp\" (\"id\", \"email\") AS (SELECT * FROM \"users\") SELECT * FROM \"temp\"",
temp.with(temp, columns: [id, email], recursive: false, hint: nil, as: users))
}
func test_with_multiple_compilesWithClause() {
let temp = Table("temp")
let second = Table("second")
let third = Table("third")
let query = temp
.with(temp, recursive: true, as: users)
.with(second, recursive: true, as: posts)
.with(third, hint: .materialized, as: categories)
assertSQL(
"""
WITH RECURSIVE \"temp\" AS (SELECT * FROM \"users\"),
\"second\" AS (SELECT * FROM \"posts\"),
\"third\" AS MATERIALIZED (SELECT * FROM \"categories\")
SELECT * FROM \"temp\"
""".replacingOccurrences(of: "\n", with: ""),
query
)
}
func test_insert_compilesInsertExpression() {
assertSQL(
"INSERT INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30)",
users.insert(email <- "[email protected]", age <- 30)
)
}
func test_insert_withOnConflict_compilesInsertOrOnConflictExpression() {
assertSQL(
"INSERT OR REPLACE INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30)",
users.insert(or: .replace, email <- "[email protected]", age <- 30)
)
}
func test_insert_compilesInsertExpressionWithDefaultValues() {
assertSQL("INSERT INTO \"users\" DEFAULT VALUES", users.insert())
}
func test_insert_withQuery_compilesInsertExpressionWithSelectStatement() {
let emails = Table("emails")
assertSQL(
"INSERT INTO \"emails\" SELECT \"email\" FROM \"users\" WHERE \"admin\"",
emails.insert(users.select(email).filter(admin))
)
}
func test_insert_many_compilesInsertManyExpression() {
assertSQL(
"""
INSERT INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30), ('[email protected]', 32),
('[email protected]', 83)
""".replacingOccurrences(of: "\n", with: ""),
users.insertMany([[email <- "[email protected]", age <- 30],
[email <- "[email protected]", age <- 32], [email <- "[email protected]", age <- 83]])
)
}
func test_insert_many_compilesInsertManyNoneExpression() {
assertSQL(
"INSERT INTO \"users\" DEFAULT VALUES",
users.insertMany([])
)
}
func test_insert_many_withOnConflict_compilesInsertManyOrOnConflictExpression() {
assertSQL(
"""
INSERT OR REPLACE INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30),
('[email protected]', 32), ('[email protected]', 83)
""".replacingOccurrences(of: "\n", with: ""),
users.insertMany(or: .replace, [[email <- "[email protected]", age <- 30],
[email <- "[email protected]", age <- 32],
[email <- "[email protected]", age <- 83]])
)
}
func test_insert_encodable() throws {
let emails = Table("emails")
let value = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let insert = try emails.insert(value)
assertSQL(
"""
INSERT INTO \"emails\" (\"int\", \"string\", \"bool\", \"float\", \"double\", \"date\", \"uuid\")
VALUES (1, '2', 1, 3.0, 4.0, '1970-01-01T00:00:00.000', 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F')
""".replacingOccurrences(of: "\n", with: ""),
insert
)
}
#if !os(Linux) // depends on exact JSON serialization
func test_insert_encodable_with_nested_encodable() throws {
let emails = Table("emails")
let value1 = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let value = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: "optional", sub: value1)
let insert = try emails.insert(value)
let encodedJSON = try JSONEncoder().encode(value1)
let encodedJSONString = String(data: encodedJSON, encoding: .utf8)!
let expectedSQL =
"""
INSERT INTO \"emails\" (\"int\", \"string\", \"bool\", \"float\", \"double\", \"date\", \"uuid\", \"optional\",
\"sub\") VALUES (1, '2', 1, 3.0, 4.0, '1970-01-01T00:00:00.000', 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F',
'optional', '\(encodedJSONString)')
""".replacingOccurrences(of: "\n", with: "")
// As JSON serialization gives a different result each time, we extract JSON and compare it by deserializing it
// and keep comparing the query but with the json replaced by the `JSON` string
let (expectedQuery, expectedJSON) = extractAndReplace(expectedSQL, regex: "\\{.*\\}", with: "JSON")
let (actualQuery, actualJSON) = extractAndReplace(insert.asSQL(), regex: "\\{.*\\}", with: "JSON")
XCTAssertEqual(expectedQuery, actualQuery)
XCTAssertEqual(
try JSONDecoder().decode(TestCodable.self, from: expectedJSON.data(using: .utf8)!),
try JSONDecoder().decode(TestCodable.self, from: actualJSON.data(using: .utf8)!)
)
}
#endif
func test_insert_and_search_for_UUID() throws {
struct Test: Codable {
var uuid: UUID
var string: String
}
let testUUID = UUID()
let testValue = Test(uuid: testUUID, string: "value")
let db = try Connection(.temporary)
try db.run(table.create { t in
t.column(uuid)
t.column(string)
}
)
let iQuery = try table.insert(testValue)
try db.run(iQuery)
let fQuery = table.filter(uuid == testUUID)
if let result = try db.pluck(fQuery) {
let testValueReturned = Test(uuid: result[uuid], string: result[string])
XCTAssertEqual(testUUID, testValueReturned.uuid)
} else {
XCTFail("Search for uuid failed")
}
}
func test_upsert_withOnConflict_compilesInsertOrOnConflictExpression() {
assertSQL(
"""
INSERT INTO \"users\" (\"email\", \"age\") VALUES ('[email protected]', 30) ON CONFLICT (\"email\")
DO UPDATE SET \"age\" = \"excluded\".\"age\"
""".replacingOccurrences(of: "\n", with: ""),
users.upsert(email <- "[email protected]", age <- 30, onConflictOf: email)
)
}
func test_upsert_encodable() throws {
let emails = Table("emails")
let string = Expression<String>("string")
let value = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let insert = try emails.upsert(value, onConflictOf: string)
assertSQL(
"""
INSERT INTO \"emails\" (\"int\", \"string\", \"bool\", \"float\", \"double\", \"date\", \"uuid\")
VALUES (1, '2', 1, 3.0, 4.0, '1970-01-01T00:00:00.000', 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F') ON CONFLICT (\"string\")
DO UPDATE SET \"int\" = \"excluded\".\"int\", \"bool\" = \"excluded\".\"bool\",
\"float\" = \"excluded\".\"float\", \"double\" = \"excluded\".\"double\", \"date\" = \"excluded\".\"date\",
\"uuid\" = \"excluded\".\"uuid\"
""".replacingOccurrences(of: "\n", with: ""),
insert
)
}
func test_insert_many_encodables() throws {
let emails = Table("emails")
let value1 = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let value2 = TestCodable(int: 2, string: "3", bool: true, float: 3, double: 5,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: "optional", sub: nil)
let value3 = TestCodable(int: 3, string: "4", bool: true, float: 3, double: 6,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let insert = try emails.insertMany([value1, value2, value3])
assertSQL(
"""
INSERT INTO \"emails\" (\"int\", \"string\", \"bool\", \"float\", \"double\", \"date\", \"uuid\", \"optional\", \"sub\")
VALUES (1, '2', 1, 3.0, 4.0, '1970-01-01T00:00:00.000', 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F', NULL, NULL),
(2, '3', 1, 3.0, 5.0, '1970-01-01T00:00:00.000', 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F', 'optional', NULL),
(3, '4', 1, 3.0, 6.0, '1970-01-01T00:00:00.000', 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F', NULL, NULL)
""".replacingOccurrences(of: "\n", with: ""),
insert
)
}
func test_update_compilesUpdateExpression() {
assertSQL(
"UPDATE \"users\" SET \"age\" = 30, \"admin\" = 1 WHERE (\"id\" = 1)",
users.filter(id == 1).update(age <- 30, admin <- true)
)
}
func test_update_compilesUpdateLimitOrderExpression() {
assertSQL(
"UPDATE \"users\" SET \"age\" = 30 ORDER BY \"id\" LIMIT 1",
users.order(id).limit(1).update(age <- 30)
)
}
func test_update_encodable() throws {
let emails = Table("emails")
let value = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let update = try emails.update(value)
assertSQL(
"""
UPDATE \"emails\" SET \"int\" = 1, \"string\" = '2', \"bool\" = 1, \"float\" = 3.0, \"double\" = 4.0,
\"date\" = '1970-01-01T00:00:00.000', \"uuid\" = 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F'
""".replacingOccurrences(of: "\n", with: ""),
update
)
}
func test_update_encodable_with_nested_encodable() throws {
let emails = Table("emails")
let value1 = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: nil)
let value = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
date: Date(timeIntervalSince1970: 0), uuid: testUUIDValue, optional: nil, sub: value1)
let update = try emails.update(value)
// NOTE: As Linux JSON decoding doesn't order keys the same way, we need to check prefix, suffix,
// and extract JSON to decode it and check the decoded object.
let expectedPrefix =
"""
UPDATE \"emails\" SET \"int\" = 1, \"string\" = '2', \"bool\" = 1, \"float\" = 3.0, \"double\" = 4.0,
\"date\" = '1970-01-01T00:00:00.000', \"uuid\" = 'E621E1F8-C36C-495A-93FC-0C247A3E6E5F', \"sub\" = '
""".replacingOccurrences(of: "\n", with: "")
let expectedSuffix = "'"
let sql = update.asSQL()
XCTAssert(sql.hasPrefix(expectedPrefix))
XCTAssert(sql.hasSuffix(expectedSuffix))
let extractedJSON = String(sql[
sql.index(sql.startIndex, offsetBy: expectedPrefix.count) ..<
sql.index(sql.endIndex, offsetBy: -expectedSuffix.count)
])
let decodedJSON = try JSONDecoder().decode(TestCodable.self, from: extractedJSON.data(using: .utf8)!)
XCTAssertEqual(decodedJSON, value1)
}
func test_delete_compilesDeleteExpression() {
assertSQL(
"DELETE FROM \"users\" WHERE (\"id\" = 1)",
users.filter(id == 1).delete()
)
}
func test_delete_compilesDeleteLimitOrderExpression() {
assertSQL(
"DELETE FROM \"users\" ORDER BY \"id\" LIMIT 1",
users.order(id).limit(1).delete()
)
}
func test_delete_compilesExistsExpression() {
assertSQL(
"SELECT EXISTS (SELECT * FROM \"users\")",
users.exists
)
}
func test_count_returnsCountExpression() {
assertSQL("SELECT count(*) FROM \"users\"", users.count)
}
func test_scalar_returnsScalarExpression() {
assertSQL("SELECT \"int\" FROM \"table\"", table.select(int) as ScalarQuery<Int>)
assertSQL("SELECT \"intOptional\" FROM \"table\"", table.select(intOptional) as ScalarQuery<Int?>)
assertSQL("SELECT DISTINCT \"int\" FROM \"table\"", table.select(distinct: int) as ScalarQuery<Int>)
assertSQL("SELECT DISTINCT \"intOptional\" FROM \"table\"", table.select(distinct: intOptional) as ScalarQuery<Int?>)
}
func test_subscript_withExpression_returnsNamespacedExpression() {
let query = Table("query")
assertSQL("\"query\".\"blob\"", query[data])
assertSQL("\"query\".\"blobOptional\"", query[dataOptional])
assertSQL("\"query\".\"bool\"", query[bool])
assertSQL("\"query\".\"boolOptional\"", query[boolOptional])
assertSQL("\"query\".\"date\"", query[date])
assertSQL("\"query\".\"dateOptional\"", query[dateOptional])
assertSQL("\"query\".\"double\"", query[double])
assertSQL("\"query\".\"doubleOptional\"", query[doubleOptional])
assertSQL("\"query\".\"int\"", query[int])
assertSQL("\"query\".\"intOptional\"", query[intOptional])
assertSQL("\"query\".\"int64\"", query[int64])
assertSQL("\"query\".\"int64Optional\"", query[int64Optional])
assertSQL("\"query\".\"string\"", query[string])
assertSQL("\"query\".\"stringOptional\"", query[stringOptional])
assertSQL("\"query\".*", query[*])
}
func test_tableNamespacedByDatabase() {
let table = Table("table", database: "attached")
assertSQL("SELECT * FROM \"attached\".\"table\"", table)
}
}