Skip to content

Commit

Permalink
A couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kosak committed Nov 5, 2024
1 parent 267986b commit e2da685
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/test/java/io/deephaven/csv/CsvReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.*;
Expand Down Expand Up @@ -1978,6 +1979,73 @@ public void fixedColumnWidthsShortRows(boolean allowMissingColumns) throws CsvRe
}
}

/**
* If there is no header row, the caller needs to specify column widths.
*/
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void noHeaderRowRequiresFixColumnWidthsSpecified(boolean specifyColumnWidths) throws CsvReaderException {
final String input =
""
+ "GOOG Dividend 0.25 200\n"
+ "T Dividend 0.15 300\n"
+ "Z Coupon 0.18 500\n";

final ColumnSet expected =
ColumnSet.of(
Column.ofRefs("Column1", "GOOG", "T", "Z"),
Column.ofRefs("Column2", "Dividend", "Dividend", "Coupon"),
Column.ofValues("Column3", 0.25, 0.15, 0.18),
Column.ofValues("Column4", 200, 300, 500));

final CsvSpecs.Builder specsBase = defaultCsvBuilder().hasFixedWidthColumns(true).hasHeaderRow(false)
.delimiter(' ').ignoreSurroundingSpaces(true);

if (specifyColumnWidths) {
final CsvSpecs specs = specsBase.fixedColumnWidths(Arrays.asList(6, 9, 8, 3)).build();
invokeTest(specs, input, expected);
} else {
final CsvSpecs specs = specsBase.build();
Assertions.assertThatThrownBy(() -> invokeTest(specs, input, expected))
.hasMessage("Can't proceed because hasHeaderRow is false but fixedColumnWidths is unspecified");
}
}

/**
* If there is no header row, the caller may specify column names. Otherwise synthetic column names will be
* generated.
*/
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void columnNamesMayBeSpecified(boolean specifyColumnNames) throws CsvReaderException {
final String input =
""
+ "GOOG Dividend 0.25 200\n"
+ "T Dividend 0.15 300\n"
+ "Z Coupon 0.18 500\n";

final String[] expectedColumnNames = specifyColumnNames ?
new String[]{"Sym", "Type", "Price", "SecurityId"} :
new String[]{"Column1", "Column2", "Column3", "Column4"};


final ColumnSet expected =
ColumnSet.of(
Column.ofRefs(expectedColumnNames[0], "GOOG", "T", "Z"),
Column.ofRefs(expectedColumnNames[1], "Dividend", "Dividend", "Coupon"),
Column.ofValues(expectedColumnNames[2], 0.25, 0.15, 0.18),
Column.ofValues(expectedColumnNames[3], 200, 300, 500));

CsvSpecs.Builder specsBuilder = defaultCsvBuilder().hasFixedWidthColumns(true).hasHeaderRow(false)
.delimiter(' ').ignoreSurroundingSpaces(true).fixedColumnWidths(Arrays.asList(6, 9, 8, 3));

if (specifyColumnNames) {
specsBuilder = specsBuilder.headers(Arrays.asList(expectedColumnNames));
}

invokeTest(specsBuilder.build(), input, expected);
}

/**
* All six Unicode characters ♡♥❥❦◑╳ are in the Basic Multilingual Plane and can all be represented with a single
* Java char. Therefore, they are counted the same with both counting conventions.
Expand Down

0 comments on commit e2da685

Please sign in to comment.