Skip to content

Commit

Permalink
Core: Switch tests to JUnit5 in avro, data.avro packages (#8380)
Browse files Browse the repository at this point in the history
  • Loading branch information
skytin1004 authored Sep 20, 2023
1 parent 09a5dbc commit c1e877a
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 215 deletions.
78 changes: 43 additions & 35 deletions core/src/test/java/org/apache/iceberg/avro/TestAvroNameMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
import org.apache.iceberg.types.Comparators;
import org.apache.iceberg.types.Types;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

@SuppressWarnings("unchecked")
public class TestAvroNameMapping extends TestAvroReadProjection {
Expand Down Expand Up @@ -81,11 +80,13 @@ public void testMapProjections() throws IOException {

Record projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
// field id 5 comes from read schema
Assert.assertNotNull(
"Field missing from table mapping is renamed",
projected.getSchema().getField("location_r5"));
Assert.assertNull("location field should not be read", projected.get("location_r5"));
Assert.assertEquals(34L, projected.get("id"));
Assertions.assertThat(projected.getSchema().getField("location_r5"))
.as("Field missing from table mapping is renamed")
.isNotNull();
Assertions.assertThat(projected.get("location_r5"))
.as("location field should not be read")
.isNull();
Assertions.assertThat(projected.get("id")).isEqualTo(34L);

// Table mapping partially project `location` map value
nameMapping =
Expand All @@ -104,9 +105,12 @@ public void testMapProjections() throws IOException {

projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
Record projectedL1 = ((Map<String, Record>) projected.get("location")).get("l1");
Assert.assertNotNull(
"Field missing from table mapping is renamed", projectedL1.getSchema().getField("long_r2"));
Assert.assertNull("location.value.long, should not be read", projectedL1.get("long_r2"));
Assertions.assertThat(projectedL1.getSchema().getField("long_r2"))
.as("Field missing from table mapping is renamed")
.isNotNull();
Assertions.assertThat(projectedL1.get("long_r2"))
.as("location.value.long, should not be read")
.isNull();
}

@Test
Expand Down Expand Up @@ -176,13 +180,15 @@ public void testComplexMapKeys() throws IOException {
Map<Record, Record> projectedLocation = (Map<Record, Record>) projected.get("location");
Record projectedKey = projectedLocation.keySet().iterator().next();
Record projectedValue = projectedLocation.values().iterator().next();
Assert.assertEquals(
0, Comparators.charSequences().compare("k1", (CharSequence) projectedKey.get("k1")));
Assert.assertEquals(
0, Comparators.charSequences().compare("k2", (CharSequence) projectedKey.get("k2")));
Assert.assertEquals(52.995143f, projectedValue.get("lat"));
Assert.assertNotNull(projectedValue.getSchema().getField("long_r2"));
Assert.assertNull(projectedValue.get("long_r2"));
Assertions.assertThat(
Comparators.charSequences().compare("k1", (CharSequence) projectedKey.get("k1")))
.isEqualTo(0);
Assertions.assertThat(
Comparators.charSequences().compare("k2", (CharSequence) projectedKey.get("k2")))
.isEqualTo(0);
Assertions.assertThat(projectedValue.get("lat")).isEqualTo(52.995143f);
Assertions.assertThat(projectedValue.getSchema().getField("long_r2")).isNotNull();
Assertions.assertThat(projectedValue.get("long_r2")).isNull();
}

@Test
Expand Down Expand Up @@ -243,11 +249,11 @@ public void testArrayProjections() throws Exception {
Schema readSchema = writeSchema;

Record projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
Assert.assertNotNull(
"Field missing from table mapping is renamed", projected.getSchema().getField("point_r22"));
Assert.assertNull("point field is not projected", projected.get("point_r22"));
Assert.assertEquals(34L, projected.get("id"));

Assertions.assertThat(projected.getSchema().getField("point_r22"))
.as("Field missing from table mapping is renamed")
.isNotNull();
Assertions.assertThat(projected.get("point_r22")).as("point field is not projected").isNull();
Assertions.assertThat(projected.get("id")).isEqualTo(34L);
// point array is partially projected
nameMapping =
MappingUtil.create(
Expand All @@ -263,12 +269,12 @@ public void testArrayProjections() throws Exception {

projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
Record point = ((List<Record>) projected.get("point")).get(0);

Assert.assertNotNull(
"Field missing from table mapping is renamed", point.getSchema().getField("y_r18"));
Assert.assertEquals("point.x is projected", 1, point.get("x"));
Assert.assertNull("point.y is not projected", point.get("y_r18"));
Assert.assertEquals(34L, projected.get("id"));
Assertions.assertThat(point.getSchema().getField("y_r18"))
.as("Field missing from table mapping is renamed")
.isNotNull();
Assertions.assertThat(point.get("x")).as("point.x is projected").isEqualTo(1);
Assertions.assertThat(point.get("y_r18")).as("point.y is not projected").isNull();
Assertions.assertThat(projected.get("id")).isEqualTo(34L);
}

@Test
Expand Down Expand Up @@ -316,8 +322,9 @@ public void testAliases() throws IOException {
Types.NestedField.required(19, "y", Types.IntegerType.get())))));

Record projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
Assert.assertEquals(
"x is read as y", 1, ((List<Record>) projected.get("points")).get(0).get("y"));
Assertions.assertThat(((List<Record>) projected.get("points")).get(0).get("y"))
.as("x is read as y")
.isEqualTo(1);

readSchema =
new Schema(
Expand All @@ -331,8 +338,9 @@ public void testAliases() throws IOException {
Types.NestedField.required(19, "z", Types.IntegerType.get())))));

projected = writeAndRead(writeSchema, readSchema, record, nameMapping);
Assert.assertEquals(
"x is read as z", 1, ((List<Record>) projected.get("points")).get(0).get("z"));
Assertions.assertThat(((List<Record>) projected.get("points")).get(0).get("z"))
.as("x is read as z")
.isEqualTo(1);
}

@Test
Expand All @@ -349,7 +357,7 @@ public void testInferredMapping() throws IOException {
Schema readSchema = writeSchema;
// Pass null for nameMapping so that it is automatically inferred from read schema
Record projected = writeAndRead(writeSchema, readSchema, record, null);
Assert.assertEquals(record, projected);
Assertions.assertThat(projected).isEqualTo(record);
}

@Test
Expand All @@ -369,15 +377,15 @@ protected Record writeAndRead(
Record record = super.writeAndRead(desc, writeSchema, readSchema, inputRecord);
Record projectedWithNameMapping =
writeAndRead(writeSchema, readSchema, inputRecord, MappingUtil.create(writeSchema));
Assert.assertEquals(record, projectedWithNameMapping);
Assertions.assertThat(projectedWithNameMapping).isEqualTo(record);
return record;
}

private Record writeAndRead(
Schema writeSchema, Schema readSchema, Record record, NameMapping nameMapping)
throws IOException {

File file = temp.newFile();
File file = temp.resolve("test.avro").toFile();
// Write without file ids
org.apache.avro.Schema writeAvroSchema = RemoveIds.removeIds(writeSchema);
DatumWriter<Record> datumWriter = new GenericDatumWriter<>(writeAvroSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.types.Types;
import org.junit.Assert;
import org.junit.Test;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestAvroReadProjection extends TestReadProjection {
@Override
protected GenericData.Record writeAndRead(
String desc, Schema writeSchema, Schema readSchema, GenericData.Record record)
throws IOException {
File file = temp.newFile(desc + ".avro");
file.delete();
File file = temp.resolve(desc + ".avro").toFile();

try (FileAppender<GenericData.Record> appender =
Avro.write(Files.localOutput(file)).schema(writeSchema).build()) {
Expand Down Expand Up @@ -73,13 +72,11 @@ public void testAvroArrayAsLogicalMap() throws IOException {

GenericData.Record projected =
writeAndRead("full_projection", writeSchema, writeSchema, record);
Assert.assertEquals(
"Should contain correct value list",
values1,
((Map<Long, List<Long>>) projected.get("map")).get(100L));
Assert.assertEquals(
"Should contain correct value list",
values2,
((Map<Long, List<Long>>) projected.get("map")).get(200L));
Assertions.assertThat(((Map<Long, List<Long>>) projected.get("map")).get(100L))
.as("Should contain correct value list")
.isEqualTo(values1);
Assertions.assertThat(((Map<Long, List<Long>>) projected.get("map")).get(200L))
.as("Should contain correct value list")
.isEqualTo(values2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package org.apache.iceberg.avro;

import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.junit.Assert.assertEquals;

import java.util.Collections;
import java.util.function.Supplier;
import org.apache.avro.SchemaBuilder;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.Types;
import org.junit.Test;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestBuildAvroProjection {

Expand Down Expand Up @@ -68,11 +68,13 @@ public void projectArrayWithElementSchemaUnchanged() {

final org.apache.avro.Schema actual = testSubject.array(expected, supplier);

assertEquals("Array projection produced undesired array schema", expected, actual);
assertEquals(
"Unexpected element ID discovered on the projected array schema",
0,
Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue());
Assertions.assertThat(actual)
.as("Array projection produced undesired array schema")
.isEqualTo(expected);
Assertions.assertThat(
Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue())
.as("Unexpected element ID discovered on the projected array schema")
.isEqualTo(0);
}

@Test
Expand Down Expand Up @@ -141,11 +143,13 @@ public void projectArrayWithExtraFieldInElementSchema() {

final org.apache.avro.Schema actual = testSubject.array(extraField, supplier);

assertEquals("Array projection produced undesired array schema", expected, actual);
assertEquals(
"Unexpected element ID discovered on the projected array schema",
0,
Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue());
Assertions.assertThat(actual)
.as("Array projection produced undesired array schema")
.isEqualTo(expected);
Assertions.assertThat(
Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue())
.as("Unexpected element ID discovered on the projected array schema")
.isEqualTo(0);
}

@Test
Expand Down Expand Up @@ -202,11 +206,13 @@ public void projectArrayWithLessFieldInElementSchema() {

final org.apache.avro.Schema actual = testSubject.array(lessField, supplier);

assertEquals("Array projection produced undesired array schema", expected, actual);
assertEquals(
"Unexpected element ID discovered on the projected array schema",
0,
Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue());
Assertions.assertThat(actual)
.as("Array projection produced undesired array schema")
.isEqualTo(expected);
Assertions.assertThat(
Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue())
.as("Unexpected element ID discovered on the projected array schema")
.isEqualTo(0);
}

@Test
Expand Down Expand Up @@ -250,15 +256,15 @@ public void projectMapWithValueSchemaUnchanged() {

final org.apache.avro.Schema actual = testSubject.map(expected, supplier);

assertEquals("Map projection produced undesired map schema", expected, actual);
assertEquals(
"Unexpected key ID discovered on the projected map schema",
0,
Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue());
assertEquals(
"Unexpected value ID discovered on the projected map schema",
1,
Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue());
Assertions.assertThat(actual)
.as("Map projection produced undesired map schema")
.isEqualTo(expected);
Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue())
.as("Unexpected key ID discovered on the projected map schema")
.isEqualTo(0);
Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue())
.as("Unexpected value ID discovered on the projected map schema")
.isEqualTo(1);
}

@Test
Expand Down Expand Up @@ -331,15 +337,15 @@ public void projectMapWithExtraFieldInValueSchema() {

final org.apache.avro.Schema actual = testSubject.map(extraField, supplier);

assertEquals("Map projection produced undesired map schema", expected, actual);
assertEquals(
"Unexpected key ID discovered on the projected map schema",
0,
Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue());
assertEquals(
"Unexpected value ID discovered on the projected map schema",
1,
Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue());
Assertions.assertThat(actual)
.as("Map projection produced undesired map schema")
.isEqualTo(expected);
Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue())
.as("Unexpected key ID discovered on the projected map schema")
.isEqualTo(0);
Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue())
.as("Unexpected value ID discovered on the projected map schema")
.isEqualTo(1);
}

@Test
Expand Down Expand Up @@ -400,14 +406,14 @@ public void projectMapWithLessFieldInValueSchema() {

final org.apache.avro.Schema actual = testSubject.map(lessField, supplier);

assertEquals("Map projection produced undesired map schema", expected, actual);
assertEquals(
"Unexpected key ID discovered on the projected map schema",
0,
Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue());
assertEquals(
"Unexpected value ID discovered on the projected map schema",
1,
Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue());
Assertions.assertThat(actual)
.as("Map projection produced undesired map schema")
.isEqualTo(expected);
Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue())
.as("Unexpected key ID discovered on the projected map schema")
.isEqualTo(0);
Assertions.assertThat(Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue())
.as("Unexpected value ID discovered on the projected map schema")
.isEqualTo(1);
}
}
Loading

0 comments on commit c1e877a

Please sign in to comment.