|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from perdoo.comic.archives import CB7Archive, CBZArchive |
| 6 | +from perdoo.comic.archives.sevenzip import PY7ZR_AVAILABLE |
| 7 | +from perdoo.comic.errors import ComicArchiveError |
| 8 | +from perdoo.utils import list_files |
| 9 | + |
| 10 | +pytestmark = pytest.mark.skipif(not PY7ZR_AVAILABLE, reason="py7zr not installed") |
| 11 | + |
| 12 | + |
| 13 | +def test_is_archive(cb7_path: Path) -> None: |
| 14 | + assert CB7Archive.is_archive(path=cb7_path) is True |
| 15 | + |
| 16 | + |
| 17 | +def test_list_filenames(cb7_archive: CB7Archive) -> None: |
| 18 | + assert set(cb7_archive.list_filenames()) == {"info.txt", "001.jpg"} |
| 19 | + |
| 20 | + |
| 21 | +def test_read_file(cb7_archive: CB7Archive) -> None: |
| 22 | + assert cb7_archive.read_file(filename="info.txt") == b"Fake data" |
| 23 | + assert cb7_archive.read_file(filename="001.jpg") == b"Fake image" |
| 24 | + |
| 25 | + |
| 26 | +def test_unsupported_functions(cb7_archive: CB7Archive) -> None: |
| 27 | + with pytest.raises(ComicArchiveError, match=r"Unable to write"): |
| 28 | + cb7_archive.write_file(filename="info.txt", data=b"Updated data") |
| 29 | + with pytest.raises(ComicArchiveError, match=r"Unable to delete"): |
| 30 | + cb7_archive.delete_file(filename="info.txt") |
| 31 | + with pytest.raises(ComicArchiveError, match=r"Unable to rename"): |
| 32 | + cb7_archive.rename_file(filename="info.txt", new_name="new.txt") |
| 33 | + |
| 34 | + |
| 35 | +def test_extract_files(cb7_archive: CB7Archive, tmp_path: Path) -> None: |
| 36 | + dest = tmp_path / "out" |
| 37 | + dest.mkdir(parents=True, exist_ok=True) |
| 38 | + cb7_archive.extract_files(destination=dest) |
| 39 | + |
| 40 | + assert (dest / "info.txt").read_text(encoding="UTF-8") == "Fake data" |
| 41 | + assert (dest / "001.jpg").read_bytes() == b"Fake image" |
| 42 | + |
| 43 | + |
| 44 | +def test_archive_files(cb7_archive: CB7Archive, tmp_path: Path) -> None: |
| 45 | + dest = tmp_path / "out" |
| 46 | + dest.mkdir(parents=True, exist_ok=True) |
| 47 | + cb7_archive.extract_files(destination=dest) |
| 48 | + archive = CB7Archive.archive_files( |
| 49 | + src=dest, output_name=cb7_archive.filepath.stem, files=list_files(path=dest) |
| 50 | + ) |
| 51 | + |
| 52 | + assert cb7_archive.filepath == archive |
| 53 | + |
| 54 | + |
| 55 | +def test_convert_from(cbz_archive: CBZArchive) -> None: |
| 56 | + old_filenames = cbz_archive.list_filenames() |
| 57 | + archive = CB7Archive.convert_from(old_archive=cbz_archive) |
| 58 | + |
| 59 | + assert isinstance(archive, CB7Archive) |
| 60 | + assert archive.filepath.suffix == ".cb7" |
| 61 | + assert archive.filepath.exists() |
| 62 | + assert not cbz_archive.filepath.exists() |
| 63 | + assert archive.list_filenames() == old_filenames |
| 64 | + assert archive.read_file(filename="info.txt") == b"Fake data" |
0 commit comments