|
| 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 | +#include "iceberg/update/update_location.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | + |
| 25 | +#include <arrow/filesystem/mockfs.h> |
| 26 | +#include <gmock/gmock.h> |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +#include "iceberg/arrow/arrow_fs_file_io_internal.h" |
| 30 | +#include "iceberg/result.h" |
| 31 | +#include "iceberg/test/matchers.h" |
| 32 | +#include "iceberg/test/update_test_base.h" |
| 33 | + |
| 34 | +namespace iceberg { |
| 35 | + |
| 36 | +class UpdateLocationTest : public UpdateTestBase {}; |
| 37 | + |
| 38 | +TEST_F(UpdateLocationTest, SetLocationSuccess) { |
| 39 | + const std::string new_location = "/warehouse/new_location"; |
| 40 | + |
| 41 | + // Create metadata directory for the new location |
| 42 | + auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>( |
| 43 | + static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs()); |
| 44 | + ASSERT_TRUE(arrow_fs != nullptr); |
| 45 | + ASSERT_TRUE(arrow_fs->CreateDir(new_location + "/metadata").ok()); |
| 46 | + |
| 47 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 48 | + update->SetLocation(new_location); |
| 49 | + |
| 50 | + ICEBERG_UNWRAP_OR_FAIL(auto result, update->Apply()); |
| 51 | + EXPECT_EQ(result, new_location); |
| 52 | + |
| 53 | + // Commit and verify the location was persisted |
| 54 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 55 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 56 | + EXPECT_EQ(reloaded->location(), new_location); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(UpdateLocationTest, SetLocationMultipleTimes) { |
| 60 | + // Test that setting location multiple times uses the last value |
| 61 | + const std::string final_location = "/warehouse/final_location"; |
| 62 | + |
| 63 | + // Create metadata directory for the new location |
| 64 | + auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>( |
| 65 | + static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs()); |
| 66 | + ASSERT_TRUE(arrow_fs != nullptr); |
| 67 | + ASSERT_TRUE(arrow_fs->CreateDir(final_location + "/metadata").ok()); |
| 68 | + |
| 69 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 70 | + update->SetLocation("/warehouse/first_location") |
| 71 | + .SetLocation("/warehouse/second_location") |
| 72 | + .SetLocation(final_location); |
| 73 | + |
| 74 | + ICEBERG_UNWRAP_OR_FAIL(auto result, update->Apply()); |
| 75 | + EXPECT_EQ(result, final_location); |
| 76 | + |
| 77 | + // Commit and verify the final location was persisted |
| 78 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 79 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 80 | + EXPECT_EQ(reloaded->location(), final_location); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_F(UpdateLocationTest, SetEmptyLocation) { |
| 84 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 85 | + update->SetLocation(""); |
| 86 | + |
| 87 | + auto result = update->Apply(); |
| 88 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 89 | + EXPECT_THAT(result, HasErrorMessage("Location cannot be empty")); |
| 90 | +} |
| 91 | + |
| 92 | +TEST_F(UpdateLocationTest, ApplyWithoutSettingLocation) { |
| 93 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 94 | + |
| 95 | + auto result = update->Apply(); |
| 96 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument)); |
| 97 | + EXPECT_THAT(result, HasErrorMessage("Location must be set before applying")); |
| 98 | +} |
| 99 | + |
| 100 | +TEST_F(UpdateLocationTest, MultipleUpdatesSequentially) { |
| 101 | + // Get arrow_fs for creating directories |
| 102 | + auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>( |
| 103 | + static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs()); |
| 104 | + ASSERT_TRUE(arrow_fs != nullptr); |
| 105 | + |
| 106 | + // First update |
| 107 | + const std::string first_location = "/warehouse/first"; |
| 108 | + ASSERT_TRUE(arrow_fs->CreateDir(first_location + "/metadata").ok()); |
| 109 | + |
| 110 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 111 | + update->SetLocation(first_location); |
| 112 | + ICEBERG_UNWRAP_OR_FAIL(auto result, update->Apply()); |
| 113 | + EXPECT_EQ(result, first_location); |
| 114 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 115 | + |
| 116 | + // Reload and verify |
| 117 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 118 | + EXPECT_EQ(reloaded->location(), first_location); |
| 119 | + |
| 120 | + // Second update |
| 121 | + const std::string second_location = "/warehouse/second"; |
| 122 | + ASSERT_TRUE(arrow_fs->CreateDir(second_location + "/metadata").ok()); |
| 123 | + |
| 124 | + ICEBERG_UNWRAP_OR_FAIL(update, reloaded->NewUpdateLocation()); |
| 125 | + update->SetLocation(second_location); |
| 126 | + ICEBERG_UNWRAP_OR_FAIL(result, update->Apply()); |
| 127 | + EXPECT_EQ(result, second_location); |
| 128 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 129 | + |
| 130 | + // Reload and verify |
| 131 | + ICEBERG_UNWRAP_OR_FAIL(reloaded, catalog_->LoadTable(table_ident_)); |
| 132 | + EXPECT_EQ(reloaded->location(), second_location); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace iceberg |
0 commit comments