-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_dataframe_bridges.py
More file actions
419 lines (341 loc) · 14.8 KB
/
Copy pathtest_dataframe_bridges.py
File metadata and controls
419 lines (341 loc) · 14.8 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Tests for DataFrame bridges (Pandas, Polars, Dask)."""
from unittest.mock import patch
import pytest
from iterable.helpers.bridges import to_dask
from iterable.helpers.detect import open_iterable
class TestPandasBridge:
"""Test to_pandas() method."""
def test_to_pandas_single_dataframe(self):
"""Test converting entire iterable to single DataFrame."""
try:
import pandas as pd
except ImportError:
pytest.skip("pandas not installed")
with open_iterable("fixtures/2cols6rows.csv") as source:
df = source.to_pandas()
assert isinstance(df, pd.DataFrame)
assert len(df) == 6
assert list(df.columns) == ["id", "name"]
# CSV reads as string, so check string value
assert df.iloc[0]["id"] == "1"
assert df.iloc[0]["name"] == "John"
def test_to_pandas_chunked(self):
"""Test converting iterable to chunked DataFrames."""
try:
import pandas as pd
except ImportError:
pytest.skip("pandas not installed")
with open_iterable("fixtures/2cols6rows.csv") as source:
chunks = list(source.to_pandas(chunksize=2))
assert len(chunks) == 3 # 6 rows / 2 = 3 chunks
assert all(isinstance(chunk, pd.DataFrame) for chunk in chunks)
assert all(len(chunk) == 2 for chunk in chunks[:2])
assert len(chunks[2]) == 2 # Last chunk also has 2 rows
# Verify data integrity
total_rows = sum(len(chunk) for chunk in chunks)
assert total_rows == 6
def test_to_pandas_empty(self):
"""Test converting empty iterable."""
try:
import pandas as pd
except ImportError:
pytest.skip("pandas not installed")
# Create empty CSV file
with open("testdata/empty.csv", "w") as f:
f.write("id,name\n")
try:
with open_iterable("testdata/empty.csv") as source:
df = source.to_pandas()
assert isinstance(df, pd.DataFrame)
assert len(df) == 0
# Empty CSV files may not preserve column names when there are no rows
# This is acceptable behavior
assert len(df.columns) == 0 or list(df.columns) == ["id", "name"]
finally:
import os
if os.path.exists("testdata/empty.csv"):
os.remove("testdata/empty.csv")
def test_to_pandas_nested_data(self):
"""Test converting iterable with nested data structures."""
try:
import pandas as pd
except ImportError:
pytest.skip("pandas not installed")
# Create JSONL with nested data
with open("testdata/nested.jsonl", "w") as f:
f.write('{"id": 1, "data": {"nested": "value"}}\n')
f.write('{"id": 2, "data": {"nested": "value2"}}\n')
try:
with open_iterable("testdata/nested.jsonl") as source:
df = source.to_pandas()
assert isinstance(df, pd.DataFrame)
assert len(df) == 2
assert "id" in df.columns
assert "data" in df.columns
# Nested data should be preserved as dict
assert isinstance(df.iloc[0]["data"], dict)
finally:
import os
if os.path.exists("testdata/nested.jsonl"):
os.remove("testdata/nested.jsonl")
def test_to_pandas_import_error(self):
"""Test ImportError when pandas is not installed."""
with patch.dict("sys.modules", {"pandas": None}):
with open_iterable("fixtures/2cols6rows.csv") as source:
with pytest.raises(ImportError) as exc_info:
source.to_pandas()
assert "pandas is required" in str(exc_info.value)
assert "pip install pandas" in str(exc_info.value)
def test_to_pandas_various_formats(self):
"""Test to_pandas with various formats."""
try:
import pandas as pd
except ImportError:
pytest.skip("pandas not installed")
# Test with JSONL
with open("testdata/test.jsonl", "w") as f:
f.write('{"a": 1, "b": 2}\n')
f.write('{"a": 3, "b": 4}\n')
try:
with open_iterable("testdata/test.jsonl") as source:
df = source.to_pandas()
assert isinstance(df, pd.DataFrame)
assert len(df) == 2
finally:
import os
if os.path.exists("testdata/test.jsonl"):
os.remove("testdata/test.jsonl")
class TestPolarsBridge:
"""Test to_polars() method."""
def test_to_polars_single_dataframe(self):
"""Test converting entire iterable to single DataFrame."""
try:
import polars as pl
except ImportError:
pytest.skip("polars not installed")
with open_iterable("fixtures/2cols6rows.csv") as source:
df = source.to_polars()
assert isinstance(df, pl.DataFrame)
assert len(df) == 6
assert df.columns == ["id", "name"]
# CSV reads as string, so check string value
assert df[0, "id"] == "1"
assert df[0, "name"] == "John"
def test_to_polars_chunked(self):
"""Test converting iterable to chunked DataFrames."""
try:
import polars as pl
except ImportError:
pytest.skip("polars not installed")
with open_iterable("fixtures/2cols6rows.csv") as source:
chunks = list(source.to_polars(chunksize=2))
assert len(chunks) == 3 # 6 rows / 2 = 3 chunks
assert all(isinstance(chunk, pl.DataFrame) for chunk in chunks)
assert all(len(chunk) == 2 for chunk in chunks[:2])
assert len(chunks[2]) == 2 # Last chunk also has 2 rows
# Verify data integrity
total_rows = sum(len(chunk) for chunk in chunks)
assert total_rows == 6
def test_to_polars_empty(self):
"""Test converting empty iterable."""
try:
import polars as pl
except ImportError:
pytest.skip("polars not installed")
# Create empty CSV file
with open("testdata/empty.csv", "w") as f:
f.write("id,name\n")
try:
with open_iterable("testdata/empty.csv") as source:
df = source.to_polars()
assert isinstance(df, pl.DataFrame)
assert len(df) == 0
# Empty CSV files may not preserve column names when there are no rows
# This is acceptable behavior
assert len(df.columns) == 0 or df.columns == ["id", "name"]
finally:
import os
if os.path.exists("testdata/empty.csv"):
os.remove("testdata/empty.csv")
def test_to_polars_nested_data(self):
"""Test converting iterable with nested data structures."""
try:
import polars as pl
except ImportError:
pytest.skip("polars not installed")
# Create JSONL with nested data
with open("testdata/nested.jsonl", "w") as f:
f.write('{"id": 1, "data": {"nested": "value"}}\n')
f.write('{"id": 2, "data": {"nested": "value2"}}\n')
try:
with open_iterable("testdata/nested.jsonl") as source:
df = source.to_polars()
assert isinstance(df, pl.DataFrame)
assert len(df) == 2
assert "id" in df.columns
assert "data" in df.columns
finally:
import os
if os.path.exists("testdata/nested.jsonl"):
os.remove("testdata/nested.jsonl")
def test_to_polars_import_error(self):
"""Test ImportError when polars is not installed."""
with patch.dict("sys.modules", {"polars": None}):
with open_iterable("fixtures/2cols6rows.csv") as source:
with pytest.raises(ImportError) as exc_info:
source.to_polars()
assert "polars is required" in str(exc_info.value)
assert "pip install polars" in str(exc_info.value)
def test_to_polars_various_formats(self):
"""Test to_polars with various formats."""
try:
import polars as pl
except ImportError:
pytest.skip("polars not installed")
# Test with JSONL
with open("testdata/test.jsonl", "w") as f:
f.write('{"a": 1, "b": 2}\n')
f.write('{"a": 3, "b": 4}\n')
try:
with open_iterable("testdata/test.jsonl") as source:
df = source.to_polars()
assert isinstance(df, pl.DataFrame)
assert len(df) == 2
finally:
import os
if os.path.exists("testdata/test.jsonl"):
os.remove("testdata/test.jsonl")
class TestDaskBridge:
"""Test to_dask() method and helper function."""
def test_to_dask_single_file(self):
"""Test converting single file iterable to Dask DataFrame."""
try:
import dask.dataframe as dd
except ImportError:
pytest.skip("dask not installed")
with open_iterable("fixtures/2cols6rows.csv") as source:
ddf = source.to_dask()
assert isinstance(ddf, dd.DataFrame)
# Compute to verify data
df = ddf.compute()
assert len(df) == 6
assert list(df.columns) == ["id", "name"]
def test_to_dask_empty(self):
"""Test converting empty iterable."""
try:
import dask.dataframe as dd
except ImportError:
pytest.skip("dask not installed")
# Create empty CSV file
with open("testdata/empty.csv", "w") as f:
f.write("id,name\n")
try:
with open_iterable("testdata/empty.csv") as source:
ddf = source.to_dask()
assert isinstance(ddf, dd.DataFrame)
df = ddf.compute()
assert len(df) == 0
# Empty CSV files may not preserve column names when there are no rows
# This is acceptable behavior
assert len(df.columns) == 0 or list(df.columns) == ["id", "name"]
finally:
import os
if os.path.exists("testdata/empty.csv"):
os.remove("testdata/empty.csv")
def test_to_dask_import_error(self):
"""Test ImportError when dask is not installed."""
with patch.dict("sys.modules", {"dask": None}):
with open_iterable("fixtures/2cols6rows.csv") as source:
with pytest.raises(ImportError) as exc_info:
source.to_dask()
assert "dask[dataframe] is required" in str(exc_info.value)
assert "pip install 'dask[dataframe]'" in str(exc_info.value)
def test_to_dask_multi_file_helper(self):
"""Test to_dask() helper function with multiple files."""
try:
import dask.dataframe as dd
except ImportError:
pytest.skip("dask not installed")
# Create test files
with open("testdata/file1.csv", "w") as f:
f.write("id,name\n")
f.write("1,Alice\n")
f.write("2,Bob\n")
with open("testdata/file2.jsonl", "w") as f:
f.write('{"id": 3, "name": "Charlie"}\n')
f.write('{"id": 4, "name": "Diana"}\n')
try:
ddf = to_dask(["testdata/file1.csv", "testdata/file2.jsonl"])
assert isinstance(ddf, dd.DataFrame)
df = ddf.compute()
assert len(df) == 4
assert list(df.columns) == ["id", "name"]
# Verify data from both files
assert df.iloc[0]["name"] == "Alice"
assert df.iloc[3]["name"] == "Diana"
finally:
import os
for fname in ["testdata/file1.csv", "testdata/file2.jsonl"]:
if os.path.exists(fname):
os.remove(fname)
def test_to_dask_multi_file_single_string(self):
"""Test to_dask() helper with single file as string."""
try:
import dask.dataframe as dd
except ImportError:
pytest.skip("dask not installed")
with open("testdata/single.csv", "w") as f:
f.write("id,name\n")
f.write("1,Test\n")
try:
ddf = to_dask("testdata/single.csv")
assert isinstance(ddf, dd.DataFrame)
df = ddf.compute()
assert len(df) == 1
finally:
import os
if os.path.exists("testdata/single.csv"):
os.remove("testdata/single.csv")
def test_to_dask_multi_file_empty_list(self):
"""Test to_dask() helper with empty file list."""
# This test doesn't require dask to be installed - it should fail before import
try:
with pytest.raises(ValueError) as exc_info:
to_dask([])
assert "files list cannot be empty" in str(exc_info.value)
except ImportError:
# If dask is not installed, the function will raise ImportError before checking empty list
# This is acceptable behavior
pass
def test_to_dask_multi_file_invalid_type(self):
"""Test to_dask() helper with invalid file type."""
# This test doesn't require dask to be installed - it should fail before import
try:
with pytest.raises(ValueError) as exc_info:
to_dask(123) # Invalid type
assert "files must be a string or list of strings" in str(exc_info.value)
except ImportError:
# If dask is not installed, the function will raise ImportError before checking type
# This is acceptable behavior
pass
def test_to_dask_multi_file_all_empty(self):
"""Test to_dask() helper when all files are empty."""
try:
import dask.dataframe as dd
except ImportError:
pytest.skip("dask not installed")
# Create empty files
with open("testdata/empty1.csv", "w") as f:
f.write("id,name\n")
with open("testdata/empty2.csv", "w") as f:
f.write("id,name\n")
try:
ddf = to_dask(["testdata/empty1.csv", "testdata/empty2.csv"])
assert isinstance(ddf, dd.DataFrame)
df = ddf.compute()
assert len(df) == 0
finally:
import os
for fname in ["testdata/empty1.csv", "testdata/empty2.csv"]:
if os.path.exists(fname):
os.remove(fname)