From 396c0b539cdf39dc70037b26edcf8cc1b42cdc38 Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 23 Jul 2026 14:35:43 -0700 Subject: [PATCH] Test: assert zip-compression flag resolves to the right sink constant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_zip_lzma_compression_round_trips / test_default_zip_uses_deflate built a real zip and read compress_type back off it — that only reconfirms zipfile.ZipFile() honors its own compression= kwarg (ZipExportSink's own tests already cover that forwarding). What this command owns is resolving the --zip-compression string to the right zipfile constant; assert that resolution directly against the mocked ZipExportSink construction call instead. Drops the sample-doc copytree setup those tests needed only to build a real archive. --- .../tests/test_management_exporter.py | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.py index 7734de7f8..70c454e21 100644 --- a/src/documents/tests/test_management_exporter.py +++ b/src/documents/tests/test_management_exporter.py @@ -1119,47 +1119,47 @@ class TestExportImport( skip_checks=True, ) - def test_zip_lzma_compression_round_trips(self) -> None: - shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", + def test_zip_compression_flag_resolves_to_sink_constant(self) -> None: + # Whether zipfile actually compresses with the chosen method is + # Python's contract (and ZipExportSink's own tests already cover the + # forwarding). What this command owns is resolving the CLI string to + # the right constant, so assert that resolution directly. + with mock.patch( + "documents.management.commands.document_exporter.ZipExportSink", + ) as sink_cls: + call_command( + "document_exporter", + self.target, + "--zip", + "--zip-compression", + "lzma", + skip_checks=True, + ) + sink_cls.assert_called_once_with( + mock.ANY, + mock.ANY, + delete=False, + compression=ZIP_LZMA, + compresslevel=None, ) - call_command( - "document_exporter", - self.target, - "--zip", - "--zip-compression", - "lzma", - skip_checks=True, - ) - expected = str( - self.target / f"export-{timezone.localdate().isoformat()}.zip", - ) - self.assertIsFile(expected) - with ZipFile(expected) as zip_file: - info = zip_file.getinfo("manifest.json") - # manifest.json carries the chosen method; deflated is the default - self.assertEqual(info.compress_type, ZIP_LZMA) - def test_default_zip_uses_deflate(self) -> None: - shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", + def test_default_zip_compression_resolves_to_deflate(self) -> None: + with mock.patch( + "documents.management.commands.document_exporter.ZipExportSink", + ) as sink_cls: + call_command( + "document_exporter", + self.target, + "--zip", + skip_checks=True, + ) + sink_cls.assert_called_once_with( + mock.ANY, + mock.ANY, + delete=False, + compression=ZIP_DEFLATED, + compresslevel=None, ) - call_command( - "document_exporter", - self.target, - "--zip", - skip_checks=True, - ) - expected = str( - self.target / f"export-{timezone.localdate().isoformat()}.zip", - ) - with ZipFile(expected) as zip_file: - info = zip_file.getinfo("manifest.json") - self.assertEqual(info.compress_type, ZIP_DEFLATED) @pytest.mark.management