|
| 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 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 40 | + const std::string new_location = "/warehouse/new_location"; |
| 41 | + update->SetLocation(new_location); |
| 42 | + |
| 43 | + ICEBERG_UNWRAP_OR_FAIL(auto result, update->Apply()); |
| 44 | + EXPECT_EQ(result, new_location); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_F(UpdateLocationTest, SetLocationMultipleTimes) { |
| 48 | + // Test that setting location multiple times uses the last value |
| 49 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 50 | + update->SetLocation("/warehouse/first_location") |
| 51 | + .SetLocation("/warehouse/second_location") |
| 52 | + .SetLocation("/warehouse/final_location"); |
| 53 | + |
| 54 | + ICEBERG_UNWRAP_OR_FAIL(auto result, update->Apply()); |
| 55 | + EXPECT_EQ(result, "/warehouse/final_location"); |
| 56 | +} |
| 57 | + |
| 58 | +TEST_F(UpdateLocationTest, SetEmptyLocation) { |
| 59 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 60 | + update->SetLocation(""); |
| 61 | + |
| 62 | + auto result = update->Apply(); |
| 63 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 64 | + EXPECT_THAT(result, HasErrorMessage("Location cannot be empty")); |
| 65 | +} |
| 66 | + |
| 67 | +TEST_F(UpdateLocationTest, ApplyWithoutSettingLocation) { |
| 68 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 69 | + |
| 70 | + auto result = update->Apply(); |
| 71 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument)); |
| 72 | + EXPECT_THAT(result, HasErrorMessage("Location must be set before applying")); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(UpdateLocationTest, CommitSuccess) { |
| 76 | + // Test empty commit (should fail since location is not set) |
| 77 | + ICEBERG_UNWRAP_OR_FAIL(auto empty_update, table_->NewUpdateLocation()); |
| 78 | + auto empty_commit_result = empty_update->Commit(); |
| 79 | + EXPECT_THAT(empty_commit_result, IsError(ErrorKind::kInvalidArgument)); |
| 80 | + |
| 81 | + // Test commit with location change |
| 82 | + // For MockFS, we need to create the metadata directory at the new location |
| 83 | + const std::string new_location = "/warehouse/new_table_location"; |
| 84 | + auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>( |
| 85 | + static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs()); |
| 86 | + ASSERT_TRUE(arrow_fs != nullptr); |
| 87 | + ASSERT_TRUE(arrow_fs->CreateDir(new_location + "/metadata").ok()); |
| 88 | + |
| 89 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 90 | + update->SetLocation(new_location); |
| 91 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 92 | + |
| 93 | + // Verify the location was committed |
| 94 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 95 | + EXPECT_EQ(reloaded->location(), new_location); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(UpdateLocationTest, CommitWithRelativePath) { |
| 99 | + // Test that relative paths work |
| 100 | + const std::string relative_location = "warehouse/relative_location"; |
| 101 | + |
| 102 | + // Create metadata directory for the new location |
| 103 | + auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>( |
| 104 | + static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs()); |
| 105 | + ASSERT_TRUE(arrow_fs != nullptr); |
| 106 | + ASSERT_TRUE(arrow_fs->CreateDir(relative_location + "/metadata").ok()); |
| 107 | + |
| 108 | + ICEBERG_UNWRAP_OR_FAIL(auto update, table_->NewUpdateLocation()); |
| 109 | + update->SetLocation(relative_location); |
| 110 | + |
| 111 | + EXPECT_THAT(update->Commit(), IsOk()); |
| 112 | + |
| 113 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| 114 | + EXPECT_EQ(reloaded->location(), relative_location); |
| 115 | +} |
| 116 | + |
| 117 | +TEST_F(UpdateLocationTest, MultipleUpdatesSequentially) { |
| 118 | + // Get arrow_fs for creating directories |
| 119 | + auto arrow_fs = std::dynamic_pointer_cast<::arrow::fs::internal::MockFileSystem>( |
| 120 | + static_cast<arrow::ArrowFileSystemFileIO&>(*file_io_).fs()); |
| 121 | + ASSERT_TRUE(arrow_fs != nullptr); |
| 122 | + |
| 123 | + // First update |
| 124 | + const std::string first_location = "/warehouse/first"; |
| 125 | + ASSERT_TRUE(arrow_fs->CreateDir(first_location + "/metadata").ok()); |
| 126 | + |
| 127 | + ICEBERG_UNWRAP_OR_FAIL(auto update1, table_->NewUpdateLocation()); |
| 128 | + update1->SetLocation(first_location); |
| 129 | + EXPECT_THAT(update1->Commit(), IsOk()); |
| 130 | + |
| 131 | + // Reload and verify |
| 132 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded1, catalog_->LoadTable(table_ident_)); |
| 133 | + EXPECT_EQ(reloaded1->location(), first_location); |
| 134 | + |
| 135 | + // Second update |
| 136 | + const std::string second_location = "/warehouse/second"; |
| 137 | + ASSERT_TRUE(arrow_fs->CreateDir(second_location + "/metadata").ok()); |
| 138 | + |
| 139 | + ICEBERG_UNWRAP_OR_FAIL(auto update2, reloaded1->NewUpdateLocation()); |
| 140 | + update2->SetLocation(second_location); |
| 141 | + EXPECT_THAT(update2->Commit(), IsOk()); |
| 142 | + |
| 143 | + // Reload and verify |
| 144 | + ICEBERG_UNWRAP_OR_FAIL(auto reloaded2, catalog_->LoadTable(table_ident_)); |
| 145 | + EXPECT_EQ(reloaded2->location(), second_location); |
| 146 | +} |
| 147 | + |
| 148 | +} // namespace iceberg |
0 commit comments