mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Make sure RowModel.init(row: Row) can be overridden independently from init().
This commit is contained in:
1 parent
cf7beefb0a
commit
c1db46e18d
3 files changed
+129
-1
No files matched your search
@@ -0,0 +1,125 @@
|
||||
//
|
||||
// RowModelInitializersTests.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 06/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
// Tests about how minimal can class go regarding their initializers
|
||||
|
||||
// What happens for a class without property, without any initializer?
|
||||
class EmptyRowModelWithoutInitializer : RowModel {
|
||||
// nothing is required
|
||||
}
|
||||
|
||||
// What happens if we add a mutable property, still without any initializer?
|
||||
// A compiler error: class 'RowModelWithoutInitializer' has no initializers
|
||||
//
|
||||
// class RowModelWithoutInitializer : RowModel {
|
||||
// let name: String?
|
||||
// }
|
||||
|
||||
// What happens with a mutable property, and init(row: Row)?
|
||||
class RowModelWithMutablePropertyAndRowInitializer : RowModel {
|
||||
var name: String?
|
||||
|
||||
required init(row: Row) {
|
||||
super.init(row: row) // super.init(row: row) is required
|
||||
self.name = "toto" // property can be set before or after super.init
|
||||
}
|
||||
}
|
||||
|
||||
// What happens with a mutable property, and init()?
|
||||
class RowModelWithMutablePropertyAndEmptyInitializer : RowModel {
|
||||
var name: String?
|
||||
|
||||
override init() {
|
||||
super.init() // super.init() is required
|
||||
self.name = "toto" // property can be set before or after super.init
|
||||
}
|
||||
|
||||
required init(row: Row) { // init(row: row) is required
|
||||
super.init(row: row) // super.init(row: row) is required
|
||||
}
|
||||
}
|
||||
|
||||
// What happens with a mutable property, and a custom initializer()?
|
||||
class RowModelWithMutablePropertyAndCustomInitializer : RowModel {
|
||||
var name: String?
|
||||
|
||||
init(name: String? = nil) {
|
||||
self.name = name
|
||||
super.init() // super.init() is required
|
||||
}
|
||||
|
||||
required init(row: Row) { // init(row: row) is required
|
||||
super.init(row: row) // super.init(row: row) is required
|
||||
}
|
||||
}
|
||||
|
||||
// What happens with an immutable property?
|
||||
class RowModelWithImmutableProperty : RowModel {
|
||||
let initializedFromRow: Bool
|
||||
|
||||
required init(row: Row) { // An initializer is required, and the minimum is init(row: row)
|
||||
initializedFromRow = true // property must bet set before super.init(row: row)
|
||||
super.init(row: row) // super.init(row: row) is required
|
||||
}
|
||||
}
|
||||
|
||||
// What happens with an immutable property and init()?
|
||||
class RowModelWithPedigree : RowModel {
|
||||
let initializedFromRow: Bool
|
||||
|
||||
override init() {
|
||||
initializedFromRow = false // property must bet set before super.init(row: row)
|
||||
super.init() // super.init() is required
|
||||
}
|
||||
|
||||
required init(row: Row) { // An initializer is required, and the minimum is init(row: row)
|
||||
initializedFromRow = true // property must bet set before super.init(row: row)
|
||||
super.init(row: row) // super.init(row: row) is required
|
||||
}
|
||||
}
|
||||
|
||||
// What happens with an immutable property and a custom initializer()?
|
||||
class RowModelWithImmutablePropertyAndCustomInitializer : RowModel {
|
||||
let initializedFromRow: Bool
|
||||
|
||||
init(name: String? = nil) {
|
||||
initializedFromRow = false // property must bet set before super.init(row: row)
|
||||
super.init() // super.init() is required
|
||||
}
|
||||
|
||||
required init(row: Row) { // An initializer is required, and the minimum is init(row: row)
|
||||
initializedFromRow = true // property must bet set before super.init(row: row)
|
||||
super.init(row: row) // super.init(row: row) is required
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelInitializersTests : RowModelTestCase {
|
||||
|
||||
func testFetchedRowModelAreInitializedFromRow() {
|
||||
|
||||
// Here we test that RowModel.init(row: Row) can be overriden independently from RowModel.init().
|
||||
// People must be able to perform some initialization work when fetching row models from the database.
|
||||
|
||||
// The basics: test the initializedFromRow property:
|
||||
XCTAssertFalse(RowModelWithPedigree().initializedFromRow)
|
||||
XCTAssertTrue(RowModelWithPedigree(row: Row(sqliteDictionary: [:])).initializedFromRow)
|
||||
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("CREATE TABLE pedigrees (foo INTEGER)")
|
||||
try db.execute("INSERT INTO pedigrees (foo) VALUES (NULL)")
|
||||
|
||||
let pedigree = db.fetchOne(RowModelWithPedigree.self, "SELECT * FROM pedigrees")!
|
||||
XCTAssertTrue(pedigree.initializedFromRow) // very important
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
560DB2901B4AD49300B14958 /* RowModelInitializersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560DB28F1B4AD49300B14958 /* RowModelInitializersTests.swift */; };
|
||||
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 */; };
|
||||
@@ -55,6 +56,7 @@
|
||||
/* End PBXContainerItemProxy section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
560DB28F1B4AD49300B14958 /* RowModelInitializersTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelInitializersTests.swift; sourceTree = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
@@ -163,6 +165,7 @@
|
||||
56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */,
|
||||
5623E08B1B49970E00B20B7F /* RowModelDescriptionTests.swift */,
|
||||
56CC3CA41B48289200A9A534 /* RowModelFetchTests.swift */,
|
||||
560DB28F1B4AD49300B14958 /* RowModelInitializersTests.swift */,
|
||||
56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */,
|
||||
56C9E6581B468EB200DDC541 /* RowModelTestCase.swift */,
|
||||
);
|
||||
@@ -401,6 +404,7 @@
|
||||
56C9E65F1B468EB200DDC541 /* PrimaryKeyNoneTests.swift in Sources */,
|
||||
56C9E6681B4696E100DDC541 /* SelectStatementTests.swift in Sources */,
|
||||
5623E08A1B494A8A00B20B7F /* RawRepresentableTests.swift in Sources */,
|
||||
560DB2901B4AD49300B14958 /* RowModelInitializersTests.swift in Sources */,
|
||||
56CC3CA51B48289200A9A534 /* RowModelFetchTests.swift in Sources */,
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */,
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */,
|
||||
|
||||
@@ -5,4 +5,3 @@
|
||||
- [ ] Pierlo complains that databaseDictionary returns a dictionary of values when updateFromDatabaseRow eats a Row. See if we can do something about that.
|
||||
- [ ] Seriously question what RowModel.save() should do when primary key is set, and yet the object does not exists in the database. What does ActiveRecord do?
|
||||
- [ ] RowModel.update() should return true if it actually did update something.
|
||||
- [ ] Make sure RowModel.init(row: Row) can be overriden independently from init(). People must be able to perform some initialization work when fetching row models from the database.
|
||||
Reference in new issue
Block a user