-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_bulk_operations_optimization.py
More file actions
337 lines (262 loc) · 10.3 KB
/
Copy pathtest_bulk_operations_optimization.py
File metadata and controls
337 lines (262 loc) · 10.3 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""Tests for optimized bulk operations in various formats.
This test suite verifies that bulk operation optimizations work correctly
and maintain backward compatibility while providing performance improvements.
"""
import pytest
@pytest.mark.skipif(not hasattr(pytest, "importorskip") or True, reason="Requires optional dependencies")
class TestParquetBulkOptimization:
"""Test optimized Parquet bulk operations."""
def test_parquet_batch_caching(self, tmp_path):
"""Test that Parquet read_bulk() uses batch caching correctly."""
try:
import pyarrow as pa
import pyarrow.parquet as pq
except ImportError:
pytest.skip("PyArrow not available")
# Create test Parquet file with multiple batches
test_file = tmp_path / "test.parquet"
table = pa.table(
{
"col1": list(range(100)), # 100 rows
"col2": [f"value_{i}" for i in range(100)],
}
)
pq.write_table(table, test_file, row_group_size=30) # Creates multiple row groups
from iterable.datatypes.parquet import ParquetIterable
# Read in bulk - should use batch caching
iterable = ParquetIterable(str(test_file), batch_size=20)
chunk1 = iterable.read_bulk(15) # Read 15 rows (less than batch size)
assert len(chunk1) == 15
assert chunk1[0]["col1"] == 0
assert chunk1[14]["col1"] == 14
# Read more - should use cached batch
chunk2 = iterable.read_bulk(10) # Should use remaining 5 from batch + 5 from next batch
assert len(chunk2) == 10
assert chunk2[0]["col1"] == 15
assert chunk2[9]["col1"] == 24
# Verify position is correct
assert iterable.pos == 25
iterable.close()
def test_parquet_bulk_reads_all_rows(self, tmp_path):
"""Test that Parquet read_bulk() reads all rows correctly."""
try:
import pyarrow as pa
import pyarrow.parquet as pq
except ImportError:
pytest.skip("PyArrow not available")
# Create test Parquet file
test_file = tmp_path / "test.parquet"
table = pa.table({"col1": list(range(50)), "col2": [f"val_{i}" for i in range(50)]})
pq.write_table(table, test_file)
from iterable.datatypes.parquet import ParquetIterable
iterable = ParquetIterable(str(test_file))
all_rows = []
while True:
chunk = iterable.read_bulk(10)
if not chunk:
break
all_rows.extend(chunk)
assert len(all_rows) == 50
assert all_rows[0]["col1"] == 0
assert all_rows[49]["col1"] == 49
iterable.close()
@pytest.mark.skipif(not hasattr(pytest, "importorskip") or True, reason="Requires optional dependencies")
class TestArrowBulkOptimization:
"""Test optimized Arrow bulk operations."""
def test_arrow_batch_caching(self, tmp_path):
"""Test that Arrow read_bulk() uses batch caching correctly."""
try:
import pyarrow as pa
import pyarrow.feather as feather
except ImportError:
pytest.skip("PyArrow not available")
# Create test Arrow file
test_file = tmp_path / "test.arrow"
table = pa.table({"col1": list(range(100)), "col2": [f"value_{i}" for i in range(100)]})
feather.write_feather(table, test_file)
from iterable.datatypes.arrow import ArrowIterable
iterable = ArrowIterable(str(test_file), batch_size=20)
chunk1 = iterable.read_bulk(15) # Read 15 rows
assert len(chunk1) == 15
assert chunk1[0]["col1"] == 0
# Read more - should use cached batch
chunk2 = iterable.read_bulk(10)
assert len(chunk2) == 10
assert chunk2[0]["col1"] == 15
assert iterable.pos == 25
iterable.close()
def test_arrow_bulk_reads_all_rows(self, tmp_path):
"""Test that Arrow read_bulk() reads all rows correctly."""
try:
import pyarrow as pa
import pyarrow.feather as feather
except ImportError:
pytest.skip("PyArrow not available")
test_file = tmp_path / "test.arrow"
table = pa.table({"col1": list(range(50)), "col2": [f"val_{i}" for i in range(50)]})
feather.write_feather(table, test_file)
from iterable.datatypes.arrow import ArrowIterable
iterable = ArrowIterable(str(test_file))
all_rows = []
while True:
chunk = iterable.read_bulk(10)
if not chunk:
break
all_rows.extend(chunk)
assert len(all_rows) == 50
iterable.close()
@pytest.mark.skipif(not hasattr(pytest, "importorskip") or True, reason="Requires optional dependencies")
class TestARFFBulkOptimization:
"""Test optimized ARFF bulk operations."""
def test_arff_slicing_optimization(self, tmp_path):
"""Test that ARFF read_bulk() uses slicing correctly."""
try:
import arff # noqa: F401
except ImportError:
pytest.skip("ARFF library not available")
# Create test ARFF file
test_file = tmp_path / "test.arff"
arff_content = """@relation test
@attribute col1 numeric
@attribute col2 string
@data
1,value1
2,value2
3,value3
4,value4
5,value5
"""
with open(test_file, "w") as f:
f.write(arff_content)
from iterable.datatypes.arff import ARFFIterable
iterable = ARFFIterable(str(test_file))
chunk = iterable.read_bulk(3)
assert len(chunk) == 3
assert chunk[0]["col1"] == 1
assert chunk[2]["col1"] == 3
# Read more
chunk2 = iterable.read_bulk(2)
assert len(chunk2) == 2
assert chunk2[0]["col1"] == 4
assert chunk2[1]["col1"] == 5
# Should be at end
chunk3 = iterable.read_bulk(10)
assert len(chunk3) == 0
iterable.close()
def test_arff_bulk_position_tracking(self, tmp_path):
"""Test that ARFF read_bulk() correctly tracks position."""
try:
import arff # noqa: F401
except ImportError:
pytest.skip("ARFF library not available")
test_file = tmp_path / "test.arff"
arff_content = """@relation test
@attribute col1 numeric
@data
1
2
3
4
5
"""
with open(test_file, "w") as f:
f.write(arff_content)
from iterable.datatypes.arff import ARFFIterable
iterable = ARFFIterable(str(test_file))
assert iterable.pos == 0
_ = iterable.read_bulk(3)
assert iterable.pos == 3
_ = iterable.read_bulk(2)
assert iterable.pos == 5
iterable.close()
@pytest.mark.skipif(not hasattr(pytest, "importorskip") or True, reason="Requires optional dependencies")
class TestTOMLBulkOptimization:
"""Test optimized TOML bulk operations."""
def test_toml_slicing_optimization(self, tmp_path):
"""Test that TOML read_bulk() uses slicing correctly."""
try:
import tomli # noqa: F401
except ImportError:
try:
import toml # noqa: F401
except ImportError:
pytest.skip("TOML library not available")
# Create test TOML file
test_file = tmp_path / "test.toml"
toml_content = """[items]
[[items]]
key1 = "value1"
[[items]]
key2 = "value2"
[[items]]
key3 = "value3"
"""
with open(test_file, "w") as f:
f.write(toml_content)
from iterable.datatypes.toml import TOMLIterable
iterable = TOMLIterable(str(test_file))
chunk = iterable.read_bulk(2)
assert len(chunk) == 2
assert "key1" in chunk[0] or "_table" in chunk[0]
# Read more
chunk2 = iterable.read_bulk(1)
assert len(chunk2) == 1
iterable.close()
def test_toml_bulk_position_tracking(self, tmp_path):
"""Test that TOML read_bulk() correctly tracks position."""
try:
import tomli # noqa: F401
except ImportError:
try:
import toml # noqa: F401
except ImportError:
pytest.skip("TOML library not available")
test_file = tmp_path / "test.toml"
toml_content = """[items]
[[items]]
key1 = "value1"
[[items]]
key2 = "value2"
"""
with open(test_file, "w") as f:
f.write(toml_content)
from iterable.datatypes.toml import TOMLIterable
iterable = TOMLIterable(str(test_file))
assert iterable.pos == 0
iterable.read_bulk(1)
assert iterable.pos == 1
iterable.read_bulk(1)
assert iterable.pos == 2
iterable.close()
class TestBulkOperationsBackwardCompatibility:
"""Test that bulk operation optimizations maintain backward compatibility."""
def test_bulk_operations_return_lists(self, tmp_path):
"""Test that optimized bulk operations still return lists."""
# Test with CSV (already optimized)
test_file = tmp_path / "test.csv"
test_file.write_text("col1,col2\nval1,val2\nval3,val4\n")
from iterable.helpers.detect import open_iterable
with open_iterable(str(test_file)) as source:
chunk = source.read_bulk(2)
assert isinstance(chunk, list)
assert len(chunk) == 2
def test_bulk_operations_respect_num_parameter(self, tmp_path):
"""Test that bulk operations respect the num parameter."""
test_file = tmp_path / "test.csv"
test_file.write_text("col1,col2\n" + "\n".join([f"val{i},val{i}" for i in range(10)]))
from iterable.helpers.detect import open_iterable
with open_iterable(str(test_file)) as source:
chunk = source.read_bulk(5)
assert len(chunk) == 5
chunk2 = source.read_bulk(3)
assert len(chunk2) == 3
def test_bulk_operations_handle_end_of_file(self, tmp_path):
"""Test that bulk operations handle end of file correctly."""
test_file = tmp_path / "test.csv"
test_file.write_text("col1,col2\nval1,val2\n")
from iterable.helpers.detect import open_iterable
with open_iterable(str(test_file)) as source:
chunk1 = source.read_bulk(10) # Request more than available
assert len(chunk1) == 1
chunk2 = source.read_bulk(10) # Should return empty list
assert len(chunk2) == 0