|
| 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/file_io.h" |
| 21 | + |
| 22 | +#include <string> |
| 23 | +#include <utility> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include <gtest/gtest.h> |
| 27 | + |
| 28 | +#include "iceberg/test/matchers.h" |
| 29 | + |
| 30 | +namespace iceberg { |
| 31 | +namespace { |
| 32 | + |
| 33 | +class RecordingFileIO : public FileIO { |
| 34 | + public: |
| 35 | + explicit RecordingFileIO(std::string failure_path = "") |
| 36 | + : failure_path_(std::move(failure_path)) {} |
| 37 | + |
| 38 | + Status DeleteFile(const std::string& file_location) override { |
| 39 | + deleted_paths.push_back(file_location); |
| 40 | + if (file_location == failure_path_) { |
| 41 | + return IOError("failed to delete {}", file_location); |
| 42 | + } |
| 43 | + return {}; |
| 44 | + } |
| 45 | + |
| 46 | + std::vector<std::string> deleted_paths; |
| 47 | + |
| 48 | + private: |
| 49 | + std::string failure_path_; |
| 50 | +}; |
| 51 | + |
| 52 | +TEST(FileIOTest, DeleteFilesFallsBackToDeleteFileForEachPath) { |
| 53 | + RecordingFileIO file_io; |
| 54 | + std::vector<std::string> paths = {"file-a.avro", "file-b.avro"}; |
| 55 | + |
| 56 | + EXPECT_THAT(file_io.DeleteFiles(paths), IsOk()); |
| 57 | + EXPECT_THAT(file_io.deleted_paths, ::testing::ElementsAre("file-a.avro", "file-b.avro")); |
| 58 | +} |
| 59 | + |
| 60 | +TEST(FileIOTest, DeleteFilesReturnsFirstDeleteFileError) { |
| 61 | + RecordingFileIO file_io("file-b.avro"); |
| 62 | + std::vector<std::string> paths = {"file-a.avro", "file-b.avro", "file-c.avro"}; |
| 63 | + |
| 64 | + auto status = file_io.DeleteFiles(paths); |
| 65 | + |
| 66 | + EXPECT_THAT(status, IsError(ErrorKind::kIOError)); |
| 67 | + EXPECT_THAT(status, HasErrorMessage("failed to delete file-b.avro")); |
| 68 | + EXPECT_THAT(file_io.deleted_paths, ::testing::ElementsAre("file-a.avro", "file-b.avro")); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace |
| 72 | +} // namespace iceberg |
0 commit comments