@Test public void testWideningNullableToNotNullable() { Schema input = Schema.of(Schema.Field.nullable("f0", FieldType.INT32)); Schema output = Schema.of(Schema.Field.of("f0", FieldType.INT32)); List<Cast.CompatibilityError> errors = Cast.Widening.of().apply(input, output); Cast.CompatibilityError expected = Cast.CompatibilityError.create( Arrays.asList("f0"), "Can't cast nullable field to non-nullable field"); assertThat(errors, containsInAnyOrder(expected)); }
@Test public void testNarrowingNullableToNotNullable() { Schema input = Schema.of(Schema.Field.nullable("f0", FieldType.INT32)); Schema output = Schema.of(Schema.Field.of("f0", FieldType.INT32)); List<Cast.CompatibilityError> errors = Cast.Narrowing.of().apply(input, output); assertThat(errors, empty()); } }
public void testWideningOrder(TypeName input, TypeName output) { Schema inputSchema = Schema.of(Schema.Field.of("f0", FieldType.of(input))); Schema outputSchema = Schema.of(Schema.Field.of("f0", FieldType.of(output))); List<Cast.CompatibilityError> errors = Cast.Widening.of().apply(inputSchema, outputSchema); if (NUMERIC_ORDER.indexOf(input) <= NUMERIC_ORDER.indexOf(output)) { assertThat(input + " is before " + output, errors, empty()); } else { assertThat(input + " is after " + output, errors, not(empty())); } }
@Test(expected = IllegalArgumentException.class) public void testIgnoreNullFail() { Schema inputSchema = Schema.of(Schema.Field.nullable("f0", Schema.FieldType.INT32)); Schema outputSchema = Schema.of(Schema.Field.of("f0", Schema.FieldType.INT32)); Cast.castRow(Row.withSchema(inputSchema).addValue(null).build(), inputSchema, outputSchema); }
@Test public void testWrongName() { Schema schema = Schema.of(Field.of("f_byte", FieldType.BYTE)); thrown.expect(IllegalArgumentException.class); schema.getField("f_string"); }
@Test public void testWrongIndex() { Schema schema = Schema.of(Field.of("f_byte", FieldType.BYTE)); thrown.expect(IndexOutOfBoundsException.class); schema.getField(1); }
@Test public void testByteBufferEquality() { byte[] a0 = new byte[] {1, 2, 3, 4}; byte[] b0 = new byte[] {1, 2, 3, 4}; Schema schema = Schema.of(Schema.Field.of("bytes", Schema.FieldType.BYTES)); Row a = Row.withSchema(schema).addValue(ByteBuffer.wrap(a0)).build(); Row b = Row.withSchema(schema).addValue(ByteBuffer.wrap(b0)).build(); Assert.assertEquals(a, b); } }
@Test public void testArrayOfRowSchema() { Schema nestedSchema = Schema.of(Field.of("f1_str", FieldType.STRING)); FieldType arrayType = FieldType.array(FieldType.row(nestedSchema)); Schema schema = Schema.of(Field.of("f_array", arrayType)); Field field = schema.getField("f_array"); assertEquals("f_array", field.getName()); assertEquals(arrayType, field.getType()); }
@Test public void testNestedSchema() { Schema nestedSchema = Schema.of(Field.of("f1_str", FieldType.STRING)); Schema schema = Schema.of(Field.of("nested", FieldType.row(nestedSchema))); Field inner = schema.getField("nested").getType().getRowSchema().getField("f1_str"); assertEquals("f1_str", inner.getName()); assertEquals(FieldType.STRING, inner.getType()); }
@Test public void testConsistentWithEqualsArrayOfArrayOfBytes() throws Exception { FieldType fieldType = FieldType.array(FieldType.array(FieldType.BYTES)); Schema schema = Schema.of(Schema.Field.of("f1", fieldType)); RowCoder coder = RowCoder.of(schema); List<byte[]> innerList1 = Collections.singletonList(new byte[] {1, 2, 3, 4}); List<List<byte[]>> list1 = Collections.singletonList(innerList1); Row row1 = Row.withSchema(schema).addValue(list1).build(); List<byte[]> innerList2 = Collections.singletonList(new byte[] {1, 2, 3, 4}); List<List<byte[]>> list2 = Collections.singletonList(innerList2); Row row2 = Row.withSchema(schema).addValue(list2).build(); Assume.assumeTrue(coder.consistentWithEquals()); CoderProperties.coderConsistentWithEquals(coder, row1, row2); } }
@Test public void testConsistentWithEqualsArrayOfBytes() throws Exception { FieldType fieldType = FieldType.array(FieldType.BYTES); Schema schema = Schema.of(Schema.Field.of("f1", fieldType)); RowCoder coder = RowCoder.of(schema); List<byte[]> list1 = Collections.singletonList(new byte[] {1, 2, 3, 4}); Row row1 = Row.withSchema(schema).addValue(list1).build(); List<byte[]> list2 = Collections.singletonList(new byte[] {1, 2, 3, 4}); Row row2 = Row.withSchema(schema).addValue(list2).build(); Assume.assumeTrue(coder.consistentWithEquals()); CoderProperties.coderConsistentWithEquals(coder, row1, row2); }
@Test public void testArraySchema() { FieldType arrayType = FieldType.array(FieldType.STRING); Schema schema = Schema.of(Field.of("f_array", arrayType)); Field field = schema.getField("f_array"); assertEquals("f_array", field.getName()); assertEquals(arrayType, field.getType()); }
@Test @Ignore public void testConsistentWithEqualsMapWithBytesKeyField() throws Exception { FieldType fieldType = FieldType.map(FieldType.BYTES, FieldType.INT32); Schema schema = Schema.of(Schema.Field.of("f1", fieldType)); RowCoder coder = RowCoder.of(schema); Map<byte[], Integer> map1 = Collections.singletonMap(new byte[] {1, 2, 3, 4}, 1); Row row1 = Row.withSchema(schema).addValue(map1).build(); Map<byte[], Integer> map2 = Collections.singletonMap(new byte[] {1, 2, 3, 4}, 1); Row row2 = Row.withSchema(schema).addValue(map2).build(); Assume.assumeTrue(coder.consistentWithEquals()); CoderProperties.coderConsistentWithEquals(coder, row1, row2); }
@Test public void testNestedArraySchema() { FieldType arrayType = FieldType.array(FieldType.array(FieldType.STRING)); Schema schema = Schema.of(Field.of("f_array", arrayType)); Field field = schema.getField("f_array"); assertEquals("f_array", field.getName()); assertEquals(arrayType, field.getType()); }
@Test public void testByteArrayEquality() { byte[] a0 = new byte[] {1, 2, 3, 4}; byte[] b0 = new byte[] {1, 2, 3, 4}; Schema schema = Schema.of(Schema.Field.of("bytes", Schema.FieldType.BYTES)); Row a = Row.withSchema(schema).addValue(a0).build(); Row b = Row.withSchema(schema).addValue(b0).build(); Assert.assertEquals(a, b); }
@Test public void testConsistentWithEqualsBytesField() throws Exception { Schema schema = Schema.of(Schema.Field.of("f1", FieldType.BYTES)); Row row1 = Row.withSchema(schema).addValue(new byte[] {1, 2, 3, 4}).build(); Row row2 = Row.withSchema(schema).addValue(new byte[] {1, 2, 3, 4}).build(); RowCoder coder = RowCoder.of(schema); Assume.assumeTrue(coder.consistentWithEquals()); CoderProperties.coderConsistentWithEquals(coder, row1, row2); }