rdblue commented on code in PR #4071:
URL: https://github.com/apache/iceberg/pull/4071#discussion_r849958943
##########
core/src/test/java/org/apache/iceberg/TestSnapshotManager.java:
##########
@@ -250,4 +248,449 @@ public void testCherryPickOverwrite() {
lastSnapshotId, table.currentSnapshot().snapshotId());
validateTableFiles(table, FILE_A, FILE_B);
}
+
+ @Test
+ public void testCreateBranch() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ // Test a basic case of creating a branch
+ table.manageSnapshots()
+ .createBranch("branch1", snapshotId)
+ .commit();
+ SnapshotRef expectedBranch = table.ops().refresh().ref("branch1");
+ Assert.assertTrue(expectedBranch != null &&
+ expectedBranch.equals(SnapshotRef.branchBuilder(snapshotId).build()));
+ }
+
+ @Test
+ public void testCreateBranchFailsWhenRefAlreadyExists() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ table.manageSnapshots()
+ .createBranch("branch1", snapshotId)
+ .commit();
+ // Trying to create a branch with an existing name should fail
+ AssertHelpers.assertThrows("Creating branch which already exists should
fail",
+ IllegalArgumentException.class, "Ref branch1 already exists",
+ () -> table.manageSnapshots().createBranch("branch1",
snapshotId).commit());
+
+ // Trying to create another branch within the same chain
+ AssertHelpers.assertThrows("Creating branch which already exists should
fail",
+ IllegalArgumentException.class, "Ref branch2 already exists",
+ () -> table.manageSnapshots().createBranch("branch2",
snapshotId).createBranch("branch2", snapshotId).commit());
+ }
+
+
+ @Test
+ public void testCreateTag() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ // Test a basic case of creating a tag
+ table.manageSnapshots()
+ .createTag("tag1", snapshotId)
+ .commit();
+ SnapshotRef expectedTag = table.ops().refresh().ref("tag1");
+
+ Assert.assertTrue(expectedTag != null &&
+ expectedTag.equals(SnapshotRef.tagBuilder(snapshotId).build()));
+ }
+
+ @Test
+ public void testCreateTagFailsWhenRefAlreadyExists() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ table.manageSnapshots()
+ .createTag("tag1", snapshotId)
+ .commit();
+
+ // Trying to create a tag with an existing name should fail
+ AssertHelpers.assertThrows("Creating tag which already exists should fail",
+ IllegalArgumentException.class, "Ref tag1 already exists",
+ () -> table.manageSnapshots().createTag("tag1", snapshotId).commit());
+
+ // Trying to create another tag within the same chain
+ AssertHelpers.assertThrows("Creating branch which already exists should
fail",
+ IllegalArgumentException.class, "Ref tag2 already exists",
+ () -> table.manageSnapshots()
+ .createTag("tag2", snapshotId)
+ .createTag("tag2", snapshotId).commit());
+ }
+
+ @Test
+ public void testRemoveBranch() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ // Test a basic case of creating and then removing a branch and tag
+ table.manageSnapshots()
+ .createBranch("branch1", snapshotId)
+ .commit();
+ table.manageSnapshots()
+ .removeBranch("branch1")
+ .commit();
+
+ TableMetadata updated = table.ops().refresh();
+ SnapshotRef expectedBranch = updated.ref("branch1");
+ Assert.assertNull(expectedBranch);
+
+ // Test chained creating and removal of branch and tag
+ table.manageSnapshots()
+ .createBranch("branch2", snapshotId)
+ .removeBranch("branch2")
+ .commit();
+ updated = table.ops().refresh();
+ Assert.assertNull(updated.ref("branch2"));
+ }
+
+ @Test
+ public void testRemovingNonExistingBranchFails() {
+ AssertHelpers.assertThrows("Trying to remove non-existent branch should
fail",
+ IllegalArgumentException.class, "Branch does not exist: non-existing",
+ () -> table.manageSnapshots().removeBranch("non-existing").commit());
+ }
+
+ @Test
+ public void testRemovingMainBranchFails() {
+ AssertHelpers.assertThrows("Removing main should fail",
+ IllegalArgumentException.class, "Cannot remove main branch",
+ () ->
table.manageSnapshots().removeBranch(SnapshotRef.MAIN_BRANCH).commit());
+ }
+
+ @Test
+ public void testRemoveTag() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ // Test a basic case of creating and then removing a branch and tag
+ table.manageSnapshots()
+ .createTag("tag1", snapshotId)
+ .commit();
+ table.manageSnapshots()
+ .removeTag("tag1")
+ .commit();
+ TableMetadata updated = table.ops().refresh();
+ SnapshotRef expectedTag = updated.ref("tag1");
+ Assert.assertNull(expectedTag);
+
+ // Test chained creating and removal of a tag
+ table.manageSnapshots()
+ .createTag("tag2", snapshotId)
+ .removeTag("tag2")
+ .commit();
+ Assert.assertEquals(updated, table.ops().refresh());
+ Assert.assertNull(updated.ref("tag2"));
+ }
+
+ @Test
+ public void testRemovingNonExistingTagFails() {
+ AssertHelpers.assertThrows("Removing a non-existing tag should fail",
+ IllegalArgumentException.class, "Tag does not exist: non-existing",
+ () -> table.manageSnapshots().removeTag("non-existing").commit());
+ }
+
+ @Test
+ public void testReplaceBranch() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .set("wap.id", "123")
+ .stageOnly()
+ .commit();
+ Snapshot firstSnapshot = Iterables.getOnlyElement(table.snapshots());
+ table.manageSnapshots().createBranch("branch1",
firstSnapshot.snapshotId()).commit();
+ table.newAppend()
+ .appendFile(FILE_B)
+ .set("wap.id", "456")
+ .stageOnly()
+ .commit();
+ Snapshot secondSnapshot = Iterables.get(table.snapshots(), 1);
+ table.manageSnapshots().createBranch("branch2",
secondSnapshot.snapshotId()).commit();
+ table.manageSnapshots().replaceBranch("branch1", "branch2").commit();
+ Assert.assertEquals(table.ops().refresh().ref("branch1").snapshotId(),
secondSnapshot.snapshotId());
+ }
+
+ @Test
+ public void testReplaceBranchNonExistingTargetBranchFails() {
+ AssertHelpers.assertThrows("Replacing a non-existing branch should fail",
+ IllegalArgumentException.class, "Target branch does not exist:
non-existing",
+ () -> table.manageSnapshots().replaceBranch("non-existing",
"other-branch").commit());
+ }
+
+ @Test
+ public void testReplaceBranchNonExistingSourceFails() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+ long snapshotId = table.currentSnapshot().snapshotId();
+ table.manageSnapshots()
+ .createBranch("branch1", snapshotId)
+ .commit();
+ AssertHelpers.assertThrows("Replacing where the source ref does not exist
should fail",
+ IllegalArgumentException.class, "Ref does not exist: non-existing",
+ () -> table.manageSnapshots().replaceBranch("branch1",
"non-existing").commit());
+ }
+
+ @Test
+ public void testFastForward() {
+ table.newAppend()
+ .appendFile(FILE_A)
+ .commit();
+
+ table.newAppend()
+ .appendFile(FILE_B)
+ .set("wap.id", "123456789")
Review Comment:
This ID isn't really needed. `stageOnly` is sufficient. This is just a way
to get Spark to call `stageOnly`.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]