rdblue commented on a change in pull request #3691:
URL: https://github.com/apache/iceberg/pull/3691#discussion_r790316448



##########
File path: python/tests/io/test_base.py
##########
@@ -0,0 +1,188 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import os
+import tempfile
+
+import pytest
+
+from iceberg.io.base import FileIO, InputFile, OutputFile
+
+
+class LocalInputFile(InputFile):
+    """An InputFile implementation for local files (for test use only)"""
+
+    def __init__(self, location: str):
+        if not location.startswith("file://"):
+            raise ValueError("LocalInputFile location must start with 
`file://`")
+        super().__init__(location=location.split("file://")[1])
+
+    def __len__(self):
+        return os.path.getsize(self.location)
+
+    def exists(self):
+        return os.path.exists(self.location)
+
+    def open(self):
+        return open(self.location, "rb")
+
+
+class LocalOutputFile(OutputFile):
+    """An OutputFile implementation for local files (for test use only)"""
+
+    def __init__(self, location: str):
+        if not location.startswith("file://"):
+            raise ValueError("LocalOutputFile location must start with 
`file://`")
+        super().__init__(location=location.split("file://")[1])
+
+    def __len__(self):
+        return len(self._file_obj)
+
+    def exists(self):
+        return os.path.exists(self.location)
+
+    def to_input_file(self):
+        return LocalInputFile(location=f"file://{self.location}")
+
+    def create(self, overwrite: bool = False) -> None:
+        if not overwrite and self.exists():
+            raise FileExistsError(f"{self.location} already exists")
+
+        return open(self.location, "wb")
+
+
+class LocalFileIO(FileIO):
+    """A FileIO implementation for local files (for test use only)"""
+
+    def new_input(self, location: str):
+        return LocalInputFile(location=location)
+
+    def new_output(self, location: str):
+        return LocalOutputFile(location=location)
+
+    def delete(self, location: str):
+        os.remove(location)
+
+
+@pytest.mark.parametrize("CustomInputFile", [LocalInputFile])
+def test_custom_local_input_file(CustomInputFile):
+    with tempfile.NamedTemporaryFile("wb") as tmpfilename:
+        # Write to the temporary file and seek to the beginning
+        tmpfilename.write(b"foo")
+        tmpfilename.seek(0)
+
+        # Instantiate the input file
+        input_file = CustomInputFile(location=f"file://{tmpfilename.name}")
+
+        # Test opening and reading the file
+        f = input_file.open()
+        data = f.read()
+        assert data == b"foo"
+
+
+@pytest.mark.parametrize("CustomOutputFile", [LocalOutputFile])
+def test_custom_local_output_file(CustomOutputFile):
+    with tempfile.TemporaryDirectory() as tmpdirname:
+        output_file_location = os.path.join(tmpdirname, "foo.txt")
+
+        # Instantiate an output file
+        output_file = 
CustomOutputFile(location=f"file://{output_file_location}")

Review comment:
       Do you have a test that `location` is the original location that was 
passed in?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to