iceberg-cpp
Loading...
Searching...
No Matches
update_test_base.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <format>
23#include <memory>
24#include <string>
25
26#include <arrow/filesystem/mockfs.h>
27#include <gtest/gtest.h>
28
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"
38#include "iceberg/util/uuid.h"
39
40namespace iceberg {
41
43class UpdateTestBase : public ::testing::Test {
44 protected:
45 virtual std::string MetadataResource() const { return "TableMetadataV2Valid.json"; }
46 virtual std::string TableName() const { return "test_table"; }
47
48 void SetUp() override {
49 table_ident_ = TableIdentifier{.name = TableName()};
50 table_location_ = "/warehouse/" + TableName();
51
53 RegisterTableFromResource(MetadataResource());
54 }
55
59 catalog_ =
60 InMemoryCatalog::Make("test_catalog", file_io_, "/warehouse/", /*properties=*/{});
61
62 // Arrow MockFS cannot automatically create directories.
63 auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>(
64 static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs());
65 ASSERT_TRUE(arrow_fs != nullptr);
66 ASSERT_TRUE(arrow_fs->CreateDir(table_location_ + "/metadata").ok());
67 }
68
72 void RegisterTableFromResource(const std::string& resource_name) {
73 std::ignore = catalog_->DropTable(table_ident_, /*purge=*/false);
74
75 auto metadata_location = std::format("{}/metadata/00001-{}.metadata.json",
76 table_location_, Uuid::GenerateV7().ToString());
77 ICEBERG_UNWRAP_OR_FAIL(auto metadata, ReadTableMetadataFromResource(resource_name));
78 metadata->location = table_location_;
79 ASSERT_THAT(TableMetadataUtil::Write(*file_io_, metadata_location, *metadata),
80 IsOk());
81
82 ICEBERG_UNWRAP_OR_FAIL(table_,
83 catalog_->RegisterTable(table_ident_, metadata_location));
84 }
85
87 std::shared_ptr<TableMetadata> ReloadMetadata() {
88 auto result = catalog_->LoadTable(table_ident_);
89 EXPECT_TRUE(result.has_value()) << "Failed to reload table";
90 return result.value()->metadata();
91 }
92
94 void ExpectRef(const std::string& name, SnapshotRefType type, int64_t snapshot_id) {
95 auto metadata = ReloadMetadata();
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);
100 }
101
102 void ExpectBranch(const std::string& name, int64_t snapshot_id) {
103 ExpectRef(name, SnapshotRefType::kBranch, snapshot_id);
104 }
105
106 void ExpectTag(const std::string& name, int64_t snapshot_id) {
107 ExpectRef(name, SnapshotRefType::kTag, snapshot_id);
108 }
109
111 void ExpectNoRef(const std::string& name) {
112 auto metadata = ReloadMetadata();
113 EXPECT_FALSE(metadata->refs.contains(name)) << "Ref should not exist: " << name;
114 }
115
117 void ExpectCurrentSnapshot(int64_t snapshot_id) {
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);
123 }
124
126 template <typename T>
127 void ExpectCommitOk(const T& result) {
128 EXPECT_THAT(result, IsOk());
129 }
130
132 template <typename T>
133 void ExpectCommitError(const T& result, ErrorKind kind, const std::string& message) {
134 EXPECT_THAT(result, IsError(kind));
135 EXPECT_THAT(result, HasErrorMessage(message));
136 }
137
138 TableIdentifier table_ident_;
139 std::string table_location_;
140 std::shared_ptr<FileIO> file_io_;
141 std::shared_ptr<InMemoryCatalog> catalog_;
142 std::shared_ptr<Table> table_;
143};
144
147 protected:
148 std::string MetadataResource() const override {
149 return "TableMetadataV2ValidMinimal.json";
150 }
151 std::string TableName() const override { return "minimal_table"; }
152};
153
154} // namespace iceberg
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
static Result< std::string > Write(FileIO &io, const TableMetadata *base, const std::string &base_metadata_location, TableMetadata &metadata)
Write a new metadata file to storage.
Definition table_metadata.cc:475
UUID (Universally Unique Identifier) representation.