rdblue commented on a change in pull request #2465:
URL: https://github.com/apache/iceberg/pull/2465#discussion_r615319859
##########
File path: core/src/test/java/org/apache/iceberg/TestSchemaUpdate.java
##########
@@ -1223,4 +1223,98 @@ public void testMoveBetweenStructsFails() {
.moveBefore("s2.x", "s1.a")
.apply());
}
+
+ @Test
+ public void testAddIdentifierField() {
+ Schema newSchema = new SchemaUpdate(SCHEMA, SCHEMA_LAST_COLUMN_ID)
+ .allowIncompatibleChanges()
+ .addIdentifierField("id")
+ .addRequiredColumn("id2", Types.StringType.get())
+ .addIdentifierField("id2")
+ .apply();
+ Assert.assertEquals(Sets.newHashSet(1, SCHEMA_LAST_COLUMN_ID + 1),
newSchema.identifierFieldIds());
+
+ AssertHelpers.assertThrows("Should fail if column with name not exist",
+ IllegalArgumentException.class,
+ "Cannot find column of the given name in existing or newly added
columns",
+ () -> new SchemaUpdate(SCHEMA,
SCHEMA_LAST_COLUMN_ID).addIdentifierField("unknown").apply());
+
+ AssertHelpers.assertThrows("Should fail if column with name not exist",
+ IllegalArgumentException.class,
+ "it is not a required field",
+ () -> new SchemaUpdate(SCHEMA,
SCHEMA_LAST_COLUMN_ID).addIdentifierField("data").apply());
+
+ AssertHelpers.assertThrows("Should fail if column with name not exist",
+ IllegalArgumentException.class,
+ "it is not a primitive field",
+ () -> new SchemaUpdate(SCHEMA,
SCHEMA_LAST_COLUMN_ID).addIdentifierField("locations").apply());
+ }
+
+ @Test
+ public void testRenameIdentifierField() {
+ Schema schemaWithIdentifierFields = new SchemaUpdate(SCHEMA,
SCHEMA_LAST_COLUMN_ID)
+ .allowIncompatibleChanges()
+ .addIdentifierField("id")
+ .apply();
+
+ Schema newSchema = new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .renameColumn("id", "id2")
+ .apply();
+
+ Assert.assertEquals(Sets.newHashSet(1), newSchema.identifierFieldIds());
+ }
+
+ @Test
+ public void testMoveIdentifierField() {
+ Schema schemaWithIdentifierFields = new SchemaUpdate(SCHEMA,
SCHEMA_LAST_COLUMN_ID)
+ .allowIncompatibleChanges()
+ .addIdentifierField("id")
+ .apply();
+
+ Schema newSchema = new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .moveAfter("id", "locations")
+ .apply();
+ Assert.assertEquals(Sets.newHashSet(1), newSchema.identifierFieldIds());
+
+ newSchema = new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .moveBefore("id", "locations")
+ .apply();
+ Assert.assertEquals(Sets.newHashSet(1), newSchema.identifierFieldIds());
+
+ newSchema = new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .moveFirst("id")
+ .apply();
+ Assert.assertEquals(Sets.newHashSet(1), newSchema.identifierFieldIds());
+ }
+
+ @Test
+ public void testRemoveIdentifierField() {
+ Schema schemaWithIdentifierFields = new SchemaUpdate(SCHEMA,
SCHEMA_LAST_COLUMN_ID)
+ .allowIncompatibleChanges()
+ .addIdentifierField("id")
+ .apply();
+
+ AssertHelpers.assertThrows("Should fail if deleting column before deleting
it as identifier field",
+ IllegalArgumentException.class,
+ "Cannot delete a column that is an identifier field",
+ () -> new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .deleteColumn("id").apply());
+
+ Schema newSchema = new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .removeIdentifierField("id")
+ .apply();
+ Assert.assertEquals(Sets.newHashSet(), newSchema.identifierFieldIds());
+
+ AssertHelpers.assertThrows("Should fail if column with name not exist",
+ IllegalArgumentException.class,
+ "Cannot find column of name",
+ () -> new SchemaUpdate(schemaWithIdentifierFields,
SCHEMA_LAST_COLUMN_ID)
+ .removeIdentifierField("unknown").apply());
Review comment:
I think that this should be a separate test case. This is testing that
you can't remove an unknown identifier field. The code above this point tests
the behavior of `deleteColumn` with an identifier field, then is a basic test
of `removeIdentifierField`. Those are both separate cases as well.
Can you separate each idea into its own test case? That makes tests much
easier to maintain and to find out what's going wrong when they fail because
they are only testing one specific case. In addition, it also makes more test
failures visible because one test failure won't prevent another test case from
running.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]