|
| 1 | +package io.github.dfa1.vortex.integration; |
| 2 | + |
| 3 | +import de.siegmar.fastcsv.writer.CsvWriter; |
| 4 | +import dev.hardwood.InputFile; |
| 5 | +import dev.hardwood.metadata.RepetitionType; |
| 6 | +import dev.hardwood.reader.ParquetFileReader; |
| 7 | +import dev.hardwood.reader.RowReader; |
| 8 | +import dev.hardwood.schema.ColumnSchema; |
| 9 | +import io.github.dfa1.vortex.core.error.VortexException; |
| 10 | +import io.github.dfa1.vortex.csv.CsvExporter; |
| 11 | +import io.github.dfa1.vortex.csv.ExportOptions; |
| 12 | +import org.junit.jupiter.api.DynamicTest; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.TestFactory; |
| 15 | +import org.opentest4j.TestAbortedException; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.StringWriter; |
| 19 | +import java.io.UncheckedIOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 29 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 30 | + |
| 31 | +/// Real-world cross-implementation conformance over the Raincloud corpus |
| 32 | +/// ([issue #205](https://github.com/dfa1/vortex-java/issues/205)). |
| 33 | +/// |
| 34 | +/// Each corpus dataset is a `.vortex` file written by the Vortex Python bindings with |
| 35 | +/// its `.parquet` sibling built from the same Arrow table. The parquet file is the |
| 36 | +/// oracle: hardwood reads it row-by-row and formats cells with the exact rules of |
| 37 | +/// `CsvExporter.cellValue` (null → empty field, `Double.toString`, …), writing through |
| 38 | +/// the same fastcsv writer so quoting is identical. A zero-diff against vortex-java's |
| 39 | +/// CSV export proves every value survived the Python-write / Java-read boundary. |
| 40 | +/// |
| 41 | +/// Skipped (visibly, via assumption) when the corpus is not hydrated — run |
| 42 | +/// `scripts/hydrate-raincloud-corpus.sh` first, or point `RAINCLOUD_CORPUS_MANIFEST` |
| 43 | +/// at a manifest TSV (`slug<TAB>vortex-path<TAB>parquet-path` per line). |
| 44 | +/// |
| 45 | +/// Expected per-slug status lives in `src/test/resources/raincloud/expected-status.csv`. |
| 46 | +/// Known gaps assert that the failure still occurs, so fixing the reader forces the |
| 47 | +/// matrix entry to flip to `ok` in the same change. |
| 48 | +class RaincloudConformanceIntegrationTest { |
| 49 | + |
| 50 | + private static final Path DEFAULT_MANIFEST = |
| 51 | + Path.of(System.getProperty("user.home"), ".cache", "raincloud", "corpus-manifest.tsv"); |
| 52 | + |
| 53 | + @Test |
| 54 | + void corpusIsHydrated() { |
| 55 | + // Given / When / Then — visible skip marker when the corpus is absent; the |
| 56 | + // factory below yields zero tests in that case, which would otherwise pass silently |
| 57 | + assumeTrue(Files.exists(manifestPath()), |
| 58 | + "raincloud corpus not hydrated — run scripts/hydrate-raincloud-corpus.sh"); |
| 59 | + } |
| 60 | + |
| 61 | + @TestFactory |
| 62 | + Stream<DynamicTest> conformancePerSlug() throws IOException { |
| 63 | + Path manifest = manifestPath(); |
| 64 | + if (!Files.exists(manifest)) { |
| 65 | + return Stream.empty(); |
| 66 | + } |
| 67 | + Map<String, String> expected = readExpectedStatus(); |
| 68 | + return Files.readAllLines(manifest).stream() |
| 69 | + .filter(line -> !line.isBlank()) |
| 70 | + .map(line -> line.split("\t")) |
| 71 | + .map(parts -> DynamicTest.dynamicTest(parts[0], () -> { |
| 72 | + String slug = parts[0]; |
| 73 | + Path vortex = Path.of(parts[1]); |
| 74 | + Path parquet = Path.of(parts[2]); |
| 75 | + // slugs missing from the matrix are treated as untriaged, not failing |
| 76 | + String status = expected.getOrDefault(slug, "untriaged"); |
| 77 | + switch (status) { |
| 78 | + case "ok" -> assertMatchesParquetOracle(vortex, parquet); |
| 79 | + case "untriaged" -> reportUntriaged(vortex, parquet); |
| 80 | + default -> assertStillFails(vortex, parquet, status); |
| 81 | + } |
| 82 | + })); |
| 83 | + } |
| 84 | + |
| 85 | + private static void assertMatchesParquetOracle(Path vortex, Path parquet) throws IOException { |
| 86 | + // Given |
| 87 | + List<String> oracleLines = oracleLines(parquet); |
| 88 | + |
| 89 | + // When |
| 90 | + List<String> result = exportVortex(vortex); |
| 91 | + |
| 92 | + // Then |
| 93 | + assertLinesMatch(result, oracleLines); |
| 94 | + } |
| 95 | + |
| 96 | + /// Runs the full conformance check but reports the outcome as an aborted test |
| 97 | + /// either way: only triaged matrix entries may count as green or red. The abort |
| 98 | + /// message says which way to flip the entry. |
| 99 | + private static void reportUntriaged(Path vortex, Path parquet) { |
| 100 | + try { |
| 101 | + assertMatchesParquetOracle(vortex, parquet); |
| 102 | + } catch (TestAbortedException e) { |
| 103 | + throw e; // oracle limitation, not a conformance outcome |
| 104 | + } catch (AssertionError | Exception e) { |
| 105 | + throw new TestAbortedException( |
| 106 | + "untriaged slug fails — classify as gap:<issue> in expected-status.csv: " + e); |
| 107 | + } |
| 108 | + throw new TestAbortedException("untriaged slug passes — flip its matrix entry to ok"); |
| 109 | + } |
| 110 | + |
| 111 | + private static void assertStillFails(Path vortex, Path parquet, String status) { |
| 112 | + // Given / When — a decode gap still reproduces when the export itself throws; |
| 113 | + // no oracle needed (the oracle may not even read this parquet, e.g. nested columns) |
| 114 | + List<String> result; |
| 115 | + try { |
| 116 | + result = exportVortex(vortex); |
| 117 | + } catch (VortexException e) { |
| 118 | + return; |
| 119 | + } catch (IOException e) { |
| 120 | + throw new UncheckedIOException(e); |
| 121 | + } |
| 122 | + |
| 123 | + // Then — the export now succeeds, so the gap must still be a silent-corruption |
| 124 | + // one (e.g. #208): values must still mismatch the oracle. A clean pass means the |
| 125 | + // gap was fixed: flip the expected-status.csv entry to ok in the same change |
| 126 | + List<String> oracleLines = oracleLines(parquet); |
| 127 | + assertThatThrownBy(() -> assertLinesMatch(result, oracleLines)) |
| 128 | + .as("known gap %s no longer reproduces — flip its matrix entry to ok", status) |
| 129 | + .isInstanceOf(AssertionError.class); |
| 130 | + } |
| 131 | + |
| 132 | + private static void assertLinesMatch(List<String> result, List<String> oracleLines) { |
| 133 | + assertThat(result).hasSameSizeAs(oracleLines); |
| 134 | + for (int i = 0; i < oracleLines.size(); i++) { |
| 135 | + assertThat(result.get(i)).as("line %d", i + 1).isEqualTo(oracleLines.get(i)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static List<String> exportVortex(Path vortex) throws IOException { |
| 140 | + StringWriter out = new StringWriter(); |
| 141 | + CsvExporter.exportCsv(vortex, out, ExportOptions.defaults()); |
| 142 | + return out.toString().lines().toList(); |
| 143 | + } |
| 144 | + |
| 145 | + /// Reads the parquet sibling through hardwood; an oracle-side failure (nested |
| 146 | + /// columns, unsupported physical type) aborts the slug rather than failing it — |
| 147 | + /// it says nothing about vortex-java. |
| 148 | + private static List<String> oracleLines(Path parquet) { |
| 149 | + StringWriter out = new StringWriter(); |
| 150 | + try { |
| 151 | + writeOracleCsv(parquet, out); |
| 152 | + } catch (TestAbortedException e) { |
| 153 | + throw e; |
| 154 | + } catch (Exception e) { |
| 155 | + throw new TestAbortedException("oracle cannot read the parquet sibling: " + e); |
| 156 | + } |
| 157 | + return out.toString().lines().toList(); |
| 158 | + } |
| 159 | + |
| 160 | + /// Oracle: hardwood reads the parquet sibling and emits CSV through the same |
| 161 | + /// fastcsv writer configuration as `CsvExporter`, using its exact cell rules. |
| 162 | + private static void writeOracleCsv(Path parquet, StringWriter out) throws IOException { |
| 163 | + try (ParquetFileReader pfr = ParquetFileReader.open(InputFile.of(parquet)); |
| 164 | + RowReader rows = pfr.rowReader(); |
| 165 | + CsvWriter csv = CsvWriter.builder().fieldSeparator(',').build(out)) { |
| 166 | + |
| 167 | + List<ColumnSchema> cols = pfr.getFileSchema().getColumns(); |
| 168 | + csv.writeRecord(cols.stream().map(ColumnSchema::name).toList()); |
| 169 | + |
| 170 | + String[] row = new String[cols.size()]; |
| 171 | + while (rows.hasNext()) { |
| 172 | + rows.next(); |
| 173 | + for (int c = 0; c < cols.size(); c++) { |
| 174 | + row[c] = oracleCell(cols.get(c), rows); |
| 175 | + } |
| 176 | + csv.writeRecord(row); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /// Formats a parquet cell with the exact rules of `CsvExporter.cellValue`: |
| 182 | + /// null rows export as an empty field, valid rows use the JDK canonical |
| 183 | + /// `toString` of the value. |
| 184 | + /// |
| 185 | + /// @param col the column schema |
| 186 | + /// @param rows the row reader positioned at the current row |
| 187 | + /// @return the formatted cell string |
| 188 | + private static String oracleCell(ColumnSchema col, RowReader rows) { |
| 189 | + String name = col.name(); |
| 190 | + if (col.repetitionType() == RepetitionType.OPTIONAL && rows.isNull(name)) { |
| 191 | + return ""; |
| 192 | + } |
| 193 | + return switch (col.type()) { |
| 194 | + case INT32 -> Integer.toString(rows.getInt(name)); |
| 195 | + case INT64 -> Long.toString(rows.getLong(name)); |
| 196 | + case FLOAT -> Float.toString(rows.getFloat(name)); |
| 197 | + case DOUBLE -> Double.toString(rows.getDouble(name)); |
| 198 | + case BOOLEAN -> Boolean.toString(rows.getBoolean(name)); |
| 199 | + case BYTE_ARRAY -> rows.getString(name); |
| 200 | + // aborts (not fails) the slug: the oracle can't format this physical type |
| 201 | + // yet, which is an oracle limitation rather than a vortex-java gap |
| 202 | + default -> throw new TestAbortedException( |
| 203 | + "oracle cannot format parquet type " + col.type() + " (column: " + name + ")"); |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | + private static Path manifestPath() { |
| 208 | + String env = System.getenv("RAINCLOUD_CORPUS_MANIFEST"); |
| 209 | + return env != null ? Path.of(env) : DEFAULT_MANIFEST; |
| 210 | + } |
| 211 | + |
| 212 | + private static Map<String, String> readExpectedStatus() throws IOException { |
| 213 | + try (var in = RaincloudConformanceIntegrationTest.class |
| 214 | + .getResourceAsStream("/raincloud/expected-status.csv")) { |
| 215 | + if (in == null) { |
| 216 | + throw new UncheckedIOException(new IOException("expected-status.csv not on test classpath")); |
| 217 | + } |
| 218 | + Map<String, String> statuses = new HashMap<>(); |
| 219 | + for (String line : new String(in.readAllBytes()).lines().toList()) { |
| 220 | + if (line.isBlank() || line.startsWith("#")) { |
| 221 | + continue; |
| 222 | + } |
| 223 | + String[] parts = line.split(",", 2); |
| 224 | + statuses.put(parts[0].trim(), parts[1].trim()); |
| 225 | + } |
| 226 | + return statuses; |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments