Repository Public License
1// ORIX Convert — Packages/EngineData/Tests/EngineDataTests/MatrixTests.swift2// Description: Every declared tabular path converts the canonical dataset, passes the engine's own validation, and3// is read back and compared cell by cell with the expected values.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineData910struct PathCase: Sendable, CustomTestStringConvertible {11 let source: FormatID12 let destination: FormatID13 var testDescription: String { "\(source.rawValue) → \(destination.rawValue)" }14}1516let tabularPaths: [PathCase] = DataEngine().paths.filter { !FormatClass.isTreePath($0.source, $0.destination) }.map { PathCase(source: $0.source, destination: $0.destination) }1718/// What the canonical dataset becomes on a path, as canonical texts.19func expectedTexts(source: FormatID, destination: FormatID) -> (names: [String], rows: [[String?]]) {20 var rows = Canonical.texts21 let names = Canonical.names22 let formats = [source, destination]23 // XML cannot tell an empty element from a missing one; LibreOffice's XLS filter drops empty strings.24 if formats.contains(.xml) || formats.contains(.xls) {25 for r in rows.indices { for c in rows[r].indices where rows[r][c] == "" { rows[r][c] = nil } }26 }27 return (names, rows)28}2930@Suite("Tabular path matrix")31struct MatrixTests {32 @Test("canonical dataset through every declared tabular path", arguments: tabularPaths)33 func path(_ p: PathCase) async throws {34 let scratch = Scratch("matrix")35 let source = try await Canonical.fixture(p.source, in: scratch)36 let converted = try await runConversion(source, p.source, to: p.destination, in: scratch)37 #expect(converted.output.files.count == 1)38 #expect(converted.output.facts["rows"] == "4")3940 let checks = try await DataEngine().validate(converted.output, request: converted.request, source: nil)41 #expect(!checks.isEmpty)42 for c in checks { #expect(c.passed, "validation “\(c.name)” failed: \(c.detail)") }4344 let preview = try await DataPreview.preview(url: converted.file, format: p.destination, rows: 100)45 let expected = expectedTexts(source: p.source, destination: p.destination)46 // Property lists have no key order and formats that omit empty values return columns in order of first47 // appearance: compare by column name there, by position everywhere else.48 let unordered: Set<FormatID> = [.plist, .toml, .xml]49 if unordered.contains(p.source) || unordered.contains(p.destination) {50 #expect(Set(preview.columns.map(\.name)) == Set(expected.names))51 } else {52 #expect(preview.columns.map(\.name) == expected.names)53 }54 #expect(preview.rows.count == expected.rows.count)55 let index = Dictionary(uniqueKeysWithValues: preview.columns.enumerated().map { ($0.element.name, $0.offset) })56 for (r, (got, want)) in zip(preview.rows, expected.rows).enumerated() {57 for (c, name) in expected.names.enumerated() {58 let value = index[name].flatMap { $0 < got.count ? got[$0] : nil }59 #expect(Validator.equivalent(value, want[c]), "row \(r + 1) column \(name): got \(String(describing: value)), expected \(String(describing: want[c]))")60 }61 }62 #expect(preview.totalRows == 4)63 }64}65