mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Test that all sequences can be iterated several times
This commit is contained in:
1 parent
86bc7d47b1
commit
7269326b5b
5 files changed
+111
-18
No files matched your search
@@ -331,7 +331,7 @@ class DatabaseTests : GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchedRowsSequenceCanBeIteratedTwice() {
|
||||
func testRowSequenceCanBeIteratedTwice() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute("CREATE TABLE persons (name TEXT)")
|
||||
@@ -339,8 +339,29 @@ class DatabaseTests : GRDBTestCase {
|
||||
try db.execute("INSERT INTO persons (name) VALUES (:name)", bindings: ["name": "Barbara"])
|
||||
|
||||
let rows = db.fetchRows("SELECT * FROM persons ORDER BY name")
|
||||
var names1: [String?] = rows.map { $0.value(named: "name")! as String }
|
||||
var names2: [String?] = rows.map { $0.value(named: "name")! as String }
|
||||
var names1: [String?] = rows.map { $0.value(named: "name") as String? }
|
||||
var names2: [String?] = rows.map { $0.value(named: "name") as String? }
|
||||
|
||||
XCTAssertEqual(names1[0]!, "Arthur")
|
||||
XCTAssertEqual(names1[1]!, "Barbara")
|
||||
XCTAssertEqual(names2[0]!, "Arthur")
|
||||
XCTAssertEqual(names2[1]!, "Barbara")
|
||||
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testValueSequenceCanBeIteratedTwice() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute("CREATE TABLE persons (name TEXT)")
|
||||
try db.execute("INSERT INTO persons (name) VALUES (:name)", bindings: ["name": "Arthur"])
|
||||
try db.execute("INSERT INTO persons (name) VALUES (:name)", bindings: ["name": "Barbara"])
|
||||
|
||||
let nameSequence = db.fetch(String.self, "SELECT name FROM persons ORDER BY name")
|
||||
var names1: [String?] = Array(nameSequence).map { $0 }
|
||||
var names2: [String?] = Array(nameSequence).map { $0 }
|
||||
|
||||
XCTAssertEqual(names1[0]!, "Arthur")
|
||||
XCTAssertEqual(names1[1]!, "Barbara")
|
||||
|
||||
+43
-2
@@ -24,9 +24,9 @@
|
||||
|
||||
import XCTest
|
||||
|
||||
class RowModelSelectStatementTests: RowModelTests {
|
||||
class RowModelFetchTests: RowModelTests {
|
||||
|
||||
func testBlah() {
|
||||
func testSelectStatement() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
@@ -44,4 +44,45 @@ class RowModelSelectStatementTests: RowModelTests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDatabaseRowModelSequenceCanBeIteratedTwice() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
try Person(name: "Barbara", age: 37).insert(db)
|
||||
|
||||
let personSequence = db.fetch(Person.self, "SELECT * FROM persons ORDER BY name")
|
||||
var names1: [String?] = personSequence.map { $0.name }
|
||||
var names2: [String?] = personSequence.map { $0.name }
|
||||
|
||||
XCTAssertEqual(names1[0]!, "Arthur")
|
||||
XCTAssertEqual(names1[1]!, "Barbara")
|
||||
XCTAssertEqual(names2[0]!, "Arthur")
|
||||
XCTAssertEqual(names2[1]!, "Barbara")
|
||||
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSelectStatementRowModelSequenceCanBeIteratedTwice() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
try Person(name: "Barbara", age: 37).insert(db)
|
||||
|
||||
let statement = try db.selectStatement("SELECT * FROM persons ORDER BY name")
|
||||
let personSequence = statement.fetch(Person.self)
|
||||
var names1: [String?] = personSequence.map { $0.name }
|
||||
var names2: [String?] = personSequence.map { $0.name }
|
||||
|
||||
XCTAssertEqual(names1[0]!, "Arthur")
|
||||
XCTAssertEqual(names1[1]!, "Barbara")
|
||||
XCTAssertEqual(names2[0]!, "Arthur")
|
||||
XCTAssertEqual(names2[1]!, "Barbara")
|
||||
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,9 +53,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
|
||||
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
|
||||
@@ -69,9 +67,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
|
||||
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])! }
|
||||
@@ -82,9 +78,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
|
||||
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]]
|
||||
@@ -99,9 +93,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
|
||||
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]]
|
||||
@@ -110,4 +102,44 @@ class SelectStatementTests : GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRowSequenceCanBeIteratedTwice() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
let statement = try db.selectStatement("SELECT * FROM persons ORDER BY name")
|
||||
let rows = statement.fetchRows()
|
||||
var names1: [String?] = rows.map { $0.value(named: "name") as String? }
|
||||
var names2: [String?] = rows.map { $0.value(named: "name") as String? }
|
||||
|
||||
XCTAssertEqual(names1[0]!, "Arthur")
|
||||
XCTAssertEqual(names1[1]!, "Barbara")
|
||||
XCTAssertEqual(names1[2]!, "Craig")
|
||||
XCTAssertEqual(names2[0]!, "Arthur")
|
||||
XCTAssertEqual(names2[1]!, "Barbara")
|
||||
XCTAssertEqual(names2[2]!, "Craig")
|
||||
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testValueSequenceCanBeIteratedTwice() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
let statement = try db.selectStatement("SELECT name FROM persons ORDER BY name")
|
||||
let nameSequence = statement.fetch(String.self)
|
||||
var names1: [String?] = Array(nameSequence).map { $0 }
|
||||
var names2: [String?] = Array(nameSequence).map { $0 }
|
||||
|
||||
XCTAssertEqual(names1[0]!, "Arthur")
|
||||
XCTAssertEqual(names1[1]!, "Barbara")
|
||||
XCTAssertEqual(names1[2]!, "Craig")
|
||||
XCTAssertEqual(names2[0]!, "Arthur")
|
||||
XCTAssertEqual(names2[1]!, "Barbara")
|
||||
XCTAssertEqual(names2[2]!, "Craig")
|
||||
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */; };
|
||||
56CC3C9B1B4464AE00A9A534 /* RowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */; };
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */; };
|
||||
56CC3CA51B48289200A9A534 /* RowModelSelectStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */; };
|
||||
56CC3CA51B48289200A9A534 /* RowModelFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */; };
|
||||
56E4BB7F1B452A6200FD324B /* SQLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB7E1B452A6200FD324B /* SQLite.swift */; };
|
||||
56E4BB811B45373600FD324B /* Bindings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB801B45373600FD324B /* Bindings.swift */; };
|
||||
DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -80,7 +80,7 @@
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
|
||||
56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = "<group>"; };
|
||||
56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteValueConvertibleTests.swift; sourceTree = "<group>"; };
|
||||
56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelSelectStatementTests.swift; sourceTree = "<group>"; };
|
||||
56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelFetchTests.swift; sourceTree = "<group>"; };
|
||||
56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = "<group>"; };
|
||||
56E4BB7E1B452A6200FD324B /* SQLite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLite.swift; sourceTree = "<group>"; };
|
||||
56E4BB801B45373600FD324B /* Bindings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bindings.swift; sourceTree = "<group>"; };
|
||||
@@ -160,7 +160,7 @@
|
||||
56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */,
|
||||
56C9E6561B468EB200DDC541 /* PrimaryKeySQLiteRowIDTests.swift */,
|
||||
56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */,
|
||||
56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */,
|
||||
56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */,
|
||||
56C9E6581B468EB200DDC541 /* RowModelTests.swift */,
|
||||
);
|
||||
path = RowModel;
|
||||
@@ -398,7 +398,7 @@
|
||||
56C9E65F1B468EB200DDC541 /* PrimaryKeyNoneTests.swift in Sources */,
|
||||
56C9E6681B4696E100DDC541 /* SelectStatementTests.swift in Sources */,
|
||||
5623E08A1B494A8A00B20B7F /* RawRepresentableTests.swift in Sources */,
|
||||
56CC3CA51B48289200A9A534 /* RowModelSelectStatementTests.swift in Sources */,
|
||||
56CC3CA51B48289200A9A534 /* RowModelFetchTests.swift in Sources */,
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */,
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */,
|
||||
56C9E6601B468EB200DDC541 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user