You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/01/04 20:56:03 UTC

[GitHub] [iceberg] samredai commented on a change in pull request #3691: Add FileIO, InputFile, and OutputFile abstract base classes

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



##########
File path: python/tests/io/test_base.py
##########
@@ -0,0 +1,116 @@
+# 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 io
+
+from iceberg.io.base import FileIO, InputFile, OutputFile
+
+
+class FooInputFile(InputFile):
+    def __len__(self):
+        return io.BytesIO(b"foo").getbuffer().nbytes
+
+    def exists(self):
+        return True
+
+    def __enter__(self):
+        super().__enter__()
+        return io.BytesIO(b"foo")
+
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        super().__exit__(exc_type, exc_value, exc_traceback)
+        return
+
+
+class FooOutputFile(OutputFile):
+    def __call__(self, overwrite: bool = False, **kwargs):
+        super().__call__(overwrite=True)
+        return self
+
+    def __len__(self):
+        return len(self._file_obj)
+
+    def exists(self):
+        return True
+
+    def to_input_file(self):
+        return FooInputFile(location=self.location)
+
+    def __enter__(self):
+        self._mock_storage = io.BytesIO()
+        return self._mock_storage
+
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        super().__exit__(exc_type, exc_value, exc_traceback)
+        return
+
+
+class FooFileIO(FileIO):
+    def new_input(self, location: str):
+        return FooInputFile(location=location)
+
+    def new_output(self, location: str):
+        return FooOutputFile(location=location)
+
+    def delete(self, location: str):
+        return
+
+
+def test_custom_input_file():
+
+    input_file = FooInputFile(location="foo/bar.json")
+    assert input_file.location == "foo/bar.json"
+
+    with input_file as f:
+        data = f.read()
+
+    assert data == b"foo"
+
+
+def test_custom_output_file():
+
+    output_file = FooOutputFile(location="foo/bar.json")
+    assert output_file.location == "foo/bar.json"

Review comment:
       There's actually some python standard library stuff I could use to create a tempdir that's automatically cleaned up. That's a good idea I'll update the tests!




-- 
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-unsubscribe@iceberg.apache.org

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



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