Files
GRDB.swift/GRDB Tests/Statement/SelectStatementTests.swift
T
2015-07-05 06:06:48 +02:00

114 lines
4.3 KiB
Swift

//
// GRDB.swift
// https://github.com/groue/GRDB.swift
// Copyright (c) 2015 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import XCTest
import GRDB
class SelectStatementTests : GRDBTestCase {
override func setUp() {
super.setUp()
var migrator = DatabaseMigrator()
migrator.registerMigration("createPersons") { db in
try db.execute(
"CREATE TABLE persons (" +
"id INTEGER PRIMARY KEY, " +
"creationTimestamp DOUBLE, " +
"name TEXT NOT NULL, " +
"age INT" +
")")
try db.execute("INSERT INTO persons (name, age) VALUES (?,?)", bindings: ["Arthur", 41])
try db.execute("INSERT INTO persons (name, age) VALUES (?,?)", bindings: ["Barbara", 26])
try db.execute("INSERT INTO persons (name, age) VALUES (?,?)", bindings: ["Craig", 13])
}
assertNoError {
try migrator.migrate(dbQueue)
}
}
func testArrayBindingsWithSetter() {
assertNoError {
try dbQueue.inDatabase { db in
let statement = try db.selectStatement("SELECT COUNT(*) FROM persons WHERE age < ?")
let ages = [20, 30, 40, 50]
let counts = ages.map { age -> Int in
statement.bindings = [age]
return statement.fetchOne(Int.self)!
}
XCTAssertEqual(counts, [1,2,2,3])
}
}
}
func testArrayBindingsInFetch() {
assertNoError {
try dbQueue.inDatabase { db in
let statement = try db.selectStatement("SELECT COUNT(*) FROM persons WHERE age < ?")
let ages = [20, 30, 40, 50]
let counts = ages.map { statement.fetchOne(Int.self, bindings: [$0])! }
XCTAssertEqual(counts, [1,2,2,3])
}
}
}
func testDictionaryBindingsWithSetter() {
assertNoError {
try dbQueue.inDatabase { db in
let statement = try db.selectStatement("SELECT COUNT(*) FROM persons WHERE age < :age")
// TODO: why is this explicit type declaration required?
let ageDicts: [[String: SQLiteValueConvertible?]] = [["age": 20], ["age": 30], ["age": 40], ["age": 50]]
let counts = ageDicts.map { ageDict -> Int in
statement.bindings = Bindings(ageDict)
return statement.fetchOne(Int.self)!
}
XCTAssertEqual(counts, [1,2,2,3])
}
}
}
func testDictionaryBindingsInFetch() {
assertNoError {
try dbQueue.inDatabase { db in
let statement = try db.selectStatement("SELECT COUNT(*) FROM persons WHERE age < :age")
// TODO: why is this explicit type declaration required?
let ageDicts: [[String: SQLiteValueConvertible?]] = [["age": 20], ["age": 30], ["age": 40], ["age": 50]]
let counts = ageDicts.map { statement.fetchOne(Int.self, bindings: Bindings($0))! }
XCTAssertEqual(counts, [1,2,2,3])
}
}
}
}