-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_core.py
More file actions
275 lines (197 loc) · 11.2 KB
/
test_core.py
File metadata and controls
275 lines (197 loc) · 11.2 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import pytest
from conftest import SimpleDataStream, SimpleParams
from contraqctor import _typing
from contraqctor.contract.base import DataStreamCollection
class TestDataStream:
"""Tests for the DataStream class."""
def test_creation(self, text_file):
"""Test creating a DataStream."""
stream = SimpleDataStream(name="test", description="Test stream", reader_params=SimpleParams(path=text_file))
assert stream.name == "test"
assert stream.description == "Test stream"
assert not stream.is_collection
assert stream.parent is None
assert not stream.has_data
with pytest.raises(ValueError):
# Accessing data before loading should raise ValueError
_ = stream.data
def test_load(self, text_file):
"""Test loading data from a DataStream."""
stream = SimpleDataStream(name="test", reader_params=SimpleParams(path=text_file))
stream.load()
assert stream.has_data
assert stream.data == "Test content"
def test_read(self, text_file):
"""Test reading data without loading it."""
stream = SimpleDataStream(name="test", reader_params=SimpleParams(path=text_file))
data = stream.read()
assert data == "Test content"
assert not stream.has_data # read() doesn't store the data
def test_bind_reader_params(self, text_file):
"""Test post-instantiating binding of reader parameters."""
stream = SimpleDataStream(name="test")
assert _typing.is_unset(stream.reader_params)
stream.bind_reader_params(SimpleParams(path=text_file))
assert not _typing.is_unset(stream.reader_params)
with pytest.raises(ValueError):
# Binding params again should raise ValueError
stream.bind_reader_params(SimpleParams(path=text_file))
def test_at_not_implemented(self):
"""Test that at() method raises NotImplementedError."""
stream = SimpleDataStream(name="test")
with pytest.raises(NotImplementedError):
stream.at("key")
def test_resolved_name(self):
"""Test resolved_name property."""
stream = SimpleDataStream(name="test")
assert stream.resolved_name == "test"
# Name with prohibited characters should raise an error
with pytest.raises(ValueError):
SimpleDataStream(name="test::invalid")
def test_invalid_name(self, text_file):
"""Test creating a DataStream with an invalid name."""
with pytest.raises(ValueError, match="Name cannot contain '::' character."):
SimpleDataStream(
name="test::invalid", description="Test stream", reader_params=SimpleParams(path=text_file)
)
def test_clear_data(self, text_file):
"""Test clearing loaded data."""
stream = SimpleDataStream(name="test", reader_params=SimpleParams(path=text_file))
stream.load()
assert stream.has_data
stream.clear()
assert not stream.has_data
with pytest.raises(ValueError):
_ = stream.data # Accessing data after clearing should raise ValueError
class TestDataStreamCollection:
"""Tests for the DataStreamCollection anonymous class."""
def test_creation(self, text_file):
"""Test creating a DataStreamCollection."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(
name="collection", description="Test collection", data_streams=[stream1, stream2]
)
assert collection.name == "collection"
assert collection.description == "Test collection"
assert collection.is_collection
assert collection.has_data # data_streams are set directly
assert len(collection.data) == 2
def test_at_method(self, text_file):
"""Test accessing streams with at() method."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
assert collection.at("stream1") == stream1
assert collection.at("stream2") == stream2
with pytest.raises(KeyError):
collection.at("nonexistent")
def test_indexing(self, text_file):
"""Test accessing streams with indexing."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
assert collection["stream1"] == stream1
assert collection["stream2"] == stream2
with pytest.raises(KeyError):
collection["nonexistent"]
def test_add_stream(self, text_file):
"""Test adding a stream to a collection."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(name="collection", data_streams=[stream1])
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection.add_stream(stream2)
assert len(collection.data) == 2
assert collection.at("stream2") == stream2
# Adding a stream with an existing name should raise KeyError
stream3 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
with pytest.raises(KeyError):
collection.add_stream(stream3)
def test_remove_stream(self, text_file):
"""Test removing a stream from a collection."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
collection.remove_stream("stream1")
assert len(collection.data) == 1
with pytest.raises(KeyError):
collection.at("stream1")
with pytest.raises(KeyError):
collection.remove_stream("nonexistent")
def test_parent_references(self, text_file):
"""Test that parent references are properly set."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
assert stream1.parent == collection
assert stream2.parent == collection
def test_iter_streams(self, text_file):
"""Test iterating through data streams."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
inner_collection = DataStreamCollection(name="inner", data_streams=[stream2])
outer_collection = DataStreamCollection(name="outer", data_streams=[stream1, inner_collection])
streams = [x for x in outer_collection.iter_all()]
assert len(streams) == 3 # stream1, stream2, and inner_collection
assert stream1 in streams
assert stream2 in streams
assert inner_collection in streams
streams = [x for x in outer_collection]
assert len(streams) == 2 # stream1, inner_collection
assert stream1 in streams
assert stream2 not in streams
assert inner_collection in streams
def test_duplicate_names(self, text_file):
"""Test that duplicate names raise an error."""
stream1 = SimpleDataStream(name="duplicate", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="duplicate", reader_params=SimpleParams(path=text_file))
with pytest.raises(ValueError):
DataStreamCollection(name="collection", data_streams=[stream1, stream2])
def test_resolved_name(self, text_file):
"""Test resolved_name property in nested collections."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
inner_collection = DataStreamCollection(name="inner", data_streams=[stream2])
outer_collection = DataStreamCollection(name="outer", data_streams=[stream1, inner_collection]) # noqa: F841
assert stream1.resolved_name == "outer::stream1"
assert inner_collection.resolved_name == "outer::inner"
assert stream2.resolved_name == "outer::inner::stream2"
level3 = SimpleDataStream(name="level3", reader_params=SimpleParams(path=text_file))
level2 = DataStreamCollection(name="level2", data_streams=[level3])
level1 = DataStreamCollection(name="level1", data_streams=[level2])
root = DataStreamCollection(name="root", data_streams=[level1]) # noqa: F841
assert level3.resolved_name == "root::level1::level2::level3"
class TestLoadAllChildren:
"""Tests for loading all children datastreams recursively."""
def test_load_all_success(self, text_file):
"""Test load_all with successful loads."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=text_file))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
result = collection.load_all()
assert result.collect_errors() == []
assert stream1.has_data
assert stream2.has_data
def test_load_all_with_exception(self, text_file, temp_dir):
"""Test load_all with an exception."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
nonexistent_path = temp_dir / "nonexistent.txt"
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=nonexistent_path))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
result = collection.load_all()
errors = result.collect_errors()
assert len(errors) == 1
assert errors[0].data_stream == stream2
assert isinstance(errors[0].exception, FileNotFoundError)
assert stream1.has_data
assert not stream2.has_data
with pytest.raises(FileNotFoundError):
raise errors[0].exception
def test_load_all_strict(self, text_file, temp_dir):
"""Test load_all with strict=True."""
stream1 = SimpleDataStream(name="stream1", reader_params=SimpleParams(path=text_file))
nonexistent_path = temp_dir / "nonexistent.txt"
stream2 = SimpleDataStream(name="stream2", reader_params=SimpleParams(path=nonexistent_path))
collection = DataStreamCollection(name="collection", data_streams=[stream1, stream2])
with pytest.raises(FileNotFoundError):
collection.load_all(strict=True)