2525#include < iceberg/result.h>
2626#include < nlohmann/json.hpp>
2727
28+ #include " gmock/gmock.h"
2829#include " iceberg/partition_spec.h"
2930#include " iceberg/schema.h"
3031#include " iceberg/snapshot.h"
@@ -56,6 +57,16 @@ Result<std::unique_ptr<PartitionField>> FromJsonHelper(const nlohmann::json& jso
5657 return PartitionFieldFromJson (json);
5758}
5859
60+ template <>
61+ Result<std::unique_ptr<SnapshotRef>> FromJsonHelper (const nlohmann::json& json) {
62+ return SnapshotRefFromJson (json);
63+ }
64+
65+ template <>
66+ Result<std::unique_ptr<Snapshot>> FromJsonHelper (const nlohmann::json& json) {
67+ return SnapshotFromJson (json);
68+ }
69+
5970// Helper function to reduce duplication in testing
6071template <typename T>
6172void TestJsonConversion (const T& obj, const nlohmann::json& expected_json) {
@@ -117,7 +128,8 @@ TEST(JsonPartitionTest, PartitionFieldFromJsonMissingField) {
117128
118129 auto result = PartitionFieldFromJson (invalid_json);
119130 EXPECT_FALSE (result.has_value ());
120- EXPECT_EQ (result.error ().kind , ErrorKind::kJsonParseError );
131+ EXPECT_THAT (result, IsError (ErrorKind::kJsonParseError ));
132+ EXPECT_THAT (result, HasErrorMessage (" Missing 'source-id'" ));
121133}
122134
123135TEST (JsonPartitionTest, PartitionSpec) {
@@ -162,12 +174,7 @@ TEST(JsonInternalTest, SnapshotRefBranch) {
162174 "max-snapshot-age-ms":123456789,
163175 "max-ref-age-ms":987654321})" _json;
164176
165- auto json = SnapshotRefToJson (ref);
166- EXPECT_EQ (expected_json, json) << " JSON conversion mismatch." ;
167-
168- auto obj_ex = SnapshotRefFromJson (expected_json);
169- EXPECT_TRUE (obj_ex.has_value ()) << " Failed to deserialize JSON." ;
170- EXPECT_EQ (ref, *obj_ex.value ()) << " Deserialized object mismatch." ;
177+ TestJsonConversion (ref, expected_json);
171178}
172179
173180TEST (JsonInternalTest, SnapshotRefTag) {
@@ -179,12 +186,7 @@ TEST(JsonInternalTest, SnapshotRefTag) {
179186 "type":"tag",
180187 "max-ref-age-ms":54321})" _json;
181188
182- auto json = SnapshotRefToJson (ref);
183- EXPECT_EQ (expected_json, json) << " JSON conversion mismatch." ;
184-
185- auto obj_ex = SnapshotRefFromJson (expected_json);
186- EXPECT_TRUE (obj_ex.has_value ()) << " Failed to deserialize JSON." ;
187- EXPECT_EQ (ref, *obj_ex.value ()) << " Deserialized object mismatch." ;
189+ TestJsonConversion (ref, expected_json);
188190}
189191
190192TEST (JsonInternalTest, Snapshot) {
@@ -213,12 +215,7 @@ TEST(JsonInternalTest, Snapshot) {
213215 },
214216 "schema-id":42})" _json;
215217
216- auto json = SnapshotToJson (snapshot);
217- EXPECT_EQ (expected_json, json) << " JSON conversion mismatch." ;
218-
219- auto obj_ex = SnapshotFromJson (expected_json);
220- EXPECT_TRUE (obj_ex.has_value ()) << " Failed to deserialize JSON." ;
221- EXPECT_EQ (snapshot, *obj_ex.value ()) << " Deserialized object mismatch." ;
218+ TestJsonConversion (snapshot, expected_json);
222219}
223220
224221TEST (JsonInternalTest, SnapshotFromJsonWithInvalidSummary) {
0 commit comments