Skip to content

Commit 3d0840b

Browse files
Fix tests
1 parent f0bc1db commit 3d0840b

12 files changed

Lines changed: 381 additions & 136 deletions

File tree

perdoo/comic/archives/sevenzip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from tempfile import TemporaryDirectory
88
from typing import ClassVar
99

10-
from perdoo.comic.archive._base import Archive
10+
from perdoo.comic.archives._base import Archive
1111
from perdoo.comic.errors import ComicArchiveError
1212
from perdoo.utils import list_files
1313

perdoo/comic/archives/zip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def rename_file(self, filename: str, new_name: str, override: bool = False) -> N
8282
if new_name in archive.namelist():
8383
if not override:
8484
raise ComicArchiveError(
85-
f"Unable to rename {filename} as {new_name} already extsts in {self.filepath.name}."
85+
f"Unable to rename {filename} as {new_name} already exists in {self.filepath.name}."
8686
)
8787
removed.append(archive.remove(new_name))
8888
removed.append(archive.remove(archive.copy(filename, new_name)))

perdoo/comic/comic.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ def convert_to(self, extension: Literal["cbz", "cbt", "cb7"]) -> None:
3737
self._archive = cls.convert_from(old_archive=self.archive)
3838

3939
def read_metadata(self, session: ArchiveSession) -> tuple[MetronInfo | None, ComicInfo | None]:
40-
metroninfo = None
40+
metron_info = None
4141
if session.contains(filename=MetronInfo.FILENAME):
42-
metroninfo = MetronInfo.from_bytes(content=session.read(filename=MetronInfo.FILENAME))
43-
44-
comicinfo = None
42+
metron_info = MetronInfo.from_bytes(content=session.read(filename=MetronInfo.FILENAME))
43+
comic_info = None
4544
if session.contains(filename=ComicInfo.FILENAME):
46-
comicinfo = ComicInfo.from_bytes(content=session.read(filename=ComicInfo.FILENAME))
47-
48-
return metroninfo, comicinfo
45+
comic_info = ComicInfo.from_bytes(content=session.read(filename=ComicInfo.FILENAME))
46+
return metron_info, comic_info
4947

5048
def list_images(self) -> list[Path]:
5149
return humansorted(
@@ -73,7 +71,7 @@ def validate_naming(self, naming: str) -> bool:
7371
return all(img.name.startswith(template) for img in self.list_images())
7472

7573
def move_to(self, naming: str, output_folder: Path) -> None:
76-
output = output_folder / f"{naming}{self.archive.EXTENSION}"
74+
output = output_folder / (naming + self.archive.EXTENSION)
7775
if output.exists():
7876
LOGGER.warning("'%s' already exists, skipping", output)
7977
return

tests/comic/archives/__init__.py

Whitespace-only changes.

tests/comic/archives/conftest.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from perdoo.comic.archives import CB7Archive, CBTArchive, CBZArchive
6+
7+
8+
@pytest.fixture
9+
def archive_files() -> dict[str, bytes]:
10+
return {"info.txt": b"Fake data", "001.jpg": b"Fake image"}
11+
12+
13+
@pytest.fixture
14+
def src(tmp_path: Path) -> Path:
15+
src = tmp_path / "src"
16+
src.mkdir(parents=True, exist_ok=True)
17+
return src
18+
19+
20+
@pytest.fixture
21+
def cbz_path(src: Path, archive_files: dict[str, bytes]) -> Path:
22+
created: list[Path] = []
23+
for filename, data in archive_files.items():
24+
tmp = src / filename
25+
tmp.write_bytes(data)
26+
created.append(tmp)
27+
return CBZArchive.archive_files(src=src, output_name="sample", files=created)
28+
29+
30+
@pytest.fixture
31+
def cbz_archive(cbz_path: Path) -> CBZArchive:
32+
return CBZArchive(filepath=cbz_path)
33+
34+
35+
@pytest.fixture
36+
def cbt_path(src: Path, archive_files: dict[str, bytes]) -> Path:
37+
created: list[Path] = []
38+
for filename, data in archive_files.items():
39+
tmp = src / filename
40+
tmp.write_bytes(data)
41+
created.append(tmp)
42+
return CBTArchive.archive_files(src=src, output_name="sample", files=created)
43+
44+
45+
@pytest.fixture
46+
def cbt_archive(cbt_path: Path) -> CBTArchive:
47+
return CBTArchive(filepath=cbt_path)
48+
49+
50+
@pytest.fixture
51+
def cb7_path(src: Path, archive_files: dict[str, bytes]) -> Path:
52+
created: list[Path] = []
53+
for filename, data in archive_files.items():
54+
tmp = src / filename
55+
tmp.write_bytes(data)
56+
created.append(tmp)
57+
return CB7Archive.archive_files(src=src, output_name="sample", files=created)
58+
59+
60+
@pytest.fixture
61+
def cb7_archive(cb7_path: Path) -> CB7Archive:
62+
return CB7Archive(filepath=cb7_path)

tests/comic/archives/test_base.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from pathlib import Path
2+
from typing import ClassVar
3+
4+
import pytest
5+
6+
from perdoo.comic.archives import Archive, CBTArchive, CBZArchive
7+
from perdoo.comic.errors import ComicArchiveError
8+
9+
10+
def test_load_selects_cbz(cbz_path: Path) -> None:
11+
loaded = Archive.load(filepath=cbz_path)
12+
13+
assert isinstance(loaded, CBZArchive)
14+
assert loaded.filepath == cbz_path
15+
16+
17+
def test_load_selects_cbt(cbt_path: Path) -> None:
18+
loaded = Archive.load(filepath=cbt_path)
19+
20+
assert isinstance(loaded, CBTArchive)
21+
assert loaded.filepath == cbt_path
22+
23+
24+
def test_load_unsupported(tmp_path: Path) -> None:
25+
tmp = tmp_path / "sample.xyz"
26+
tmp.write_bytes(b"Unsupported")
27+
28+
with pytest.raises(ComicArchiveError, match=r"Unsupported archive format"):
29+
Archive.load(filepath=tmp)
30+
31+
32+
def test_default_operations(tmp_path: Path, cbz_archive: CBZArchive) -> None:
33+
class DummyArchive(Archive):
34+
EXTENSION: ClassVar[str] = ".xyz"
35+
IS_READABLE: ClassVar[bool] = False
36+
IS_WRITEABLE: ClassVar[bool] = False
37+
IS_EDITABLE: ClassVar[bool] = False
38+
39+
@classmethod
40+
def is_archive(cls, path: Path) -> bool: # noqa: ARG003
41+
return False
42+
43+
def list_filenames(self) -> list[str]:
44+
return []
45+
46+
def extract_files(self, destination: Path) -> None: # noqa: ARG002
47+
return None
48+
49+
tmp = DummyArchive(filepath=tmp_path / "sample.xyz")
50+
with pytest.raises(ComicArchiveError, match=r"Unable to read"):
51+
tmp.read_file(filename="info.txt")
52+
with pytest.raises(ComicArchiveError, match=r"Unable to write"):
53+
tmp.write_file(filename="info.txt", data=b"Hello World")
54+
with pytest.raises(ComicArchiveError, match=r"Unable to delete"):
55+
tmp.delete_file(filename="info.txt")
56+
with pytest.raises(ComicArchiveError, match=r"Unable to rename"):
57+
tmp.rename_file(filename="info.txt", new_name="new.txt")
58+
with pytest.raises(ComicArchiveError, match=r"Unable to archive"):
59+
DummyArchive.archive_files(src=tmp_path, output_name="sample", files=[])
60+
with pytest.raises(ComicArchiveError, match=r"Unable to convert"):
61+
DummyArchive.convert_from(old_archive=cbz_archive)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from perdoo.comic.archives import ArchiveSession, CBTArchive, CBZArchive
4+
from perdoo.comic.errors import ComicArchiveError
5+
6+
7+
def test_editable_session(cbz_archive: CBZArchive) -> None:
8+
with ArchiveSession(archive=cbz_archive) as session:
9+
assert session.contains(filename="info.txt")
10+
assert session.read(filename="info.txt") == b"Fake data"
11+
12+
session.write(filename="info.txt", data="Updated data")
13+
session.write(filename="src.txt", data=b"Hello World")
14+
session.rename(filename="src.txt", new_name="new.txt")
15+
session.delete(filename="001.jpg")
16+
17+
assert cbz_archive.read_file(filename="info.txt") == b"Updated data"
18+
assert "001.jpg" not in cbz_archive.list_filenames()
19+
assert "src.txt" not in cbz_archive.list_filenames()
20+
assert "new.txt" in cbz_archive.list_filenames()
21+
22+
23+
def test_non_editable_session(cbt_archive: CBTArchive) -> None:
24+
with ArchiveSession(archive=cbt_archive) as session:
25+
assert session.contains(filename="info.txt")
26+
assert session.read(filename="info.txt") == b"Fake data"
27+
28+
session.write(filename="info.txt", data="Updated data")
29+
session.write(filename="src.txt", data=b"Hello World")
30+
session.rename(filename="src.txt", new_name="new.txt")
31+
session.delete(filename="001.jpg")
32+
33+
# TODO: assert cbt_archive.read_file(filename="info.txt") != b"Updated data"
34+
assert "001.jpg" in cbt_archive.list_filenames()
35+
assert "src.txt" not in cbt_archive.list_filenames()
36+
assert "new.txt" not in cbt_archive.list_filenames()
37+
38+
39+
def test_session_rename_raises_exception(cbz_archive: CBZArchive) -> None:
40+
with ArchiveSession(cbz_archive) as session:
41+
with pytest.raises(ComicArchiveError, match=r"Unable to rename"):
42+
session.rename(filename="new.txt", new_name="info.txt")
43+
with pytest.raises(ComicArchiveError, match=r"Unable to rename"):
44+
session.rename(filename="info.txt", new_name="001.jpg", override=False)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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"

tests/comic/archives/test_tar.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from perdoo.comic.archives import CBTArchive, CBZArchive
6+
from perdoo.comic.errors import ComicArchiveError
7+
from perdoo.utils import list_files
8+
9+
10+
def test_is_archive(cbt_path: Path) -> None:
11+
assert CBTArchive.is_archive(path=cbt_path) is True
12+
13+
14+
def test_list_filenames(cbt_archive: CBTArchive) -> None:
15+
assert set(cbt_archive.list_filenames()) == {"info.txt", "001.jpg"}
16+
17+
18+
def test_unsupported_functions(cbt_archive: CBTArchive) -> None:
19+
with pytest.raises(ComicArchiveError, match=r"Unable to read"):
20+
cbt_archive.read_file(filename="info.txt")
21+
with pytest.raises(ComicArchiveError, match=r"Unable to write"):
22+
cbt_archive.write_file(filename="info.txt", data=b"Updated data")
23+
with pytest.raises(ComicArchiveError, match=r"Unable to delete"):
24+
cbt_archive.delete_file(filename="info.txt")
25+
with pytest.raises(ComicArchiveError, match=r"Unable to rename"):
26+
cbt_archive.rename_file(filename="info.txt", new_name="new.txt")
27+
28+
29+
def test_extract_files(cbt_archive: CBTArchive, tmp_path: Path) -> None:
30+
dest = tmp_path / "out"
31+
dest.mkdir(parents=True, exist_ok=True)
32+
cbt_archive.extract_files(destination=dest)
33+
34+
assert (dest / "info.txt").read_text(encoding="UTF-8") == "Fake data"
35+
assert (dest / "001.jpg").read_bytes() == b"Fake image"
36+
37+
38+
def test_archive_files(cbt_archive: CBTArchive, tmp_path: Path) -> None:
39+
dest = tmp_path / "out"
40+
dest.mkdir(parents=True, exist_ok=True)
41+
cbt_archive.extract_files(destination=dest)
42+
archive = CBTArchive.archive_files(
43+
src=dest, output_name=cbt_archive.filepath.stem, files=list_files(path=dest)
44+
)
45+
46+
assert cbt_archive.filepath == archive
47+
48+
49+
def test_convert_from(cbz_archive: CBZArchive) -> None:
50+
old_filenames = cbz_archive.list_filenames()
51+
archive = CBTArchive.convert_from(old_archive=cbz_archive)
52+
53+
assert isinstance(archive, CBTArchive)
54+
assert archive.filepath.suffix == ".cbt"
55+
assert archive.filepath.exists()
56+
assert not cbz_archive.filepath.exists()
57+
assert archive.list_filenames() == old_filenames
58+
# TODO: assert archive.read_file(filename="info.txt") == b"Fake data"

0 commit comments

Comments
 (0)