Repository Public License
1// ORIX Convert — Packages/EngineScientific/Tests/EngineScientificTests/EngineSurfaceTests.swift2// Description: The engine as the core sees it: declared paths, options, preflight warnings, and the remaining3// path (NetCDF → Parquet) converted and validated.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineScientific910@Suite struct EngineSurfaceTests {11 @Test func declaredPaths() {12 let engine = ScientificEngine()13 #expect(makeEngines().map(\.id) == ["scientific"])14 #expect(engine.paths.map(\.id) == [15 "scientific:hdf5>csv", "scientific:hdf5>parquet", "scientific:hdf5>json",16 "scientific:netcdf>csv", "scientific:netcdf>parquet", "scientific:netcdf>json",17 "scientific:hdf5>netcdf", "scientific:netcdf>hdf5", "scientific:netcdf>netcdf#netcdf.reformat", "scientific:hdf5>hdf5#hdf5.repack",18 ])19 #expect(engine.inspectableFormats == [.hdf5, .netcdf])20 for path in engine.paths {21 let options = engine.options(for: path)22 #expect(!options.isEmpty && Set(options.map(\.id)).count == options.count)23 #expect(!path.limitations.isEmpty)24 if ScientificEngine.tabular.contains(path.destination) {25 #expect(options.map(\.id).prefix(7) == ["variables", "subset", "layout", "fill", "packed", "time.decode", "rows.limit"])26 #expect(path.fidelity == .partial)27 } else {28 #expect(options.contains { $0.id == "compression" } && path.fidelity == .structural)29 }30 }31 // A request for a path that is not declared is refused.32 #expect(engine.path(for: ConversionRequest(source: URL(fileURLWithPath: "/x.h5"), sourceFormat: .hdf5, destinationFormat: .hdf5)) == nil)33 #expect(engine.path(for: ConversionRequest(source: URL(fileURLWithPath: "/x.h5"), sourceFormat: .hdf5, destinationFormat: .arrow)) == nil)34 }3536 @Test func undeclaredPathIsRefused() async {37 let temp = TempDir()38 let source = Fixture.single(temp.file("s.h5"))39 await #expect(throws: ConversionError.unsupportedPath(source: .hdf5, destination: .arrow)) {40 _ = try await ScientificEngine().convert(ConversionRequest(source: source, sourceFormat: .hdf5, destinationFormat: .arrow), context: ConversionContext(workDirectory: temp.url))41 }42 }4344 @Test func netCDFToParquet() async throws {45 let temp = TempDir()46 // Classic source: record variable, three coordinates, fill → null.47 let classic = try await convert(Fixture.classic(temp.file("c.nc")), .netcdf, to: .parquet, in: temp, options: ["variables": .string("/temp"), "fill": .string("null")])48 let p = try ParquetFile(url: classic.file)49 #expect(p.rows == 18 && p.columns.map(\.name) == ["time", "lat", "lon", "temp"])50 // time: int32 values widened to 64-bit labels; lat: float32; lon: float64; temp: int16, nullable.51 #expect(p.columns.map(\.physical) == [2, 4, 5, 1] && p.columns.map(\.converted) == [18, nil, nil, 16] && p.columns.map(\.optional) == [false, false, false, true])52 let temp_ = try p.read(column: 3, group: 0), time = try p.read(column: 0, group: 0), lon = try p.read(column: 2, group: 0)53 #expect((0..<18).map { temp_.isNull($0) ? "null" : temp_.values.text($0) } == ["0", "1", "2", "10", "11", "12", "100", "101", "null", "110", "111", "112", "200", "201", "202", "210", "211", "212"])54 #expect((0..<18).map { time.values.text($0) } == [String](repeating: "0", count: 6) + [String](repeating: "6", count: 6) + [String](repeating: "12", count: 6))55 #expect((0..<3).map { lon.values.text($0) } == ["-75.0", "-74.5", "-74.0"])56 #expect(p.metadata["orix_convert.attributes"]?.contains("\"/temp\":{\"_FillValue\":-999,\"scale_factor\":0.5,\"units\":\"K\"}") == true)57 #expect(p.metadata["orix_convert.transformation"]?.contains("\"source_format\":\"NetCDF classic (CDF-1)\"") == true)58 // NetCDF-4 source.59 let nc4 = try await convert(Fixture.netCDF4(temp.file("raw.nc")), .netcdf, to: .parquet, in: temp, options: ["variables": .string("/temp"), "layout": .string("wide")])60 let q = try ParquetFile(url: nc4.file)61 #expect(q.columns.map(\.name) == ["time", "x=1.5", "x=2.5", "x=3.5"] && q.rows == 2)62 #expect(try (0..<2).map { try q.read(column: 3, group: 0).values.text($0) } == ["12.0", "22.0"])63 }6465 @Test func preflightTellsWhatFlatteningDoes() throws {66 let temp = TempDir()67 let source = Fixture.everything(in: temp.url)68 let engine = ScientificEngine()69 let csv = engine.preflight(ConversionRequest(source: source, sourceFormat: .hdf5, destinationFormat: .csv, options: ConversionOptions(["variables": .string("/chunked/grid")])), inspection: nil)70 #expect(csv.map(\.code) == ["sci-packed-raw", "sci-fill-kept", "sci-flattened", "sci-attributes-not-carried"])71 #expect(csv[2].message.contains("600 row(s)"))72 let parquet = engine.preflight(ConversionRequest(source: source, sourceFormat: .hdf5, destinationFormat: .parquet, options: ConversionOptions(["variables": .string("/chunked/grid"), "packed": .string("unpack"), "fill": .string("null")])), inspection: nil)73 #expect(parquet.map(\.code) == ["sci-flattened"])74 #expect(engine.preflight(ConversionRequest(source: source, sourceFormat: .hdf5, destinationFormat: .netcdf), inspection: nil).isEmpty)75 // A file that cannot be opened gives a message, not a crash.76 let missing = engine.preflight(ConversionRequest(source: temp.file("nope.h5"), sourceFormat: .hdf5, destinationFormat: .csv), inspection: nil)77 #expect(missing.map(\.code) == ["sci-selection-needed"])78 }7980 @Test func publicBrowserAPI() async throws {81 let temp = TempDir()82 let file = try await SciFile.open(Fixture.everything(in: temp.url))83 #expect(try file.children(of: "/time").map(\.name) == ["t", "values"])84 #expect(try file.preview("/time/t").values == ["0.0", "1.5", "24.0", "48.0", "100000.0"])85 let classic = try await SciFile.open(Fixture.classic(temp.file("c.nc")))86 #expect(try classic.children().count == 7)87 #expect(throws: ConversionError.self) { _ = try classic.children(of: "/group") }88 }89}90