Debian Codegraph @

tests/test_integration.py

raw
1 import shutil
2 import sys
3 import unittest
4 from io import BytesIO
5 from pathlib import Path
6 from secrets import token_bytes, token_urlsafe
7 from tempfile import TemporaryDirectory
8
9 if sys.version_info >= (3, 14):
10 from compression import zstd
11 import tarfile
12 import zipfile
13 else:
14 from backports import zstd
15 from backports.zstd import register_shutil, tarfile, zipfile
16 register_shutil()
17
18 if sys.version_info >= (3, 11):
19 from typing import assert_type
20 else:
21 assert_type = lambda *_: None
22
23 # these tests are simple checks for main use cases
24 # to make sure they work with the conditional import in 3.14 as well
25
26
27 class TestCompat(unittest.TestCase):
28 def test_compress_decompress(self) -> None:
29 raw = token_bytes(1_000)
30 assert_type(raw, bytes)
31 compressed = zstd.compress(raw)
32 assert_type(compressed, bytes)
33 decompressed = zstd.decompress(compressed)
34 assert_type(decompressed, bytes)
35 self.assertEqual(decompressed, raw)
36
37 def test_zstdfile(self) -> None:
38 raw = token_bytes(1_000)
39 fobj = BytesIO()
40 with zstd.ZstdFile(fobj, "w") as fzstd:
41 fzstd.write(raw)
42 self.assertTrue(fobj.tell() > 0)
43 fobj.seek(0)
44 with zstd.ZstdFile(fobj) as fzstd:
45 data = fzstd.read()
46 assert_type(data, bytes)
47 self.assertEqual(data, raw)
48 self.assertTrue(fobj.tell() > 0)
49
50 def test_open(self) -> None:
51 raw = token_bytes(1_000)
52 fobj = BytesIO()
53 with zstd.open(fobj, "w") as fzstd:
54 fzstd.write(raw)
55 self.assertTrue(fobj.tell() > 0)
56 fobj.seek(0)
57 with zstd.open(fobj) as fzstd:
58 data = fzstd.read()
59 assert_type(data, bytes)
60 self.assertEqual(data, raw)
61 self.assertTrue(fobj.tell() > 0)
62
63 def test_open_binary(self) -> None:
64 raw = token_bytes(1_000)
65 fobj = BytesIO()
66 with zstd.open(fobj, "wb") as fzstd:
67 fzstd.write(raw)
68 self.assertTrue(fobj.tell() > 0)
69 fobj.seek(0)
70 with zstd.open(fobj, "rb") as fzstd:
71 data = fzstd.read()
72 assert_type(data, bytes)
73 self.assertEqual(data, raw)
74 self.assertTrue(fobj.tell() > 0)
75
76 def test_open_text(self) -> None:
77 raw = token_urlsafe(1_000)
78 fobj = BytesIO()
79 with zstd.open(fobj, "wt") as fzstd:
80 fzstd.write(raw)
81 self.assertTrue(fobj.tell() > 0)
82 fobj.seek(0)
83 with zstd.open(fobj, "rt") as fzstd:
84 data = fzstd.read()
85 assert_type(data, str)
86 self.assertEqual(data, raw)
87 self.assertTrue(fobj.tell() > 0)
88
89 def test_tarfile(self) -> None:
90 raw = token_bytes(1_000)
91 raw_name = token_urlsafe(10)
92 with TemporaryDirectory() as tmpfile:
93 path = Path(tmpfile) / "archive.tar.zst"
94 with tarfile.open(path, "w:zst") as tf:
95 ti = tarfile.TarInfo(raw_name)
96 ti.size = len(raw)
97 tf.addfile(ti, BytesIO(raw))
98
99 with tarfile.open(path) as tf:
100 self.assertEqual(tf.getnames(), [raw_name])
101 extracted = tf.extractfile(raw_name)
102 assert extracted is not None # for type checkers
103 with extracted as fobj:
104 self.assertEqual(fobj.read(), raw)
105
106 shutil.unpack_archive(path, tmpfile)
107 self.assertEqual((Path(tmpfile) / raw_name).read_bytes(), raw)
108
109 def test_zipfile(self) -> None:
110 raw = token_bytes(1_000)
111 raw_name = token_urlsafe(10)
112 with TemporaryDirectory() as tmpfile:
113 path = Path(tmpfile) / "archive.zip"
114 with zipfile.ZipFile(path, "w") as zf:
115 zf.writestr(raw_name, raw, zipfile.ZIP_ZSTANDARD)
116
117 with zipfile.ZipFile(path) as zf:
118 self.assertEqual(zf.namelist(), [raw_name])
119 self.assertEqual(zf.read(raw_name), raw)
120
121 shutil.unpack_archive(path, tmpfile)
122 self.assertEqual((Path(tmpfile) / raw_name).read_bytes(), raw)
123
124 def test_shutil_make_archive(self) -> None:
125 raw = token_bytes(1_000)
126 raw_name = token_urlsafe(10)
127 with TemporaryDirectory() as tmpfile:
128 path_src = Path(tmpfile) / "src"
129 path_src.mkdir()
130 (path_src / raw_name).write_bytes(raw)
131
132 path_dst = Path(tmpfile) / "archive"
133 shutil.make_archive(path_dst.as_posix(), "zstdtar", path_src)
134
135 with path_dst.with_suffix(".tar.zst").open("rb") as f:
136 self.assertEqual(f.read(4), bytes.fromhex("28 b5 2f fd"))
137
138
139 if __name__ == "__main__":
140 unittest.main()