forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data_split.py
More file actions
291 lines (217 loc) · 9.03 KB
/
Copy pathtest_data_split.py
File metadata and controls
291 lines (217 loc) · 9.03 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
"""Tests for soup data split — train/val/test splitting."""
import json
from pathlib import Path
def _make_jsonl(path: Path, count: int, field: str = "category"):
"""Create a JSONL file with count rows, optional category field."""
lines = []
for idx in range(count):
row = {"text": f"sample {idx}", field: f"cat_{idx % 3}"}
lines.append(json.dumps(row))
path.write_text("\n".join(lines), encoding="utf-8")
def _read_jsonl(path: Path) -> list:
"""Read JSONL file and return list of dicts."""
rows = []
with open(path, encoding="utf-8") as fh:
for line in fh:
line = line.strip()
if line:
rows.append(json.loads(line))
return rows
# ─── CLI Tests ────────────────────────────────────────────────────────────
class TestDataSplitCLI:
"""Test soup data split command via CLI."""
def test_split_in_help(self):
"""Data help should mention split subcommand."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(app, ["data", "--help"])
assert "split" in result.output.lower()
def test_split_basic_ratio(self, tmp_path):
"""Basic split with --val 20 should produce two files."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 100)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "20"],
)
assert result.exit_code == 0
train_file = tmp_path / "data_train.jsonl"
val_file = tmp_path / "data_val.jsonl"
assert train_file.exists()
assert val_file.exists()
train_rows = _read_jsonl(train_file)
val_rows = _read_jsonl(val_file)
assert len(train_rows) + len(val_rows) == 100
assert len(val_rows) == 20
def test_split_train_val_test(self, tmp_path):
"""Split with --val 10 --test 10 should produce three files."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 100)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "10", "--test", "10"],
)
assert result.exit_code == 0
train_file = tmp_path / "data_train.jsonl"
val_file = tmp_path / "data_val.jsonl"
test_file = tmp_path / "data_test.jsonl"
assert train_file.exists()
assert val_file.exists()
assert test_file.exists()
train_rows = _read_jsonl(train_file)
val_rows = _read_jsonl(val_file)
test_rows = _read_jsonl(test_file)
assert len(train_rows) + len(val_rows) + len(test_rows) == 100
assert len(val_rows) == 10
assert len(test_rows) == 10
def test_split_absolute_counts(self, tmp_path):
"""Split with --absolute should use absolute counts."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 100)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "15", "--absolute"],
)
assert result.exit_code == 0
val_file = tmp_path / "data_val.jsonl"
val_rows = _read_jsonl(val_file)
assert len(val_rows) == 15
def test_split_seed_reproducible(self, tmp_path):
"""Same seed should produce the same split."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 50)
runner = CliRunner()
# First run
result1 = runner.invoke(
app,
["data", "split", str(data_file), "--val", "20", "--seed", "42"],
)
assert result1.exit_code == 0
val1 = _read_jsonl(tmp_path / "data_val.jsonl")
# Second run (overwrite)
result2 = runner.invoke(
app,
["data", "split", str(data_file), "--val", "20", "--seed", "42"],
)
assert result2.exit_code == 0
val2 = _read_jsonl(tmp_path / "data_val.jsonl")
assert val1 == val2
def test_split_file_not_found(self, tmp_path):
"""Should error if input file doesn't exist."""
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(tmp_path / "nope.jsonl"), "--val", "10"],
)
assert result.exit_code != 0
def test_split_no_val_or_test(self, tmp_path):
"""Should error if neither --val nor --test is specified."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 50)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file)],
)
assert result.exit_code != 0
def test_split_empty_dataset(self, tmp_path):
"""Should error on empty dataset."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
data_file.write_text("", encoding="utf-8")
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "10"],
)
assert result.exit_code != 0
# ─── Edge Case Tests ─────────────────────────────────────────────────────
class TestDataSplitEdgeCases:
"""Test edge cases for data split."""
def test_split_val_pct_too_large(self, tmp_path):
"""val=90 should work but leave 10% for train."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 100)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "90"],
)
assert result.exit_code == 0
train_rows = _read_jsonl(tmp_path / "data_train.jsonl")
assert len(train_rows) == 10
def test_split_stratified(self, tmp_path):
"""Stratified split should preserve category distribution."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 90) # 30 per category (cat_0, cat_1, cat_2)
runner = CliRunner()
result = runner.invoke(
app,
[
"data", "split", str(data_file),
"--val", "30", "--stratify", "category",
],
)
assert result.exit_code == 0
val_rows = _read_jsonl(tmp_path / "data_val.jsonl")
# Each category should have ~10 samples (30% of 30)
categories = {}
for row in val_rows:
cat = row.get("category", "unknown")
categories[cat] = categories.get(cat, 0) + 1
# Each category should be represented
assert len(categories) == 3
# Should be roughly equal (~9-10 each from 30, rounding allowed)
for count in categories.values():
assert 8 <= count <= 11
def test_split_absolute_val_exceeds_dataset(self, tmp_path):
"""Should error if absolute val count exceeds dataset size."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 10)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "20", "--absolute"],
)
assert result.exit_code != 0
# ─── Security Tests ──────────────────────────────────────────────────────
class TestDataSplitSecurity:
"""Security tests for data split."""
def test_output_files_in_same_directory(self, tmp_path):
"""Output files should be created in the same directory as input."""
from typer.testing import CliRunner
from soup_cli.cli import app
data_file = tmp_path / "data.jsonl"
_make_jsonl(data_file, 20)
runner = CliRunner()
result = runner.invoke(
app,
["data", "split", str(data_file), "--val", "20"],
)
assert result.exit_code == 0
# Output should be in same dir as input
assert (tmp_path / "data_train.jsonl").exists()
assert (tmp_path / "data_val.jsonl").exists()