Skip to content

Add file like bad scenario testing #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,78 @@ def seek(self, offset: int, whence: int) -> bytes:

assert file_counter.num_seeks > last_frame_seeks

def test_file_like_method_check_fails(self):
class ReadMethodMissing:
def seek(self, offset: int, whence: int) -> bytes:
return bytes()

with pytest.raises(RuntimeError, match="must implement a read method"):
create_from_file_like(ReadMethodMissing(), "approximate")

class SeekMethodMissing:
def read(self, size: int) -> bytes:
return bytes()

with pytest.raises(RuntimeError, match="must implement a seek method"):
create_from_file_like(SeekMethodMissing(), "approximate")

class ReadMethodWrongSignature:
def __init__(self, file: io.RawIOBase):
self._file = file

# io.RawIOBase says we should accept a single int; wrong signature on purpose
def read(self) -> bytes:
return bytes()

def seek(self, offset: int, whence: int) -> bytes:
return self._file.seeK(offset, whence)

with pytest.raises(
TypeError, match="takes 1 positional argument but 2 were given"
):
create_from_file_like(
ReadMethodWrongSignature(open(NASA_VIDEO.path, mode="rb", buffering=0)),
"approximate",
)

class SeekMethodWrongSignature:
def __init__(self, file: io.RawIOBase):
self._file = file

def read(self, size: int) -> bytes:
return self._file.read(size)

# io.RawIOBase says we should accept two ints; wrong signature on purpose
def seek(self, offset: int) -> bytes:
return bytes()

with pytest.raises(
TypeError, match="takes 2 positional arguments but 3 were given"
):
create_from_file_like(
SeekMethodWrongSignature(open(NASA_VIDEO.path, mode="rb", buffering=0)),
"approximate",
)

def test_file_like_read_fails(self):
class BadReader(io.RawIOBase):

def __init__(self, file: io.RawIOBase):
self._file = file

def read(self, size: int) -> bytes:
# We intentionally read more than requested.
return self._file.read(size + 10)

def seek(self, offset: int, whence: int) -> bytes:
return self._file.seek(offset, whence)

with pytest.raises(RuntimeError, match="does not conform to read protocol"):
create_from_file_like(
BadReader(open(NASA_VIDEO.path, mode="rb", buffering=0)),
"approximate",
)


if __name__ == "__main__":
pytest.main()
Loading