SPB Git

Repository Public

TestSupport.swift 4.1 KB 89 lines swift

1import Foundation2import Testing3import SheetCore4import SheetData5import SheetUI6@testable import FeatureData78enum TestFiles {9    static func directory() -> URL {10        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-featuredata-tests-\(UUID().uuidString)")11        try! FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)12        return url13    }1415    /// Deterministic synthetic orders: id, region (4 values), product (3), quantity, amount, order_date.16    @discardableResult17    static func ordersCSV(_ rows: Int, in dir: URL, name: String = "orders.csv") -> URL {18        let regions = ["North", "South", "East", "West"], products = ["Widget", "Gadget", "Gizmo"]19        var text = "id,region,product,quantity,amount,order_date\n"20        for i in 0..<rows {21            let q = i % 7 + 122            let amount = Double(q) * (10 + Double(i % 13))23            text += "\(i + 1),\(regions[i % 4]),\(products[i % 3]),\(q),\(amount),2024-\(String(format: "%02d", i % 12 + 1))-\(String(format: "%02d", i % 28 + 1))\n"24        }25        let url = dir.appendingPathComponent(name)26        try! text.write(to: url, atomically: true, encoding: .utf8)27        return url28    }29}3031@MainActor func makeSession(_ sheets: [String] = ["Sheet1"]) -> WorkbookSession {32    WorkbookSession(workbook: Workbook(sheetNames: sheets), locale: Locale(identifier: "en_US"))33}3435/// Fills A1:D(n+1) of the active sheet with a small sales table and returns its range.36@MainActor @discardableResult func fillSales(_ session: WorkbookSession, rows: Int = 24, sheet: Int = 0) -> RangeRef {37    let regions = ["North", "South", "East"], products = ["Widget", "Gadget"]38    session.perform("Fill") { e in39        for (c, h) in ["Region", "Product", "Units", "Revenue"].enumerated() { e.set(sheet: sheet, row: 0, col: c, .value(.text(h))) }40        for i in 0..<rows {41            e.set(sheet: sheet, row: i + 1, col: 0, .value(.text(regions[i % 3])))42            e.set(sheet: sheet, row: i + 1, col: 1, .value(.text(products[i % 2])))43            e.set(sheet: sheet, row: i + 1, col: 2, .value(.number(Double(i % 5 + 1))))44            e.set(sheet: sheet, row: i + 1, col: 3, .value(.number(Double((i % 5 + 1) * 10 + i))))45        }46    }47    return RangeRef(r0: 0, c0: 0, r1: rows, c1: 3)48}4950@MainActor func waitUntil(_ what: String = "condition", timeout: Double = 20, _ condition: () -> Bool) async {51    let start = Date()52    while !condition() {53        if Date().timeIntervalSince(start) > timeout { Issue.record("timed out waiting for \(what)"); return }54        try? await Task.sleep(for: .milliseconds(10))55    }56}5758/// Records the engine calls a grid makes and serves synthetic pages.59final class MockProvider: PageProvider, @unchecked Sendable {60    enum Call: Equatable {61        case schema62        case count(sort: [SortSpec], filters: [String])63        case page(offset: Int, limit: Int, sort: [SortSpec], filters: [String])64    }65    private let lock = NSLock()66    private var recorded: [Call] = []67    let total: Int68    var delay: TimeInterval = 069    init(total: Int) { self.total = total }7071    var calls: [Call] { lock.lock(); defer { lock.unlock() }; return recorded }72    private func record(_ c: Call) { lock.lock(); recorded.append(c); lock.unlock() }7374    func schema(of source: GridSource) throws -> [ColumnInfo] {75        record(.schema)76        return [ColumnInfo(name: "id", type: .integer, declaredType: "BIGINT"), ColumnInfo(name: "name", type: .string, declaredType: "VARCHAR")]77    }78    func page(_ query: GridQuery, schema: [ColumnInfo], offset: Int, limit: Int) throws -> DataTable {79        record(.page(offset: offset, limit: limit, sort: query.sort, filters: query.filters.map(\.summary)))80        if delay > 0 { Thread.sleep(forTimeInterval: delay) }81        let rows = Array(offset..<min(total, offset + limit))82        return DataTable(columns: [DataColumn(name: "id", storage: .int64(rows.map(Int64.init))), DataColumn(name: "name", storage: .string(rows.map { "row \($0)" }))])83    }84    func count(_ query: GridQuery, schema: [ColumnInfo]) throws -> Int {85        record(.count(sort: query.sort, filters: query.filters.map(\.summary)))86        return total87    }88}89
Files main
Menu

Theme