|
| 1 | +package io.github.dfa1.vortex.cli; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.io.TempDir; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.PrintStream; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +class CliIT { |
| 15 | + |
| 16 | + @Test |
| 17 | + void importExportRoundTrip(@TempDir Path tmp) throws Exception { |
| 18 | + // Given |
| 19 | + Path csvIn = tmp.resolve("data.csv"); |
| 20 | + Files.writeString(csvIn, "id,name\n1,Alice\n2,Bob\n3,Charlie\n"); |
| 21 | + |
| 22 | + // When — import CSV to Vortex |
| 23 | + int importExit = ImportCommand.run(new String[]{"import", csvIn.toString()}); |
| 24 | + assertThat(importExit).isZero(); |
| 25 | + |
| 26 | + // When — export Vortex back to CSV via stdout |
| 27 | + Path vortexPath = tmp.resolve("data.vortex"); |
| 28 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 29 | + PrintStream saved = System.out; |
| 30 | + System.setOut(new PrintStream(baos, true, StandardCharsets.UTF_8)); |
| 31 | + int exportExit; |
| 32 | + try { |
| 33 | + exportExit = ExportCommand.run(new String[]{"export", vortexPath.toString()}); |
| 34 | + } finally { |
| 35 | + System.setOut(saved); |
| 36 | + } |
| 37 | + |
| 38 | + // Then |
| 39 | + assertThat(exportExit).isZero(); |
| 40 | + String[] lines = baos.toString(StandardCharsets.UTF_8).split("\r?\n"); |
| 41 | + assertThat(lines).hasSize(4); |
| 42 | + assertThat(lines[0]).isEqualTo("id,name"); |
| 43 | + assertThat(lines[1]).isEqualTo("1,Alice"); |
| 44 | + assertThat(lines[2]).isEqualTo("2,Bob"); |
| 45 | + assertThat(lines[3]).isEqualTo("3,Charlie"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void inspectPrintsSchema(@TempDir Path tmp) throws Exception { |
| 50 | + // Given — create a vortex file via import |
| 51 | + Path csvIn = tmp.resolve("data.csv"); |
| 52 | + Files.writeString(csvIn, "id,score\n1,9.5\n"); |
| 53 | + ImportCommand.run(new String[]{"import", csvIn.toString()}); |
| 54 | + Path vortexPath = tmp.resolve("data.vortex"); |
| 55 | + |
| 56 | + // When |
| 57 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 58 | + PrintStream saved = System.out; |
| 59 | + System.setOut(new PrintStream(baos, true, StandardCharsets.UTF_8)); |
| 60 | + int exit; |
| 61 | + try { |
| 62 | + exit = InspectCommand.run(new String[]{"inspect", vortexPath.toString()}); |
| 63 | + } finally { |
| 64 | + System.setOut(saved); |
| 65 | + } |
| 66 | + |
| 67 | + // Then |
| 68 | + assertThat(exit).isZero(); |
| 69 | + String output = baos.toString(StandardCharsets.UTF_8); |
| 70 | + assertThat(output).contains("Schema:"); |
| 71 | + assertThat(output).contains("id"); |
| 72 | + assertThat(output).contains("score"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void schemaPrintsMachineReadableDType(@TempDir Path tmp) throws Exception { |
| 77 | + // Given — create a vortex file via import |
| 78 | + Path csvIn = tmp.resolve("data.csv"); |
| 79 | + Files.writeString(csvIn, "id,name\n1,Alice\n"); |
| 80 | + ImportCommand.run(new String[]{"import", csvIn.toString()}); |
| 81 | + Path vortexPath = tmp.resolve("data.vortex"); |
| 82 | + |
| 83 | + // When |
| 84 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 85 | + PrintStream saved = System.out; |
| 86 | + System.setOut(new PrintStream(baos, true, StandardCharsets.UTF_8)); |
| 87 | + int exit; |
| 88 | + try { |
| 89 | + exit = SchemaCommand.run(new String[]{"schema", vortexPath.toString()}); |
| 90 | + } finally { |
| 91 | + System.setOut(saved); |
| 92 | + } |
| 93 | + |
| 94 | + // Then |
| 95 | + assertThat(exit).isZero(); |
| 96 | + assertThat(baos.toString(StandardCharsets.UTF_8).strip()) |
| 97 | + .isEqualTo("struct<id: I64, name: utf8>"); |
| 98 | + } |
| 99 | +} |
0 commit comments