Skip to content

Commit b6941cd

Browse files
committed
Add more tests for blob handling
1 parent 8f8eec7 commit b6941cd

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

SQLite.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@
5151
19A17254FBA7894891F7297B /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
5252
19A174D78559CD30679BCCCB /* FTS5Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1721B8984686B9963B45D /* FTS5Tests.swift */; };
5353
19A1750CEE9B05267995CF3D /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
54+
19A178072B371489E6A1E839 /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
55+
19A17E04C4C0956715C5676A /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
5456
19A17EC0D68BA8C03288ADF7 /* FTS5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1730E4390C775C25677D1 /* FTS5.swift */; };
57+
19A17FB80B94E882050AA908 /* FoundationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19A1794CC4D7827E997E32A7 /* FoundationTests.swift */; };
5558
EE247AD71C3F04ED00AE3E12 /* SQLite.h in Headers */ = {isa = PBXBuildFile; fileRef = EE247AD61C3F04ED00AE3E12 /* SQLite.h */; settings = {ATTRIBUTES = (Public, ); }; };
5659
EE247ADE1C3F04ED00AE3E12 /* SQLite.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EE247AD31C3F04ED00AE3E12 /* SQLite.framework */; };
5760
EE247B031C3F06E900AE3E12 /* Blob.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247AEE1C3F06E900AE3E12 /* Blob.swift */; };
@@ -162,6 +165,7 @@
162165
03A65E961C6BB3210062603F /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
163166
19A1721B8984686B9963B45D /* FTS5Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5Tests.swift; sourceTree = "<group>"; };
164167
19A1730E4390C775C25677D1 /* FTS5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5.swift; sourceTree = "<group>"; };
168+
19A1794CC4D7827E997E32A7 /* FoundationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationTests.swift; sourceTree = "<group>"; };
165169
39548A631CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
166170
39548A651CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
167171
39548A671CA63C740003E3B5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
@@ -415,6 +419,7 @@
415419
EE247B161C3F127200AE3E12 /* TestHelpers.swift */,
416420
EE247AE41C3F04ED00AE3E12 /* Info.plist */,
417421
19A1721B8984686B9963B45D /* FTS5Tests.swift */,
422+
19A1794CC4D7827E997E32A7 /* FoundationTests.swift */,
418423
);
419424
path = SQLiteTests;
420425
sourceTree = "<group>";
@@ -824,6 +829,7 @@
824829
03A65E8E1C6BB3030062603F /* OperatorsTests.swift in Sources */,
825830
03A65E951C6BB3030062603F /* TestHelpers.swift in Sources */,
826831
19A17254FBA7894891F7297B /* FTS5Tests.swift in Sources */,
832+
19A17E04C4C0956715C5676A /* FoundationTests.swift in Sources */,
827833
);
828834
runOnlyForDeploymentPostprocessing = 0;
829835
};
@@ -880,6 +886,7 @@
880886
EE247B2E1C3F141E00AE3E12 /* OperatorsTests.swift in Sources */,
881887
EE247B251C3F137700AE3E12 /* ConnectionTests.swift in Sources */,
882888
19A171E6FA242F72A308C594 /* FTS5Tests.swift in Sources */,
889+
19A17FB80B94E882050AA908 /* FoundationTests.swift in Sources */,
883890
);
884891
runOnlyForDeploymentPostprocessing = 0;
885892
};
@@ -929,6 +936,7 @@
929936
EE247B5E1C3F3FC700AE3E12 /* SetterTests.swift in Sources */,
930937
EE247B5B1C3F3FC700AE3E12 /* QueryTests.swift in Sources */,
931938
19A174D78559CD30679BCCCB /* FTS5Tests.swift in Sources */,
939+
19A178072B371489E6A1E839 /* FoundationTests.swift in Sources */,
932940
);
933941
runOnlyForDeploymentPostprocessing = 0;
934942
};

SQLite/Core/Blob.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public struct Blob {
3131
}
3232

3333
public init(bytes: UnsafeRawPointer, length: Int) {
34-
// TODO correct count
3534
let i8bufptr = UnsafeBufferPointer(start: bytes.assumingMemoryBound(to: UInt8.self), count: length)
3635
self.init(bytes: [UInt8](i8bufptr))
3736
}

SQLite/Core/Statement.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,12 @@ public struct Cursor {
238238
}
239239

240240
public subscript(idx: Int) -> Blob {
241-
let bytes = sqlite3_column_blob(handle, Int32(idx))
242-
let length = Int(sqlite3_column_bytes(handle, Int32(idx)))
243-
return Blob(bytes: bytes!, length: length)
241+
if let pointer = sqlite3_column_blob(handle, Int32(idx)) {
242+
let length = Int(sqlite3_column_bytes(handle, Int32(idx)))
243+
return Blob(bytes: pointer, length: length)
244+
} else {
245+
fatalError("sqlite3_column_blob returned NULL")
246+
}
244247
}
245248

246249
// MARK: -

SQLite/Foundation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ extension Data : Value {
3131
}
3232

3333
public static func fromDatatypeValue(_ dataValue: Blob) -> Data {
34-
return Data(bytes: UnsafePointer<UInt8>(dataValue.bytes), count: dataValue.bytes.count)
34+
return Data(bytes: dataValue.bytes)
3535
}
3636

3737
public var datatypeValue: Blob {
38-
return withUnsafeBytes { ptr -> Blob in
39-
return Blob(bytes: ptr, length: count)
38+
return withUnsafeBytes { (pointer: UnsafePointer<UInt8>) -> Blob in
39+
return Blob(bytes: pointer, length: count)
4040
}
4141
}
4242

SQLiteTests/FoundationTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import XCTest
2+
import SQLite
3+
4+
class FoundationTests : XCTestCase {
5+
func testDataFromBlob() {
6+
let data = Data(bytes: [1, 2, 3])
7+
let blob = data.datatypeValue
8+
XCTAssertEqual([1, 2, 3], blob.bytes)
9+
}
10+
11+
func testBlobToData() {
12+
let blob = Blob(bytes: [1, 2, 3])
13+
let data = Data.fromDatatypeValue(blob)
14+
XCTAssertEqual(Data(bytes: [1, 2, 3]), data)
15+
}
16+
}

SQLiteTests/StatementTests.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import XCTest
22
import SQLite
33

4-
class StatementTests : XCTestCase {
4+
class StatementTests : SQLiteTestCase {
5+
override func setUp() {
6+
super.setUp()
7+
CreateUsersTable()
8+
}
9+
10+
func test_cursor_to_blob() {
11+
try! InsertUsers("alice")
12+
let statement = try! db.prepare("SELECT email FROM users")
13+
XCTAssert(try! statement.step())
14+
let blob = statement.row[0] as Blob
15+
XCTAssertEqual("alice@example.com", String(bytes: blob.bytes, encoding: .utf8)!)
16+
}
517
}

0 commit comments

Comments
 (0)