|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import pickle |
| 18 | +import uuid |
| 19 | + |
| 20 | +import pytest |
| 21 | +from pytest_lazyfixture import lazy_fixture |
| 22 | + |
| 23 | +from pyiceberg.io import FileIO |
| 24 | +from pyiceberg.io.fsspec import FsspecFileIO |
| 25 | +from pyiceberg.io.pyarrow import PyArrowFileIO |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.adls |
| 29 | +@pytest.mark.parametrize( |
| 30 | + "fileio", |
| 31 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 32 | +) |
| 33 | +def test_new_input_file_adls(fileio: FileIO) -> None: |
| 34 | + """Test creating a new input file from a file-io""" |
| 35 | + filename = str(uuid.uuid4()) |
| 36 | + |
| 37 | + input_file = fileio.new_input(f"abfss://tests/{filename}") |
| 38 | + assert input_file.location == f"abfss://tests/{filename}" |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.adls |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "fileio", |
| 44 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 45 | +) |
| 46 | +def test_new_abfss_output_file_adls(fileio: FsspecFileIO) -> None: |
| 47 | + """Test creating a new output file from a file-io""" |
| 48 | + filename = str(uuid.uuid4()) |
| 49 | + |
| 50 | + output_file = fileio.new_output(f"abfss://tests/{filename}") |
| 51 | + assert output_file.location == f"abfss://tests/{filename}" |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.adls |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "fileio", |
| 57 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 58 | +) |
| 59 | +def test_write_and_read_file_adls(fileio: FileIO) -> None: |
| 60 | + """Test writing and reading a file using FsspecInputFile and FsspecOutputFile""" |
| 61 | + filename = str(uuid.uuid4()) |
| 62 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 63 | + with output_file.create() as f: |
| 64 | + f.write(b"foo") |
| 65 | + |
| 66 | + input_file = fileio.new_input(f"abfss://tests/{filename}") |
| 67 | + assert input_file.open().read() == b"foo" |
| 68 | + |
| 69 | + fileio.delete(input_file) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.adls |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "fileio", |
| 75 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 76 | +) |
| 77 | +def test_getting_length_of_file_adls(fileio: FileIO) -> None: |
| 78 | + """Test getting the length of aInputFile and FsspecOutputFile""" |
| 79 | + filename = str(uuid.uuid4()) |
| 80 | + |
| 81 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 82 | + with output_file.create() as f: |
| 83 | + f.write(b"foobar") |
| 84 | + |
| 85 | + assert len(output_file) == 6 |
| 86 | + |
| 87 | + input_file = fileio.new_input(location=f"abfss://tests/{filename}") |
| 88 | + assert len(input_file) == 6 |
| 89 | + |
| 90 | + fileio.delete(output_file) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.adls |
| 94 | +@pytest.mark.parametrize( |
| 95 | + "fileio", |
| 96 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 97 | +) |
| 98 | +def test_file_tell_adls(fileio: FileIO) -> None: |
| 99 | + """Test finding cursor position for a file-io file""" |
| 100 | + |
| 101 | + filename = str(uuid.uuid4()) |
| 102 | + |
| 103 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 104 | + with output_file.create() as write_file: |
| 105 | + write_file.write(b"foobar") |
| 106 | + |
| 107 | + input_file = fileio.new_input(location=f"abfss://tests/{filename}") |
| 108 | + f = input_file.open() |
| 109 | + |
| 110 | + f.seek(0) |
| 111 | + assert f.tell() == 0 |
| 112 | + f.seek(1) |
| 113 | + assert f.tell() == 1 |
| 114 | + f.seek(3) |
| 115 | + assert f.tell() == 3 |
| 116 | + f.seek(0) |
| 117 | + assert f.tell() == 0 |
| 118 | + |
| 119 | + fileio.delete(f"abfss://tests/{filename}") |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.adls |
| 123 | +@pytest.mark.parametrize( |
| 124 | + "fileio", |
| 125 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 126 | +) |
| 127 | +def test_read_specified_bytes_for_file_adls(fileio: FileIO) -> None: |
| 128 | + """Test reading a specified number of bytes from a file-io file""" |
| 129 | + |
| 130 | + filename = str(uuid.uuid4()) |
| 131 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 132 | + with output_file.create() as write_file: |
| 133 | + write_file.write(b"foo") |
| 134 | + |
| 135 | + input_file = fileio.new_input(location=f"abfss://tests/{filename}") |
| 136 | + f = input_file.open() |
| 137 | + |
| 138 | + f.seek(0) |
| 139 | + assert b"f" == f.read(1) |
| 140 | + f.seek(0) |
| 141 | + assert b"fo" == f.read(2) |
| 142 | + f.seek(1) |
| 143 | + assert b"o" == f.read(1) |
| 144 | + f.seek(1) |
| 145 | + assert b"oo" == f.read(2) |
| 146 | + f.seek(0) |
| 147 | + assert b"foo" == f.read(999) # test reading amount larger than entire content length |
| 148 | + |
| 149 | + fileio.delete(input_file) |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.adls |
| 153 | +@pytest.mark.parametrize( |
| 154 | + "fileio", |
| 155 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 156 | +) |
| 157 | +def test_raise_on_opening_file_not_found_adls(fileio: FileIO) -> None: |
| 158 | + """Test that a input file raises appropriately when the adls file is not found""" |
| 159 | + |
| 160 | + filename = str(uuid.uuid4()) |
| 161 | + input_file = fileio.new_input(location=f"abfss://tests/{filename}") |
| 162 | + with pytest.raises(FileNotFoundError) as exc_info: |
| 163 | + input_file.open().read() |
| 164 | + |
| 165 | + assert filename in str(exc_info.value) |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.adls |
| 169 | +@pytest.mark.parametrize( |
| 170 | + "fileio", |
| 171 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 172 | +) |
| 173 | +def test_checking_if_a_file_exists_adls(fileio: FileIO) -> None: |
| 174 | + """Test checking if a file exists""" |
| 175 | + |
| 176 | + non_existent_file = fileio.new_input(location="abfss://tests/does-not-exist.txt") |
| 177 | + assert not non_existent_file.exists() |
| 178 | + |
| 179 | + filename = str(uuid.uuid4()) |
| 180 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 181 | + assert not output_file.exists() |
| 182 | + with output_file.create() as f: |
| 183 | + f.write(b"foo") |
| 184 | + |
| 185 | + existing_input_file = fileio.new_input(location=f"abfss://tests/{filename}") |
| 186 | + assert existing_input_file.exists() |
| 187 | + |
| 188 | + existing_output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 189 | + assert existing_output_file.exists() |
| 190 | + |
| 191 | + fileio.delete(existing_output_file) |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.adls |
| 195 | +@pytest.mark.parametrize( |
| 196 | + "fileio", |
| 197 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 198 | +) |
| 199 | +def test_closing_a_file_adls(fileio: FileIO) -> None: |
| 200 | + """Test closing an output file and input file""" |
| 201 | + filename = str(uuid.uuid4()) |
| 202 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 203 | + with output_file.create() as write_file: |
| 204 | + write_file.write(b"foo") |
| 205 | + assert not write_file.closed # type: ignore |
| 206 | + assert write_file.closed # type: ignore |
| 207 | + |
| 208 | + input_file = fileio.new_input(location=f"abfss://tests/{filename}") |
| 209 | + f = input_file.open() |
| 210 | + assert not f.closed # type: ignore |
| 211 | + f.close() |
| 212 | + assert f.closed # type: ignore |
| 213 | + |
| 214 | + fileio.delete(f"abfss://tests/{filename}") |
| 215 | + |
| 216 | + |
| 217 | +@pytest.mark.adls |
| 218 | +@pytest.mark.parametrize( |
| 219 | + "fileio", |
| 220 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 221 | +) |
| 222 | +def test_converting_an_outputfile_to_an_inputfile_adls(fileio: FileIO) -> None: |
| 223 | + """Test converting an output file to an input file""" |
| 224 | + filename = str(uuid.uuid4()) |
| 225 | + output_file = fileio.new_output(location=f"abfss://tests/{filename}") |
| 226 | + input_file = output_file.to_input_file() |
| 227 | + assert input_file.location == output_file.location |
| 228 | + |
| 229 | + |
| 230 | +@pytest.mark.adls |
| 231 | +@pytest.mark.parametrize( |
| 232 | + "fileio", |
| 233 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 234 | +) |
| 235 | +def test_writing_avro_file_adls(fileio: FileIO, generated_manifest_entry_file: str) -> None: |
| 236 | + """Test that bytes match when reading a local avro file and then reading it again""" |
| 237 | + filename = str(uuid.uuid4()) |
| 238 | + with PyArrowFileIO().new_input(location=generated_manifest_entry_file).open() as f: |
| 239 | + b1 = f.read() |
| 240 | + with fileio.new_output(location=f"abfss://tests/{filename}").create() as out_f: |
| 241 | + out_f.write(b1) |
| 242 | + with fileio.new_input(location=f"abfss://tests/{filename}").open() as in_f: |
| 243 | + b2 = in_f.read() |
| 244 | + assert b1 == b2 # Check that bytes of read from local avro file match bytes written to adls |
| 245 | + |
| 246 | + fileio.delete(f"abfss://tests/{filename}") |
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.adls |
| 250 | +@pytest.mark.parametrize( |
| 251 | + "fileio", |
| 252 | + [lazy_fixture("adls_fsspec_fileio"), lazy_fixture("adls_pyarrow_fileio")], |
| 253 | +) |
| 254 | +def test_pickle_round_trip_adls(fileio: FileIO) -> None: |
| 255 | + _test_pickle_round_trip(fileio, "abfss://tests/foo.txt") |
| 256 | + |
| 257 | + |
| 258 | +def _test_pickle_round_trip(fileio: FileIO, location: str) -> None: |
| 259 | + serialized_file_io = pickle.dumps(fileio) |
| 260 | + deserialized_file_io = pickle.loads(serialized_file_io) |
| 261 | + output_file = deserialized_file_io.new_output(location) |
| 262 | + with output_file.create() as f: |
| 263 | + f.write(b"foo") |
| 264 | + |
| 265 | + input_file = deserialized_file_io.new_input(location) |
| 266 | + with input_file.open() as f: |
| 267 | + data = f.read() |
| 268 | + assert data == b"foo" |
| 269 | + assert len(input_file) == 3 |
| 270 | + deserialized_file_io.delete(location) |
0 commit comments