From 0a85de066e56964f0cc263b2fa251efa9c76c40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 Jul 2015 19:07:14 +0200 Subject: [PATCH] RowModel adopts CustomStringConvertible --- .../RowModel/PrimaryKeyMultipleTests.swift | 2 +- GRDB Tests/RowModel/PrimaryKeyNoneTests.swift | 2 +- .../RowModel/PrimaryKeyRowIDTests.swift | 2 +- .../RowModel/PrimaryKeySingleTests.swift | 2 +- GRDB Tests/RowModel/RowModelFetchTests.swift | 2 +- .../RowModel/RowModelSubClassTests.swift | 2 +- ...odelTests.swift => RowModelTestCase.swift} | 2 +- GRDB.xcodeproj/project.pbxproj | 16 +++--- GRDB/RowModel.swift | 18 +++++++ RowModelDescriptionTests.swift | 52 +++++++++++++++++++ TODO.md | 3 +- 11 files changed, 88 insertions(+), 15 deletions(-) rename GRDB Tests/RowModel/{RowModelTests.swift => RowModelTestCase.swift} (97%) create mode 100644 RowModelDescriptionTests.swift diff --git a/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift b/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift index 8c383ab79..d020c7fa1 100644 --- a/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyMultipleTests.swift @@ -62,7 +62,7 @@ class Citizenship: RowModel { } -class PrimaryKeyMultipleTests: RowModelTests { +class PrimaryKeyMultipleTests: RowModelTestCase { func testInsert() { // Models with Multiple primary key should be able to be inserted when diff --git a/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift b/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift index 2d49f9e24..735a5fe65 100644 --- a/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyNoneTests.swift @@ -48,7 +48,7 @@ class Item: RowModel { } } -class PrimaryKeyNoneTests: RowModelTests { +class PrimaryKeyNoneTests: RowModelTestCase { func testInsert() { // Models with None primary key should be able to be inserted. diff --git a/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift b/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift index 7e0ce661f..408930e40 100644 --- a/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeyRowIDTests.swift @@ -85,7 +85,7 @@ class Person: RowModel { } } -class PrimaryKeyRowIDTests: RowModelTests { +class PrimaryKeyRowIDTests: RowModelTestCase { func testInsert() { // Models with RowID primary key should be able to be inserted with a diff --git a/GRDB Tests/RowModel/PrimaryKeySingleTests.swift b/GRDB Tests/RowModel/PrimaryKeySingleTests.swift index 2f96b28f5..9a114c908 100644 --- a/GRDB Tests/RowModel/PrimaryKeySingleTests.swift +++ b/GRDB Tests/RowModel/PrimaryKeySingleTests.swift @@ -71,7 +71,7 @@ class Pet: RowModel { } } -class PrimaryKeySingleTests: RowModelTests { +class PrimaryKeySingleTests: RowModelTestCase { func testInsertWithNonNilPrimaryKey() { // Models with Single primary key should be able to be inserted when diff --git a/GRDB Tests/RowModel/RowModelFetchTests.swift b/GRDB Tests/RowModel/RowModelFetchTests.swift index 53fbc1843..322b2cef7 100644 --- a/GRDB Tests/RowModel/RowModelFetchTests.swift +++ b/GRDB Tests/RowModel/RowModelFetchTests.swift @@ -24,7 +24,7 @@ import XCTest -class RowModelFetchTests: RowModelTests { +class RowModelFetchTests: RowModelTestCase { func testSelectStatement() { assertNoError { diff --git a/GRDB Tests/RowModel/RowModelSubClassTests.swift b/GRDB Tests/RowModel/RowModelSubClassTests.swift index 68b6b357c..c40d5f7b9 100644 --- a/GRDB Tests/RowModel/RowModelSubClassTests.swift +++ b/GRDB Tests/RowModel/RowModelSubClassTests.swift @@ -34,7 +34,7 @@ class PersonWithPet: Person { } } -class RowModelSubClassTests: RowModelTests { +class RowModelSubClassTests: RowModelTestCase { func testSelect() { assertNoError { diff --git a/GRDB Tests/RowModel/RowModelTests.swift b/GRDB Tests/RowModel/RowModelTestCase.swift similarity index 97% rename from GRDB Tests/RowModel/RowModelTests.swift rename to GRDB Tests/RowModel/RowModelTestCase.swift index e590610ca..4ebb2da46 100644 --- a/GRDB Tests/RowModel/RowModelTests.swift +++ b/GRDB Tests/RowModel/RowModelTestCase.swift @@ -25,7 +25,7 @@ import XCTest import GRDB -class RowModelTests : GRDBTestCase { +class RowModelTestCase : GRDBTestCase { override func setUp() { super.setUp() diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 615653f36..5fee800fa 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 5610C2091B483AA500F8B4EC /* InMemoryDatabaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5610C2081B483AA500F8B4EC /* InMemoryDatabaseTests.swift */; }; 5610C20E1B4852D600F8B4EC /* RowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5610C20D1B4852D600F8B4EC /* RowTests.swift */; }; 5623E08A1B494A8A00B20B7F /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E0891B494A8A00B20B7F /* RawRepresentableTests.swift */; }; + 5623E08C1B49970E00B20B7F /* RowModelDescriptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */; }; 565E191E1B42B2B600D32814 /* Database.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565E191D1B42B2B600D32814 /* Database.swift */; }; 5699AE571B42C34A00C35172 /* Statement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE561B42C34A00C35172 /* Statement.swift */; }; 5699AE591B42CD7800C35172 /* Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5699AE581B42CD7800C35172 /* Row.swift */; }; @@ -24,7 +25,7 @@ 56C9E6601B468EB200DDC541 /* PrimaryKeySingleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */; }; 56C9E6611B468EB200DDC541 /* PrimaryKeyRowIDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6561B468EB200DDC541 /* PrimaryKeyRowIDTests.swift */; }; 56C9E6621B468EB200DDC541 /* RowModelSubClassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */; }; - 56C9E6631B468EB200DDC541 /* RowModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6581B468EB200DDC541 /* RowModelTests.swift */; }; + 56C9E6631B468EB200DDC541 /* RowModelTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6581B468EB200DDC541 /* RowModelTestCase.swift */; }; 56C9E6641B468EB200DDC541 /* UpdateStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E65A1B468EB200DDC541 /* UpdateStatementTests.swift */; }; 56C9E6681B4696E100DDC541 /* SelectStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6671B4696E100DDC541 /* SelectStatementTests.swift */; }; 56CC3C8F1B43A5A700A9A534 /* DatabaseQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */; }; @@ -57,6 +58,7 @@ 5610C2081B483AA500F8B4EC /* InMemoryDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryDatabaseTests.swift; sourceTree = ""; }; 5610C20D1B4852D600F8B4EC /* RowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowTests.swift; sourceTree = ""; }; 5623E0891B494A8A00B20B7F /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = SOURCE_ROOT; }; + 5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelDescriptionTests.swift; sourceTree = SOURCE_ROOT; }; 565E191D1B42B2B600D32814 /* Database.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Database.swift; sourceTree = ""; }; 5699AE561B42C34A00C35172 /* Statement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Statement.swift; sourceTree = ""; }; 5699AE581B42CD7800C35172 /* Row.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Row.swift; sourceTree = ""; }; @@ -71,7 +73,7 @@ 56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrimaryKeySingleTests.swift; sourceTree = ""; }; 56C9E6561B468EB200DDC541 /* PrimaryKeyRowIDTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrimaryKeyRowIDTests.swift; sourceTree = ""; }; 56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelSubClassTests.swift; sourceTree = ""; }; - 56C9E6581B468EB200DDC541 /* RowModelTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelTests.swift; sourceTree = ""; }; + 56C9E6581B468EB200DDC541 /* RowModelTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelTestCase.swift; sourceTree = ""; }; 56C9E65A1B468EB200DDC541 /* UpdateStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpdateStatementTests.swift; sourceTree = ""; }; 56C9E6671B4696E100DDC541 /* SelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectStatementTests.swift; sourceTree = ""; }; 56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueue.swift; sourceTree = ""; }; @@ -157,11 +159,12 @@ children = ( 56C9E6531B468EB200DDC541 /* PrimaryKeyMultipleTests.swift */, 56C9E6541B468EB200DDC541 /* PrimaryKeyNoneTests.swift */, - 56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */, 56C9E6561B468EB200DDC541 /* PrimaryKeyRowIDTests.swift */, - 56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */, + 56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */, + 5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */, 56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */, - 56C9E6581B468EB200DDC541 /* RowModelTests.swift */, + 56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */, + 56C9E6581B468EB200DDC541 /* RowModelTestCase.swift */, ); path = RowModel; sourceTree = ""; @@ -385,7 +388,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 56C9E6631B468EB200DDC541 /* RowModelTests.swift in Sources */, + 56C9E6631B468EB200DDC541 /* RowModelTestCase.swift in Sources */, 5610C2091B483AA500F8B4EC /* InMemoryDatabaseTests.swift in Sources */, 56C9E65C1B468EB200DDC541 /* DatabaseMigratorTests.swift in Sources */, 56C9E65D1B468EB200DDC541 /* CustomSQLiteValueConvertibleTests.swift in Sources */, @@ -402,6 +405,7 @@ 56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */, 56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */, 56C9E6601B468EB200DDC541 /* PrimaryKeySingleTests.swift in Sources */, + 5623E08C1B49970E00B20B7F /* RowModelDescriptionTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index b4348f780..663fd57e8 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -261,6 +261,24 @@ public class RowModel { } } +extension RowModel : CustomStringConvertible { + public var description: String { + return "<\(reflect(self.dynamicType).summary)" + "".join(databaseDictionary.map { (key, value) in + if var string = value as? String { + string = string.stringByReplacingOccurrencesOfString("\\", withString: "\\\\") + string = string.stringByReplacingOccurrencesOfString("\n", withString: "\\n") + string = string.stringByReplacingOccurrencesOfString("\r", withString: "\\r") + string = string.stringByReplacingOccurrencesOfString("\t", withString: "\\t") + string = string.stringByReplacingOccurrencesOfString("\"", withString: "\\\"") + return " \(key):\"\(string)\"" + } else if let value = value { + return " \(key):\(value)" + } else { + return " \(key):nil" + }}) + ">" + } +} + extension Database { // let persons = db.fetch(Person.self, "SELECT ...", bindings: ...) diff --git a/RowModelDescriptionTests.swift b/RowModelDescriptionTests.swift new file mode 100644 index 000000000..44bd51bcd --- /dev/null +++ b/RowModelDescriptionTests.swift @@ -0,0 +1,52 @@ +// +// RowModelDescriptionTests.swift +// GRDB +// +// Created by Gwendal Roué on 05/07/2015. +// Copyright © 2015 Gwendal Roué. All rights reserved. +// + +import XCTest +import GRDB + +class EmptyRowModel : RowModel { +} + +class SingleColumnRowModel : RowModel { + var name: String? + + override var databaseDictionary: [String: SQLiteValueConvertible?] { + return ["name": name] + } +} + +class DoubleColumnRowModel : RowModel { + var name: String? + var age: Int? + + override var databaseDictionary: [String: SQLiteValueConvertible?] { + return ["name": name, "age": age] + } +} + +class RowModelDescriptionTests: RowModelTestCase { + + func testEmptyRowModelDescription() { + let model = EmptyRowModel() + XCTAssertEqual(model.description, "") + } + + func testSimpleRowModelDescription() { + let model = SingleColumnRowModel() + model.name = "foo" + XCTAssertEqual(model.description, "") + } + + func testDoubleColumnRowModelDescription() { + let model = DoubleColumnRowModel() + model.name = "foo" + model.age = 35 + XCTAssertTrue(["", ""].indexOf(model.description) != nil) + } + +} diff --git a/TODO.md b/TODO.md index 9134b646b..015c84be9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ - [ ] RowModel.reload(db) - [ ] RowModel support for implicit RowID column (see https://www.sqlite.org/autoinc.html) -- [ ] RowModel.description -- [ ] db.fetch(RowModel.type, primaryKeys:[...]) // several primary keys \ No newline at end of file +- [ ] db.fetch(RowModel.type, primaryKeys:[...]) // several primary keys