|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +package dev.vortex.spark; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import java.nio.file.Path; |
| 10 | +import org.apache.spark.sql.SparkSession; |
| 11 | +import org.junit.jupiter.api.AfterAll; |
| 12 | +import org.junit.jupiter.api.BeforeAll; |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.TestInstance; |
| 16 | +import org.junit.jupiter.api.io.TempDir; |
| 17 | + |
| 18 | +/** Tests for schema inference failure reporting in {@link VortexDataSourceV2}. */ |
| 19 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 20 | +public final class VortexDataSourceInferSchemaTest { |
| 21 | + |
| 22 | + private SparkSession spark; |
| 23 | + |
| 24 | + @TempDir |
| 25 | + Path tempDir; |
| 26 | + |
| 27 | + @BeforeAll |
| 28 | + public void setUp() { |
| 29 | + spark = SparkSession.builder() |
| 30 | + .appName("VortexInferSchemaTest") |
| 31 | + .master("local[1]") |
| 32 | + .config("spark.driver.host", "127.0.0.1") |
| 33 | + .config("spark.ui.enabled", "false") |
| 34 | + .getOrCreate(); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterAll |
| 38 | + public void tearDown() { |
| 39 | + if (spark != null) { |
| 40 | + spark.stop(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @DisplayName("Reading a directory without Vortex files reports the offending path") |
| 46 | + public void inferSchemaFailureNamesThePath() { |
| 47 | + String emptyDir = tempDir.toString(); |
| 48 | + |
| 49 | + Throwable thrown = assertThrows( |
| 50 | + Throwable.class, () -> spark.read().format("vortex").load(emptyDir)); |
| 51 | + |
| 52 | + String allMessages = messagesOf(thrown); |
| 53 | + assertTrue( |
| 54 | + allMessages.contains("no .vortex files found"), |
| 55 | + "error should explain that no Vortex files were found, got: " + allMessages); |
| 56 | + assertTrue(allMessages.contains(emptyDir), "error should name the offending path, got: " + allMessages); |
| 57 | + } |
| 58 | + |
| 59 | + /** Concatenates the messages of the whole cause chain, since Spark may wrap data source exceptions. */ |
| 60 | + private static String messagesOf(Throwable thrown) { |
| 61 | + StringBuilder sb = new StringBuilder(); |
| 62 | + for (Throwable t = thrown; t != null; t = t.getCause()) { |
| 63 | + sb.append(t.getMessage()).append('\n'); |
| 64 | + } |
| 65 | + return sb.toString(); |
| 66 | + } |
| 67 | +} |
0 commit comments