mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Update tests
This commit is contained in:
1 parent
7a2c1413fe
commit
8e2fb1fb22
29 files changed
+289
-324
No files matched your search
@@ -3,6 +3,6 @@ MACOSX_DEPLOYMENT_TARGET = 10.10
|
||||
TVOS_DEPLOYMENT_TARGET = 9.0
|
||||
WATCHOS_DEPLOYMENT_TARGET = 2.0
|
||||
|
||||
//// Compile with all opt-in APIs
|
||||
//GCC_PREPROCESSOR_DEFINITIONS = $(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1
|
||||
//OTHER_SWIFT_FLAGS = $(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
// Compile with all opt-in APIs
|
||||
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1
|
||||
OTHER_SWIFT_FLAGS = $(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK
|
||||
@@ -8,15 +8,15 @@ class DatabaseAbortedTransactionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbReader.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
_ = try dbReader.read {
|
||||
try Row.fetchAll($0, sql: "SELECT wait()")
|
||||
try dbReader.read { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
_ = try Row.fetchAll(db, sql: "SELECT wait()")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
@@ -47,13 +47,13 @@ class DatabaseAbortedTransactionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbReader.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
try! dbReader.read { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let wasInTransaction = db.isInsideTransaction
|
||||
do {
|
||||
_ = try Row.fetchAll(db, sql: "SELECT wait()")
|
||||
@@ -95,14 +95,14 @@ class DatabaseAbortedTransactionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbWriter.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
try dbWriter.write { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
@@ -142,15 +142,15 @@ class DatabaseAbortedTransactionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbWriter.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
do {
|
||||
try dbWriter.write { db in
|
||||
do {
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
@@ -220,13 +220,13 @@ class DatabaseAbortedTransactionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbWriter.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
let block1 = {
|
||||
try! dbWriter.writeWithoutTransaction { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
try db.inTransaction {
|
||||
do {
|
||||
try db.execute(sql: "INSERT INTO t SELECT wait()")
|
||||
|
||||
@@ -21,9 +21,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { nil }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try DatabaseValue.fetchOne(db, sql: "SELECT f()")!.isNull)
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { Int64(1) }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int64.fetchOne(db, sql: "SELECT f()")!, Int64(1))
|
||||
}
|
||||
}
|
||||
@@ -47,9 +47,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func step(_ values: [DatabaseValue]) { }
|
||||
func finalize() -> DatabaseValueConvertible? { 1e100 }
|
||||
}
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Double.fetchOne(db, sql: "SELECT f()")!, 1e100)
|
||||
}
|
||||
}
|
||||
@@ -60,9 +60,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { "foo" }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo")
|
||||
}
|
||||
}
|
||||
@@ -75,9 +75,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Data.fetchOne(db, sql: "SELECT f()")!, "foo".data(using: .utf8))
|
||||
}
|
||||
}
|
||||
@@ -88,9 +88,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { CustomValueType() }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try CustomValueType.fetchOne(db, sql: "SELECT f()") != nil)
|
||||
}
|
||||
}
|
||||
@@ -106,9 +106,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Bool.fetchOne(db, sql: "SELECT f(NULL)")!)
|
||||
XCTAssertFalse(try Bool.fetchOne(db, sql: "SELECT f(1)")!)
|
||||
XCTAssertFalse(try Bool.fetchOne(db, sql: "SELECT f(1.1)")!)
|
||||
@@ -127,9 +127,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Int64.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try Int64.fetchOne(db, sql: "SELECT f(1)")!, 1)
|
||||
XCTAssertEqual(try Int64.fetchOne(db, sql: "SELECT f(1.1)")!, 1)
|
||||
@@ -145,9 +145,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Double.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try Double.fetchOne(db, sql: "SELECT f(1)")!, 1.0)
|
||||
XCTAssertEqual(try Double.fetchOne(db, sql: "SELECT f(1.1)")!, 1.1)
|
||||
@@ -163,9 +163,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try String.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f('foo')")!, "foo")
|
||||
}
|
||||
@@ -180,9 +180,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Data.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try Data.fetchOne(db, sql: "SELECT f(?)", arguments: ["foo".data(using: .utf8)])!, "foo".data(using: .utf8))
|
||||
XCTAssertEqual(try Data.fetchOne(db, sql: "SELECT f(?)", arguments: [Data()])!, Data())
|
||||
@@ -198,9 +198,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try CustomValueType.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertTrue(try CustomValueType.fetchOne(db, sql: "SELECT f('CustomValueType')") != nil)
|
||||
}
|
||||
@@ -214,9 +214,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { "foo" }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo")
|
||||
do {
|
||||
try db.execute(sql: "SELECT f(1)")
|
||||
@@ -239,9 +239,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT upper(?)", arguments: ["Roué"])!, "ROUé")
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f(?)", arguments: ["Roué"])!, "ROUÉ")
|
||||
XCTAssertTrue(try String.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
@@ -267,9 +267,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 2, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 2, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(1, 2)")!, 3)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
@@ -292,9 +292,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { result }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f()")!, 0)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(1)")!, 1)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(1, 1)")!, 2)
|
||||
@@ -311,9 +311,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { fatalError() }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -332,9 +332,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { fatalError() }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -353,9 +353,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { fatalError() }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -374,9 +374,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() -> DatabaseValueConvertible? { fatalError() }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -399,9 +399,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -420,9 +420,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -441,9 +441,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -462,9 +462,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -490,9 +490,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() throws -> DatabaseValueConvertible? { sum }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(a) FROM (SELECT 1 AS a UNION ALL SELECT 2 UNION ALL SELECT 3)")!, 6)
|
||||
}
|
||||
}
|
||||
@@ -508,9 +508,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
func finalize() throws -> DatabaseValueConvertible? { sum }
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 1, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
let row = try Row.fetchOne(db, sql: "SELECT f(a), f(b) FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 2, 4 UNION ALL SELECT 3, 6)")!
|
||||
XCTAssertEqual(row[0], 6)
|
||||
XCTAssertEqual(row[1], 12)
|
||||
@@ -539,9 +539,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(allocationCount, 0)
|
||||
XCTAssertEqual(aliveCount, 0)
|
||||
try db.execute(sql: "SELECT f()")
|
||||
@@ -572,9 +572,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(allocationCount, 0)
|
||||
XCTAssertEqual(aliveCount, 0)
|
||||
_ = try? db.execute(sql: "SELECT f()")
|
||||
@@ -606,9 +606,9 @@ class DatabaseAggregateTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
dbQueue.add(function: fn)
|
||||
dbQueue.inDatabase { db in
|
||||
let fn = DatabaseFunction("f", argumentCount: 0, aggregate: Aggregate.self)
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(allocationCount, 0)
|
||||
XCTAssertEqual(aliveCount, 0)
|
||||
_ = try? db.execute(sql: "SELECT f()")
|
||||
|
||||
@@ -54,9 +54,9 @@ class DatabaseCollationTests: GRDBTestCase {
|
||||
return .orderedDescending
|
||||
}
|
||||
}
|
||||
dbQueue.add(collation: collation)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(collation: collation)
|
||||
try db.execute(sql: "CREATE TABLE strings (id INTEGER PRIMARY KEY, name TEXT COLLATE LOCALIZED_STANDARD)")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (1, 'a')")
|
||||
try db.execute(sql: "INSERT INTO strings VALUES (2, 'aa')")
|
||||
|
||||
@@ -8,7 +8,7 @@ class DatabaseConfigurationTests: GRDBTestCase {
|
||||
// prepareDatabase is called when connection opens
|
||||
var connectionCount = 0
|
||||
var configuration = Configuration()
|
||||
configuration.prepareDatabase = { db in
|
||||
configuration.prepareDatabase { db in
|
||||
connectionCount += 1
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class DatabaseConfigurationTests: GRDBTestCase {
|
||||
var error: TestError?
|
||||
|
||||
var configuration = Configuration()
|
||||
configuration.prepareDatabase = { db in
|
||||
configuration.prepareDatabase { db in
|
||||
if let error = error {
|
||||
throw error
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ class DatabaseCursorTests: GRDBTestCase {
|
||||
func testStepError() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
let cursor = try Int.fetchCursor(db, sql: "SELECT throw()")
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
@@ -51,8 +51,8 @@ class DatabaseCursorTests: GRDBTestCase {
|
||||
func testStepDatabaseError() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = DatabaseError(resultCode: ResultCode(rawValue: 0xDEAD), message: "custom error")
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
let cursor = try Int.fetchCursor(db, sql: "SELECT throw()")
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
|
||||
@@ -53,8 +53,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return nil
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try DatabaseValue.fetchOne(db, sql: "SELECT f()")!.isNull)
|
||||
}
|
||||
}
|
||||
@@ -64,8 +64,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return Int64(1)
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int64.fetchOne(db, sql: "SELECT f()")!, Int64(1))
|
||||
}
|
||||
}
|
||||
@@ -75,8 +75,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return 1e100
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Double.fetchOne(db, sql: "SELECT f()")!, 1e100)
|
||||
}
|
||||
}
|
||||
@@ -86,8 +86,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return "foo"
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo")
|
||||
}
|
||||
}
|
||||
@@ -97,8 +97,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return "foo".data(using: .utf8)
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Data.fetchOne(db, sql: "SELECT f()")!, "foo".data(using: .utf8))
|
||||
}
|
||||
}
|
||||
@@ -108,8 +108,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return CustomValueType()
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try CustomValueType.fetchOne(db, sql: "SELECT f()") != nil)
|
||||
}
|
||||
}
|
||||
@@ -121,8 +121,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return dbValues[0].isNull
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Bool.fetchOne(db, sql: "SELECT f(NULL)")!)
|
||||
XCTAssertFalse(try Bool.fetchOne(db, sql: "SELECT f(1)")!)
|
||||
XCTAssertFalse(try Bool.fetchOne(db, sql: "SELECT f(1.1)")!)
|
||||
@@ -137,8 +137,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return Int64.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Int64.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try Int64.fetchOne(db, sql: "SELECT f(1)")!, 1)
|
||||
XCTAssertEqual(try Int64.fetchOne(db, sql: "SELECT f(1.1)")!, 1)
|
||||
@@ -150,8 +150,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return Double.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Double.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try Double.fetchOne(db, sql: "SELECT f(1)")!, 1.0)
|
||||
XCTAssertEqual(try Double.fetchOne(db, sql: "SELECT f(1.1)")!, 1.1)
|
||||
@@ -163,8 +163,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return String.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try String.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f('foo')")!, "foo")
|
||||
}
|
||||
@@ -175,8 +175,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return Data.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try Data.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertEqual(try Data.fetchOne(db, sql: "SELECT f(?)", arguments: ["foo".data(using: .utf8)])!, "foo".data(using: .utf8))
|
||||
XCTAssertEqual(try Data.fetchOne(db, sql: "SELECT f(?)", arguments: [Data()])!, Data())
|
||||
@@ -188,8 +188,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
return CustomValueType.fromDatabaseValue(dbValues[0])
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertTrue(try CustomValueType.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
XCTAssertTrue(try CustomValueType.fetchOne(db, sql: "SELECT f('CustomValueType')") != nil)
|
||||
}
|
||||
@@ -202,8 +202,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return "foo"
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo")
|
||||
do {
|
||||
try db.execute(sql: "SELECT f(1)")
|
||||
@@ -222,8 +222,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
|
||||
String.fromDatabaseValue(dbValues[0]).map { $0.uppercased() }
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT upper(?)", arguments: ["Roué"])!, "ROUé")
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f(?)", arguments: ["Roué"])!, "ROUÉ")
|
||||
XCTAssertTrue(try String.fetchOne(db, sql: "SELECT f(NULL)") == nil)
|
||||
@@ -245,8 +245,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let ints = dbValues.compactMap { Int.fromDatabaseValue($0) }
|
||||
return ints.reduce(0, +)
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(1, 2)")!, 3)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
@@ -265,8 +265,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f") { dbValues in
|
||||
return dbValues.count
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f()")!, 0)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(1)")!, 1)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f(1, 1)")!, 2)
|
||||
@@ -280,8 +280,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f") { dbValues in
|
||||
throw DatabaseError(message: "custom error message")
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -297,8 +297,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f") { dbValues in
|
||||
throw DatabaseError(resultCode: ResultCode(rawValue: 123))
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -314,8 +314,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f") { dbValues in
|
||||
throw DatabaseError(resultCode: ResultCode(rawValue: 123), message: "custom error message")
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -331,8 +331,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f") { dbValues in
|
||||
throw NSError(domain: "CustomErrorDomain", code: 123, userInfo: [NSLocalizedDescriptionKey: "custom error message"])
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
do {
|
||||
try db.execute(sql: "SELECT f()")
|
||||
XCTFail("Expected DatabaseError")
|
||||
@@ -353,8 +353,8 @@ class DatabaseFunctionTests: GRDBTestCase {
|
||||
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
|
||||
return x
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
x = 321
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT f()")!, 321)
|
||||
}
|
||||
|
||||
@@ -486,7 +486,7 @@ class DatabaseMigratorTests : GRDBTestCase {
|
||||
}
|
||||
|
||||
// Apply 1st migrator
|
||||
dbConfiguration.prepareDatabase = { db in
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
let function = DatabaseFunction("testFunction", argumentCount: 0, pure: true) { _ in "Arthur" }
|
||||
db.add(function: function)
|
||||
}
|
||||
|
||||
@@ -4,18 +4,19 @@ import GRDB
|
||||
class DatabasePoolCollationTests: GRDBTestCase {
|
||||
|
||||
func testCollationIsSharedBetweenWriterAndReaders() throws {
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
let collation1 = DatabaseCollation("collation1") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedAscending : .orderedDescending)
|
||||
}
|
||||
db.add(collation: collation1)
|
||||
|
||||
let collation2 = DatabaseCollation("collation2") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedDescending : .orderedAscending)
|
||||
}
|
||||
db.add(collation: collation2)
|
||||
}
|
||||
let dbPool = try makeDatabasePool()
|
||||
|
||||
let collation1 = DatabaseCollation("collation1") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedAscending : .orderedDescending)
|
||||
}
|
||||
dbPool.add(collation: collation1)
|
||||
|
||||
let collation2 = DatabaseCollation("collation2") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedDescending : .orderedAscending)
|
||||
}
|
||||
dbPool.add(collation: collation2)
|
||||
|
||||
try dbPool.write { db in
|
||||
// Both collations are available in writer
|
||||
try db.execute(sql: """
|
||||
@@ -34,27 +35,5 @@ class DatabasePoolCollationTests: GRDBTestCase {
|
||||
XCTAssertEqual(try String.fetchAll(db, sql: "SELECT text FROM items ORDER BY text COLLATE collation1"), ["a", "b", "c"])
|
||||
XCTAssertEqual(try String.fetchAll(db, sql: "SELECT text FROM items ORDER BY text COLLATE collation2"), ["c", "b", "a"])
|
||||
}
|
||||
|
||||
dbPool.remove(collation: collation1)
|
||||
|
||||
do {
|
||||
// collation1 is no longer available in writer
|
||||
try dbPool.write { db in
|
||||
_ = try String.fetchAll(db, sql: "SELECT text FROM items ORDER BY text COLLATE collation1")
|
||||
}
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
|
||||
XCTAssert(error.message!.contains("collation1"))
|
||||
}
|
||||
|
||||
do {
|
||||
// collation1 is no longer available in reader
|
||||
try dbPool.read { db in
|
||||
_ = try String.fetchAll(db, sql: "SELECT text FROM items ORDER BY text COLLATE collation1")
|
||||
}
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
|
||||
XCTAssert(error.message!.contains("collation1"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,13 @@ import GRDB
|
||||
class DatabasePoolFunctionTests: GRDBTestCase {
|
||||
|
||||
func testFunctionIsSharedBetweenWriterAndReaders() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
|
||||
let function1 = DatabaseFunction("function1", argumentCount: 1, pure: true) { (dbValues: [DatabaseValue]) in
|
||||
return dbValues[0]
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.add(function: DatabaseFunction("function1", argumentCount: 1, pure: true) { (dbValues: [DatabaseValue]) in
|
||||
return dbValues[0]
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
dbPool.add(function: function1)
|
||||
let dbPool = try makeDatabasePool()
|
||||
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE items (text TEXT)")
|
||||
@@ -19,14 +19,5 @@ class DatabasePoolFunctionTests: GRDBTestCase {
|
||||
try dbPool.read { db in
|
||||
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT function1(text) FROM items")!, "a")
|
||||
}
|
||||
|
||||
let function2 = DatabaseFunction("function2", argumentCount: 1, pure: true) { (dbValues: [DatabaseValue]) in
|
||||
return "foo"
|
||||
}
|
||||
dbPool.add(function: function2)
|
||||
|
||||
try dbPool.read { db in
|
||||
XCTAssertTrue(try String.fetchOne(db, sql: "SELECT function2(text) FROM items") == "foo")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ class DatabasePoolTests: GRDBTestCase {
|
||||
func testPersistentWALModeEnabled() throws {
|
||||
let path: String
|
||||
do {
|
||||
dbConfiguration.prepareDatabase = { db in
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
var flag: CInt = 1
|
||||
let code = withUnsafeMutablePointer(to: &flag) { flagP in
|
||||
sqlite3_file_control(db.sqliteConnection, nil, SQLITE_FCNTL_PERSIST_WAL, flagP)
|
||||
@@ -35,7 +35,7 @@ class DatabasePoolTests: GRDBTestCase {
|
||||
func testPersistentWALModeDisabled() throws {
|
||||
let path: String
|
||||
do {
|
||||
dbConfiguration.prepareDatabase = { db in
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
var flag: CInt = 0
|
||||
let code = withUnsafeMutablePointer(to: &flag) { flagP in
|
||||
sqlite3_file_control(db.sqliteConnection, nil, SQLITE_FCNTL_PERSIST_WAL, flagP)
|
||||
|
||||
@@ -34,11 +34,11 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
}
|
||||
return int + 1
|
||||
}
|
||||
dbQueue.add(function: fn)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: fn)
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT succ(1)"), 2) // 2
|
||||
db.remove(function: fn)
|
||||
}
|
||||
dbQueue.remove(function: fn)
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "SELECT succ(1)")
|
||||
@@ -61,11 +61,11 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
let collation = DatabaseCollation("test_collation_foo") { (string1, string2) in
|
||||
return (string1 as NSString).localizedStandardCompare(string2)
|
||||
}
|
||||
dbQueue.add(collation: collation)
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(collation: collation)
|
||||
try db.execute(sql: "CREATE TABLE files (name TEXT COLLATE TEST_COLLATION_FOO)")
|
||||
db.remove(collation: collation)
|
||||
}
|
||||
dbQueue.remove(collation: collation)
|
||||
do {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE files_fail (name TEXT COLLATE TEST_COLLATION_FOO)")
|
||||
|
||||
@@ -163,10 +163,10 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
|
||||
func testAddFunction() throws {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
let f = DatabaseFunction("f", argumentCount: 0, pure: true) { _ in 0 }
|
||||
dbReader.add(function: f)
|
||||
let value = try dbReader.read { db in
|
||||
try Int.fetchOne(db, sql: "SELECT f()")
|
||||
let value = try dbReader.read { db -> Int? in
|
||||
let f = DatabaseFunction("f", argumentCount: 0, pure: true) { _ in 0 }
|
||||
db.add(function: f)
|
||||
return try Int.fetchOne(db, sql: "SELECT f()")
|
||||
}
|
||||
XCTAssertEqual(value, 0)
|
||||
}
|
||||
@@ -180,10 +180,10 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
|
||||
func testAddCollation() throws {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
let collation = DatabaseCollation("c") { _, _ in .orderedSame }
|
||||
dbReader.add(collation: collation)
|
||||
let value = try dbReader.read { db in
|
||||
try Int.fetchOne(db, sql: "SELECT 'foo' AS str ORDER BY str COLLATE c")
|
||||
let value = try dbReader.read { db -> Int? in
|
||||
let collation = DatabaseCollation("c") { _, _ in .orderedSame }
|
||||
db.add(collation: collation)
|
||||
return try Int.fetchOne(db, sql: "SELECT 'foo' AS str ORDER BY str COLLATE c")
|
||||
}
|
||||
XCTAssertEqual(value, 0)
|
||||
}
|
||||
|
||||
@@ -132,9 +132,11 @@ class DatabaseSnapshotTests: GRDBTestCase {
|
||||
// MARK: - Functions
|
||||
|
||||
func testSnapshotInheritPoolFunctions() throws {
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
let function = DatabaseFunction("foo", argumentCount: 0, pure: true) { _ in return "foo" }
|
||||
db.add(function: function)
|
||||
}
|
||||
let dbPool = try makeDatabasePool()
|
||||
let function = DatabaseFunction("foo", argumentCount: 0, pure: true) { _ in return "foo" }
|
||||
dbPool.add(function: function)
|
||||
|
||||
let snapshot = try dbPool.makeSnapshot()
|
||||
try snapshot.read { db in
|
||||
@@ -142,24 +144,16 @@ class DatabaseSnapshotTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSnapshotFunctions() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
let snapshot = try dbPool.makeSnapshot()
|
||||
let function = DatabaseFunction("foo", argumentCount: 0, pure: true) { _ in return "foo" }
|
||||
snapshot.add(function: function)
|
||||
try snapshot.read { db in
|
||||
try XCTAssertEqual(String.fetchOne(db, sql: "SELECT foo()")!, "foo")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Collations
|
||||
|
||||
func testSnapshotInheritPoolCollations() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
let collation = DatabaseCollation("reverse") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedDescending : .orderedAscending)
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
let collation = DatabaseCollation("reverse") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedDescending : .orderedAscending)
|
||||
}
|
||||
db.add(collation: collation)
|
||||
}
|
||||
dbPool.add(collation: collation)
|
||||
let dbPool = try makeDatabasePool()
|
||||
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE items (text TEXT)")
|
||||
@@ -174,25 +168,6 @@ class DatabaseSnapshotTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSnapshotCollations() throws {
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE items (text TEXT)")
|
||||
try db.execute(sql: "INSERT INTO items (text) VALUES ('a')")
|
||||
try db.execute(sql: "INSERT INTO items (text) VALUES ('b')")
|
||||
try db.execute(sql: "INSERT INTO items (text) VALUES ('c')")
|
||||
}
|
||||
|
||||
let snapshot = try dbPool.makeSnapshot()
|
||||
let collation = DatabaseCollation("reverse") { (string1, string2) in
|
||||
return (string1 == string2) ? .orderedSame : ((string1 < string2) ? .orderedDescending : .orderedAscending)
|
||||
}
|
||||
snapshot.add(collation: collation)
|
||||
try snapshot.read { db in
|
||||
XCTAssertEqual(try String.fetchAll(db, sql: "SELECT text FROM items ORDER BY text COLLATE reverse"), ["c", "b", "a"])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Concurrency
|
||||
|
||||
func testReadBlockIsolationStartingWithRead() throws {
|
||||
|
||||
@@ -265,11 +265,13 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
||||
func test(_ dbQueue: DatabaseQueue) throws {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
let block1 = {
|
||||
do {
|
||||
@@ -306,11 +308,13 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
let block1 = {
|
||||
do {
|
||||
try dbQueue.inTransaction { db in
|
||||
@@ -348,11 +352,13 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
let block1 = {
|
||||
do {
|
||||
try dbQueue.write { db in
|
||||
@@ -391,11 +397,13 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
||||
let semaphore1 = DispatchSemaphore(value: 0)
|
||||
let semaphore2 = DispatchSemaphore(value: 0)
|
||||
|
||||
dbQueue.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("wait", argumentCount: 0, pure: true) { _ in
|
||||
semaphore1.signal()
|
||||
semaphore2.wait()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
let block1 = {
|
||||
do {
|
||||
try dbQueue.write { db in
|
||||
@@ -475,7 +483,7 @@ class DatabaseSuspensionTests : GRDBTestCase {
|
||||
}
|
||||
do {
|
||||
var configuration = Configuration()
|
||||
configuration.prepareDatabase = { db in
|
||||
configuration.prepareDatabase { db in
|
||||
try db.execute(sql: "PRAGMA journal_mode=truncate")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(configuration: configuration)
|
||||
|
||||
@@ -119,7 +119,7 @@ class DatabaseTraceTests : GRDBTestCase {
|
||||
func testTraceFromConfigurationWithDefaultOptions() throws {
|
||||
var events: [String] = []
|
||||
var configuration = Configuration()
|
||||
configuration.prepareDatabase = { db in
|
||||
configuration.prepareDatabase { db in
|
||||
db.trace { event in
|
||||
events.append("SQL: \(event)")
|
||||
}
|
||||
|
||||
@@ -61,9 +61,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: DatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
@@ -169,9 +169,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchAllStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ array: @autoclosure () throws -> [Fetched], sql: String) throws {
|
||||
do {
|
||||
_ = try array()
|
||||
@@ -270,9 +270,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchSetStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ set: @autoclosure () throws -> Set<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try set()
|
||||
@@ -417,9 +417,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchOneStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ value: @autoclosure () throws -> Fetched?, sql: String) throws {
|
||||
do {
|
||||
_ = try value()
|
||||
@@ -524,9 +524,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testOptionalFetchCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: NullableDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
@@ -634,9 +634,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testOptionalFetchAllStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ array: @autoclosure () throws -> [Fetched?], sql: String) throws {
|
||||
do {
|
||||
_ = try array()
|
||||
@@ -735,9 +735,9 @@ class DatabaseValueConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testOptionalFetchSetStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ set: @autoclosure () throws -> Set<Fetched?>, sql: String) throws {
|
||||
do {
|
||||
_ = try set()
|
||||
|
||||
@@ -258,15 +258,17 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
||||
var compressCalled = false
|
||||
var uncompressCalled = false
|
||||
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.add(function: DatabaseFunction("zipit", argumentCount: 1, pure: true, function: { dbValues in
|
||||
compressCalled = true
|
||||
return dbValues[0]
|
||||
}))
|
||||
db.add(function: DatabaseFunction("unzipit", argumentCount: 1, pure: true, function: { dbValues in
|
||||
uncompressCalled = true
|
||||
return dbValues[0]
|
||||
}))
|
||||
}
|
||||
let dbPool = try makeDatabasePool()
|
||||
dbPool.add(function: DatabaseFunction("zipit", argumentCount: 1, pure: true, function: { dbValues in
|
||||
compressCalled = true
|
||||
return dbValues[0]
|
||||
}))
|
||||
dbPool.add(function: DatabaseFunction("unzipit", argumentCount: 1, pure: true, function: { dbValues in
|
||||
uncompressCalled = true
|
||||
return dbValues[0]
|
||||
}))
|
||||
|
||||
try dbPool.write { db in
|
||||
try db.create(virtualTable: "documents", using: FTS4()) { t in
|
||||
|
||||
@@ -196,9 +196,8 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
|
||||
|
||||
func testStopWordsTokenizerDatabaseQueue() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: StopWordsTokenizer.self)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(tokenizer: StopWordsTokenizer.self)
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = StopWordsTokenizer.tokenizerDescriptor()
|
||||
t.column("content")
|
||||
@@ -217,9 +216,10 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testStopWordsTokenizerDatabasePool() throws {
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.add(tokenizer: StopWordsTokenizer.self)
|
||||
}
|
||||
let dbPool = try makeDatabasePool()
|
||||
dbPool.add(tokenizer: StopWordsTokenizer.self)
|
||||
|
||||
try dbPool.write { db in
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = StopWordsTokenizer.tokenizerDescriptor()
|
||||
@@ -249,10 +249,10 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
|
||||
|
||||
func testNFKCTokenizer() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: NFKCTokenizer.self)
|
||||
|
||||
// Without NFKC conversion
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(tokenizer: NFKCTokenizer.self)
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = .unicode61()
|
||||
t.column("content")
|
||||
@@ -306,9 +306,9 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
|
||||
|
||||
func testSynonymTokenizer() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: SynonymsTokenizer.self)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(tokenizer: SynonymsTokenizer.self)
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = SynonymsTokenizer.tokenizerDescriptor()
|
||||
t.column("content")
|
||||
|
||||
@@ -96,9 +96,9 @@ class FTS5WrapperTokenizerTests: GRDBTestCase {
|
||||
|
||||
func testStopWordsTokenizerDatabaseQueue() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: StopWordsTokenizer.self)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(tokenizer: StopWordsTokenizer.self)
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = StopWordsTokenizer.tokenizerDescriptor()
|
||||
t.column("content")
|
||||
@@ -117,8 +117,10 @@ class FTS5WrapperTokenizerTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
func testStopWordsTokenizerDatabasePool() throws {
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.add(tokenizer: StopWordsTokenizer.self)
|
||||
}
|
||||
let dbPool = try makeDatabaseQueue()
|
||||
dbPool.add(tokenizer: StopWordsTokenizer.self)
|
||||
|
||||
try dbPool.write { db in
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
@@ -149,10 +151,10 @@ class FTS5WrapperTokenizerTests: GRDBTestCase {
|
||||
|
||||
func testLatinAsciiTokenizer() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: LatinAsciiTokenizer.self)
|
||||
|
||||
// Without Latin ASCII conversion
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(tokenizer: LatinAsciiTokenizer.self)
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = .unicode61()
|
||||
t.column("content")
|
||||
@@ -186,9 +188,9 @@ class FTS5WrapperTokenizerTests: GRDBTestCase {
|
||||
|
||||
func testSynonymsTokenizer() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: SynonymsTokenizer.self)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(tokenizer: SynonymsTokenizer.self)
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = SynonymsTokenizer.tokenizerDescriptor()
|
||||
t.column("content")
|
||||
|
||||
@@ -70,8 +70,9 @@ class FetchableRecordTests: GRDBTestCase {
|
||||
func testFetchCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
|
||||
func test(_ cursor: RecordCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
@@ -182,8 +183,8 @@ class FetchableRecordTests: GRDBTestCase {
|
||||
func testFetchAllStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ array: @autoclosure () throws -> [Fetched], sql: String) throws {
|
||||
do {
|
||||
_ = try array()
|
||||
@@ -287,8 +288,8 @@ class FetchableRecordTests: GRDBTestCase {
|
||||
func testFetchSetStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ set: @autoclosure () throws -> Set<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try set()
|
||||
@@ -416,8 +417,8 @@ class FetchableRecordTests: GRDBTestCase {
|
||||
func testFetchOneStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ value: @autoclosure () throws -> Fetched?, sql: String) throws {
|
||||
do {
|
||||
_ = try value()
|
||||
|
||||
@@ -103,7 +103,7 @@ class GRDBTestCase: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
dbConfiguration.prepareDatabase = { db in
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.trace { event in
|
||||
self.sqlQueries.append(event.description)
|
||||
}
|
||||
|
||||
@@ -15,21 +15,24 @@ private let tableRequest = Reader.all()
|
||||
|
||||
class QueryInterfaceExpressionsTests: GRDBTestCase {
|
||||
|
||||
var collation: DatabaseCollation!
|
||||
var customFunction: DatabaseFunction!
|
||||
let collation = DatabaseCollation("localized_case_insensitive") { (lhs, rhs) in
|
||||
return (lhs as NSString).localizedCaseInsensitiveCompare(rhs)
|
||||
}
|
||||
|
||||
let customFunction = DatabaseFunction("avgOf", pure: true) { dbValues in
|
||||
let sum = dbValues.compactMap { Int.fromDatabaseValue($0) }.reduce(0, +)
|
||||
return Double(sum) / Double(dbValues.count)
|
||||
}
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.add(collation: self.collation)
|
||||
db.add(function: self.customFunction)
|
||||
}
|
||||
}
|
||||
|
||||
override func setup(_ dbWriter: DatabaseWriter) throws {
|
||||
collation = DatabaseCollation("localized_case_insensitive") { (lhs, rhs) in
|
||||
return (lhs as NSString).localizedCaseInsensitiveCompare(rhs)
|
||||
}
|
||||
dbWriter.add(collation: collation)
|
||||
|
||||
customFunction = DatabaseFunction("avgOf", pure: true) { dbValues in
|
||||
let sum = dbValues.compactMap { Int.fromDatabaseValue($0) }.reduce(0, +)
|
||||
return Double(sum) / Double(dbValues.count)
|
||||
}
|
||||
dbWriter.add(function: self.customFunction)
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createReaders") { db in
|
||||
try db.execute(sql: """
|
||||
|
||||
@@ -15,14 +15,18 @@ private let tableRequest = Reader.all()
|
||||
|
||||
class QueryInterfaceRequestTests: GRDBTestCase {
|
||||
|
||||
var collation: DatabaseCollation!
|
||||
let collation = DatabaseCollation("localized_case_insensitive") { (lhs, rhs) in
|
||||
return (lhs as NSString).localizedCaseInsensitiveCompare(rhs)
|
||||
}
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
dbConfiguration.prepareDatabase { db in
|
||||
db.add(collation: self.collation)
|
||||
}
|
||||
}
|
||||
|
||||
override func setup(_ dbWriter: DatabaseWriter) throws {
|
||||
collation = DatabaseCollation("localized_case_insensitive") { (lhs, rhs) in
|
||||
return (lhs as NSString).localizedCaseInsensitiveCompare(rhs)
|
||||
}
|
||||
dbWriter.add(collation: collation)
|
||||
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createReaders") { db in
|
||||
try db.execute(sql: """
|
||||
|
||||
@@ -51,9 +51,9 @@ class RowFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: RowCursor, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
@@ -160,9 +160,9 @@ class RowFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchAllStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ array: @autoclosure () throws -> [Row], sql: String) throws {
|
||||
do {
|
||||
_ = try array()
|
||||
@@ -262,9 +262,9 @@ class RowFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchSetStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ set: @autoclosure () throws -> Set<Row>, sql: String) throws {
|
||||
do {
|
||||
_ = try set()
|
||||
@@ -388,9 +388,9 @@ class RowFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchOneStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ value: @autoclosure () throws -> Row?, sql: String) throws {
|
||||
do {
|
||||
_ = try value()
|
||||
|
||||
@@ -40,9 +40,9 @@ class SelectStatementTests : GRDBTestCase {
|
||||
|
||||
func testStatementCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: StatementCursor) throws {
|
||||
let sql = cursor._statement.sql
|
||||
do {
|
||||
@@ -134,14 +134,14 @@ class SelectStatementTests : GRDBTestCase {
|
||||
|
||||
func testCachedSelectStatementStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
var needsThrow = false
|
||||
dbQueue.add(function: DatabaseFunction("bomb", argumentCount: 0, pure: false) { _ in
|
||||
if needsThrow {
|
||||
throw DatabaseError(message: "boom")
|
||||
}
|
||||
return "success"
|
||||
})
|
||||
try dbQueue.inDatabase { db in
|
||||
var needsThrow = false
|
||||
db.add(function: DatabaseFunction("bomb", argumentCount: 0, pure: false) { _ in
|
||||
if needsThrow {
|
||||
throw DatabaseError(message: "boom")
|
||||
}
|
||||
return "success"
|
||||
})
|
||||
let sql = "SELECT bomb()"
|
||||
|
||||
needsThrow = false
|
||||
|
||||
@@ -116,9 +116,9 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchCursorStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: FastDatabaseValueCursor<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
@@ -226,9 +226,9 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchAllStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ array: @autoclosure () throws -> [Fetched], sql: String) throws {
|
||||
do {
|
||||
_ = try array()
|
||||
@@ -329,9 +329,9 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchSetStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ set: @autoclosure () throws -> Set<Fetched>, sql: String) throws {
|
||||
do {
|
||||
_ = try set()
|
||||
@@ -477,9 +477,9 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testFetchOneStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ value: @autoclosure () throws -> Fetched?, sql: String) throws {
|
||||
do {
|
||||
_ = try value()
|
||||
@@ -658,9 +658,9 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testOptionalFetchAllStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ array: @autoclosure () throws -> [Fetched?], sql: String) throws {
|
||||
do {
|
||||
_ = try array()
|
||||
@@ -761,9 +761,9 @@ class StatementColumnConvertibleFetchTests: GRDBTestCase {
|
||||
|
||||
func testOptionalFetchSetStepFailure() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
dbQueue.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
try dbQueue.inDatabase { db in
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ set: @autoclosure () throws -> Set<Fetched?>, sql: String) throws {
|
||||
do {
|
||||
_ = try set()
|
||||
|
||||
@@ -231,9 +231,9 @@ class TableDefinitionTests: GRDBTestCase {
|
||||
return .rollback
|
||||
}
|
||||
|
||||
let collation = DatabaseCollation("foo") { (lhs, rhs) in .orderedSame }
|
||||
dbQueue.add(collation: collation)
|
||||
try dbQueue.inTransaction { db in
|
||||
let collation = DatabaseCollation("foo") { (lhs, rhs) in .orderedSame }
|
||||
db.add(collation: collation)
|
||||
try db.create(table: "test") { t in
|
||||
t.column("name", .text).collate(collation)
|
||||
}
|
||||
|
||||
@@ -164,11 +164,11 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
func testUpdateStatementAcceptsSelectQueriesAndConsumeAllRows() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
var index = 0
|
||||
dbQueue.add(function: DatabaseFunction("seq", argumentCount: 0, pure: false) { _ in
|
||||
defer { index += 1 }
|
||||
return index
|
||||
})
|
||||
try dbQueue.inDatabase { db in
|
||||
db.add(function: DatabaseFunction("seq", argumentCount: 0, pure: false) { _ in
|
||||
defer { index += 1 }
|
||||
return index
|
||||
})
|
||||
try db.execute(sql: "SELECT seq() UNION ALL SELECT seq() UNION ALL SELECT seq()")
|
||||
let statement = try db.makeUpdateStatement(sql: "SELECT seq() UNION ALL SELECT seq() UNION ALL SELECT seq()")
|
||||
try statement.execute()
|
||||
|
||||
Reference in new issue
Block a user