Add unit tests

This commit is contained in:
2025-10-25 18:56:55 +03:00
parent ddc47abdde
commit bbb7f14650
38 changed files with 1051 additions and 526 deletions

View File

@@ -1,50 +1,26 @@
import Foundation
import Testing
import DataLiteCore
struct BinaryFloatingPointTests {
@Test func testFloatToSQLiteRawValue() {
let floatValue: Float = 3.14
let rawValue = floatValue.sqliteValue
#expect(rawValue == .real(Double(floatValue)))
@Test func floatingPointToSQLiteValue() {
#expect(Float(3.14).sqliteValue == .real(Double(Float(3.14))))
#expect(Double(3.14).sqliteValue == .real(3.14))
}
@Test func testDoubleToSQLiteRawValue() {
let doubleValue: Double = 3.14
let rawValue = doubleValue.sqliteValue
#expect(rawValue == .real(doubleValue))
}
@Test func testFloatInitializationFromSQLiteRawValue() {
let realValue: SQLiteValue = .real(3.14)
let floatValue = Float(realValue)
#expect(floatValue != nil)
#expect(floatValue == 3.14)
@Test func floatingPointFromSQLiteValue() {
#expect(Float(SQLiteValue.real(3.14)) == 3.14)
#expect(Float(SQLiteValue.int(42)) == 42)
let intValue: SQLiteValue = .int(42)
let floatFromInt = Float(intValue)
#expect(floatFromInt != nil)
#expect(floatFromInt == 42.0)
}
@Test func testDoubleInitializationFromSQLiteRawValue() {
let realValue: SQLiteValue = .real(3.14)
let doubleValue = Double(realValue)
#expect(doubleValue != nil)
#expect(doubleValue == 3.14)
#expect(Double(SQLiteValue.real(3.14)) == 3.14)
#expect(Double(SQLiteValue.int(42)) == 42)
let intValue: SQLiteValue = .int(42)
let doubleFromInt = Double(intValue)
#expect(doubleFromInt != nil)
#expect(doubleFromInt == 42.0)
}
@Test func testInitializationFailureFromInvalidSQLiteRawValue() {
let nullValue: SQLiteValue = .null
#expect(Float(nullValue) == nil)
#expect(Double(nullValue) == nil)
#expect(Float(SQLiteValue.text("42")) == nil)
#expect(Float(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Float(SQLiteValue.null) == nil)
let textValue: SQLiteValue = .text("Invalid")
#expect(Float(textValue) == nil)
#expect(Double(textValue) == nil)
#expect(Double(SQLiteValue.text("42")) == nil)
#expect(Double(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Double(SQLiteValue.null) == nil)
}
}

View File

@@ -3,7 +3,7 @@ import Foundation
import DataLiteCore
struct BinaryIntegerTests {
@Test func testIntegerToSQLiteValue() {
@Test func integerToSQLiteValue() {
#expect(Int(42).sqliteValue == .int(42))
#expect(Int8(42).sqliteValue == .int(42))
#expect(Int16(42).sqliteValue == .int(42))
@@ -17,7 +17,7 @@ struct BinaryIntegerTests {
#expect(UInt64(42).sqliteValue == .int(42))
}
@Test func testIntegerInitializationFromSQLiteValue() {
@Test func integerFromSQLiteValue() {
#expect(Int(SQLiteValue.int(42)) == 42)
#expect(Int8(SQLiteValue.int(42)) == 42)
#expect(Int16(SQLiteValue.int(42)) == 42)
@@ -29,15 +29,61 @@ struct BinaryIntegerTests {
#expect(UInt16(SQLiteValue.int(42)) == 42)
#expect(UInt32(SQLiteValue.int(42)) == 42)
#expect(UInt64(SQLiteValue.int(42)) == 42)
}
@Test func testInvalidIntegerInitialization() {
#expect(Int(SQLiteValue.real(3.14)) == nil)
#expect(Int8(SQLiteValue.text("test")) == nil)
#expect(UInt32(SQLiteValue.blob(Data([0x01, 0x02]))) == nil)
// Out-of-range conversion
let largeValue = Int64.max
#expect(Int8(exactly: largeValue) == nil)
#expect(Int(SQLiteValue.real(42)) == nil)
#expect(Int(SQLiteValue.text("42")) == nil)
#expect(Int(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Int(SQLiteValue.null) == nil)
#expect(Int8(SQLiteValue.real(42)) == nil)
#expect(Int8(SQLiteValue.text("42")) == nil)
#expect(Int8(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Int8(SQLiteValue.null) == nil)
#expect(Int8(SQLiteValue.int(Int64.max)) == nil)
#expect(Int16(SQLiteValue.real(42)) == nil)
#expect(Int16(SQLiteValue.text("42")) == nil)
#expect(Int16(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Int16(SQLiteValue.null) == nil)
#expect(Int16(SQLiteValue.int(Int64.max)) == nil)
#expect(Int32(SQLiteValue.real(42)) == nil)
#expect(Int32(SQLiteValue.text("42")) == nil)
#expect(Int32(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Int32(SQLiteValue.null) == nil)
#expect(Int32(SQLiteValue.int(Int64.max)) == nil)
#expect(Int64(SQLiteValue.real(42)) == nil)
#expect(Int64(SQLiteValue.text("42")) == nil)
#expect(Int64(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(Int64(SQLiteValue.null) == nil)
#expect(UInt(SQLiteValue.real(42)) == nil)
#expect(UInt(SQLiteValue.text("42")) == nil)
#expect(UInt(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(UInt(SQLiteValue.null) == nil)
#expect(UInt8(SQLiteValue.real(42)) == nil)
#expect(UInt8(SQLiteValue.text("42")) == nil)
#expect(UInt8(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(UInt8(SQLiteValue.null) == nil)
#expect(UInt8(SQLiteValue.int(Int64.max)) == nil)
#expect(UInt16(SQLiteValue.real(42)) == nil)
#expect(UInt16(SQLiteValue.text("42")) == nil)
#expect(UInt16(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(UInt16(SQLiteValue.null) == nil)
#expect(UInt16(SQLiteValue.int(Int64.max)) == nil)
#expect(UInt32(SQLiteValue.real(42)) == nil)
#expect(UInt32(SQLiteValue.text("42")) == nil)
#expect(UInt32(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(UInt32(SQLiteValue.null) == nil)
#expect(UInt32(SQLiteValue.int(Int64.max)) == nil)
#expect(UInt64(SQLiteValue.real(42)) == nil)
#expect(UInt64(SQLiteValue.text("42")) == nil)
#expect(UInt64(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(UInt64(SQLiteValue.null) == nil)
}
}

View File

@@ -3,20 +3,20 @@ import Foundation
import DataLiteCore
struct BoolTests {
@Test func testBoolToSQLiteRawValue() {
@Test func boolToSQLiteValue() {
#expect(true.sqliteValue == .int(1))
#expect(false.sqliteValue == .int(0))
}
@Test func testSQLiteRawValueToBool() {
#expect(Bool(.int(1)) == true)
#expect(Bool(.int(0)) == false)
@Test func boolFromSQLiteValue() {
#expect(Bool(SQLiteValue.int(1)) == true)
#expect(Bool(SQLiteValue.int(0)) == false)
#expect(Bool(.int(-1)) == nil)
#expect(Bool(.int(2)) == nil)
#expect(Bool(.real(1.0)) == nil)
#expect(Bool(.text("true")) == nil)
#expect(Bool(.blob(Data())) == nil)
#expect(Bool(.null) == nil)
#expect(Bool(SQLiteValue.int(-1)) == nil)
#expect(Bool(SQLiteValue.int(2)) == nil)
#expect(Bool(SQLiteValue.real(1.0)) == nil)
#expect(Bool(SQLiteValue.text("true")) == nil)
#expect(Bool(SQLiteValue.blob(Data())) == nil)
#expect(Bool(SQLiteValue.null) == nil)
}
}

View File

@@ -3,20 +3,18 @@ import Foundation
import DataLiteCore
struct DataSQLiteRawRepresentableTests {
@Test func testDataToSQLiteRawValue() {
@Test func dataToSQLiteValue() {
let data = Data([0x01, 0x02, 0x03])
#expect(data.sqliteValue == .blob(data))
}
@Test func testSQLiteRawValueToData() {
@Test func dataFromSQLiteValue() {
let data = Data([0x01, 0x02, 0x03])
let rawValue = SQLiteValue.blob(data)
#expect(Data(SQLiteValue.blob(data)) == data)
#expect(Data(rawValue) == data)
#expect(Data(.int(1)) == nil)
#expect(Data(.real(1.0)) == nil)
#expect(Data(.text("blob")) == nil)
#expect(Data(.null) == nil)
#expect(Data(SQLiteValue.int(1)) == nil)
#expect(Data(SQLiteValue.real(1.0)) == nil)
#expect(Data(SQLiteValue.text("blob")) == nil)
#expect(Data(SQLiteValue.null) == nil)
}
}

View File

@@ -3,30 +3,23 @@ import Foundation
import DataLiteCore
struct DateSQLiteRawRepresentableTests {
@Test func testDateToSQLiteRawValue() {
@Test func dateToSQLiteValue() {
let date = Date(timeIntervalSince1970: 1609459200)
let formatter = ISO8601DateFormatter()
let dateString = formatter.string(from: date)
let dateString = "2021-01-01T00:00:00Z"
#expect(date.sqliteValue == .text(dateString))
}
@Test func testSQLiteRawValueToDate() {
@Test func dateFromSQLiteValue() {
let date = Date(timeIntervalSince1970: 1609459200)
let formatter = ISO8601DateFormatter()
let dateString = formatter.string(from: date)
let dateString = "2021-01-01T00:00:00Z"
let rawText = SQLiteValue.text(dateString)
#expect(Date(rawText) == date)
#expect(Date(SQLiteValue.text(dateString)) == date)
#expect(Date(SQLiteValue.int(1609459200)) == date)
#expect(Date(SQLiteValue.real(1609459200)) == date)
let rawInt = SQLiteValue.int(1609459200)
#expect(Date(rawInt) == date)
let rawReal = SQLiteValue.real(1609459200)
#expect(Date(rawReal) == date)
#expect(Date(.blob(Data([0x01, 0x02, 0x03]))) == nil)
#expect(Date(.null) == nil)
#expect(Date(.text("Invalid date format")) == nil)
#expect(Date(SQLiteValue.blob(Data([0x01, 0x02, 0x03]))) == nil)
#expect(Date(SQLiteValue.null) == nil)
#expect(Date(SQLiteValue.text("Invalid date format")) == nil)
}
}

View File

@@ -3,23 +3,30 @@ import Foundation
import DataLiteCore
struct RawRepresentableTests {
@Test func testRawRepresentableToSQLiteRawValue() {
let color: Color = .green
#expect(color.sqliteValue == .int(1))
@Test func rawRepresentableToSQLiteValue() {
#expect(Color.red.sqliteValue == .int(Color.red.rawValue))
#expect(Color.green.sqliteValue == .int(Color.green.rawValue))
#expect(Color.blue.sqliteValue == .int(Color.blue.rawValue))
}
@Test func testSQLiteRawValueToRawRepresentable() {
#expect(Color(.int(2)) == .blue)
@Test func rawRepresentableFromSQLiteValue() {
#expect(Color(SQLiteValue.int(0)) == .red)
#expect(Color(SQLiteValue.int(1)) == .green)
#expect(Color(SQLiteValue.int(2)) == .blue)
#expect(Color(.int(42)) == nil)
#expect(Color(.text("red")) == nil)
#expect(Color(.blob(Data([0x01, 0x02]))) == nil)
#expect(Color(.null) == nil)
#expect(Color(SQLiteValue.int(42)) == nil)
#expect(Color(SQLiteValue.real(0)) == nil)
#expect(Color(SQLiteValue.real(1)) == nil)
#expect(Color(SQLiteValue.real(2)) == nil)
#expect(Color(SQLiteValue.real(42)) == nil)
#expect(Color(SQLiteValue.text("red")) == nil)
#expect(Color(SQLiteValue.blob(Data([0x01, 0x02]))) == nil)
#expect(Color(SQLiteValue.null) == nil)
}
}
private extension RawRepresentableTests {
enum Color: Int, SQLiteRepresentable {
enum Color: Int64, SQLiteRepresentable {
case red
case green
case blue

View File

@@ -3,15 +3,16 @@ import Foundation
import DataLiteCore
struct StringTests {
@Test func testStringToSQLiteRawValue() {
@Test func stringToSQLiteValue() {
#expect("Hello, SQLite!".sqliteValue == .text("Hello, SQLite!"))
}
@Test func testSQLiteRawValueToString() {
@Test func stringFromSQLiteValue() {
#expect(String(SQLiteValue.text("Hello, SQLite!")) == "Hello, SQLite!")
#expect(String(SQLiteValue.int(42)) == nil)
#expect(String(SQLiteValue.blob(Data([0x01, 0x02]))) == nil)
#expect(String(SQLiteValue.real(42)) == nil)
#expect(String(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(String(SQLiteValue.null) == nil)
}
}

View File

@@ -3,18 +3,19 @@ import Foundation
import DataLiteCore
struct UUIDTests {
@Test func testUUIDToSQLiteRawValue() {
@Test func uuidToSQLiteValue() {
let uuid = UUID(uuidString: "123e4567-e89b-12d3-a456-426614174000")!
#expect(uuid.sqliteValue == .text("123E4567-E89B-12D3-A456-426614174000"))
}
@Test func testSQLiteRawValueToUUID() {
@Test func uuidFromSQLiteValue() {
let raw = SQLiteValue.text("123e4567-e89b-12d3-a456-426614174000")
#expect(UUID(raw) == UUID(uuidString: "123e4567-e89b-12d3-a456-426614174000"))
#expect(UUID(.text("invalid-uuid-string")) == nil)
#expect(UUID(.int(42)) == nil)
#expect(UUID(.blob(Data([0x01, 0x02]))) == nil)
#expect(UUID(.null) == nil)
#expect(UUID(SQLiteValue.int(42)) == nil)
#expect(UUID(SQLiteValue.real(42)) == nil)
#expect(UUID(SQLiteValue.text("42")) == nil)
#expect(UUID(SQLiteValue.blob(Data([0x42]))) == nil)
#expect(UUID(SQLiteValue.null) == nil)
}
}