26#include <arrow/filesystem/mockfs.h>
27#include <gtest/gtest.h>
29#include "iceberg/arrow/arrow_io_internal.h"
30#include "iceberg/catalog/memory/in_memory_catalog.h"
31#include "iceberg/result.h"
32#include "iceberg/snapshot.h"
33#include "iceberg/table.h"
36#include "iceberg/test/matchers.h"
37#include "iceberg/test/test_resource.h"
45 virtual std::string MetadataResource()
const {
return "TableMetadataV2Valid.json"; }
46 virtual std::string TableName()
const {
return "test_table"; }
48 void SetUp()
override {
50 table_location_ =
"/warehouse/" + TableName();
60 InMemoryCatalog::Make(
"test_catalog", file_io_,
"/warehouse/", {});
63 auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>(
65 ASSERT_TRUE(arrow_fs !=
nullptr);
66 ASSERT_TRUE(arrow_fs->CreateDir(table_location_ +
"/metadata").ok());
73 std::ignore = catalog_->DropTable(table_ident_,
false);
75 auto metadata_location = std::format(
"{}/metadata/00001-{}.metadata.json",
77 ICEBERG_UNWRAP_OR_FAIL(
auto metadata, ReadTableMetadataFromResource(resource_name));
78 metadata->location = table_location_;
82 ICEBERG_UNWRAP_OR_FAIL(table_,
83 catalog_->RegisterTable(table_ident_, metadata_location));
88 auto result = catalog_->LoadTable(table_ident_);
89 EXPECT_TRUE(result.has_value()) <<
"Failed to reload table";
90 return result.value()->metadata();
94 void ExpectRef(
const std::string& name, SnapshotRefType type, int64_t snapshot_id) {
96 auto it = metadata->refs.find(name);
97 ASSERT_NE(it, metadata->refs.end()) <<
"Ref not found: " << name;
98 EXPECT_EQ(it->second->type(), type);
99 EXPECT_EQ(it->second->snapshot_id, snapshot_id);
102 void ExpectBranch(
const std::string& name, int64_t snapshot_id) {
103 ExpectRef(name, SnapshotRefType::kBranch, snapshot_id);
106 void ExpectTag(
const std::string& name, int64_t snapshot_id) {
107 ExpectRef(name, SnapshotRefType::kTag, snapshot_id);
113 EXPECT_FALSE(metadata->refs.contains(name)) <<
"Ref should not exist: " << name;
118 auto result = catalog_->LoadTable(table_ident_);
119 ASSERT_TRUE(result.has_value());
120 auto snap_result = result.value()->current_snapshot();
121 ASSERT_TRUE(snap_result.has_value());
122 EXPECT_EQ(snap_result.value()->snapshot_id, snapshot_id);
126 template <
typename T>
128 EXPECT_THAT(result, IsOk());
132 template <
typename T>
134 EXPECT_THAT(result, IsError(kind));
135 EXPECT_THAT(result, HasErrorMessage(message));
139 std::string table_location_;
140 std::shared_ptr<FileIO> file_io_;
141 std::shared_ptr<InMemoryCatalog> catalog_;
142 std::shared_ptr<Table> table_;
148 std::string MetadataResource()
const override {
149 return "TableMetadataV2ValidMinimal.json";
151 std::string TableName()
const override {
return "minimal_table"; }
Test fixture for table update operations on minimal table metadata.
Definition update_test_base.h:146
Base test fixture for table update operations.
Definition update_test_base.h:43
void ExpectCommitError(const T &result, ErrorKind kind, const std::string &message)
Assert that a commit failed with the given error kind and message substring.
Definition update_test_base.h:133
void RegisterTableFromResource(const std::string &resource_name)
Register a table from a metadata resource file.
Definition update_test_base.h:72
void InitializeFileIO()
Initialize file IO and create necessary directories.
Definition update_test_base.h:57
void ExpectRef(const std::string &name, SnapshotRefType type, int64_t snapshot_id)
Assert that a ref exists with the given type and snapshot id.
Definition update_test_base.h:94
void ExpectCurrentSnapshot(int64_t snapshot_id)
Assert the current snapshot id after reloading.
Definition update_test_base.h:117
void ExpectCommitOk(const T &result)
Assert that a commit succeeded.
Definition update_test_base.h:127
std::shared_ptr< TableMetadata > ReloadMetadata()
Reload the table from catalog and return its metadata.
Definition update_test_base.h:87
void ExpectNoRef(const std::string &name)
Assert that a ref does not exist.
Definition update_test_base.h:111
static Uuid GenerateV7()
Generate UUID version 7 per RFC 9562, with the current timestamp.
Definition uuid.cc:137
A concrete implementation of FileIO for Arrow file system.
Definition arrow_io_internal.h:54
const std::shared_ptr<::arrow::fs::FileSystem > & fs() const
Get the Arrow file system.
Definition arrow_io_internal.h:81
static std::unique_ptr< FileIO > MakeMockFileIO()
Make an in-memory FileIO backed by arrow::fs::internal::MockFileSystem.
Definition arrow_io.cc:571
Identifies a table in iceberg catalog.
Definition table_identifier.h:46
UUID (Universally Unique Identifier) representation.