Skip to content

Commit a9468c2

Browse files
committed
Update unit tests
1 parent b574247 commit a9468c2

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

‎Tests/CoreModelSQLiteTests/CustomFunctionTests.swift‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ private struct SeededGenerator: RandomNumberGenerator {
6363
/// Whether this SQLite build includes the R*Tree module (an optional compile-time
6464
/// feature), determined by attempting to create an R*Tree virtual table in memory.
6565
private let isRTreeAvailable: Bool = {
66-
guard let connection = try? Connection(.inMemory) else { return false }
66+
guard let connection = try? Connection(path: .inMemory) else { return false }
6767
do {
68-
try connection.execute("CREATE VIRTUAL TABLE rtree_probe USING rtree(id, minX, maxX)")
68+
try connection.run("CREATE VIRTUAL TABLE rtree_probe USING rtree(id, minX, maxX)")
6969
return true
7070
} catch {
7171
return false
@@ -300,14 +300,18 @@ func appManagedRTreePrefilter() async throws {
300300
let maxLon = referenceLongitude + lonDelta
301301

302302
// Query the app's R*Tree (through the app's own read connection) for candidate ids.
303-
let reader = try Connection(path, readonly: true)
304-
let candidates = Set(try reader.prepare("""
303+
let reader = try Connection(path: path, isReadOnly: true)
304+
let statement = try reader.prepare("""
305305
SELECT m.site_id FROM "Site_rtree" r
306306
JOIN "Site_rtree_map" m ON m.rowid = r.id
307307
WHERE r.minLat <= ? AND r.maxLat >= ? AND r.minLon <= ? AND r.maxLon >= ?
308-
""", maxLat, minLat, maxLon, minLon).compactMap { row in
309-
(row[0] as? String).map { ObjectID(rawValue: $0) }
310-
})
308+
""", [maxLat.binding, minLat.binding, maxLon.binding, minLon.binding])
309+
var candidates = Set<ObjectID>()
310+
while let row = try statement.failableNext() {
311+
if let site = row[0]?.textValue {
312+
candidates.insert(ObjectID(rawValue: site))
313+
}
314+
}
311315

312316
// Combine the R*Tree candidate set with the exact distance filter via the library.
313317
let request = FetchRequest(

‎Tests/CoreModelSQLiteTests/DistancePerformanceTests.swift‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,21 @@ struct DistancePerformanceBenchmarks {
109109

110110
#expect(try await database.fetch(sqlRequest).count == benchmarkInMemory(try await database.fetch(allRequest), radius: radius).count)
111111

112-
let clock = ContinuousClock()
113112
var sqlMatches = 0
114-
let sqlStart = clock.now
113+
let sqlStart = Date()
115114
for _ in 0..<benchmarkIterations { sqlMatches = try await database.fetch(sqlRequest).count }
116-
let sqlTime = clock.now - sqlStart
115+
let sqlTime = Date().timeIntervalSince(sqlStart)
117116

118117
var memMatches = 0
119-
let memStart = clock.now
118+
let memStart = Date()
120119
for _ in 0..<benchmarkIterations { memMatches = benchmarkInMemory(try await database.fetch(allRequest), radius: radius).count }
121-
let memTime = clock.now - memStart
120+
let memTime = Date().timeIntervalSince(memStart)
122121

123122
print("""
124123
125124
===== Selective filter+sort: \(benchmarkRowCount) rows, \(sqlMatches) matches, \(benchmarkIterations) iterations =====
126-
SQL (filter+sort in SQLite): avg \(sqlTime / benchmarkIterations)
127-
in-memory (fetch-all + Swift): avg \(memTime / benchmarkIterations)
125+
SQL (filter+sort in SQLite): avg \(sqlTime / Double(benchmarkIterations)) seconds
126+
in-memory (fetch-all + Swift): avg \(memTime / Double(benchmarkIterations)) seconds
128127
""")
129128
#expect(sqlMatches == memMatches)
130129
}
@@ -139,22 +138,21 @@ struct DistancePerformanceBenchmarks {
139138
// no radius filter: `.infinity` keeps every row
140139
#expect(try await database.fetch(sqlRequest).map(\.id) == benchmarkInMemory(try await database.fetch(allRequest), radius: .infinity))
141140

142-
let clock = ContinuousClock()
143141
var sqlCount = 0
144-
let sqlStart = clock.now
142+
let sqlStart = Date()
145143
for _ in 0..<benchmarkIterations { sqlCount = try await database.fetch(sqlRequest).count }
146-
let sqlTime = clock.now - sqlStart
144+
let sqlTime = Date().timeIntervalSince(sqlStart)
147145

148146
var memCount = 0
149-
let memStart = clock.now
147+
let memStart = Date()
150148
for _ in 0..<benchmarkIterations { memCount = benchmarkInMemory(try await database.fetch(allRequest), radius: .infinity).count }
151-
let memTime = clock.now - memStart
149+
let memTime = Date().timeIntervalSince(memStart)
152150

153151
print("""
154152
155153
===== Sort, no filter: \(benchmarkRowCount) rows returned, \(benchmarkIterations) iterations =====
156-
SQL (ORDER BY in SQLite): avg \(sqlTime / benchmarkIterations)
157-
in-memory (fetch-all + Swift sort): avg \(memTime / benchmarkIterations)
154+
SQL (ORDER BY in SQLite): avg \(sqlTime / Double(benchmarkIterations)) seconds
155+
in-memory (fetch-all + Swift sort): avg \(memTime / Double(benchmarkIterations)) seconds
158156
""")
159157
#expect(sqlCount == memCount)
160158
}

0 commit comments

Comments
 (0)