CsvReader extends FileReader implements DataReader<CsvReadOptions> { public Table read(CsvReadOptions options) throws IOException { return read(options, false); } CsvReader(); static void register(ReaderRegistry registry); Table read(CsvReadOptions options); String printColumnTypes(CsvReadOptions options); @Override Table read(Source source);  }
@Test public void testMaxCharsPerColumnPass() throws IOException { final Reader reader = new StringReader("Text" + LINE_END + "\"short\"" + LINE_END + "1234567890" + LINE_END); final int maxCharsPerColumn = 12; Table result = Table.read().csv(CsvReadOptions.builder(reader).maxCharsPerColumn(maxCharsPerColumn)); assertEquals(2, result.rowCount()); }
@Test public void testMaxCharsPerColumnException() { final Reader reader = new StringReader("Text" + LINE_END + "\"short\"" + LINE_END + "1234567890" + LINE_END); final int maxCharsPerColumn = 8; assertThrows( TextParsingException.class, () -> { Table.read().csv(CsvReadOptions.builder(reader).maxCharsPerColumn(maxCharsPerColumn)); }); }
@Test public void testWithBusData() throws IOException { Table table = Table.read() .csv(CsvReadOptions.builder("../data/bus_stop_test.csv").columnTypes(bus_types)); assertEquals( "[stop_id, stop_name, stop_desc, stop_lat, stop_lon]", table.columnNames().toString()); table = table.sortDescendingOn("stop_id"); table.removeColumns("stop_desc"); }
@Test public void testWithColumnSKIP() throws IOException { Table table = Table.read() .csv( CsvReadOptions.builder("../data/bus_stop_test.csv") .columnTypes(bus_types_with_SKIP)); assertEquals(4, table.columnCount()); assertEquals("[stop_id, stop_name, stop_lat, stop_lon]", table.columnNames().toString()); }
@Test public void testWithColumnSKIPWithoutHeader() throws IOException { Table table = Table.read() .csv( CsvReadOptions.builder("../data/bus_stop_noheader_test.csv") .header(false) .columnTypes(bus_types_with_SKIP)); assertEquals(4, table.columnCount()); assertEquals("[C0, C1, C3, C4]", table.columnNames().toString()); }
@Test public void testWithBushData() throws IOException { ColumnType[] types = {LOCAL_DATE, DOUBLE, STRING}; Table table = Table.read().csv(CsvReadOptions.builder("../data/bush.csv").columnTypes(types)); assertEquals(323, table.rowCount()); assertEquals("[date, approval, who]", table.columnNames().toString()); }
@Test public void testBushDataWithoutSamplingForTypeDetection() throws IOException { Table table = Table.read().csv(CsvReadOptions.builder("../data/bush.csv").sample(false)); assertEquals(323, table.rowCount()); assertEquals("[date, approval, who]", table.columnNames().toString()); }
@Test public void testWithMissingValue() throws IOException { CsvReadOptions options = CsvReadOptions.builder("../data/missing_values.csv") .dateFormat(DateTimeFormatter.ofPattern("yyyy.MM.dd")) .header(true) .missingValueIndicator("-") .build(); Table t = Table.read().csv(options); assertEquals(1, t.stringColumn(0).countMissing()); assertEquals(1, t.numberColumn(1).countMissing()); assertEquals(1, t.numberColumn(2).countMissing()); }
@Test public void testWindowsAndLinuxLineEndings() throws IOException { Reader reader = new StringReader( "TestCol\n" + "foobar1\n" + "foobar2\n" + "foobar3\n" + "foobar4\r\n" + "foobar5\r\n" + "foobar6\r\n"); Table t = Table.read().csv(reader); assertEquals(1, t.columnCount()); assertEquals(6, t.rowCount()); }
@Test public void testCustomLineEndings() throws IOException { CsvReadOptions options = CsvReadOptions.builder("../data/alt_line_endings.csv").lineEnding("~").header(true).build(); Table t = Table.read().csv(options); assertEquals(2, t.columnCount()); assertEquals(2, t.rowCount()); }
@Test public void testDateWithFormatter1() throws IOException { final boolean header = false; final char delimiter = ','; final boolean useSampling = true; CsvReadOptions options = CsvReadOptions.builder("../data/date_format_test.txt") .header(header) .separator(delimiter) .sample(useSampling) .dateFormat(DateTimeFormatter.ofPattern("yyyy.MM.dd")) .build(); final Table table = Table.read().csv(options); DateColumn date = table.dateColumn(0); assertFalse(date.isEmpty()); }
@Test public void testDateWithFormatter2() throws IOException { final boolean header = false; final char delimiter = ','; final boolean useSampling = true; CsvReadOptions options = CsvReadOptions.builder("../data/date_format_test.txt") .header(header) .separator(delimiter) .sample(useSampling) .dateFormat(DateTimeFormatter.ofPattern("yyyy.MM.dd")) .build(); final Table table = Table.read().csv(options); DateColumn date = table.dateColumn(0); assertFalse(date.isEmpty()); }
@Test public void testLoadFromUrlWithColumnTypes() throws IOException { ColumnType[] types = {LOCAL_DATE, DOUBLE, STRING}; Table table; try (InputStream input = new File("../data/bush.csv").toURI().toURL().openStream()) { table = Table.read() .csv( CsvReadOptions.builder(input) .tableName("Bush approval ratings") .columnTypes(types)); } assertNotNull(table); assertEquals(3, table.columnCount()); }
@Test public void testLoadFromUrl() throws IOException { Table table; try (InputStream input = new File("../data/bush.csv").toURI().toURL().openStream()) { table = Table.read().csv(CsvReadOptions.builder(input).tableName("Bush approval ratings")); } assertNotNull(table); assertEquals(3, table.columnCount()); }
@Test public void testLoadFromFileStream() throws IOException { String location = "../data/bush.csv"; Table table; File file = Paths.get(location).toFile(); try (InputStream input = new FileInputStream(file)) { table = Table.read().csv(CsvReadOptions.builder(input).tableName("Bush approval ratings")); } assertNotNull(table); assertEquals(3, table.columnCount()); }
@Test public void testLoadFromFileStreamReader() throws IOException { String location = "../data/bush.csv"; Table table; File file = Paths.get(location).toFile(); try (Reader reader = new FileReader(file)) { table = Table.read().csv(CsvReadOptions.builder(reader).tableName("Bush approval ratings")); } assertNotNull(table); assertEquals(3, table.columnCount()); }
@Test public void testEmptyRow() throws IOException { Table table = Table.read().csv("../data/empty_row.csv"); assertEquals(5, table.rowCount()); }
@Test public void testShortRow() { assertThrows( AddCellToColumnException.class, () -> { Table.read().csv("../data/short_row.csv"); }); }
@Test public void testLongRow() { assertThrows( RuntimeException.class, () -> { Table.read().csv("../data/long_row.csv"); }); }
@Test public void testBoundary1() throws IOException { Table table = Table.read().csv("../data/boundaryTest1.csv"); assertEquals(2, table.rowCount()); }
@Test public void testBoundary2() throws IOException { Table table = Table.read().csv("../data/boundaryTest2.csv"); assertEquals(2, table.rowCount()); }
@Test public void testReadFailure() throws IOException { Table table1 = Table.read() .csv(CsvReadOptions.builder("../data/read_failure_test.csv").minimizeColumnSizes()); table1.structure(); ShortColumn test = table1.shortColumn("Test"); assertNotNull(test.summary()); }
@Test public void testReadFailure2() throws IOException { Table table1 = Table.read() .csv(CsvReadOptions.builder("../data/read_failure_test2.csv").minimizeColumnSizes()); table1.structure(); ShortColumn test = table1.shortColumn("Test"); assertNotNull(test.summary()); }
@Test public void testEmptyFileHeaderEnabled() throws IOException { Table table1 = Table.read().csv(CsvReadOptions.builder("../data/empty_file.csv").header(false)); assertEquals("0 rows X 0 cols", table1.shape()); }
@Test public void testEmptyFileHeaderDisabled() throws IOException { Table table1 = Table.read().csv(CsvReadOptions.builder("../data/empty_file.csv").header(false)); assertEquals("0 rows X 0 cols", table1.shape()); }
@Test public void testReadWithMaxColumnsSetting() throws IOException { Table table1 = Table.read() .csv( CsvReadOptions.builder("../data/10001_columns.csv") .maxNumberOfColumns(10001) .header(false)); assertEquals("1 rows X 10001 cols", table1.shape()); }
@Test public void testSkipLinesWithComments() throws IOException { Table table1 = Table.read() .csv( CsvReadOptions.builder("../data/with_comments.csv") .maxNumberOfColumns(3) .commentPrefix('#') .header(true)); assertEquals("3 rows X 3 cols", table1.shape()); }
@Test public void carriageReturnLineEnding() throws IOException { Table table = Table.read().csv(CsvReadOptions.builder("../data/sacramento_real_estate_transactions.csv")); assertEquals(985, table.rowCount()); }
@Test public void testReadCsvWithRowSampling() throws IOException { int expectedSampleSize = 10; Table table = Table.read().csv(CsvReadOptions.builder("../data/cake.csv").sampleSize(expectedSampleSize)); assertEquals(expectedSampleSize, table.rowCount()); assertEquals("[recipe, mix, temp, y]", table.columnNames().toString()); }
@Test public void testReadCsvWithRowSamplingParsingNumbers() throws IOException { StringBuilder csv = new StringBuilder(); csv.append("RandomNumbers\n"); Set<Integer> values = new TreeSet<>( asList( 24323, 542345, 64323, 73640, 38453, 12735, 93456, 23457, 483075, 469364, 473936)); values.forEach(v -> csv.append(v + "\n")); Reader reader = new StringReader(csv.toString()); int expectedSampleSize = 5; Table t = Table.read().csv(CsvReadOptions.builder(reader).sampleSize(expectedSampleSize)); assertEquals(1, t.columnCount()); assertEquals(expectedSampleSize, t.rowCount()); assertEquals(INTEGER, t.column(0).type()); List<Integer> intValues = t.intColumn(0).asList(); assertEquals(true, values.containsAll(intValues)); }
@Test public void preserveQuote() throws IOException { Table table = Table.create("test", StringColumn.create("colName", Arrays.asList("\""))); Writer writer = new StringWriter(); table.write().csv(writer); String string = writer.toString(); Table out = Table.read().csv(new StringReader(string)); assertEquals(table.get(0, 0), out.get(0, 0)); }