RowModel adopts CustomStringConvertible

This commit is contained in:
Gwendal Roué committed 2015-07-05 19:07:14 +02:00
1 parent f51ad18202
commit 0a85de066e
11 files changed
+88 -15

No files matched your search

@@ -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
@@ -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.
@@ -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
@@ -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
+1 -1
View File
@@ -24,7 +24,7 @@
import XCTest
class RowModelFetchTests: RowModelTests {
class RowModelFetchTests: RowModelTestCase {
func testSelectStatement() {
assertNoError {
@@ -34,7 +34,7 @@ class PersonWithPet: Person {
}
}
class RowModelSubClassTests: RowModelTests {
class RowModelSubClassTests: RowModelTestCase {
func testSelect() {
assertNoError {
@@ -25,7 +25,7 @@
import XCTest
import GRDB
class RowModelTests : GRDBTestCase {
class RowModelTestCase : GRDBTestCase {
override func setUp() {
super.setUp()
+10 -6
View File
@@ -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 = "<group>"; };
5610C20D1B4852D600F8B4EC /* RowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowTests.swift; sourceTree = "<group>"; };
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 = "<group>"; };
5699AE561B42C34A00C35172 /* Statement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Statement.swift; sourceTree = "<group>"; };
5699AE581B42CD7800C35172 /* Row.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Row.swift; sourceTree = "<group>"; };
@@ -71,7 +73,7 @@
56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrimaryKeySingleTests.swift; sourceTree = "<group>"; };
56C9E6561B468EB200DDC541 /* PrimaryKeyRowIDTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrimaryKeyRowIDTests.swift; sourceTree = "<group>"; };
56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelSubClassTests.swift; sourceTree = "<group>"; };
56C9E6581B468EB200DDC541 /* RowModelTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelTests.swift; sourceTree = "<group>"; };
56C9E6581B468EB200DDC541 /* RowModelTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelTestCase.swift; sourceTree = "<group>"; };
56C9E65A1B468EB200DDC541 /* UpdateStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpdateStatementTests.swift; sourceTree = "<group>"; };
56C9E6671B4696E100DDC541 /* SelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectStatementTests.swift; sourceTree = "<group>"; };
56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueue.swift; sourceTree = "<group>"; };
@@ -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 = "<group>";
@@ -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;
};
+18
View File
@@ -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: ...)
+52
View File
@@ -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, "<GRDB_Tests.EmptyRowModel>")
}
func testSimpleRowModelDescription() {
let model = SingleColumnRowModel()
model.name = "foo"
XCTAssertEqual(model.description, "<GRDB_Tests.SingleColumnRowModel name:\"foo\">")
}
func testDoubleColumnRowModelDescription() {
let model = DoubleColumnRowModel()
model.name = "foo"
model.age = 35
XCTAssertTrue(["<GRDB_Tests.DoubleColumnRowModel name:\"foo\" age:35>", "<GRDB_Tests.DoubleColumnRowModel age:35 name:\"foo\">"].indexOf(model.description) != nil)
}
}
+1 -2
View File
@@ -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
- [ ] db.fetch(RowModel.type, primaryKeys:[...]) // several primary keys