Skip to content

Commit

Permalink
refactor: rename assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Mar 24, 2024
1 parent 48aca45 commit f21974b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
8 changes: 4 additions & 4 deletions dataclass_io/_lib/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclass_io._lib.dataclass_extensions import DataclassInstance


def assert_readable_file(path: Path) -> None:
def assert_file_is_readable(path: Path) -> None:
"""
Check that the input file exists and is readable.
Expand All @@ -26,13 +26,13 @@ def assert_readable_file(path: Path) -> None:
raise PermissionError(f"The input file is not readable: {path}")


def assert_readable_dataclass(dc_type: type[DataclassInstance]) -> None:
def assert_dataclass_is_valid(dataclass_type: type[DataclassInstance]) -> None:
"""
Check that the input type is a parseable dataclass.
Raises:
TypeError: If the provided type is not a dataclass.
"""

if not is_dataclass(dc_type):
raise TypeError(f"The provided type must be a dataclass: {dc_type.__name__}")
if not is_dataclass(dataclass_type):
raise TypeError(f"The provided type must be a dataclass: {dataclass_type.__name__}")
10 changes: 4 additions & 6 deletions dataclass_io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
from typing import Type
from typing import TypeAlias

from dataclass_io._lib.assertions import assert_readable_dataclass
from dataclass_io._lib.assertions import assert_readable_file

# from dataclass_io._lib.dataclass_extensions import DataclassType
from dataclass_io._lib.assertions import assert_dataclass_is_valid
from dataclass_io._lib.assertions import assert_file_is_readable
from dataclass_io._lib.dataclass_extensions import DataclassInstance

ReadableFileHandle: TypeAlias = TextIOWrapper | IO | TextIO
Expand Down Expand Up @@ -58,8 +56,8 @@ def __init__(
TypeError: If the provided type is not a dataclass.
"""

assert_readable_file(path)
assert_readable_dataclass(dataclass_type)
assert_file_is_readable(path)
assert_dataclass_is_valid(dataclass_type)

self.dataclass_type = dataclass_type
self.delimiter = delimiter
Expand Down
28 changes: 14 additions & 14 deletions tests/_lib/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import pytest

from dataclass_io._lib.assertions import assert_readable_dataclass
from dataclass_io._lib.assertions import assert_readable_file
from dataclass_io._lib.assertions import assert_dataclass_is_valid
from dataclass_io._lib.assertions import assert_file_is_readable


def test_assert_readable_dataclass() -> None:
def test_assert_dataclass_is_valid() -> None:
"""
Test that we can validate if a dataclass is valid for reading.
"""
Expand All @@ -18,12 +18,12 @@ class FakeDataclass:
bar: int

try:
assert_readable_dataclass(FakeDataclass)
assert_dataclass_is_valid(FakeDataclass)
except TypeError:
raise AssertionError("Failed to validate a valid dataclass") from None


def test_assert_readable_dataclass_raises_if_not_a_dataclass() -> None:
def test_assert_dataclass_is_valid_raises_if_not_a_dataclass() -> None:
"""
Test that we raise an error if the provided type is not a dataclass.
"""
Expand All @@ -35,10 +35,10 @@ class BadDataclass:
with pytest.raises(TypeError, match="The provided type must be a dataclass: BadDataclass"):
# mypy (correctly) flags that `BadDataclass` is not a dataclass.
# We still want to test that we can enforce this at runtime, so here it's ok to ignore.
assert_readable_dataclass(BadDataclass) # type: ignore[arg-type]
assert_dataclass_is_valid(BadDataclass) # type: ignore[arg-type]


def test_assert_readable_file(tmp_path: Path) -> None:
def test_assert_file_is_readable(tmp_path: Path) -> None:
"""
Test that we can validate if a file is valid for reading.
"""
Expand All @@ -47,30 +47,30 @@ def test_assert_readable_file(tmp_path: Path) -> None:
fpath.touch()

try:
assert_readable_file(fpath)
assert_file_is_readable(fpath)
except Exception:
raise AssertionError("Failed to validate a valid file") from None


def test_assert_readable_file_raises_if_file_does_not_exist(tmp_path: Path) -> None:
def test_assert_file_is_readable_raises_if_file_does_not_exist(tmp_path: Path) -> None:
"""
Test that we can validate if a file does not exist.
"""

with pytest.raises(FileNotFoundError, match="The input file does not exist: "):
assert_readable_file(tmp_path / "does_not_exist.txt")
assert_file_is_readable(tmp_path / "does_not_exist.txt")


def test_assert_readable_file_raises_if_file_is_a_directory(tmp_path: Path) -> None:
def test_assert_file_is_readable_raises_if_file_is_a_directory(tmp_path: Path) -> None:
"""
Test that we can validate if a file does not exist.
"""

with pytest.raises(IsADirectoryError, match="The input file path is a directory: "):
assert_readable_file(tmp_path)
assert_file_is_readable(tmp_path)


def test_assert_readable_file_raises_if_file_is_unreadable(tmp_path: Path) -> None:
def test_assert_file_is_readable_raises_if_file_is_unreadable(tmp_path: Path) -> None:
"""
Test that we can validate if a file cannot be read.
"""
Expand All @@ -79,4 +79,4 @@ def test_assert_readable_file_raises_if_file_is_unreadable(tmp_path: Path) -> No
fpath.touch(0)

with pytest.raises(PermissionError, match="The input file is not readable: "):
assert_readable_file(fpath)
assert_file_is_readable(fpath)

0 comments on commit f21974b

Please sign in to comment.