Test: assert compression forwarding via call args, not a real archive

test_compression_method_is_applied_to_file_entries built a real zip and
read back compress_type — but that only confirms zipfile.ZipFile()
honors its own compression= kwarg, which is documented stdlib behavior,
not something our code could get wrong. ZipExportSink's only
responsibility is forwarding compression/compresslevel unchanged to
ZipFile(); assert that directly against the mocked constructor call.
This commit is contained in:
stumpylog
2026-07-23 14:26:01 -07:00
parent cc4917efec
commit 50dec14653
+16 -18
View File
@@ -5,6 +5,7 @@ import zipfile
from pathlib import Path
import pytest
import pytest_mock
from documents.export.sinks import DirectoryExportSink
from documents.export.sinks import ExportSink
@@ -268,13 +269,6 @@ class TestZipExportSink:
class TestZipExportSinkCompression:
@pytest.fixture()
def large_source_file(self, tmp_path: Path) -> Path:
src: Path = tmp_path / "src" / "doc.pdf"
src.parent.mkdir(parents=True)
src.write_bytes(b"PDF-CONTENT" * 100)
return src
@pytest.mark.parametrize(
("method", "constant"),
[
@@ -284,25 +278,29 @@ class TestZipExportSinkCompression:
("lzma", zipfile.ZIP_LZMA),
],
)
def test_compression_method_is_applied_to_file_entries(
def test_compression_and_level_forwarded_to_zipfile(
self,
mocker: pytest_mock.MockerFixture,
tmp_path: Path,
large_source_file: Path,
method: str,
constant: int,
) -> None:
# ZipExportSink's only responsibility here is forwarding the
# constructor's compression/compresslevel to zipfile.ZipFile
# unchanged; whether ZipFile actually compresses is Python's contract,
# not ours, so this checks the call args rather than the archive.
target: Path = tmp_path / "out"
target.mkdir()
with ZipExportSink(
target,
"export",
delete=False,
zip_cls = mocker.patch("documents.export.sinks.zipfile.ZipFile")
sink = ZipExportSink(target, "export", compression=constant, compresslevel=5)
sink._open()
zip_cls.assert_called_once_with(
mocker.ANY,
"w",
compression=constant,
) as sink:
sink.add_file(large_source_file, "doc.pdf")
with zipfile.ZipFile(target / "export.zip") as zf:
info = zf.getinfo("doc.pdf")
assert info.compress_type == constant
compresslevel=5,
allowZip64=True,
)
class TestStreamContract: