Skip to content

Commit

Permalink
chore: add sensible test IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Jul 30, 2024
1 parent 2717295 commit 198e603
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/archivefile/_adapters/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BaseArchiveAdapter(Protocol):
A base protocol that can be inherited from to implement more adapters.
Refer to `src/archivefile/_core.py` for documentation of every method and property.
"""

# fmt: off
@overload
def __init__(self, file: StrPath, mode: OpenArchiveMode = "r", *, password: str | None = None, compression_type: CompressionType | None = None, compression_level: CompressionLevel | None = None, **kwargs: Any) -> None: ...
Expand Down
13 changes: 8 additions & 5 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
Path("tests/test_data/source_STORE.zip"),
)

# Alias the pre-configured parametrize function for reusability
parametrize_files = pytest.mark.parametrize("file", files, ids=lambda x: x.name)


def test_write_rar() -> None:
with pytest.raises(NotImplementedError):
Expand Down Expand Up @@ -91,35 +94,35 @@ def test_existing_unsupported_archive(tmp_path: Path) -> None:
archive.write_text("abc1234", arcname="a.txt")


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_missing_member(file: Path) -> None:
with pytest.raises(KeyError):
with ArchiveFile(file) as archive:
archive.get_member("non-existent.member")


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_missing_member_in_read_bytes(file: Path) -> None:
with pytest.raises(KeyError):
with ArchiveFile(file) as archive:
archive.read_bytes("non-existent.member")


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_missing_member_in_read_text(file: Path) -> None:
with pytest.raises(KeyError):
with ArchiveFile(file) as archive:
archive.read_text("non-existent.member")


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_missing_member_in_extract(file: Path) -> None:
with pytest.raises(KeyError):
with ArchiveFile(file) as archive:
archive.extract("non-existent.member")


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_missing_member_in_extractall(file: Path, tmp_path: Path) -> None:
with pytest.raises(KeyError):
with ArchiveFile(file) as archive:
Expand Down
13 changes: 8 additions & 5 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,34 @@
Path("tests/test_data/source_STORE.zip"),
)

# Alias the pre-configured parametrize function for reusability
parametrize_files = pytest.mark.parametrize("file", files, ids=lambda x: x.name)

@pytest.mark.parametrize("file", files)

@parametrize_files
def test_extract(file: Path, tmp_path: Path) -> None:
with ArchiveFile(file) as archive:
member = archive.extract("pyanilist-main/README.md", destination=tmp_path)
assert member.is_file()


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_extract_without_context_manager(file: Path, tmp_path: Path) -> None:
archive = ArchiveFile(file)
extracted_file = archive.extract("pyanilist-main/README.md", destination=tmp_path)
archive.close()
assert extracted_file.is_file()


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_extract_by_member(file: Path, tmp_path: Path) -> None:
with ArchiveFile(file) as archive:
member = [member for member in archive.get_members() if member.is_file][0]
outfile = archive.extract(member, destination=tmp_path)
assert outfile.is_file()


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_extractall(file: Path, tmp_path: Path) -> None:
with ZipFile("tests/test_data/source_STORE.zip") as archive:
dest = tmp_path / uuid4().hex
Expand All @@ -73,7 +76,7 @@ def test_extractall(file: Path, tmp_path: Path) -> None:
assert control == members


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_extractall_by_members(file: Path, tmp_path: Path) -> None:
expected = [
"pyanilist-main/.gitignore",
Expand Down
15 changes: 9 additions & 6 deletions tests/test_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,46 @@
Path("tests/test_data/source_STORE.zip"),
)

# Alias the pre-configured parametrize function for reusability
parametrize_files = pytest.mark.parametrize("file", files, ids=lambda x: x.name)

@pytest.mark.parametrize("file", files)

@parametrize_files
def test_get_members(file: Path) -> None:
with ArchiveFile(file) as archive:
assert len(tuple(archive.get_members())) == 53


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_get_members_without_context_manager(file: Path) -> None:
archive = ArchiveFile(file)
total_members = len(tuple(archive.get_members()))
archive.close()
assert total_members == 53


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_get_names(file: Path) -> None:
with ArchiveFile(file) as archive:
assert len(archive.get_names()) == 53


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_get_names_without_context_manager(file: Path) -> None:
archive = ArchiveFile(file)
total_members = len(archive.get_names())
archive.close()
assert total_members == 53


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_member_and_names(file: Path) -> None:
with ArchiveFile(file) as archive:
names = tuple([member.name for member in archive.get_members()])
assert archive.get_names() == names


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_members_and_names_without_context_manager(file: Path) -> None:
archive = ArchiveFile(file)
names = tuple([member.name for member in archive.get_members()])
Expand Down
1 change: 1 addition & 0 deletions tests/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
(Path("tests/test_data/source_STORE.rar"), None, None, "RarFileAdapter"),
(Path("tests/test_data/source_STORE.zip"), CompressionType.STORED, None, "ZipFileAdapter"),
],
ids=lambda x: x.name if isinstance(x, Path) else x, # https://github.com/pytest-dev/pytest/issues/8283
)
def test_core_properties(
file: Path, compression_type: CompressionType | None, compression_level: int | None, adapter: str
Expand Down
11 changes: 7 additions & 4 deletions tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,32 @@
For more information, please refer to <https://unlicense.org>
"""

# Alias the pre-configured parametrize function for reusability
parametrize_files = pytest.mark.parametrize("file", files, ids=lambda x: x.name)

@pytest.mark.parametrize("file", files)

@parametrize_files
def test_read_text_file(file: Path) -> None:
with ArchiveFile(file) as archive:
member = archive.read_text("pyanilist-main/UNLICENSE")
assert member.strip() == unlicense.strip()


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_read_bytes_file(file: Path) -> None:
with ArchiveFile(file) as archive:
member = archive.read_bytes("pyanilist-main/UNLICENSE")
assert member.decode().strip() == unlicense.strip()


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_read_text_folder(file: Path) -> None:
with ArchiveFile(file) as archive:
member = archive.read_text("pyanilist-main/src/")
assert member == ""


@pytest.mark.parametrize("file", files)
@parametrize_files
def test_read_bytes_folder(file: Path) -> None:
with ArchiveFile(file) as archive:
member = archive.read_bytes("pyanilist-main/src/")
Expand Down

0 comments on commit 198e603

Please sign in to comment.