lidavidm commented on code in PR #74: URL: https://github.com/apache/iceberg-cpp/pull/74#discussion_r2043228407
########## test/json_internal_test.cc: ########## @@ -148,4 +148,96 @@ TEST(JsonPartitionTest, PartitionSpec) { EXPECT_EQ(spec, *parsed_spec_result.value()); } +TEST(JsonInternalTest, SnapshotRefBranch) { + SnapshotRef ref(1234567890, SnapshotRef::Branch{.min_snapshots_to_keep = 10, + .max_snapshot_age_ms = 123456789, + .max_ref_age_ms = 987654321}); + + // Create a JSON object with the expected values + nlohmann::json expected_json = + R"({"snapshot-id":1234567890, + "type":"branch", + "min-snapshots-to-keep":10, + "max-snapshot-age-ms":123456789, + "max-ref-age-ms":987654321})"_json; + + auto json = SnapshotRefToJson(ref); + EXPECT_EQ(expected_json, json) << "JSON conversion mismatch."; + + auto obj_ex = SnapshotRefFromJson(expected_json); + EXPECT_TRUE(obj_ex.has_value()) << "Failed to deserialize JSON."; + EXPECT_EQ(ref, *obj_ex.value()) << "Deserialized object mismatch."; +} + +TEST(JsonInternalTest, SnapshotRefTag) { + SnapshotRef ref(9876543210, SnapshotRef::Tag{.max_ref_age_ms = 54321}); + + // Create a JSON object with the expected values + nlohmann::json expected_json = + R"({"snapshot-id":9876543210, + "type":"tag", + "max-ref-age-ms":54321})"_json; + + auto json = SnapshotRefToJson(ref); + EXPECT_EQ(expected_json, json) << "JSON conversion mismatch."; + + auto obj_ex = SnapshotRefFromJson(expected_json); + EXPECT_TRUE(obj_ex.has_value()) << "Failed to deserialize JSON."; + EXPECT_EQ(ref, *obj_ex.value()) << "Deserialized object mismatch."; +} + +TEST(JsonInternalTest, Snapshot) { + std::unordered_map<std::string, std::string> summary = { + {SnapshotSummaryFields::kOperation, DataOperation::kAppend}, + {SnapshotSummaryFields::kAddedDataFiles, "50"}}; + + Snapshot snapshot{.snapshot_id = 1234567890, + .parent_snapshot_id = 9876543210, + .sequence_number = 99, + .timestamp_ms = 1234567890123, + .manifest_list = "/path/to/manifest_list", + .summary = summary, + .schema_id = 42}; + + // Create a JSON object with the expected values + nlohmann::json expected_json = + R"({"snapshot-id":1234567890, + "parent-snapshot-id":9876543210, + "sequence-number":99, + "timestamp-ms":1234567890123, + "manifest-list":"/path/to/manifest_list", + "summary":{ + "operation":"append", + "added-data-files":"50" + }, + "schema-id":42})"_json; + + auto json = SnapshotToJson(snapshot); + EXPECT_EQ(expected_json, json) << "JSON conversion mismatch."; + + auto obj_ex = SnapshotFromJson(expected_json); + EXPECT_TRUE(obj_ex.has_value()) << "Failed to deserialize JSON."; + EXPECT_EQ(snapshot, *obj_ex.value()) << "Deserialized object mismatch."; +} + +TEST(JsonInternalTest, SnapshotFromJsonWithInvalidSummary) { + nlohmann::json invalid_json_snapshot = + R"({"snapshot-id":1234567890, + "parent-snapshot-id":9876543210, + "sequence-number":99, + "timestamp-ms":1234567890123, + "manifest-list":"/path/to/manifest_list", + "summary":{ + "invalid-field":"value" + }, + "schema-id":42})"_json; + + auto result = SnapshotFromJson(invalid_json_snapshot); + ASSERT_FALSE(result.has_value()); + + EXPECT_EQ(result.error().kind, ErrorKind::kJsonParseError); + EXPECT_TRUE(result.error().message.find("Invalid snapshot summary field") != + std::string::npos); Review Comment: Note that for this, you can do `EXPECT_THAT(result.error().message, ::testing::HasSubstr("Invalid snapshot summary field"));` ########## src/iceberg/json_internal.cc: ########## @@ -231,6 +296,53 @@ nlohmann::json SchemaToJson(const Schema& schema) { return json; } +nlohmann::json SnapshotRefToJson(const SnapshotRef& ref) { + nlohmann::json json; + json[kSnapshotId] = ref.snapshot_id; + json[kType] = SnapshotRefTypeToString(ref.type()); + if (ref.type() == SnapshotRefType::kBranch) { + const auto& branch = std::get<SnapshotRef::Branch>(ref.retention); + if (branch.min_snapshots_to_keep.has_value()) { + json[kMinSnapshotsToKeep] = *branch.min_snapshots_to_keep; + } Review Comment: Worth writing a helper for this? -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org