Skip to content

Commit d004739

Browse files
committed
Fixing tests for Linux
1 parent 80c8b30 commit d004739

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Tests/SQLiteTests/QueryTests.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,20 @@ class QueryTests: XCTestCase {
391391
let value = TestCodable(int: 1, string: "2", bool: true, float: 3, double: 4,
392392
date: Date(timeIntervalSince1970: 0), optional: nil, sub: value1)
393393
let update = try emails.update(value)
394-
let encodedJSON = try JSONEncoder().encode(value1)
395-
let encodedJSONString = String(data: encodedJSON, encoding: .utf8)!
396-
assertSQL(
397-
"""
398-
UPDATE \"emails\" SET \"int\" = 1, \"string\" = '2', \"bool\" = 1, \"float\" = 3.0, \"double\" = 4.0,
399-
\"date\" = '1970-01-01T00:00:00.000', \"sub\" = '\(encodedJSONString)'
400-
""".replacingOccurrences(of: "\n", with: ""),
401-
update
402-
)
394+
395+
// NOTE: As Linux JSON decoding doesn't order keys the same way, we need to check prefix, suffix,
396+
// and extract JSON to decode it and check the decoded object.
397+
398+
let expectedPrefix = "UPDATE \"emails\" SET \"int\" = 1, \"string\" = '2', \"bool\" = 1, \"float\" = 3.0, \"double\" = 4.0, \"date\" = '1970-01-01T00:00:00.000', \"sub\" = '"
399+
let expectedSuffix = "'"
400+
401+
let sql = update.asSQL()
402+
XCTAssert(sql.hasPrefix(expectedPrefix))
403+
XCTAssert(sql.hasSuffix(expectedSuffix))
404+
405+
let extractedJSON = String(sql[sql.index(sql.startIndex, offsetBy: expectedPrefix.count) ..< sql.index(sql.endIndex, offsetBy: -expectedSuffix.count)])
406+
let decodedJSON = try JSONDecoder().decode(TestCodable.self, from: extractedJSON.data(using: .utf8)!)
407+
XCTAssertEqual(decodedJSON, value1)
403408
}
404409

405410
func test_delete_compilesDeleteExpression() {

Tests/SQLiteTests/TestHelpers.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ let qualifiedTable = Table("table", database: "main")
105105
let virtualTable = VirtualTable("virtual_table")
106106
let _view = View("view") // avoid Mac XCTestCase collision
107107

108-
class TestCodable: Codable {
108+
class TestCodable: Codable, Equatable {
109109
let int: Int
110110
let string: String
111111
let bool: Bool
@@ -125,4 +125,15 @@ class TestCodable: Codable {
125125
self.optional = optional
126126
self.sub = sub
127127
}
128+
129+
static func == (lhs: TestCodable, rhs: TestCodable) -> Bool {
130+
lhs.int == rhs.int &&
131+
lhs.string == rhs.string &&
132+
lhs.bool == rhs.bool &&
133+
lhs.float == rhs.float &&
134+
lhs.double == rhs.double &&
135+
lhs.date == rhs.date &&
136+
lhs.optional == rhs.optional &&
137+
lhs.sub == rhs.sub
138+
}
128139
}

0 commit comments

Comments
 (0)