mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
84 lines
2.9 KiB
Swift
84 lines
2.9 KiB
Swift
import XCTest
|
|
#if GRDBCIPHER
|
|
import GRDBCipher
|
|
#elseif GRDBCUSTOMSQLITE
|
|
import GRDBCustomSQLite
|
|
#else
|
|
import GRDB
|
|
#endif
|
|
|
|
class RequestTests: GRDBTestCase {
|
|
|
|
func testRequestFetch() throws {
|
|
struct CustomRequest : Request {
|
|
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
|
return try (db.makeSelectStatement("SELECT * FROM table1"), nil)
|
|
}
|
|
}
|
|
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.inDatabase { db in
|
|
try db.create(table: "table1") { t in
|
|
t.column("id", .integer).primaryKey()
|
|
}
|
|
try db.execute("INSERT INTO table1 DEFAULT VALUES")
|
|
try db.execute("INSERT INTO table1 DEFAULT VALUES")
|
|
|
|
let request: Request = CustomRequest() // Lose type
|
|
let rows = try Row.fetchAll(db, request)
|
|
XCTAssertEqual(lastSQLQuery, "SELECT * FROM table1")
|
|
XCTAssertEqual(rows.count, 2)
|
|
XCTAssertEqual(rows[0], ["id": 1])
|
|
XCTAssertEqual(rows[1], ["id": 2])
|
|
}
|
|
}
|
|
|
|
func testRequestFetchCount() throws {
|
|
struct CustomRequest : Request {
|
|
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
|
return try (db.makeSelectStatement("SELECT * FROM table1"), nil)
|
|
}
|
|
}
|
|
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.inDatabase { db in
|
|
try db.create(table: "table1") { t in
|
|
t.column("id", .integer).primaryKey()
|
|
}
|
|
try db.execute("INSERT INTO table1 DEFAULT VALUES")
|
|
try db.execute("INSERT INTO table1 DEFAULT VALUES")
|
|
|
|
let request: Request = CustomRequest() // Lose type
|
|
let count = try request.fetchCount(db)
|
|
XCTAssertEqual(lastSQLQuery, "SELECT COUNT(*) FROM (SELECT * FROM table1)")
|
|
XCTAssertEqual(count, 2)
|
|
}
|
|
}
|
|
|
|
func testRequestCustomizedFetchCount() throws {
|
|
struct CustomRequest : Request {
|
|
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
|
return try (db.makeSelectStatement("INVALID"), nil)
|
|
}
|
|
|
|
func fetchCount(_ db: Database) throws -> Int {
|
|
return 2
|
|
}
|
|
}
|
|
|
|
let dbQueue = try makeDatabaseQueue()
|
|
try dbQueue.inDatabase { db in
|
|
try db.create(table: "table1") { t in
|
|
t.column("id", .integer).primaryKey()
|
|
}
|
|
try db.execute("INSERT INTO table1 DEFAULT VALUES")
|
|
try db.execute("INSERT INTO table1 DEFAULT VALUES")
|
|
|
|
let request: Request = CustomRequest() // Lose type
|
|
let count = try request.fetchCount(db)
|
|
XCTAssertEqual(lastSQLQuery, "INSERT INTO table1 DEFAULT VALUES")
|
|
XCTAssertEqual(count, 2)
|
|
}
|
|
}
|
|
}
|