Debian Codegraph @

contrib/python-zstandard/tests/test_compressor_read_to_iter.py

raw
1 import io
2 import unittest
3
4 import zstandard as zstd
5
6 from .common import (
7 CustomBytesIO,
8 )
9
10
11 class TestCompressor_read_to_iter(unittest.TestCase):
12 def test_type_validation(self):
13 cctx = zstd.ZstdCompressor()
14
15 # Object with read() works.
16 for chunk in cctx.read_to_iter(io.BytesIO()):
17 pass
18
19 # Buffer protocol works.
20 for chunk in cctx.read_to_iter(b"foobar"):
21 pass
22
23 with self.assertRaisesRegex(
24 ValueError, "must pass an object with a read"
25 ):
26 for chunk in cctx.read_to_iter(True):
27 pass
28
29 def test_read_empty(self):
30 cctx = zstd.ZstdCompressor(level=1, write_content_size=False)
31
32 source = io.BytesIO()
33 it = cctx.read_to_iter(source)
34 chunks = list(it)
35 self.assertEqual(len(chunks), 1)
36 compressed = b"".join(chunks)
37 self.assertEqual(compressed, b"\x28\xb5\x2f\xfd\x00\x00\x01\x00\x00")
38
39 # And again with the buffer protocol.
40 it = cctx.read_to_iter(b"")
41 chunks = list(it)
42 self.assertEqual(len(chunks), 1)
43 compressed2 = b"".join(chunks)
44 self.assertEqual(compressed2, compressed)
45
46 def test_read_large(self):
47 cctx = zstd.ZstdCompressor(level=1, write_content_size=False)
48
49 source = io.BytesIO()
50 source.write(b"f" * zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE)
51 source.write(b"o")
52 source.seek(0)
53
54 # Creating an iterator should not perform any compression until
55 # first read.
56 it = cctx.read_to_iter(source, size=len(source.getvalue()))
57 self.assertEqual(source.tell(), 0)
58
59 # We should have exactly 2 output chunks.
60 chunks = []
61 chunk = next(it)
62 self.assertIsNotNone(chunk)
63 self.assertEqual(source.tell(), zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE)
64 chunks.append(chunk)
65 chunk = next(it)
66 self.assertIsNotNone(chunk)
67 chunks.append(chunk)
68
69 self.assertEqual(source.tell(), len(source.getvalue()))
70
71 with self.assertRaises(StopIteration):
72 next(it)
73
74 # And again for good measure.
75 with self.assertRaises(StopIteration):
76 next(it)
77
78 # We should get the same output as the one-shot compression mechanism.
79 self.assertEqual(b"".join(chunks), cctx.compress(source.getvalue()))
80
81 params = zstd.get_frame_parameters(b"".join(chunks))
82 self.assertEqual(params.content_size, zstd.CONTENTSIZE_UNKNOWN)
83 self.assertEqual(params.window_size, 262144)
84 self.assertEqual(params.dict_id, 0)
85 self.assertFalse(params.has_checksum)
86
87 # Now check the buffer protocol.
88 it = cctx.read_to_iter(source.getvalue())
89 chunks = list(it)
90 self.assertEqual(len(chunks), 2)
91
92 params = zstd.get_frame_parameters(b"".join(chunks))
93 self.assertEqual(params.content_size, zstd.CONTENTSIZE_UNKNOWN)
94 # self.assertEqual(params.window_size, 262144)
95 self.assertEqual(params.dict_id, 0)
96 self.assertFalse(params.has_checksum)
97
98 self.assertEqual(b"".join(chunks), cctx.compress(source.getvalue()))
99
100 def test_read_write_size(self):
101 source = CustomBytesIO(b"foobarfoobar")
102 cctx = zstd.ZstdCompressor(level=3)
103 for chunk in cctx.read_to_iter(source, read_size=1, write_size=1):
104 self.assertEqual(len(chunk), 1)
105
106 self.assertEqual(source._read_count, len(source.getvalue()) + 1)
107
108 def test_multithreaded(self):
109 source = io.BytesIO()
110 source.write(b"a" * 1048576)
111 source.write(b"b" * 1048576)
112 source.write(b"c" * 1048576)
113 source.seek(0)
114
115 cctx = zstd.ZstdCompressor(threads=2)
116
117 compressed = b"".join(cctx.read_to_iter(source))
118 self.assertEqual(len(compressed), 111)
119
120 def test_bad_size(self):
121 cctx = zstd.ZstdCompressor()
122
123 source = io.BytesIO(b"a" * 42)
124
125 with self.assertRaisesRegex(zstd.ZstdError, "Src size is incorrect"):
126 b"".join(cctx.read_to_iter(source, size=2))
127
128 # Test another operation on errored compressor.
129 b"".join(cctx.read_to_iter(source))
130
131 def test_read_exception(self):
132 b = CustomBytesIO(b"foo" * 1024)
133 b.read_exception = IOError("read")
134
135 cctx = zstd.ZstdCompressor()
136
137 it = cctx.read_to_iter(b)
138
139 with self.assertRaisesRegex(IOError, "read"):
140 next(it)