forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data_augment.py
More file actions
212 lines (172 loc) · 7.11 KB
/
Copy pathtest_data_augment.py
File metadata and controls
212 lines (172 loc) · 7.11 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
"""Tests for data augmentation (Part F of v0.25.0)."""
import json
import pytest
from typer.testing import CliRunner
from soup_cli.cli import app
runner = CliRunner()
# ---------------------------------------------------------------------------
# Strategy unit tests (with a mocked provider)
# ---------------------------------------------------------------------------
class FakeProvider:
"""Deterministic fake LLM provider for testing."""
def __init__(self):
self.calls = 0
def generate(self, prompt: str, max_tokens: int = 512) -> str:
self.calls += 1
return f"rewritten-{self.calls}"
class TestAugmentStrategies:
def test_rephrase_basic(self):
from soup_cli.data.augment import augment_rephrase
examples = [
{"instruction": "What is Python?", "output": "Python is a language."},
]
provider = FakeProvider()
augmented = augment_rephrase(examples, provider=provider, count=2)
# 1 original × 2 count × 2 string fields (instruction + output) = 4 provider calls
assert provider.calls == 4
# 1 original → 2 new augmentations
assert len(augmented) == 2
for row in augmented:
assert "instruction" in row or "output" in row
def test_translate_produces_langs(self):
from soup_cli.data.augment import augment_translate
examples = [
{"instruction": "Hello", "output": "Hi"},
]
provider = FakeProvider()
augmented = augment_translate(
examples, provider=provider, languages=["ru", "zh"]
)
# 1 example × 2 languages × 2 string fields = 4 provider calls
assert provider.calls == 4
assert len(augmented) == 2
def test_style_produces_styles(self):
from soup_cli.data.augment import augment_style
examples = [
{
"instruction": "Explain recursion.",
"output": "It's when a function calls itself.",
},
]
provider = FakeProvider()
augmented = augment_style(
examples, provider=provider, styles=["formal", "casual"]
)
# 1 example × 2 styles × 2 string fields = 4 provider calls
assert provider.calls == 4
assert len(augmented) == 2
def test_count_cap_enforced(self):
"""count > 10 is rejected at strategy level (not just CLI)."""
from soup_cli.data.augment import augment_rephrase
examples = [{"instruction": "x", "output": "y"}]
provider = FakeProvider()
with pytest.raises(ValueError):
augment_rephrase(examples, provider=provider, count=11)
def test_empty_examples_returns_empty(self):
from soup_cli.data.augment import (
augment_rephrase,
augment_style,
augment_translate,
)
provider = FakeProvider()
assert augment_rephrase([], provider=provider, count=2) == []
assert augment_translate([], provider=provider, languages=["ru"]) == []
assert augment_style([], provider=provider, styles=["formal"]) == []
assert provider.calls == 0
def test_translate_empty_languages_rejected(self):
from soup_cli.data.augment import augment_translate
with pytest.raises(ValueError):
augment_translate([{"x": "y"}], provider=FakeProvider(), languages=[])
def test_style_empty_styles_rejected(self):
from soup_cli.data.augment import augment_style
with pytest.raises(ValueError):
augment_style([{"x": "y"}], provider=FakeProvider(), styles=[])
def test_provider_exception_propagates(self):
"""When provider raises, augment propagates the error (fail-loud)."""
from soup_cli.data.augment import augment_rephrase
class FailingProvider:
def generate(self, prompt: str, max_tokens: int = 512) -> str:
raise RuntimeError("provider down")
with pytest.raises(RuntimeError, match="provider down"):
augment_rephrase(
[{"instruction": "x"}], provider=FailingProvider(), count=1,
)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
class TestAugmentCLI:
def _write_data(self, tmp_path):
rows = [
{"instruction": f"q{i}", "output": f"a{i}"} for i in range(3)
]
path = tmp_path / "data.jsonl"
path.write_text(
"\n".join(json.dumps(r) for r in rows) + "\n", encoding="utf-8"
)
return path
def test_augment_help(self):
result = runner.invoke(app, ["data", "augment", "--help"])
assert result.exit_code == 0
assert "augment" in result.output.lower()
def test_augment_missing_file(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
result = runner.invoke(app, [
"data", "augment",
"--input", "nonexistent.jsonl",
"--output", "out.jsonl",
"--strategy", "rephrase",
"--provider", "ollama",
])
assert result.exit_code != 0
def test_augment_count_cap(self, tmp_path, monkeypatch):
"""CLI rejects count > 10."""
monkeypatch.chdir(tmp_path)
path = self._write_data(tmp_path)
result = runner.invoke(app, [
"data", "augment",
"--input", str(path.name),
"--output", "out.jsonl",
"--strategy", "rephrase",
"--count", "15",
"--provider", "ollama",
])
assert result.exit_code != 0
def test_input_path_traversal_blocked(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
result = runner.invoke(app, [
"data", "augment",
"--input", "../../../etc/passwd",
"--output", "out.jsonl",
"--strategy", "rephrase",
"--provider", "ollama",
])
assert result.exit_code != 0
def test_output_path_traversal_blocked(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
path = self._write_data(tmp_path)
result = runner.invoke(app, [
"data", "augment",
"--input", str(path.name),
"--output", "../../evil.jsonl",
"--strategy", "rephrase",
"--provider", "ollama",
])
assert result.exit_code != 0
# ---------------------------------------------------------------------------
# Strategy validation
# ---------------------------------------------------------------------------
class TestStrategyValidation:
def test_unknown_strategy(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
path = tmp_path / "data.jsonl"
path.write_text('{"instruction": "x", "output": "y"}\n', encoding="utf-8")
result = runner.invoke(app, [
"data", "augment",
"--input", str(path.name),
"--output", "out.jsonl",
"--strategy", "bogus",
"--provider", "ollama",
])
assert result.exit_code != 0
if __name__ == "__main__":
pytest.main([__file__, "-v"])