diff --git a/core/src/test/java/tech/tablesaw/io/csv/CsvReaderTest.java b/core/src/test/java/tech/tablesaw/io/csv/CsvReaderTest.java index 2c431bd6d..f4f31ddb5 100644 --- a/core/src/test/java/tech/tablesaw/io/csv/CsvReaderTest.java +++ b/core/src/test/java/tech/tablesaw/io/csv/CsvReaderTest.java @@ -21,7 +21,16 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static tech.tablesaw.api.ColumnType.*; +import static tech.tablesaw.api.ColumnType.BOOLEAN; +import static tech.tablesaw.api.ColumnType.DOUBLE; +import static tech.tablesaw.api.ColumnType.FLOAT; +import static tech.tablesaw.api.ColumnType.INTEGER; +import static tech.tablesaw.api.ColumnType.LOCAL_DATE; +import static tech.tablesaw.api.ColumnType.LOCAL_DATE_TIME; +import static tech.tablesaw.api.ColumnType.LOCAL_TIME; +import static tech.tablesaw.api.ColumnType.SHORT; +import static tech.tablesaw.api.ColumnType.SKIP; +import static tech.tablesaw.api.ColumnType.STRING; import com.google.common.collect.ImmutableMap; import com.univocity.parsers.common.TextParsingException; @@ -398,6 +407,83 @@ public void testDateDetection1() { assertEquals(Collections.singletonList(LOCAL_TIME), actual); } + /** Asserts ISO and US date formats are supported, while EU date format is not by default. */ + @Test + public void testDateDetection2() { + + final Reader reader = + new StringReader( + "DateISO,DateUS,DateEU" + + LINE_END + + "2024-03-20,03/20/2024,20/03/2024" + + LINE_END + + "2024-03-26,03/26/2024,26/03/2024" + + LINE_END); + + final boolean header = true; + + CsvReadOptions options = CsvReadOptions.builder(reader).header(header).build(); + + final List actual = asList(new CsvReader().detectColumnTypes(reader, options)); + assertEquals(List.of(LOCAL_DATE, LOCAL_DATE, STRING), actual); + } + + /** + * Asserts ISO dates > {@code LOCAL_DATE} and US dates > {@code STRING} for date format {@code + * yyyy-MM-dd}. + */ + @Test + public void testDateDetectionIsoDate() { + + final Reader reader = + new StringReader( + "DateISO,DateUS,DateEU" + + LINE_END + + "2024-03-20,03/20/2024" + + LINE_END + + "2024-03-26,03/26/2024" + + LINE_END); + + final boolean header = true; + + CsvReadOptions options = + CsvReadOptions.builder(reader) + .header(header) + .dateFormat(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + .build(); + + final List actual = asList(new CsvReader().detectColumnTypes(reader, options)); + assertEquals(List.of(LOCAL_DATE, STRING), actual); + } + + /** + * Asserts ISO dates > {@code STRING} and US dates > {@code LOCAL_DATE} for date format {@code + * MM/dd/yyyy}. + */ + @Test + public void testDateDetectionUsDate() { + + final Reader reader = + new StringReader( + "DateISO,DateUS,DateEU" + + LINE_END + + "2024-03-20,03/20/2024" + + LINE_END + + "2024-03-26,03/26/2024" + + LINE_END); + + final boolean header = true; + + CsvReadOptions options = + CsvReadOptions.builder(reader) + .header(header) + .dateFormat(DateTimeFormatter.ofPattern("MM/dd/yyyy")) + .build(); + + final List actual = asList(new CsvReader().detectColumnTypes(reader, options)); + assertEquals(List.of(STRING, LOCAL_DATE), actual); + } + @Test public void testTimeDetection1() {