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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
from functools import partial
import pytest
from snakeoil import compression, data_source
class TestDataSource:
supports_mutable = True
@pytest.fixture(autouse=True)
def _setup(self, tmp_path):
self.dir = tmp_path
def get_obj(self, data="foonani", mutable=False):
return data_source.data_source(data, mutable=mutable)
def _test_fileobj_ro(self, attr, converter=str):
obj = self.get_obj()
# ensure that requesting mutable from an immutable isn't allowed
with pytest.raises(TypeError):
getattr(obj, attr)(True)
with getattr(obj, attr)() as handle:
assert handle.read() == converter("foonani")
with pytest.raises(handle.exceptions):
handle.write(converter("monkey"))
def _test_fileobj_wr(self, attr, converter=str):
obj = self.get_obj(mutable=True)
handle_f = getattr(obj, attr)
with handle_f() as f:
assert f.read() == converter("foonani")
with handle_f(True) as f:
f.write(converter("dar"))
with handle_f(True) as f:
assert f.read() == converter("darnani")
def test_text_fileobj(self):
self._test_fileobj_ro("text_fileobj", str)
if self.supports_mutable:
self._test_fileobj_wr("text_fileobj", str)
def test_bytes_fileobj(self):
self._test_fileobj_ro("bytes_fileobj", lambda s: s.encode())
if self.supports_mutable:
self._test_fileobj_wr("bytes_fileobj", lambda s: s.encode())
def assertContents(self, reader, writer):
with reader.bytes_fileobj() as reader_f:
reader_data = reader_f.read()
with writer.bytes_fileobj() as writer_f:
writer_data = writer_f.read()
assert reader_data == writer_data
def _mk_data(self, size=(100000)):
return "".join(str(x % 10) for x in range(size))
def test_transfer_to_data_source(self):
data = self._mk_data()
reader = self.get_obj(data=data)
if self.supports_mutable:
writer = self.get_obj(data="", mutable=True)
else:
writer = data_source.data_source("", mutable=True)
reader.transfer_to_data_source(writer)
self.assertContents(reader, writer)
def test_transfer_to_path(self, tmp_path):
data = self._mk_data()
reader = self.get_obj(data=data)
if isinstance(reader, data_source.bz2_source):
writer = data_source.bz2_source(tmp_path / "transfer_to_path", mutable=True)
else:
writer = data_source.local_source(
tmp_path / "transfer_to_path", mutable=True
)
reader.transfer_to_path(writer.path)
self.assertContents(reader, writer)
def test_transfer_data_between_files(self):
data = self._mk_data()
reader = self.get_obj(data=data)
if self.supports_mutable:
writer = self.get_obj(data="", mutable=True)
else:
writer = data_source.data_source("", mutable=True)
with reader.bytes_fileobj() as reader_f, writer.bytes_fileobj(True) as writer_f:
data_source.transfer_between_files(reader_f, writer_f)
self.assertContents(reader, writer)
class TestLocalSource(TestDataSource):
def get_obj(self, data="foonani", mutable=False, test_creation=False):
self.fp = self.dir / "localsource.test"
if not test_creation:
mode = None
if isinstance(data, bytes):
mode = "wb"
elif mode is None:
mode = "w"
with open(self.fp, mode) as f:
f.write(data)
return data_source.local_source(self.fp, mutable=mutable)
def test_bytes_fileobj(self):
data = b"foonani\xf2"
obj = self.get_obj(data=data)
# this will blow up if tries to ascii decode it.
with obj.bytes_fileobj() as f:
assert f.read() == data
def test_bytes_fileobj_create(self):
data = b"foonani\xf2"
obj = self.get_obj(test_creation=True, mutable=True)
# this will blow up if tries to ascii decode it.
with obj.bytes_fileobj(True) as f:
assert f.read() == b""
f.write(data)
with obj.bytes_fileobj() as f:
assert f.read() == data
class TestBz2Source(TestDataSource):
def get_obj(self, data="foonani", mutable=False, test_creation=False):
self.fp = self.dir / "bz2source.test.bz2"
if not test_creation:
if isinstance(data, str):
data = data.encode()
with open(self.fp, "wb") as f:
f.write(compression.compress_data("bzip2", data))
return data_source.bz2_source(self.fp, mutable=mutable)
def test_bytes_fileobj(self):
data = b"foonani\xf2"
obj = self.get_obj(data=data)
# this will blow up if tries to ascii decode it.
with obj.bytes_fileobj() as f:
assert f.read() == data
class Test_invokable_data_source(TestDataSource):
supports_mutable = False
def get_obj(self, data="foonani", mutable=False):
if isinstance(data, str):
data = data.encode("utf8")
return data_source.invokable_data_source(partial(self._get_data, data))
@staticmethod
def _get_data(data, is_text=False):
if is_text:
data = data.decode("utf8")
return data_source.text_ro_StringIO(data)
return data_source.bytes_ro_StringIO(data)
class Test_invokable_data_source_wrapper_text(Test_invokable_data_source):
supports_mutable = False
text_mode = True
def get_obj(self, mutable=False, data="foonani"):
return data_source.invokable_data_source.wrap_function(
partial(self._get_data, data), self.text_mode
)
def _get_data(self, data="foonani"):
if isinstance(data, str):
if not self.text_mode:
return data.encode("utf8")
elif self.text_mode:
return data.encode("utf8")
return data
class Test_invokable_data_source_wrapper_bytes(Test_invokable_data_source_wrapper_text):
text_mode = False
|