mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Test that RowModel.save() calls RowModel.insert() or RowModel.update() and thus activates eventual overrides of those methods.
This commit is contained in:
1 parent
222b20e55d
commit
5e14604945
1 file changed
+80
-17
@@ -25,35 +25,98 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class PersonWithPet: Person {
|
||||
var petCount: Int?
|
||||
class PersonWithOverrides: Person {
|
||||
enum SavingMethod {
|
||||
case Insert
|
||||
case Update
|
||||
}
|
||||
|
||||
var extra: Int!
|
||||
var lastSavingMethod: SavingMethod?
|
||||
|
||||
override func setDatabaseValue(dbv: DatabaseValue, forColumn column: String) {
|
||||
switch column {
|
||||
case "petCount": petCount = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
case "extra": extra = dbv.value()
|
||||
default: super.setDatabaseValue(dbv, forColumn: column)
|
||||
}
|
||||
}
|
||||
|
||||
override func insert(db: Database) throws {
|
||||
lastSavingMethod = .Insert
|
||||
try super.insert(db)
|
||||
}
|
||||
|
||||
override func update(db: Database) throws {
|
||||
lastSavingMethod = .Update
|
||||
try super.update(db)
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelSubClassTests: RowModelTestCase {
|
||||
|
||||
|
||||
// MARK: - Save
|
||||
|
||||
func testSaveWithNilPrimaryKeyCallsInsertMethod() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let rowModel = PersonWithOverrides(name: "Arthur")
|
||||
try rowModel.save(db)
|
||||
XCTAssertEqual(rowModel.lastSavingMethod!, PersonWithOverrides.SavingMethod.Insert)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSaveWithNotNilPrimaryKeyThatDoesNotMatchAnyRowCallsInsertMethod() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let rowModel = PersonWithOverrides(id: 123456, name: "Arthur")
|
||||
try rowModel.save(db)
|
||||
XCTAssertEqual(rowModel.lastSavingMethod!, PersonWithOverrides.SavingMethod.Insert)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testSaveWithNotNilPrimaryKeyThatMatchesARowCallsUpdateMethod() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let rowModel = PersonWithOverrides(name: "Arthur", age: 41)
|
||||
try rowModel.insert(db)
|
||||
rowModel.age = rowModel.age! + 1
|
||||
try rowModel.save(db)
|
||||
XCTAssertEqual(rowModel.lastSavingMethod!, PersonWithOverrides.SavingMethod.Update)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testSaveAfterDeleteCallsInsertMethod() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let rowModel = PersonWithOverrides(name: "Arthur")
|
||||
try rowModel.insert(db)
|
||||
try rowModel.delete(db)
|
||||
try rowModel.save(db)
|
||||
XCTAssertEqual(rowModel.lastSavingMethod!, PersonWithOverrides.SavingMethod.Insert)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Select
|
||||
|
||||
func testSelect() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
try Person(name: "Barbara", age: 36).insert(db)
|
||||
return .Commit
|
||||
}
|
||||
|
||||
dbQueue.inDatabase { db in
|
||||
let persons = db.fetchAll(Person.self, "SELECT * FROM persons ORDER BY name")
|
||||
let arthur = db.fetchOne(Person.self, "SELECT * FROM persons ORDER BY age DESC")!
|
||||
try dbQueue.inDatabase { db in
|
||||
let rowModel = Person(name: "Arthur", age: 41)
|
||||
try rowModel.insert(db)
|
||||
|
||||
XCTAssertEqual(persons.map { $0.name! }, ["Arthur", "Barbara"])
|
||||
XCTAssertEqual(persons.map { $0.age! }, [41, 36])
|
||||
XCTAssertEqual(arthur.name!, "Arthur")
|
||||
XCTAssertEqual(arthur.age!, 41)
|
||||
let fetchedRowModel = db.fetchOne(PersonWithOverrides.self, "SELECT *, 123 as extra FROM persons")!
|
||||
XCTAssertTrue(fetchedRowModel.id == rowModel.id)
|
||||
XCTAssertTrue(fetchedRowModel.name == rowModel.name)
|
||||
XCTAssertTrue(fetchedRowModel.age == rowModel.age)
|
||||
XCTAssertTrue(abs(fetchedRowModel.creationDate.timeIntervalSinceDate(rowModel.creationDate)) < 1e-3) // ISO-8601 is precise to the millisecond.
|
||||
XCTAssertTrue(fetchedRowModel.extra == 123)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user