mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Crash tests
This commit is contained in:
1 parent
4e08f6ed1b
commit
557d4871d0
6 files changed
+370
-86
No files matched your search
@@ -411,5 +411,48 @@ class DatabaseTests : GRDBTestCase {
|
||||
// }
|
||||
// return .Commit
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowSequenceCanNotBeGeneratedOutsideOfDatabaseQueue() {
|
||||
// assertNoError {
|
||||
// var rows: AnySequence<Row>?
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE persons (name TEXT)")
|
||||
// rows = Row.fetch(db, "SELECT * FROM persons")
|
||||
// }
|
||||
// rows!.generate()
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowSequenceCanNotBeIteratedOutsideOfDatabaseQueue() {
|
||||
// assertNoError {
|
||||
// var generator: AnyGenerator<Row>?
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE persons (name TEXT)")
|
||||
// generator = Row.fetch(db, "SELECT * FROM persons").generate()
|
||||
// }
|
||||
// generator!.next()
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testExecuteDoesNotSupportMultipleStatement() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE persons (name TEXT, age INT); CREATE TABLE books (name TEXT, age INT)")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testInvalidNamedBinding() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE persons (name TEXT, age INT)")
|
||||
// try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["XXX": "foo", "name": "Arthur", "age": 41])
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -91,28 +91,28 @@ public final class SelectStatement : Statement {
|
||||
}
|
||||
|
||||
return AnySequence { () -> AnyGenerator<T> in
|
||||
// IMPLEMENTATION NOTE
|
||||
//
|
||||
// Make sure sequences are consumed in the correct queue.
|
||||
//
|
||||
// Here we avoid this pattern:
|
||||
//
|
||||
// let rows = dbQueue.inDatabase { db in
|
||||
// try Row.fetch(db, "...")
|
||||
// }
|
||||
// for row in rows { // assertion failure
|
||||
// ...
|
||||
// }
|
||||
//
|
||||
// Here we check that sequence.generate() is called on the correct queue.
|
||||
self.assertValidQueue("SQLite statement was not used on its database queue. Consider using the fetchAll() method instead of fetch().")
|
||||
|
||||
// Let row sequences be iterated several times.
|
||||
self.reset()
|
||||
|
||||
return anyGenerator { () -> T? in
|
||||
// Make sure values are consumed in the correct queue.
|
||||
//
|
||||
// Here we avoid this pattern:
|
||||
//
|
||||
// let rows = dbQueue.inDatabase { db in
|
||||
// try Row.fetch(db, "...")
|
||||
// }
|
||||
// for row in rows { // fatal error!
|
||||
// ...
|
||||
// }
|
||||
//
|
||||
// Check that the statement was created in a database queue,
|
||||
// and then that the current database queue is the same as the
|
||||
// one where the statement was created:
|
||||
assert(self.databaseQueueID != nil)
|
||||
guard self.databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else {
|
||||
fatalError("SQLite statement was not iterated on its database queue. Consider using the fetchAll() method instead of fetch().")
|
||||
}
|
||||
// Here we check that generator.next() is called on the correct queue.
|
||||
self.assertValidQueue("SQLite statement was not used on its database queue. Consider using the fetchAll() method instead of fetch().")
|
||||
|
||||
let code = sqlite3_step(self.sqliteStatement)
|
||||
switch code {
|
||||
|
||||
@@ -120,6 +120,10 @@ public class Statement {
|
||||
fatalDatabaseError(DatabaseError(code: code, message: database.lastErrorMessage, sql: sql))
|
||||
}
|
||||
}
|
||||
|
||||
final func assertValidQueue(message: String) {
|
||||
assert(self.databaseQueueID != nil && self.databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), message)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLite identifier quoting
|
||||
|
||||
@@ -214,7 +214,7 @@ public class RowModel : RowConvertible, DatabaseTableMapping, DatabaseStorable {
|
||||
// Update managed primary key if needed
|
||||
if case .Managed(let managedColumn) = dataMapper.primaryKey {
|
||||
guard let rowID = dataMapper.storedDatabaseDictionary[managedColumn] else {
|
||||
fatalError("\(self.dynamicType).storedDatabaseDictionary must return the value for the primary key `(managedColumn)`")
|
||||
fatalError("\(self.dynamicType).storedDatabaseDictionary must return the value for the primary key \(managedColumn.quotedDatabaseIdentifier)")
|
||||
}
|
||||
if rowID == nil {
|
||||
updateFromRow(Row(dictionary: [managedColumn: changes.insertedRowID]))
|
||||
|
||||
+303
-67
@@ -2,12 +2,59 @@ import XCTest
|
||||
import GRDB
|
||||
|
||||
class RowModelWithoutDatabaseTableName: RowModel { }
|
||||
class RowModelWithoutDatabaseTable: RowModel {
|
||||
|
||||
class RowModelWithInexistingDatabaseTable: RowModel {
|
||||
override static func databaseTableName() -> String? {
|
||||
return "foo"
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelWithEmptyStoredDatabaseDictionary : RowModel {
|
||||
override static func databaseTableName() -> String? {
|
||||
return "models"
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelWithNilPrimaryKey : RowModel {
|
||||
override static func databaseTableName() -> String? {
|
||||
return "models"
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": nil]
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelForTableWithoutPrimaryKey : RowModel {
|
||||
override static func databaseTableName() -> String? {
|
||||
return "models"
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": "foo"]
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelForTableWithMultipleColumnsPrimaryKey : RowModel {
|
||||
override static func databaseTableName() -> String? {
|
||||
return "models"
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": "foo"]
|
||||
}
|
||||
}
|
||||
|
||||
class RowModelWithRowIDPrimaryKeyNotExposedInStoredDatabaseDictionary : RowModel {
|
||||
override static func databaseTableName() -> String? {
|
||||
return "models"
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["name": "foo"]
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidRowModelTests: GRDBTestCase {
|
||||
|
||||
// =========================================================================
|
||||
@@ -83,76 +130,265 @@ class InvalidRowModelTests: GRDBTestCase {
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - RowModelWithoutDatabaseTable
|
||||
// MARK: - RowModelWithInexistingDatabaseTable
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeFetchedByID() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// RowModelWithoutDatabaseTable.fetchOne(db, primaryKey: 1)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeFetchedByKey() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// RowModelWithoutDatabaseTable.fetchOne(db, key: ["id": 1])
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeInserted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithoutDatabaseTable().insert(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeUpdated() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithoutDatabaseTable().update(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeSaved() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithoutDatabaseTable().save(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeDeleted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithoutDatabaseTable().delete(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeReloaded() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithoutDatabaseTable().reload(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithoutDatabaseTableCanNotBeTestedForExistence() {
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeFetchedByID() {
|
||||
// assertNoError {
|
||||
// dbQueue.inDatabase { db in
|
||||
// RowModelWithoutDatabaseTable().exists(db)
|
||||
// RowModelWithInexistingDatabaseTable.fetchOne(db, primaryKey: 1)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeFetchedByKey() {
|
||||
// assertNoError {
|
||||
// dbQueue.inDatabase { db in
|
||||
// RowModelWithInexistingDatabaseTable.fetchOne(db, key: ["id": 1])
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeInserted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithInexistingDatabaseTable().insert(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeUpdated() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithInexistingDatabaseTable().update(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeSaved() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithInexistingDatabaseTable().save(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeDeleted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithInexistingDatabaseTable().delete(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeReloaded() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try RowModelWithInexistingDatabaseTable().reload(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithInexistingDatabaseTableCanNotBeTestedForExistence() {
|
||||
// assertNoError {
|
||||
// dbQueue.inDatabase { db in
|
||||
// RowModelWithInexistingDatabaseTable().exists(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - RowModelWithEmptyStoredDatabaseDictionary
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithEmptyStoredDatabaseDictionaryCanNotBeInserted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithEmptyStoredDatabaseDictionary().insert(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithEmptyStoredDatabaseDictionaryCanNotBeUpdated() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithEmptyStoredDatabaseDictionary().update(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithEmptyStoredDatabaseDictionaryCanNotBeSaved() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithEmptyStoredDatabaseDictionary().save(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithEmptyStoredDatabaseDictionaryCanNotBeDeleted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithEmptyStoredDatabaseDictionary().delete(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithEmptyStoredDatabaseDictionaryCanNotBeReloaded() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithEmptyStoredDatabaseDictionary().reload(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithEmptyStoredDatabaseDictionaryCanNotBeTestedForExistence() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// RowModelWithEmptyStoredDatabaseDictionary().exists(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - RowModelWithNilPrimaryKey
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithNilPrimaryKeyCanNotBeUpdated() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithNilPrimaryKey().update(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithNilPrimaryKeyCanNotBeDeleted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithNilPrimaryKey().delete(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithNilPrimaryKeyCanNotBeReloaded() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// try RowModelWithNilPrimaryKey().reload(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithNilPrimaryKeyCanNotBeTestedForExistence() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY)")
|
||||
// RowModelWithNilPrimaryKey().exists(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - RowModelForTableWithoutPrimaryKey
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelForTableWithoutPrimaryKeyCanNotBeFetchedByID() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (name TEXT)")
|
||||
// RowModelForTableWithoutPrimaryKey.fetchOne(db, primaryKey: 1)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelForTableWithoutPrimaryKeyCanNotBeUpdated() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (name TEXT)")
|
||||
// try RowModelForTableWithoutPrimaryKey().update(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelForTableWithoutPrimaryKeyCanNotBeDeleted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (name TEXT)")
|
||||
// try RowModelForTableWithoutPrimaryKey().delete(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelForTableWithoutPrimaryKeyCanNotBeReloaded() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (name TEXT)")
|
||||
// try RowModelForTableWithoutPrimaryKey().reload(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelForTableWithoutPrimaryKeyCanNotBeTestedForExistence() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (name TEXT)")
|
||||
// RowModelForTableWithoutPrimaryKey().exists(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - RowModelForTableWithMultipleColumnsPrimaryKey
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelForTableWithMultipleColumnsPrimaryKeyCanNotBeFetchedByID() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (a TEXT, b TEXT, PRIMARY KEY(a,b))")
|
||||
// RowModelForTableWithMultipleColumnsPrimaryKey.fetchOne(db, primaryKey: 1)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - RowModelWithRowIDPrimaryKeyNotExposedInStoredDatabaseDictionary
|
||||
|
||||
// CRASH TEST: this test must crash
|
||||
// func testRowModelWithRowIDPrimaryKeyNotExposedInStoredDatabaseDictionaryCanNotBeInserted() {
|
||||
// assertNoError {
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute("CREATE TABLE models (id INTEGER PRIMARY KEY, name TEXT)")
|
||||
// try RowModelWithRowIDPrimaryKeyNotExposedInStoredDatabaseDictionary().insert(db)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
- [ ] RowModel.databaseChanges. It's easy to tell which columns have changed, but difficult to tell how, because there is not always an old value. Should we return an array of changed columns instead? We only track *column changes*, not *property changes* (column "latitude" has changed, but we don't know anything about the "coordinate" property): is it a problem? What is the use case?
|
||||
- [ ] What is this RowModel.copyDatabaseValuesFrom(other) crap? Don't we just need a plain regular copy() method? The trouble is the primary key: should we copy it or not? Should it be an option? What are the use cases?
|
||||
- [ ] Read https://www.sqlite.org/isolation.html
|
||||
- [ ] Read https://www.sqlite.org/lang_createtable.html
|
||||
- [ ] Consider renaming RowModel to Record or DatabaseRecord.
|
||||
Reference in new issue
Block a user