Skip to content

Commit de779ff

Browse files
committed
test: add comprehensive tests for table requirement validations
Add tests for all newly implemented table requirement validation methods: - AssertDoesNotExist: 2 tests (success, table exists) - AssertRefSnapshotID: 6 tests (success, mismatch, missing ref, null base, nullopt success, nullopt but exists) - AssertLastAssignedFieldId: 3 tests (success, mismatch, null base) - AssertLastAssignedPartitionId: 3 tests (success, mismatch, null base) - AssertDefaultSpecID: 3 tests (success, mismatch, null base) - AssertDefaultSortOrderID: 3 tests (success, mismatch, null base) Total: 20 new tests added. All tests pass (73 tests in table_test suite).
1 parent b74cf85 commit de779ff

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

src/iceberg/test/table_metadata_builder_test.cc

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,179 @@ TEST_F(TableMetadataBuilderTest, TableRequirementAssertCurrentSchemaIDNotSet) {
264264
EXPECT_THAT(status, HasErrorMessage("schema ID is not set"));
265265
}
266266

267+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDoesNotExistSuccess) {
268+
table::AssertDoesNotExist requirement;
269+
270+
ASSERT_THAT(requirement.Validate(nullptr), IsOk());
271+
}
272+
273+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDoesNotExistTableExists) {
274+
table::AssertDoesNotExist requirement;
275+
276+
auto status = requirement.Validate(base_metadata_.get());
277+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
278+
EXPECT_THAT(status, HasErrorMessage("table already exists"));
279+
}
280+
281+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDSuccess) {
282+
auto ref = std::make_shared<SnapshotRef>();
283+
ref->snapshot_id = 100;
284+
ref->retention = SnapshotRef::Branch{};
285+
base_metadata_->refs["main"] = ref;
286+
287+
table::AssertRefSnapshotID requirement("main", 100);
288+
289+
ASSERT_THAT(requirement.Validate(base_metadata_.get()), IsOk());
290+
}
291+
292+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDMismatch) {
293+
auto ref = std::make_shared<SnapshotRef>();
294+
ref->snapshot_id = 100;
295+
ref->retention = SnapshotRef::Branch{};
296+
base_metadata_->refs["main"] = ref;
297+
298+
table::AssertRefSnapshotID requirement("main", 200);
299+
300+
auto status = requirement.Validate(base_metadata_.get());
301+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
302+
EXPECT_THAT(status, HasErrorMessage("snapshot ID does not match"));
303+
}
304+
305+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDRefMissing) {
306+
table::AssertRefSnapshotID requirement("missing-ref", 100);
307+
308+
auto status = requirement.Validate(base_metadata_.get());
309+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
310+
EXPECT_THAT(status, HasErrorMessage("missing in table metadata"));
311+
}
312+
313+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDNullBase) {
314+
table::AssertRefSnapshotID requirement("main", 100);
315+
316+
auto status = requirement.Validate(nullptr);
317+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
318+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
319+
}
320+
321+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDNulloptSuccess) {
322+
// Ref should not exist, and it doesn't
323+
table::AssertRefSnapshotID requirement("nonexistent", std::nullopt);
324+
325+
ASSERT_THAT(requirement.Validate(base_metadata_.get()), IsOk());
326+
}
327+
328+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDNulloptButExists) {
329+
auto ref = std::make_shared<SnapshotRef>();
330+
ref->snapshot_id = 100;
331+
ref->retention = SnapshotRef::Branch{};
332+
base_metadata_->refs["main"] = ref;
333+
334+
// Ref should not exist, but it does
335+
table::AssertRefSnapshotID requirement("main", std::nullopt);
336+
337+
auto status = requirement.Validate(base_metadata_.get());
338+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
339+
EXPECT_THAT(status, HasErrorMessage("should not exist"));
340+
}
341+
342+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedFieldIdSuccess) {
343+
base_metadata_->last_column_id = 10;
344+
table::AssertLastAssignedFieldId requirement(10);
345+
346+
ASSERT_THAT(requirement.Validate(base_metadata_.get()), IsOk());
347+
}
348+
349+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedFieldIdMismatch) {
350+
base_metadata_->last_column_id = 10;
351+
table::AssertLastAssignedFieldId requirement(15);
352+
353+
auto status = requirement.Validate(base_metadata_.get());
354+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
355+
EXPECT_THAT(status, HasErrorMessage("last assigned field ID does not match"));
356+
}
357+
358+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedFieldIdNullBase) {
359+
table::AssertLastAssignedFieldId requirement(10);
360+
361+
auto status = requirement.Validate(nullptr);
362+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
363+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
364+
}
365+
366+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedPartitionIdSuccess) {
367+
base_metadata_->last_partition_id = 5;
368+
table::AssertLastAssignedPartitionId requirement(5);
369+
370+
ASSERT_THAT(requirement.Validate(base_metadata_.get()), IsOk());
371+
}
372+
373+
TEST_F(TableMetadataBuilderTest,
374+
TableRequirementAssertLastAssignedPartitionIdMismatch) {
375+
base_metadata_->last_partition_id = 5;
376+
table::AssertLastAssignedPartitionId requirement(8);
377+
378+
auto status = requirement.Validate(base_metadata_.get());
379+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
380+
EXPECT_THAT(status, HasErrorMessage("last assigned partition ID does not match"));
381+
}
382+
383+
TEST_F(TableMetadataBuilderTest,
384+
TableRequirementAssertLastAssignedPartitionIdNullBase) {
385+
table::AssertLastAssignedPartitionId requirement(5);
386+
387+
auto status = requirement.Validate(nullptr);
388+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
389+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
390+
}
391+
392+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSpecIDSuccess) {
393+
base_metadata_->default_spec_id = 3;
394+
table::AssertDefaultSpecID requirement(3);
395+
396+
ASSERT_THAT(requirement.Validate(base_metadata_.get()), IsOk());
397+
}
398+
399+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSpecIDMismatch) {
400+
base_metadata_->default_spec_id = 3;
401+
table::AssertDefaultSpecID requirement(7);
402+
403+
auto status = requirement.Validate(base_metadata_.get());
404+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
405+
EXPECT_THAT(status, HasErrorMessage("default spec ID does not match"));
406+
}
407+
408+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSpecIDNullBase) {
409+
table::AssertDefaultSpecID requirement(3);
410+
411+
auto status = requirement.Validate(nullptr);
412+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
413+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
414+
}
415+
416+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSortOrderIDSuccess) {
417+
base_metadata_->default_sort_order_id = 2;
418+
table::AssertDefaultSortOrderID requirement(2);
419+
420+
ASSERT_THAT(requirement.Validate(base_metadata_.get()), IsOk());
421+
}
422+
423+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSortOrderIDMismatch) {
424+
base_metadata_->default_sort_order_id = 2;
425+
table::AssertDefaultSortOrderID requirement(4);
426+
427+
auto status = requirement.Validate(base_metadata_.get());
428+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
429+
EXPECT_THAT(status, HasErrorMessage("default sort order ID does not match"));
430+
}
431+
432+
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSortOrderIDNullBase) {
433+
table::AssertDefaultSortOrderID requirement(2);
434+
435+
auto status = requirement.Validate(nullptr);
436+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
437+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
438+
}
439+
267440
// ============================================================================
268441
// Integration Tests - End-to-End Workflow
269442
// ============================================================================

0 commit comments

Comments
 (0)