Add tests for MutablePersistableRecord on tables without explicit primary key

This commit is contained in:
Gwendal Roué committed 2022-11-26 16:27:27 +01:00
1 parent 4e4699ebed
commit a0bfbafd9c
1 file changed
+85
@@ -349,6 +349,49 @@ class MutablePersistableRecordTests: GRDBTestCase {
}
}
func testUpdateWithoutExplicitPrimaryKeyButWithExplicitRowIDSupport() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.execute(sql: "CREATE TABLE record(name)")
struct Record: Codable, MutablePersistableRecord {
var rowID: Int64?
var name: String
mutating func didInsert(_ inserted: InsertionSuccess) {
rowID = inserted.rowID
}
}
var record1 = Record(name: "Arthur")
var record2 = Record(name: "Barbara")
try record1.insert(db)
try record2.insert(db)
record1.name = "Craig"
try record1.update(db)
let names = try String.fetchAll(db, sql: "SELECT name FROM record ORDER BY rowid")
XCTAssertEqual(names, ["Craig", "Barbara"])
}
}
func testUpdateWithoutExplicitPrimaryKeyAndWithoutExplicitRowIDSupport() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.execute(sql: "CREATE TABLE record(name)")
struct Record: Codable, PersistableRecord {
var name: String
}
let record = Record(name: "Arthur")
try record.insert(db)
do {
try record.update(db)
XCTFail("Expected PersistenceError")
} catch is PersistenceError { }
}
}
func testUpdateMutablePersistableRecordPerson() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
@@ -501,6 +544,48 @@ class MutablePersistableRecordTests: GRDBTestCase {
}
}
func testDeleteWithoutExplicitPrimaryKeyButWithExplicitRowIDSupport() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.execute(sql: "CREATE TABLE record(name)")
struct Record: Codable, MutablePersistableRecord {
var rowID: Int64?
var name: String
mutating func didInsert(_ inserted: InsertionSuccess) {
rowID = inserted.rowID
}
}
var record1 = Record(name: "Arthur")
var record2 = Record(name: "Barbara")
try record1.insert(db)
try record2.insert(db)
try record1.delete(db)
let names = try String.fetchAll(db, sql: "SELECT name FROM record ORDER BY rowid")
XCTAssertEqual(names, ["Barbara"])
}
}
func testDeleteWithoutExplicitPrimaryKeyAndWithoutExplicitRowIDSupport() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.execute(sql: "CREATE TABLE record(name)")
struct Record: Codable, PersistableRecord {
var name: String
}
let record = Record(name: "Arthur")
try record.insert(db)
try XCTAssertFalse(record.delete(db))
let names = try String.fetchAll(db, sql: "SELECT name FROM record ORDER BY rowid")
XCTAssertEqual(names, ["Arthur"])
}
}
func testDeleteMutablePersistableRecordPerson() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in