public class WriteChannelConfigurationTest { private static final CsvOptions CSV_OPTIONS = CsvOptions.builder() .allowJaggedRows(true) .allowQuotedNewLines(false) .encoding(StandardCharsets.UTF_8) .build(); private static final TableId TABLE_ID = TableId.of("dataset", "table"); private static final CreateDisposition CREATE_DISPOSITION = CreateDisposition.CREATE_IF_NEEDED; private static final WriteDisposition WRITE_DISPOSITION = WriteDisposition.WRITE_APPEND; private static final Integer MAX_BAD_RECORDS = 42; private static final String FORMAT = "CSV"; private static final Boolean IGNORE_UNKNOWN_VALUES = true; private static final List<String> PROJECTION_FIELDS = ImmutableList.of("field1", "field2"); private static final Field FIELD_SCHEMA = Field.builder("IntegerField", Field.Type.integer()) .mode(Field.Mode.REQUIRED) .description("FieldDescription") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA); private static final WriteChannelConfiguration LOAD_CONFIGURATION = WriteChannelConfiguration.builder(TABLE_ID) .createDisposition(CREATE_DISPOSITION) .writeDisposition(WRITE_DISPOSITION) .formatOptions(CSV_OPTIONS) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .maxBadRecords(MAX_BAD_RECORDS) .projectionFields(PROJECTION_FIELDS) .schema(TABLE_SCHEMA) .build(); @Test public void testToBuilder() { compareLoadConfiguration(LOAD_CONFIGURATION, LOAD_CONFIGURATION.toBuilder().build()); WriteChannelConfiguration configuration = LOAD_CONFIGURATION.toBuilder().destinationTable(TableId.of("dataset", "newTable")).build(); assertEquals("newTable", configuration.destinationTable().table()); configuration = configuration.toBuilder().destinationTable(TABLE_ID).build(); compareLoadConfiguration(LOAD_CONFIGURATION, configuration); } @Test public void testOf() { WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID); assertEquals(TABLE_ID, configuration.destinationTable()); configuration = WriteChannelConfiguration.of(TABLE_ID, CSV_OPTIONS); assertEquals(TABLE_ID, configuration.destinationTable()); assertEquals(FORMAT, configuration.format()); assertEquals(CSV_OPTIONS, configuration.csvOptions()); } @Test public void testToBuilderIncomplete() { WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID); compareLoadConfiguration(configuration, configuration.toBuilder().build()); } @Test public void testBuilder() { assertEquals(TABLE_ID, LOAD_CONFIGURATION.destinationTable()); assertEquals(CREATE_DISPOSITION, LOAD_CONFIGURATION.createDisposition()); assertEquals(WRITE_DISPOSITION, LOAD_CONFIGURATION.writeDisposition()); assertEquals(CSV_OPTIONS, LOAD_CONFIGURATION.csvOptions()); assertEquals(FORMAT, LOAD_CONFIGURATION.format()); assertEquals(IGNORE_UNKNOWN_VALUES, LOAD_CONFIGURATION.ignoreUnknownValues()); assertEquals(MAX_BAD_RECORDS, LOAD_CONFIGURATION.maxBadRecords()); assertEquals(PROJECTION_FIELDS, LOAD_CONFIGURATION.projectionFields()); assertEquals(TABLE_SCHEMA, LOAD_CONFIGURATION.schema()); } @Test public void testToPbAndFromPb() { assertNull(LOAD_CONFIGURATION.toPb().getLoad().getSourceUris()); compareLoadConfiguration( LOAD_CONFIGURATION, WriteChannelConfiguration.fromPb(LOAD_CONFIGURATION.toPb())); WriteChannelConfiguration configuration = WriteChannelConfiguration.of(TABLE_ID); compareLoadConfiguration(configuration, WriteChannelConfiguration.fromPb(configuration.toPb())); } private void compareLoadConfiguration( WriteChannelConfiguration expected, WriteChannelConfiguration value) { assertEquals(expected, value); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); assertEquals(expected.destinationTable(), value.destinationTable()); assertEquals(expected.createDisposition(), value.createDisposition()); assertEquals(expected.writeDisposition(), value.writeDisposition()); assertEquals(expected.csvOptions(), value.csvOptions()); assertEquals(expected.format(), value.format()); assertEquals(expected.ignoreUnknownValues(), value.ignoreUnknownValues()); assertEquals(expected.maxBadRecords(), value.maxBadRecords()); assertEquals(expected.projectionFields(), value.projectionFields()); assertEquals(expected.schema(), value.schema()); } }
@Test public void testToBuilderIncomplete() { CsvOptions csvOptions = CsvOptions.builder().fieldDelimiter("|").build(); assertEquals(csvOptions, csvOptions.toBuilder().build()); }
@Test public void testToAndFromPb() { compareCsvOptions(CSV_OPTIONS, CsvOptions.fromPb(CSV_OPTIONS.toPb())); CsvOptions csvOptions = CsvOptions.builder().allowJaggedRows(ALLOW_JAGGED_ROWS).build(); compareCsvOptions(csvOptions, CsvOptions.fromPb(csvOptions.toPb())); }
public class CsvOptionsTest { private static final Boolean ALLOW_JAGGED_ROWS = true; private static final Boolean ALLOW_QUOTED_NEWLINE = true; private static final Charset ENCODING = StandardCharsets.UTF_8; private static final String FIELD_DELIMITER = ","; private static final String QUOTE = "\""; private static final long SKIP_LEADING_ROWS = 42L; private static final CsvOptions CSV_OPTIONS = CsvOptions.builder() .allowJaggedRows(ALLOW_JAGGED_ROWS) .allowQuotedNewLines(ALLOW_QUOTED_NEWLINE) .encoding(ENCODING) .fieldDelimiter(FIELD_DELIMITER) .quote(QUOTE) .skipLeadingRows(SKIP_LEADING_ROWS) .build(); @Test public void testToBuilder() { compareCsvOptions(CSV_OPTIONS, CSV_OPTIONS.toBuilder().build()); CsvOptions csvOptions = CSV_OPTIONS.toBuilder().fieldDelimiter(";").build(); assertEquals(";", csvOptions.fieldDelimiter()); csvOptions = csvOptions.toBuilder().fieldDelimiter(",").build(); compareCsvOptions(CSV_OPTIONS, csvOptions); } @Test public void testToBuilderIncomplete() { CsvOptions csvOptions = CsvOptions.builder().fieldDelimiter("|").build(); assertEquals(csvOptions, csvOptions.toBuilder().build()); } @Test public void testBuilder() { assertEquals(FormatOptions.CSV, CSV_OPTIONS.type()); assertEquals(ALLOW_JAGGED_ROWS, CSV_OPTIONS.allowJaggedRows()); assertEquals(ALLOW_QUOTED_NEWLINE, CSV_OPTIONS.allowQuotedNewLines()); assertEquals(ENCODING.name(), CSV_OPTIONS.encoding()); assertEquals(FIELD_DELIMITER, CSV_OPTIONS.fieldDelimiter()); assertEquals(QUOTE, CSV_OPTIONS.quote()); assertEquals(SKIP_LEADING_ROWS, (long) CSV_OPTIONS.skipLeadingRows()); } @Test public void testToAndFromPb() { compareCsvOptions(CSV_OPTIONS, CsvOptions.fromPb(CSV_OPTIONS.toPb())); CsvOptions csvOptions = CsvOptions.builder().allowJaggedRows(ALLOW_JAGGED_ROWS).build(); compareCsvOptions(csvOptions, CsvOptions.fromPb(csvOptions.toPb())); } private void compareCsvOptions(CsvOptions expected, CsvOptions value) { assertEquals(expected, value); assertEquals(expected.allowJaggedRows(), value.allowJaggedRows()); assertEquals(expected.allowQuotedNewLines(), value.allowQuotedNewLines()); assertEquals(expected.encoding(), value.encoding()); assertEquals(expected.fieldDelimiter(), value.fieldDelimiter()); assertEquals(expected.quote(), value.quote()); assertEquals(expected.skipLeadingRows(), value.skipLeadingRows()); } }