| 1 |
import io |
| 2 |
import struct |
| 3 |
import unittest |
| 4 |
|
| 5 |
import zstandard as zstd |
| 6 |
|
| 7 |
from .common import ( |
| 8 |
CustomBytesIO, |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
class TestDecompressor_copy_stream(unittest.TestCase): |
| 13 |
def test_no_read(self): |
| 14 |
source = object() |
| 15 |
dest = io.BytesIO() |
| 16 |
|
| 17 |
dctx = zstd.ZstdDecompressor() |
| 18 |
with self.assertRaises(ValueError): |
| 19 |
dctx.copy_stream(source, dest) |
| 20 |
|
| 21 |
def test_no_write(self): |
| 22 |
source = io.BytesIO() |
| 23 |
dest = object() |
| 24 |
|
| 25 |
dctx = zstd.ZstdDecompressor() |
| 26 |
with self.assertRaises(ValueError): |
| 27 |
dctx.copy_stream(source, dest) |
| 28 |
|
| 29 |
def test_empty(self): |
| 30 |
source = io.BytesIO() |
| 31 |
dest = io.BytesIO() |
| 32 |
|
| 33 |
dctx = zstd.ZstdDecompressor() |
| 34 |
# TODO should this raise an error? |
| 35 |
r, w = dctx.copy_stream(source, dest) |
| 36 |
|
| 37 |
self.assertEqual(r, 0) |
| 38 |
self.assertEqual(w, 0) |
| 39 |
self.assertEqual(dest.getvalue(), b"") |
| 40 |
|
| 41 |
def test_large_data(self): |
| 42 |
source = io.BytesIO() |
| 43 |
for i in range(255): |
| 44 |
source.write(struct.Struct(">B").pack(i) * 16384) |
| 45 |
source.seek(0) |
| 46 |
|
| 47 |
compressed = io.BytesIO() |
| 48 |
cctx = zstd.ZstdCompressor() |
| 49 |
cctx.copy_stream(source, compressed) |
| 50 |
|
| 51 |
compressed.seek(0) |
| 52 |
dest = io.BytesIO() |
| 53 |
dctx = zstd.ZstdDecompressor() |
| 54 |
r, w = dctx.copy_stream(compressed, dest) |
| 55 |
|
| 56 |
self.assertEqual(r, len(compressed.getvalue())) |
| 57 |
self.assertEqual(w, len(source.getvalue())) |
| 58 |
|
| 59 |
def test_read_write_size(self): |
| 60 |
source = CustomBytesIO(zstd.ZstdCompressor().compress(b"foobarfoobar")) |
| 61 |
|
| 62 |
dest = CustomBytesIO() |
| 63 |
dctx = zstd.ZstdDecompressor() |
| 64 |
r, w = dctx.copy_stream(source, dest, read_size=1, write_size=1) |
| 65 |
|
| 66 |
self.assertEqual(r, len(source.getvalue())) |
| 67 |
self.assertEqual(w, len(b"foobarfoobar")) |
| 68 |
self.assertEqual(source._read_count, len(source.getvalue()) + 1) |
| 69 |
self.assertEqual(dest._write_count, len(dest.getvalue())) |
| 70 |
|
| 71 |
def test_read_exception(self): |
| 72 |
source = CustomBytesIO(zstd.ZstdCompressor().compress(b"foo" * 1024)) |
| 73 |
dest = CustomBytesIO() |
| 74 |
|
| 75 |
source.read_exception = IOError("read") |
| 76 |
|
| 77 |
cctx = zstd.ZstdCompressor() |
| 78 |
|
| 79 |
with self.assertRaisesRegex(IOError, "read"): |
| 80 |
cctx.copy_stream(source, dest) |
| 81 |
|
| 82 |
def test_write_exception(self): |
| 83 |
source = CustomBytesIO(zstd.ZstdCompressor().compress(b"foo" * 1024)) |
| 84 |
dest = CustomBytesIO() |
| 85 |
|
| 86 |
dest.write_exception = IOError("write") |
| 87 |
|
| 88 |
cctx = zstd.ZstdCompressor() |
| 89 |
|
| 90 |
with self.assertRaisesRegex(IOError, "write"): |
| 91 |
cctx.copy_stream(source, dest) |