File: test_open.py

package info (click to toggle)
python-zstandard 0.23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,936 kB
  • sloc: ansic: 41,411; python: 8,665; makefile: 22; sh: 14
file content (120 lines) | stat: -rw-r--r-- 3,401 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import io
import os
import tempfile
import unittest

import zstandard as zstd


class TestOpen(unittest.TestCase):
    def test_write_binary_fileobj(self):
        buffer = io.BytesIO()

        fh = zstd.open(buffer, "wb")
        fh.write(b"foo" * 1024)
        self.assertFalse(fh.closed)
        self.assertFalse(buffer.closed)

        fh.close()
        self.assertTrue(fh.closed)
        self.assertFalse(buffer.closed)

    def test_write_binary_filename(self):
        with tempfile.TemporaryDirectory() as td:
            p = os.path.join(td, "testfile")

            fh = zstd.open(p, "wb")
            fh.write(b"foo" * 1024)
            self.assertFalse(fh.closed)

            fh.close()
            self.assertTrue(fh.closed)

    def test_write_text_fileobj(self):
        buffer = io.BytesIO()

        fh = zstd.open(buffer, "w")
        fh.write("foo")
        fh.write("foo")

    def test_write_text_filename(self):
        with tempfile.TemporaryDirectory() as td:
            p = os.path.join(td, "testfile")

            fh = zstd.open(p, "w")
            self.assertIsInstance(fh, io.TextIOWrapper)

            fh.write("foo\n")
            fh.write("bar\n")
            fh.close()
            self.assertTrue(fh.closed)

            with zstd.open(p, "r") as fh:
                self.assertEqual(fh.read(), "foo\nbar\n")

    def test_read_binary_fileobj(self):
        cctx = zstd.ZstdCompressor()
        buffer = io.BytesIO(cctx.compress(b"foo" * 1024))

        fh = zstd.open(buffer, "rb")

        self.assertEqual(fh.read(6), b"foofoo")
        self.assertFalse(fh.closed)
        self.assertFalse(buffer.closed)

        fh.close()
        self.assertTrue(fh.closed)
        self.assertFalse(buffer.closed)

        buffer = io.BytesIO(cctx.compress(b"foo" * 1024))

        with zstd.open(buffer, "rb", closefd=True) as fh:
            self.assertEqual(fh.read(), b"foo" * 1024)

        self.assertTrue(fh.closed)
        self.assertTrue(buffer.closed)

    def test_read_binary_filename(self):
        with tempfile.TemporaryDirectory() as td:
            p = os.path.join(td, "testfile")
            with open(p, "wb") as fh:
                cctx = zstd.ZstdCompressor()
                fh.write(cctx.compress(b"foo" * 1024))

            fh = zstd.open(p, "rb")

            self.assertEqual(fh.read(6), b"foofoo")
            self.assertEqual(len(fh.read()), 1024 * 3 - 6)
            self.assertFalse(fh.closed)

            fh.close()
            self.assertTrue(fh.closed)

    def test_read_text_fileobj(self):
        cctx = zstd.ZstdCompressor()
        buffer = io.BytesIO(cctx.compress(b"foo\n" * 1024))

        fh = zstd.open(buffer, "r")
        self.assertIsInstance(fh, io.TextIOWrapper)

        self.assertEqual(fh.readline(), "foo\n")

        fh.close()
        self.assertTrue(fh.closed)
        self.assertFalse(buffer.closed)

    def test_read_text_filename(self):
        with tempfile.TemporaryDirectory() as td:
            p = os.path.join(td, "testfile")
            cctx = zstd.ZstdCompressor()
            with open(p, "wb") as fh:
                fh.write(cctx.compress(b"foo\n" * 1024))

            fh = zstd.open(p, "r")

            self.assertEqual(fh.read(4), "foo\n")
            self.assertEqual(fh.readline(), "foo\n")
            self.assertFalse(fh.closed)

            fh.close()
            self.assertTrue(fh.closed)