Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion core/src/test/java/tech/tablesaw/io/csv/CsvReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ColumnType> 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<ColumnType> 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<ColumnType> actual = asList(new CsvReader().detectColumnTypes(reader, options));
assertEquals(List.of(STRING, LOCAL_DATE), actual);
}

@Test
public void testTimeDetection1() {

Expand Down