diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index dd377f925b..7a7abcaa11 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -1199,6 +1199,14 @@ 56F0B9921B6001C600A2F135 /* NSDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* NSDateTests.swift */; }; 56F26C1C1CEE3F32007969C4 /* AdapterRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565F03C11CE5D3AA00DE108F /* AdapterRowTests.swift */; }; 56F26C241CEE3F34007969C4 /* AdapterRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 565F03C11CE5D3AA00DE108F /* AdapterRowTests.swift */; }; + 56F3E7491E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E74A1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E74B1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E74C1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E74D1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E74E1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E74F1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; + 56F3E7501E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */; }; 56F5ABD91D814330001F60CB /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690C33F1D23E82A00E59934 /* Data.swift */; }; 56F5ABDA1D814330001F60CB /* NSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5657AAB81D107001006283EF /* NSData.swift */; }; 56F5ABDC1D814330001F60CB /* URL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5657AB0E1D10899D006283EF /* URL.swift */; }; @@ -1908,6 +1916,7 @@ 56ED8A7E1DAB8D6800BD0ABC /* FTS5WrapperTokenizerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5WrapperTokenizerTests.swift; sourceTree = ""; }; 56EE573C1BB317B7007A6A95 /* StatementColumnConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementColumnConvertibleTests.swift; sourceTree = ""; }; 56F0B98E1B6001C600A2F135 /* NSDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateTests.swift; sourceTree = ""; }; + 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResultCodeTests.swift; sourceTree = ""; }; 56FC98771D969DEF00E3C842 /* SQLExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLExpression.swift; sourceTree = ""; }; 56FDECE11BB32DFD009AD709 /* MetalRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalRowTests.swift; sourceTree = ""; }; 56FF453F1D2C23BA00F21EF9 /* DeleteByKeyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeleteByKeyTests.swift; sourceTree = ""; }; @@ -2434,6 +2443,7 @@ isa = PBXGroup; children = ( 56A238161B9C74A90082EB20 /* DatabaseErrorTests.swift */, + 56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */, ); path = DatabaseError; sourceTree = ""; @@ -3751,6 +3761,7 @@ 560FC5871CB00B880014AA8E /* RecordSubClassTests.swift in Sources */, 560FC5891CB00B880014AA8E /* TransactionObserverTests.swift in Sources */, 56C3F7541CF9F12400F6A361 /* SavepointTests.swift in Sources */, + 56F3E74A1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, 567A80541D41350C00C7DCEC /* IndexInfoTests.swift in Sources */, 560FC58B1CB00B880014AA8E /* DatabaseValueTests.swift in Sources */, 567156171CB142AA007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, @@ -3962,6 +3973,7 @@ 567A80551D41350C00C7DCEC /* IndexInfoTests.swift in Sources */, 567156481CB16729007DC145 /* DatabaseValueTests.swift in Sources */, 5671564A1CB16729007DC145 /* ReadOnlyDatabaseTests.swift in Sources */, + 56F3E74B1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, 5671564B1CB16729007DC145 /* Row+FoundationTests.swift in Sources */, 56A8C2431D1918EE0096E9D4 /* NSUUIDTests.swift in Sources */, 5671564C1CB16729007DC145 /* InMemoryDatabaseTests.swift in Sources */, @@ -4153,6 +4165,7 @@ 5698AC851DA380A20056AF8C /* VirtualTableModuleTests.swift in Sources */, 5698AC451DA2BED90056AF8C /* FTS3PatternTests.swift in Sources */, 56AF74701D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */, + 56F3E74E1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, 5690C33C1D23E7D200E59934 /* DateTests.swift in Sources */, 56AFCA581CB1AA9900F48B96 /* Row+FoundationTests.swift in Sources */, 56AFCA5A1CB1AA9900F48B96 /* RecordSubClassTests.swift in Sources */, @@ -4271,6 +4284,7 @@ 5698AC461DA2BED90056AF8C /* FTS3PatternTests.swift in Sources */, 56AF74711D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */, 56AFCAAE1CB1ABC800F48B96 /* RawRepresentableTests.swift in Sources */, + 56F3E74F1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, 56AFCAAF1CB1ABC800F48B96 /* DatabasePoolConcurrencyTests.swift in Sources */, 56AFCAB01CB1ABC800F48B96 /* TransactionObserverTests.swift in Sources */, 56AFCAB11CB1ABC800F48B96 /* Row+FoundationTests.swift in Sources */, @@ -4448,6 +4462,7 @@ 56EA869F1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift in Sources */, 5657AB621D108BA9006283EF /* NSURLTests.swift in Sources */, 5657AB6A1D108BA9006283EF /* URLTests.swift in Sources */, + 56F3E74D1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, 56EB0AB31BCD787300A3DC55 /* DataMemoryTests.swift in Sources */, 5672DE5C1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56A2385E1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */, @@ -4565,6 +4580,7 @@ 56D496601D81304E008276D7 /* NSUUIDTests.swift in Sources */, 56D4968D1D81316E008276D7 /* FunctionTests.swift in Sources */, 56D496661D813086008276D7 /* QueryInterfaceRequestTests.swift in Sources */, + 56F3E7491E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, 5698ACD71DA925420056AF8C /* RowTestCase.swift in Sources */, 56D496831D813147008276D7 /* SavepointTests.swift in Sources */, 56D496871D81316E008276D7 /* DatabaseTimestampTests.swift in Sources */, @@ -4849,6 +4865,7 @@ 567A805A1D41350C00C7DCEC /* IndexInfoTests.swift in Sources */, F3BA81211CFB3063003DC1BA /* PrimaryKeyNoneTests.swift in Sources */, F3BA80F71CFB3021003DC1BA /* SelectStatementTests.swift in Sources */, + 56F3E7501E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, F3BA80FE1CFB3024003DC1BA /* TransactionObserverTests.swift in Sources */, F3BA80BA1CFB2FD1003DC1BA /* DatabasePoolCollationTests.swift in Sources */, F3BA80BD1CFB2FD1003DC1BA /* DatabasePoolFunctionTests.swift in Sources */, @@ -5045,6 +5062,7 @@ F3BA81191CFB305F003DC1BA /* QueryInterfaceRequestTests.swift in Sources */, F3BA812F1CFB3064003DC1BA /* PrimaryKeyNoneTests.swift in Sources */, 5690C33A1D23E7D200E59934 /* DateTests.swift in Sources */, + 56F3E74C1E66F83A00BF0F01 /* ResultCodeTests.swift in Sources */, F3BA81001CFB3025003DC1BA /* TransactionObserverTests.swift in Sources */, F3BA80C01CFB2FD2003DC1BA /* DatabasePoolCollationTests.swift in Sources */, 5698ACA11DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, diff --git a/README.md b/README.md index efaef25209..c77caf2bbe 100644 --- a/README.md +++ b/README.md @@ -4757,9 +4757,9 @@ do { try ... } catch let error as DatabaseError { switch error.extendedResultCode { - case .SQLITE_CONSTRAINT_FOREIGNKEY: + case ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY: // foreign key constraint error - case .SQLITE_CONSTRAINT: + case ResultCode.SQLITE_CONSTRAINT: // any other constraint error default: // any other database error diff --git a/Tests/Public/Core/DatabaseError/ResultCodeTests.swift b/Tests/Public/Core/DatabaseError/ResultCodeTests.swift new file mode 100644 index 0000000000..6205002c27 --- /dev/null +++ b/Tests/Public/Core/DatabaseError/ResultCodeTests.swift @@ -0,0 +1,71 @@ +import XCTest +#if USING_SQLCIPHER + import GRDBCipher +#elseif USING_CUSTOMSQLITE + import GRDBCustomSQLite +#else + import GRDB +#endif + +import XCTest + +class ResultCodeTests: GRDBTestCase { + + func testExtendedResultCodesAreActivated() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "parents") { t in + t.column("id", .integer).primaryKey() + } + try db.create(table: "children") { t in + t.column("parentId", .integer).references("parents") + } + do { + try db.execute("INSERT INTO children (parentId) VALUES (1)") + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode.rawValue, 19) // primary SQLITE_CONSTRAINT + XCTAssertEqual(error.extendedResultCode.rawValue, 787) // extended SQLITE_CONSTRAINT_FOREIGNKEY + } + } + } + + func testResultCodeEquatable() { + XCTAssertEqual(ResultCode(rawValue: 19), .SQLITE_CONSTRAINT) + XCTAssertEqual(ResultCode(rawValue: 787), .SQLITE_CONSTRAINT_FOREIGNKEY) + XCTAssertNotEqual(ResultCode.SQLITE_CONSTRAINT, .SQLITE_CONSTRAINT_FOREIGNKEY) + } + + func testResultCodeMatch() { + XCTAssertTrue(ResultCode.SQLITE_CONSTRAINT ~= .SQLITE_CONSTRAINT) + XCTAssertTrue(ResultCode.SQLITE_CONSTRAINT ~= .SQLITE_CONSTRAINT_FOREIGNKEY) + XCTAssertTrue(ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY ~= .SQLITE_CONSTRAINT_FOREIGNKEY) + XCTAssertFalse(ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY ~= .SQLITE_CONSTRAINT) + XCTAssertFalse(ResultCode.SQLITE_TOOBIG ~= .SQLITE_CONSTRAINT) + } + + func testResultCodeSwitch() { + switch ResultCode.SQLITE_CONSTRAINT { + case ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY: XCTFail() + case ResultCode.SQLITE_CONSTRAINT: break + default: XCTFail() + } + + switch ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY { + case ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY: break + case ResultCode.SQLITE_CONSTRAINT: XCTFail() + default: XCTFail() + } + + switch ResultCode.SQLITE_CONSTRAINT_CHECK { + case ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY: XCTFail() + case ResultCode.SQLITE_CONSTRAINT: break + default: XCTFail() + } + + switch ResultCode.SQLITE_TOOBIG { + case ResultCode.SQLITE_CONSTRAINT_FOREIGNKEY: XCTFail() + case ResultCode.SQLITE_CONSTRAINT: XCTFail() + default: break + } + } +}