|
37 | 37 | #include "iceberg/test/matchers.h" |
38 | 38 | #include "iceberg/transform.h" |
39 | 39 | #include "iceberg/type.h" |
| 40 | +#include "iceberg/util/timepoint.h" |
40 | 41 |
|
41 | 42 | namespace iceberg { |
42 | 43 |
|
@@ -368,4 +369,75 @@ TEST(TableUpdateTest, SetDefaultSortOrderApplyUpdate) { |
368 | 369 | EXPECT_EQ(metadata->default_sort_order_id, 1); |
369 | 370 | } |
370 | 371 |
|
| 372 | +// Test SetSnapshotRef ApplyTo for both branch and tag |
| 373 | +TEST(TableUpdateTest, SetSnapshotRefApplyUpdate) { |
| 374 | + // Test branch ref |
| 375 | + { |
| 376 | + auto base = CreateBaseMetadata(); |
| 377 | + auto builder = TableMetadataBuilder::BuildFrom(base.get()); |
| 378 | + |
| 379 | + // Add a snapshot that the ref will point to |
| 380 | + auto snapshot = std::make_shared<Snapshot>( |
| 381 | + Snapshot{.snapshot_id = 123456789, |
| 382 | + .sequence_number = 1, |
| 383 | + .timestamp_ms = TimePointMsFromUnixMs(1000000), |
| 384 | + .manifest_list = "s3://bucket/manifest-list.avro"}); |
| 385 | + builder->AddSnapshot(snapshot); |
| 386 | + |
| 387 | + // Apply SetSnapshotRef update for a branch |
| 388 | + table::SetSnapshotRef branch_update("my-branch", 123456789, SnapshotRefType::kBranch, |
| 389 | + 5, 86400000, 604800000); |
| 390 | + branch_update.ApplyTo(*builder); |
| 391 | + |
| 392 | + ICEBERG_UNWRAP_OR_FAIL(auto metadata, builder->Build()); |
| 393 | + |
| 394 | + // Verify the branch ref was added |
| 395 | + ASSERT_EQ(metadata->refs.size(), 1); |
| 396 | + auto it = metadata->refs.find("my-branch"); |
| 397 | + ASSERT_NE(it, metadata->refs.end()); |
| 398 | + |
| 399 | + const auto& ref = it->second; |
| 400 | + EXPECT_EQ(ref->snapshot_id, 123456789); |
| 401 | + EXPECT_EQ(ref->type(), SnapshotRefType::kBranch); |
| 402 | + |
| 403 | + const auto& branch = std::get<SnapshotRef::Branch>(ref->retention); |
| 404 | + EXPECT_EQ(branch.min_snapshots_to_keep.value(), 5); |
| 405 | + EXPECT_EQ(branch.max_snapshot_age_ms.value(), 86400000); |
| 406 | + EXPECT_EQ(branch.max_ref_age_ms.value(), 604800000); |
| 407 | + } |
| 408 | + |
| 409 | + // Test tag ref |
| 410 | + { |
| 411 | + auto base = CreateBaseMetadata(); |
| 412 | + auto builder = TableMetadataBuilder::BuildFrom(base.get()); |
| 413 | + |
| 414 | + // Add a snapshot that the ref will point to |
| 415 | + auto snapshot = std::make_shared<Snapshot>( |
| 416 | + Snapshot{.snapshot_id = 987654321, |
| 417 | + .sequence_number = 1, |
| 418 | + .timestamp_ms = TimePointMsFromUnixMs(2000000), |
| 419 | + .manifest_list = "s3://bucket/manifest-list.avro"}); |
| 420 | + builder->AddSnapshot(snapshot); |
| 421 | + |
| 422 | + // Apply SetSnapshotRef update for a tag |
| 423 | + table::SetSnapshotRef tag_update("release-1.0", 987654321, SnapshotRefType::kTag, |
| 424 | + std::nullopt, std::nullopt, 259200000); |
| 425 | + tag_update.ApplyTo(*builder); |
| 426 | + |
| 427 | + ICEBERG_UNWRAP_OR_FAIL(auto metadata, builder->Build()); |
| 428 | + |
| 429 | + // Verify the tag ref was added |
| 430 | + ASSERT_EQ(metadata->refs.size(), 1); |
| 431 | + auto it = metadata->refs.find("release-1.0"); |
| 432 | + ASSERT_NE(it, metadata->refs.end()); |
| 433 | + |
| 434 | + const auto& ref = it->second; |
| 435 | + EXPECT_EQ(ref->snapshot_id, 987654321); |
| 436 | + EXPECT_EQ(ref->type(), SnapshotRefType::kTag); |
| 437 | + |
| 438 | + const auto& tag = std::get<SnapshotRef::Tag>(ref->retention); |
| 439 | + EXPECT_EQ(tag.max_ref_age_ms.value(), 259200000); |
| 440 | + } |
| 441 | +} |
| 442 | + |
371 | 443 | } // namespace iceberg |
0 commit comments