amogh-jahagirdar commented on a change in pull request #4071:
URL: https://github.com/apache/iceberg/pull/4071#discussion_r837036493



##########
File path: core/src/test/java/org/apache/iceberg/TestSnapshotManager.java
##########
@@ -250,4 +248,266 @@ 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()));
+
+    // Trying to create a branch with an existing name should fail
+    AssertHelpers.assertThrows("Should fail validation check if we try and 
create branch again",
+        ValidationException.class, "Reference with name branch1 already 
exists",
+        () -> table.manageSnapshots().createBranch("branch1", 
snapshotId).commit());
+
+    // Trying to create another branch within the same chain
+    AssertHelpers.assertThrows("Should fail validation check if we try and 
create branch again",
+        ValidationException.class, "Reference with name 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()));
+
+    // Trying to create a tag with an existing name should fail
+    AssertHelpers.assertThrows("Should fail validation check if we try and 
create tag again",
+        ValidationException.class, "Reference with name tag1 already exists",
+        () -> table.manageSnapshots().createTag("tag1", snapshotId).commit());
+
+    // Trying to create another tag within the same chain
+    AssertHelpers.assertThrows("Should fail validation check if we try and 
create tag again",
+        ValidationException.class, "Reference with name tag2 already exists",
+        () -> table.manageSnapshots()
+            .createBranch("tag2", snapshotId)
+            .createBranch("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.assertTrue(expectedBranch == null);
+
+    // Test chained creating and removal of branch and tag
+    table.manageSnapshots()
+        .createBranch("branch2", 1)
+        .removeBranch("branch2")
+        .commit();
+    updated = table.ops().refresh();
+    Assert.assertTrue(updated.ref("branch2") == null);
+
+    AssertHelpers.assertThrows("Should fail validation check if we try to 
remove a non-existant branch",
+        ValidationException.class, "No such branch with name non-existing",
+        () -> table.manageSnapshots().removeBranch("non-existing").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.assertTrue(expectedTag == null);
+
+    // Test chained creating and removal of a tag
+    table.manageSnapshots()
+        .createTag("tag2", 1)
+        .removeTag("tag2")
+        .commit();
+    updated = table.ops().refresh();
+    Assert.assertTrue(updated.ref("tag2") == null);
+    AssertHelpers.assertThrows("Should fail validation check if we try to 
remove a non-existant branch",
+        ValidationException.class, "No such tag with name non-existing",
+        () -> table.manageSnapshots().removeTag("non-existing").commit());
+  }
+
+  @Test
+  public void testReplaceBranch() {
+    table.newAppend()
+        .appendFile(FILE_A)
+        .commit();
+    long snapshotId = table.currentSnapshot().snapshotId();
+    table.manageSnapshots()
+        .createBranch("branch1", snapshotId)
+        .commit();
+    // Create a new snapshot and replace the tip of branch1 to be the new 
snapshot
+    table.newAppend()
+        .appendFile(FILE_B)
+        .commit();
+    long currentSnapshot = 
table.ops().refresh().currentSnapshot().snapshotId();
+    table.manageSnapshots().replaceBranch("branch1", currentSnapshot).commit();
+    Assert.assertEquals(table.ops().refresh().ref("branch1").snapshotId(), 
currentSnapshot);
+  }
+
+  @Test
+  public void testReplaceTag() {
+    table.newAppend()
+        .appendFile(FILE_A)
+        .commit();
+    long snapshotId = table.currentSnapshot().snapshotId();
+    table.manageSnapshots()
+        .createTag("tag1", snapshotId)
+        .commit();
+    // Create a new snapshot and replace the tip of branch1 to be the new 
snapshot
+    table.newAppend()
+        .appendFile(FILE_B)
+        .commit();
+    long currentSnapshot = 
table.ops().refresh().currentSnapshot().snapshotId();
+    table.manageSnapshots().replaceTag("tag1", currentSnapshot).commit();
+    Assert.assertEquals(table.ops().refresh().ref("tag1").snapshotId(), 
currentSnapshot);
+  }
+
+  @Test
+  public void testUpdatingBranchRetention() {
+    table.newAppend()
+        .appendFile(FILE_A)
+        .commit();
+    long snapshotId = table.currentSnapshot().snapshotId();
+    // Test creating and updating independently
+    table.manageSnapshots()
+        .createBranch("branch1", snapshotId)
+        .commit();
+    table.manageSnapshots()
+        .setMinNumberOfSnapshotsToKeep("branch1", 10)
+        .setMaxSnapshotAgeMs("branch1", 20000)
+        .commit();
+    TableMetadata updated = table.ops().refresh();
+    Assert.assertEquals(20000, (long) 
updated.ref("branch1").maxSnapshotAgeMs());
+    Assert.assertEquals(10, (long) 
updated.ref("branch1").minSnapshotsToKeep());
+    // Test creating and updating in a chain
+    table.manageSnapshots()
+        .createBranch("branch2", snapshotId)
+        .setMinNumberOfSnapshotsToKeep("branch2", 10)
+        .setMaxSnapshotAgeMs("branch2", 20000)
+        .commit();
+    updated = table.ops().refresh();
+    Assert.assertEquals(20000, (long) 
updated.ref("branch2").maxSnapshotAgeMs());
+    Assert.assertEquals(10, (long) 
updated.ref("branch2").minSnapshotsToKeep());
+  }
+
+  @Test
+  public void testUpdatingReferenceRetention() {
+    table.newAppend()
+        .appendFile(FILE_A)
+        .commit();
+    long snapshotId = table.currentSnapshot().snapshotId();
+    // Test creating and updating independently
+    table.manageSnapshots()
+        .createBranch("branch1", snapshotId)
+        .createTag("tag1", snapshotId)
+        .commit();
+    table.manageSnapshots()
+        .setMaxRefAgeMs("branch1", 10000)
+        .setMaxRefAgeMs("tag1", 10000)
+        .commit();
+    TableMetadata updated = table.ops().refresh();
+    Assert.assertEquals(10000, (long) updated.ref("branch1").maxRefAgeMs());
+    Assert.assertEquals(10000, (long) updated.ref("tag1").maxRefAgeMs());
+    // Test creating and updating in a chain
+    table.manageSnapshots()
+        .createTag("tag2", snapshotId)
+        .createBranch("branch2", snapshotId)
+        .setMaxRefAgeMs("tag2", 10000)
+        .setMaxRefAgeMs("branch2", 10000)
+        .commit();
+    updated = table.ops().refresh();
+    Assert.assertEquals(10000, (long) updated.ref("tag2").maxRefAgeMs());
+    Assert.assertEquals(10000, (long) updated.ref("branch2").maxRefAgeMs());
+  }
+
+  @Test
+  public void testCreateReferencesAndRollback() {
+    table.newAppend()
+        .appendFile(FILE_A)
+        .commit();
+    table.newAppend()
+        .appendFile(FILE_A)
+        .commit();
+    long snapshotId = table.currentSnapshot().snapshotId();
+
+    table.manageSnapshots()
+        .createBranch("branch1", snapshotId)
+        .createTag("tag1", snapshotId)
+        .rollbackTo(1)
+        .commit();
+
+    TableMetadata current = table.ops().current();
+    Assert.assertEquals(current.currentSnapshot().snapshotId(), 1);
+    SnapshotRef actualTag = current.ref("tag1");
+    SnapshotRef actualBranch = current.ref("branch1");
+    Assert.assertEquals(1, current.currentSnapshot().snapshotId());
+    Assert.assertEquals(SnapshotRef.branchBuilder(2).build(), actualBranch);
+    Assert.assertEquals(SnapshotRef.tagBuilder(2).build(), actualTag);
+  }
+

Review comment:
       Need to add a test for rename




-- 
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]

Reply via email to