|
17 | 17 | * under the License. |
18 | 18 | */ |
19 | 19 |
|
| 20 | +#include "iceberg/catalog/in_memory_catalog.h" |
| 21 | + |
20 | 22 | #include <filesystem> |
21 | 23 |
|
22 | 24 | #include <arrow/filesystem/localfs.h> |
23 | 25 | #include <gmock/gmock.h> |
24 | 26 | #include <gtest/gtest.h> |
25 | 27 |
|
26 | 28 | #include "iceberg/arrow/arrow_fs_file_io_internal.h" |
| 29 | +#include "iceberg/schema.h" |
27 | 30 | #include "iceberg/table.h" |
28 | 31 | #include "iceberg/table_metadata.h" |
29 | 32 | #include "matchers.h" |
@@ -115,42 +118,53 @@ TEST_F(InMemoryCatalogTest, RegisterTable) { |
115 | 118 | } |
116 | 119 |
|
117 | 120 | TEST_F(InMemoryCatalogTest, RefreshTable) { |
118 | | - TableIdentifier tableIdent{.ns = {}, .name = "t1"}; |
119 | | - std::shared_ptr<MockInMemoryCatalog> mock_catalog = |
120 | | - std::make_shared<MockInMemoryCatalog>( |
121 | | - "mock_catalog", file_io_, "/tmp/warehouse/", |
122 | | - std::unordered_map<std::string, std::string>()); |
123 | | - auto table_location = GenerateTestTableLocation(tableIdent.name); |
124 | | - auto buildTable = [this, &tableIdent, &mock_catalog, &table_location]( |
125 | | - int64_t snapshot_id, int64_t version) -> std::unique_ptr<Table> { |
126 | | - std::unique_ptr<TableMetadata> metadata; |
127 | | - ReadTableMetadata("TableMetadataV2Valid.json", &metadata); |
128 | | - metadata->current_snapshot_id = snapshot_id; |
129 | | - return std::make_unique<Table>( |
130 | | - tableIdent, std::move(metadata), |
131 | | - std::format("{}v{}.metadata.json", table_location, version), file_io_, |
132 | | - std::static_pointer_cast<Catalog>(mock_catalog)); |
133 | | - }; |
134 | | - |
135 | | - auto table_v0 = buildTable(3051729675574597004, 0); |
136 | | - auto table_v1 = buildTable(3055729675574597004, 1); |
137 | | - EXPECT_CALL(*mock_catalog, LoadTable(::testing::_)) |
138 | | - .WillOnce(::testing::Return( |
139 | | - ::testing::ByMove(Result<std::unique_ptr<Table>>(std::move(table_v0))))) |
140 | | - .WillOnce(::testing::Return( |
141 | | - ::testing::ByMove(Result<std::unique_ptr<Table>>(std::move(table_v1))))); |
| 121 | + TableIdentifier table_ident{.ns = {}, .name = "t1"}; |
| 122 | + auto schema = std::make_shared<Schema>( |
| 123 | + std::vector<SchemaField>{SchemaField::MakeRequired(1, "x", int64())}, |
| 124 | + /*schema_id=*/1); |
142 | 125 |
|
143 | | - auto metadata_location = std::format("{}v{}.metadata.json", table_location, 0); |
144 | | - auto table = mock_catalog->RegisterTable(tableIdent, metadata_location); |
145 | | - EXPECT_THAT(table, IsOk()); |
146 | | - ASSERT_TRUE(table.value()->current_snapshot().has_value()); |
147 | | - ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3051729675574597004); |
| 126 | + std::shared_ptr<FileIO> io; |
148 | 127 |
|
149 | | - // refresh table to new snapshot |
150 | | - auto status = table.value()->Refresh(); |
151 | | - EXPECT_THAT(status, IsOk()); |
152 | | - ASSERT_TRUE(table.value()->current_snapshot().has_value()); |
153 | | - ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3055729675574597004); |
| 128 | + auto catalog = std::make_shared<MockCatalog>(); |
| 129 | + // Mock 1st call to LoadTable |
| 130 | + EXPECT_CALL(*catalog, LoadTable(::testing::_)) |
| 131 | + .WillOnce(::testing::Return( |
| 132 | + std::make_unique<Table>(table_ident, |
| 133 | + std::make_shared<TableMetadata>(TableMetadata{ |
| 134 | + .schemas = {schema}, |
| 135 | + .current_schema_id = 1, |
| 136 | + .current_snapshot_id = 1, |
| 137 | + .snapshots = {std::make_shared<Snapshot>(Snapshot{ |
| 138 | + .snapshot_id = 1, |
| 139 | + .sequence_number = 1, |
| 140 | + })}}), |
| 141 | + "s3://location/1.json", io, catalog))); |
| 142 | + auto load_table_result = catalog->LoadTable(table_ident); |
| 143 | + ASSERT_THAT(load_table_result, IsOk()); |
| 144 | + auto loaded_table = std::move(load_table_result.value()); |
| 145 | + ASSERT_EQ(loaded_table->current_snapshot().value()->snapshot_id, 1); |
| 146 | + |
| 147 | + // Mock 2nd call to LoadTable |
| 148 | + EXPECT_CALL(*catalog, LoadTable(::testing::_)) |
| 149 | + .WillOnce(::testing::Return( |
| 150 | + std::make_unique<Table>(table_ident, |
| 151 | + std::make_shared<TableMetadata>(TableMetadata{ |
| 152 | + .schemas = {schema}, |
| 153 | + .current_schema_id = 1, |
| 154 | + .current_snapshot_id = 2, |
| 155 | + .snapshots = {std::make_shared<Snapshot>(Snapshot{ |
| 156 | + .snapshot_id = 1, |
| 157 | + .sequence_number = 1, |
| 158 | + }), |
| 159 | + std::make_shared<Snapshot>(Snapshot{ |
| 160 | + .snapshot_id = 2, |
| 161 | + .sequence_number = 2, |
| 162 | + })}}), |
| 163 | + "s3://location/2.json", io, catalog))); |
| 164 | + auto refreshed_result = loaded_table->Refresh(); |
| 165 | + ASSERT_THAT(refreshed_result, IsOk()); |
| 166 | + // check table is refreshed |
| 167 | + ASSERT_EQ(loaded_table->current_snapshot().value()->snapshot_id, 2); |
154 | 168 | } |
155 | 169 |
|
156 | 170 | TEST_F(InMemoryCatalogTest, DropTable) { |
|
0 commit comments