Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Sheet9// File : Packages/SheetData/Tests/SheetDataTests/TestSupport.swift10// Module : SheetDataTests11// Repository : https://git.spboucher.ai/orix-sheet12// Created : 2026-11-2113// Last Modified : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89import SheetCore90@testable import SheetData9192enum Fixtures {93 static let directory: URL = Bundle.module.resourceURL!.appendingPathComponent("Fixtures")94 static func url(_ name: String) -> URL { directory.appendingPathComponent(name) }9596 nonisolated(unsafe) static let expected: [String: Any] = {97 let data = try! Data(contentsOf: url("expected.json"))98 return try! JSONSerialization.jsonObject(with: data) as! [String: Any]99 }()100101 static func table(_ name: String) -> [String: Any] { expected[name] as! [String: Any] }102 static func map(_ name: String) -> [String: Double] { (expected[name] as! [String: Any]).mapValues { ($0 as! NSNumber).doubleValue } }103 static func pivot(_ name: String) -> [String: Double] {104 ((expected["pivots"] as! [String: Any])[name] as! [String: Any]).mapValues { ($0 as! NSNumber).doubleValue }105 }106 static func numbers(_ name: String) -> [Double] { (expected[name] as! [Any]).map { ($0 as! NSNumber).doubleValue } }107 static func strings(_ name: String) -> [String] { expected[name] as! [String] }108109 /// Engine with the standard datasets: sales (CSV import), customers, messy, sales_extra, and sales_r110 /// (sales + revenue and discount1 columns).111 static func engine() throws -> DataEngine {112 let e = try DataEngine()113 try e.importFile(url("sales.csv"), as: "sales")114 try e.importFile(url("customers.csv"), as: "customers")115 try e.importFile(url("messy.csv"), as: "messy")116 try e.importFile(url("sales_extra.csv"), as: "sales_extra")117 try e.createTable("sales_r", from: "SELECT *, quantity * unit_price * (1 - coalesce(discount, 0)) AS revenue, 1 + discount AS discount1 FROM sales")118 return e119 }120121 static func temporaryDirectory() -> URL {122 let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-tests-\(UUID().uuidString)")123 try! FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)124 return url125 }126}127128/// Type-neutral cell used to compare engine results with the pandas reference.129enum Canon: Equatable, CustomStringConvertible {130 case null, num(Double), str(String), bool(Bool)131132 init(_ v: DataValue) {133 switch v {134 case .null: self = .null135 case .bool(let b): self = .bool(b)136 case .int(let i): self = .num(Double(i))137 case .double(let d): self = d.isNaN ? .null : .num(d)138 case .decimal(let s): self = .num(Double(s) ?? .nan)139 case .string(let s): self = .str(s)140 case .date, .timestamp, .time: self = .str(v.text)141 case .blob(let d): self = .str(d.map { String(format: "%02x", $0) }.joined())142 }143 }144145 init(json: Any) {146 if json is NSNull { self = .null }147 else if let n = json as? NSNumber {148 self = CFGetTypeID(n) == CFBooleanGetTypeID() ? .bool(n.boolValue) : .num(n.doubleValue)149 } else { self = .str(json as? String ?? "\(json)") }150 }151152 var sortKey: String {153 switch self {154 case .null: "\u{0}"155 case .num(let d): String(format: "n%+025.6f", d)156 case .str(let s): "s" + s157 case .bool(let b): b ? "b1" : "b0"158 }159 }160161 var description: String {162 switch self {163 case .null: "NULL"164 case .num(let d): String(d)165 case .str(let s): "\"\(s)\""166 case .bool(let b): String(b)167 }168 }169170 static func == (a: Canon, b: Canon) -> Bool {171 switch (a, b) {172 case (.null, .null): true173 case (.num(let x), .num(let y)): close(x, y)174 case (.str(let x), .str(let y)): x == y175 case (.bool(let x), .bool(let y)): x == y176 default: false177 }178 }179}180181func close(_ a: Double, _ b: Double, tolerance: Double = 1e-9) -> Bool {182 abs(a - b) <= tolerance * max(1, abs(a), abs(b))183}184185/// Compares a result with a pandas reference table ({"columns": […], "rows": [[…]]}).186/// Unless `ordered`, both sides are sorted by all their values first.187func expectTable(_ actual: DataTable, matches name: String, ordered: Bool = false, sourceLocation: SourceLocation = #_sourceLocation) {188 let reference = Fixtures.table(name)189 let columns = reference["columns"] as! [String]190 #expect(actual.columnNames == columns, "columns of \(name)", sourceLocation: sourceLocation)191 var want = (reference["rows"] as! [[Any]]).map { $0.map(Canon.init(json:)) }192 var got = actual.rows.map { $0.map(Canon.init) }193 #expect(got.count == want.count, "row count of \(name): got \(got.count), want \(want.count)", sourceLocation: sourceLocation)194 guard got.count == want.count, actual.columnNames == columns else { return }195 if !ordered {196 func key(_ row: [Canon]) -> String { row.map(\.sortKey).joined(separator: "\u{1}") }197 want.sort { key($0) < key($1) }198 got.sort { key($0) < key($1) }199 }200 for i in want.indices where got[i] != want[i] {201 Issue.record("\(name): row \(i) differs\n got \(got[i])\n want \(want[i])", sourceLocation: sourceLocation)202 return203 }204}205206/// "r¦r||c¦c" keys like the Python generator.207func pivotKey(_ keys: [DataValue]) -> String {208 keys.map { k -> String in209 switch k {210 case .null: return "(null)"211 case .bool(let b): return b ? "True" : "False"212 case .string(let s): return s213 case .date, .timestamp, .time: return k.text214 default:215 let d = k.doubleValue ?? 0216 return d == d.rounded() ? String(Int(d)) : String(d)217 }218 }.joined(separator: "\u{a6}")219}220221/// Every non-empty cell of one value of a pivot result, keyed like the reference cubes.222func pivotMap(_ r: PivotResult, value: Int = 0, raw: Bool = false) -> [String: Double] {223 var out = [String: Double]()224 for (ri, row) in r.rowItems.enumerated() where row.kind != .group {225 for (ci, col) in r.columnItems.enumerated() {226 let i = ri * r.dataColumnCount + ci * r.valueCount + value227 if let v = raw ? r.raw[i] : r.cells[i] { out[pivotKey(row.keys) + "||" + pivotKey(col.keys)] = v }228 }229 }230 return out231}232233func expectMap(_ got: [String: Double], equals want: [String: Double], _ label: String, subset: Bool = false, sourceLocation: SourceLocation = #_sourceLocation) {234 if !subset {235 let missing = Set(want.keys).subtracting(got.keys), extra = Set(got.keys).subtracting(want.keys)236 #expect(missing.isEmpty && extra.isEmpty, "\(label): missing \(missing.sorted().prefix(5)), unexpected \(extra.sorted().prefix(5))", sourceLocation: sourceLocation)237 }238 for (k, w) in want {239 guard let g = got[k] else {240 if subset { Issue.record("\(label): missing cell \(k)", sourceLocation: sourceLocation); return }241 continue242 }243 if !close(g, w, tolerance: 1e-8) { Issue.record("\(label): cell \(k) = \(g), want \(w)", sourceLocation: sourceLocation); return }244 }245}246