From 7269326b5b75319b39dfd9ea92883db351a01d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 Jul 2015 16:05:51 +0200 Subject: [PATCH] Test that all sequences can be iterated several times --- GRDB Tests/Database/DatabaseTests.swift | 27 +++++++++-- ...ntTests.swift => RowModelFetchTests.swift} | 45 ++++++++++++++++- .../Statement/SelectStatementTests.swift | 48 +++++++++++++++---- GRDB.xcodeproj/project.pbxproj | 8 ++-- TODO.md | 1 - 5 files changed, 111 insertions(+), 18 deletions(-) rename GRDB Tests/RowModel/{RowModelSelectStatementTests.swift => RowModelFetchTests.swift} (50%) diff --git a/GRDB Tests/Database/DatabaseTests.swift b/GRDB Tests/Database/DatabaseTests.swift index c495179a1..cd1b78695 100644 --- a/GRDB Tests/Database/DatabaseTests.swift +++ b/GRDB Tests/Database/DatabaseTests.swift @@ -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") diff --git a/GRDB Tests/RowModel/RowModelSelectStatementTests.swift b/GRDB Tests/RowModel/RowModelFetchTests.swift similarity index 50% rename from GRDB Tests/RowModel/RowModelSelectStatementTests.swift rename to GRDB Tests/RowModel/RowModelFetchTests.swift index d8cd8cee9..53fbc1843 100644 --- a/GRDB Tests/RowModel/RowModelSelectStatementTests.swift +++ b/GRDB Tests/RowModel/RowModelFetchTests.swift @@ -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 + } + } + } } diff --git a/GRDB Tests/Statement/SelectStatementTests.swift b/GRDB Tests/Statement/SelectStatementTests.swift index f86c9c908..201b15a6a 100644 --- a/GRDB Tests/Statement/SelectStatementTests.swift +++ b/GRDB Tests/Statement/SelectStatementTests.swift @@ -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 + } + } + } } diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index df2aeb5cc..e6dc946d3 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -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 = ""; }; 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = ""; }; 56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteValueConvertibleTests.swift; sourceTree = ""; }; - 56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelSelectStatementTests.swift; sourceTree = ""; }; + 56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelFetchTests.swift; sourceTree = ""; }; 56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = ""; }; 56E4BB7E1B452A6200FD324B /* SQLite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLite.swift; sourceTree = ""; }; 56E4BB801B45373600FD324B /* Bindings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bindings.swift; sourceTree = ""; }; @@ -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 */, diff --git a/TODO.md b/TODO.md index e079482c1..69a4744a5 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1 @@ - [ ] RowModel.reload(db) -- [ ] Test that sequences of rows, values, and RowModels from database and statements can be iterated twice. \ No newline at end of file