diff --git a/Tests/GRDBTests/DataMemoryTests.swift b/Tests/GRDBTests/DataMemoryTests.swift index f2b95d91c..a6f329298 100644 --- a/Tests/GRDBTests/DataMemoryTests.swift +++ b/Tests/GRDBTests/DataMemoryTests.swift @@ -34,16 +34,6 @@ class DataMemoryTests: GRDBTestCase { } } } - - do { - // This data should not be copied - // (The deprecation warning is expected: we test a deprecated method.) - let nonCopiedData = row.dataNoCopy(atIndex: 0)! - XCTAssertEqual(nonCopiedData, data) - nonCopiedData.withUnsafeBytes { - XCTAssertEqual($0.baseAddress, blobPointer) - } - } } } @@ -72,16 +62,6 @@ class DataMemoryTests: GRDBTestCase { } } } - - do { - // This data should not be copied - // (The deprecation warning is expected: we test a deprecated method.) - let nonCopiedData = nestedRow.dataNoCopy(atIndex: 0)! - XCTAssertEqual(nonCopiedData, data) - nonCopiedData.withUnsafeBytes { - XCTAssertEqual($0.baseAddress, blobPointer) - } - } } } @@ -109,14 +89,62 @@ class DataMemoryTests: GRDBTestCase { } } } - do { - // This data should not be copied: - // (The deprecation warning is expected: we test a deprecated method.) - let nonCopiedData = row.dataNoCopy(atIndex: 0)! - XCTAssertEqual(nonCopiedData, data) - nonCopiedData.withUnsafeBytes { nonCopiedBuffer in - XCTAssertEqual(nonCopiedBuffer.baseAddress, buffer.baseAddress) - } + } + default: + XCTFail("Not a blob") + } + } + } + } + + @available(*, deprecated) + func testDeprecatedMemoryBehavior() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + // Make sure Data is on the heap (15 bytes is enough) + // For more context, see: + // https://forums.swift.org/t/swift-5-how-to-test-data-bytesnocopydeallocator/20299/2?u=gwendal.roue + let data = Data(repeating: 0xaa, count: 15) + + do { + let rows = try Row.fetchCursor(db, sql: "SELECT ?", arguments: [data]) + while let row = try rows.next() { + let blobPointer = sqlite3_column_blob(row.sqliteStatement, 0) + // This data should not be copied + let nonCopiedData = row.dataNoCopy(atIndex: 0)! + XCTAssertEqual(nonCopiedData, data) + nonCopiedData.withUnsafeBytes { + XCTAssertEqual($0.baseAddress, blobPointer) + } + } + } + + do { + let adapter = ScopeAdapter(["nested": SuffixRowAdapter(fromIndex: 0)]) + let rows = try Row.fetchCursor(db, sql: "SELECT ?", arguments: [data], adapter: adapter) + while let row = try rows.next() { + let blobPointer = sqlite3_column_blob(row.unadapted.sqliteStatement, 0) + let nestedRow = row.scopes["nested"]! + // This data should not be copied + let nonCopiedData = nestedRow.dataNoCopy(atIndex: 0)! + XCTAssertEqual(nonCopiedData, data) + nonCopiedData.withUnsafeBytes { + XCTAssertEqual($0.baseAddress, blobPointer) + } + } + } + + do { + let row = try Row.fetchOne(db, sql: "SELECT ?", arguments: [data])! + let dbValue = row.first!.1 // TODO: think about exposing a (column:,databaseValue:) tuple + switch dbValue.storage { + case .blob(let data): + data.withUnsafeBytes { buffer in + // This data should not be copied: + let nonCopiedData = row.dataNoCopy(atIndex: 0)! + XCTAssertEqual(nonCopiedData, data) + nonCopiedData.withUnsafeBytes { nonCopiedBuffer in + XCTAssertEqual(nonCopiedBuffer.baseAddress, buffer.baseAddress) } } default: diff --git a/Tests/GRDBTests/TableRecordDeleteTests.swift b/Tests/GRDBTests/TableRecordDeleteTests.swift index e88a87c65..23df2f131 100644 --- a/Tests/GRDBTests/TableRecordDeleteTests.swift +++ b/Tests/GRDBTests/TableRecordDeleteTests.swift @@ -277,7 +277,8 @@ class TableRecordDeleteTests: GRDBTestCase { _ = try Person.filter(keys: [1, 2]).deleteAndFetchCursor(db).next() XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE \"id\" IN (1, 2) RETURNING *") - + +#if GRDBCUSTOMSQLITE || GRDBCIPHER if #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) { _ = try Person.filter(id: 1).deleteAndFetchCursor(db).next() XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE \"id\" = 1 RETURNING *") @@ -285,7 +286,14 @@ class TableRecordDeleteTests: GRDBTestCase { _ = try Person.filter(ids: [1, 2]).deleteAndFetchCursor(db).next() XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE \"id\" IN (1, 2) RETURNING *") } - +#else + _ = try Person.filter(id: 1).deleteAndFetchCursor(db).next() + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE \"id\" = 1 RETURNING *") + + _ = try Person.filter(ids: [1, 2]).deleteAndFetchCursor(db).next() + XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE \"id\" IN (1, 2) RETURNING *") +#endif + _ = try Person.filter(sql: "id = 1").deleteAndFetchCursor(db).next() XCTAssertEqual(self.lastSQLQuery, "DELETE FROM \"persons\" WHERE id = 1 RETURNING *")