Skip to content

Commit

Permalink
upath: tests for open
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Mar 3, 2024
1 parent 508d726 commit c1fe486
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
23 changes: 23 additions & 0 deletions upath/implementations/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
from pathlib import PosixPath
from pathlib import WindowsPath
from typing import IO
from typing import Any
from typing import Collection
from typing import MutableMapping
Expand Down Expand Up @@ -110,6 +111,17 @@ class PosixUPath(PosixPath, LocalPath):
# assign all PosixPath methods/attrs to prevent multi inheritance issues
_set_class_attributes(locals(), src=PosixPath)

def open(
self,
mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
**fsspec_kwargs,
) -> IO[Any]:
return PosixPath.open(self, mode, buffering, encoding, errors, newline)

if sys.version_info < (3, 12):

def __new__(
Expand Down Expand Up @@ -153,6 +165,17 @@ class WindowsUPath(WindowsPath, LocalPath):
# assign all WindowsPath methods/attrs to prevent multi inheritance issues
_set_class_attributes(locals(), src=WindowsPath)

def open(
self,
mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
**fsspec_kwargs,
) -> IO[Any]:
return WindowsPath.open(self, mode, buffering, encoding, errors, newline)

if sys.version_info < (3, 12):

def __new__(
Expand Down
20 changes: 19 additions & 1 deletion upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,25 @@ def test_makedirs_exist_ok_false(self):
new_dir._accessor.makedirs(new_dir, exist_ok=False)

def test_open(self):
pass
p = self.path.joinpath("file1.txt")
with p.open(mode="r") as f:
assert f.read() == "hello world"
with p.open(mode="rb") as f:
assert f.read() == b"hello world"

def test_open_buffering(self):
p = self.path.joinpath("file1.txt")
p.open(buffering=-1)

def test_open_block_size(self):
p = self.path.joinpath("file1.txt")
with p.open(mode="r", block_size=8192) as f:
assert f.read() == "hello world"

def test_open_errors(self):
p = self.path.joinpath("file1.txt")
with p.open(mode="r", encoding="ascii", errors="strict") as f:
assert f.read() == "hello world"

def test_owner(self):
with pytest.raises(NotImplementedError):
Expand Down
20 changes: 20 additions & 0 deletions upath/tests/implementations/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ def test_mkdir_parents_true_exists_ok_true(self):
def test_mkdir_parents_true_exists_ok_false(self):
pass

def test_open(self):
p = UPath("data:text/plain;base64,aGVsbG8gd29ybGQ=")
with p.open(mode="r") as f:
assert f.read() == "hello world"
with p.open(mode="rb") as f:
assert f.read() == b"hello world"

def test_open_buffering(self):
self.path.open(buffering=-1)

def test_open_block_size(self):
p = UPath("data:text/plain;base64,aGVsbG8gd29ybGQ=")
with p.open(mode="r", block_size=8192) as f:
assert f.read() == "hello world"

def test_open_errors(self):
p = UPath("data:text/plain;base64,aGVsbG8gd29ybGQ=")
with p.open(mode="r", encoding="ascii", errors="strict") as f:
assert f.read() == "hello world"

def test_read_bytes(self, pathlib_base):
assert len(self.path.read_bytes()) == 69

Expand Down

0 comments on commit c1fe486

Please sign in to comment.