-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_parameterize_validation.py
More file actions
226 lines (183 loc) · 9.09 KB
/
test_parameterize_validation.py
File metadata and controls
226 lines (183 loc) · 9.09 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
"""
Test cases for pytest.mark.parametrize validation functionality.
"""
import ast
import pytest
from eval_protocol.pytest.parameterize import _is_pytest_parametrize_with_completion_params
def create_parametrize_decorator(argnames, argvalues, use_keyword=False):
"""Create a pytest.mark.parametrize decorator AST node."""
pytest_name = ast.Name(id="pytest", ctx=ast.Load())
mark_attr = ast.Attribute(value=pytest_name, attr="mark", ctx=ast.Load())
parametrize_attr = ast.Attribute(value=mark_attr, attr="parametrize", ctx=ast.Load())
if use_keyword:
call = ast.Call(
func=parametrize_attr,
args=[],
keywords=[
ast.keyword(arg="argnames", value=ast.Constant(value=argnames)),
ast.keyword(arg="argvalues", value=argvalues),
],
)
else:
call = ast.Call(func=parametrize_attr, args=[ast.Constant(value=argnames), argvalues], keywords=[])
return call
class TestParametrizeValidation:
"""Test cases for pytest.mark.parametrize validation."""
def test_invalid_dict_argvalues_positional(self):
"""Test that a dict as positional argvalues throws an error."""
decorator = create_parametrize_decorator(
"completion_params", ast.Dict(keys=[ast.Constant(value="model")], values=[ast.Constant(value="gpt-4")])
)
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "Use [{'model': 'gpt-4'}] instead of {'model': 'gpt-4'}" in error_msg
def test_valid_list_argvalues_positional(self):
"""Test that a list as positional argvalues works correctly."""
dict_value = ast.Dict(keys=[ast.Constant(value="model")], values=[ast.Constant(value="gpt-4")])
list_value = ast.List(elts=[dict_value], ctx=ast.Load())
decorator = create_parametrize_decorator("completion_params", list_value)
result = _is_pytest_parametrize_with_completion_params(decorator)
assert result is True
def test_invalid_dict_argvalues_keyword(self):
"""Test that a dict as keyword argvalues throws an error."""
decorator = create_parametrize_decorator(
"completion_params",
ast.Dict(keys=[ast.Constant(value="model")], values=[ast.Constant(value="gpt-4")]),
use_keyword=True,
)
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "Use [{'model': 'gpt-4'}] instead of {'model': 'gpt-4'}" in error_msg
def test_valid_list_argvalues_keyword(self):
"""Test that a list as keyword argvalues works correctly."""
dict_value = ast.Dict(keys=[ast.Constant(value="model")], values=[ast.Constant(value="gpt-4")])
list_value = ast.List(elts=[dict_value], ctx=ast.Load())
decorator = create_parametrize_decorator("completion_params", list_value, use_keyword=True)
result = _is_pytest_parametrize_with_completion_params(decorator)
assert result is True
def test_dynamic_error_simple_dict(self):
"""Test dynamic error message with a simple dict."""
decorator = create_parametrize_decorator(
"completion_params", ast.Dict(keys=[ast.Constant(value="model")], values=[ast.Constant(value="gpt-4")])
)
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "Use [{'model': 'gpt-4'}] instead of {'model': 'gpt-4'}" in error_msg
def test_dynamic_error_complex_dict(self):
"""Test dynamic error message with a complex dict."""
decorator = create_parametrize_decorator(
"completion_params",
ast.Dict(
keys=[
ast.Constant(value="model"),
ast.Constant(value="temperature"),
ast.Constant(value="max_tokens"),
],
values=[
ast.Constant(value="accounts/fireworks/models/gpt-oss-120b"),
ast.Constant(value=0.7),
ast.Constant(value=1000),
],
),
)
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "Use [{" in error_msg
assert "}] instead of {" in error_msg
assert "gpt-oss-120b" in error_msg
assert "0.7" in error_msg
assert "1000" in error_msg
def test_dynamic_error_nested_dict(self):
"""Test dynamic error message with nested structures."""
# Create a dict with nested dict
nested_dict = ast.Dict(
keys=[ast.Constant(value="config")],
values=[
ast.Dict(
keys=[ast.Constant(value="model"), ast.Constant(value="api_key")],
values=[ast.Constant(value="gpt-4"), ast.Constant(value="sk-123")],
)
],
)
decorator = create_parametrize_decorator("completion_params", nested_dict)
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "Use [{" in error_msg
assert "}] instead of {" in error_msg
def test_dynamic_error_boolean_values(self):
"""Test dynamic error message with boolean values."""
decorator = create_parametrize_decorator(
"completion_params",
ast.Dict(
keys=[ast.Constant(value="stream"), ast.Constant(value="echo")],
values=[ast.Constant(value=True), ast.Constant(value=False)],
),
)
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "True" in error_msg
assert "False" in error_msg
def test_dynamic_error_empty_dict(self):
"""Test dynamic error message with empty dict."""
decorator = create_parametrize_decorator("completion_params", ast.Dict(keys=[], values=[]))
with pytest.raises(ValueError) as exc_info:
_is_pytest_parametrize_with_completion_params(decorator)
error_msg = str(exc_info.value)
assert (
"For evaluation_test with completion_params, pytest.mark.parametrize argvalues must be a list or tuple, not a dict"
in error_msg
)
assert "Use [{}] instead of {}" in error_msg
def test_valid_tuple_argvalues(self):
"""Test that a tuple as argvalues works correctly."""
dict_value = ast.Dict(keys=[ast.Constant(value="model")], values=[ast.Constant(value="gpt-4")])
tuple_value = ast.Tuple(elts=[dict_value], ctx=ast.Load())
decorator = create_parametrize_decorator("completion_params", tuple_value)
result = _is_pytest_parametrize_with_completion_params(decorator)
assert result is True
def test_non_parametrize_decorator(self):
"""Test that non-parametrize decorators are ignored."""
# Create a different decorator
pytest_name = ast.Name(id="pytest", ctx=ast.Load())
mark_attr = ast.Attribute(value=pytest_name, attr="mark", ctx=ast.Load())
skipif_attr = ast.Attribute(value=mark_attr, attr="skipif", ctx=ast.Load())
decorator = ast.Call(func=skipif_attr, args=[ast.Constant(value=True)], keywords=[])
result = _is_pytest_parametrize_with_completion_params(decorator)
assert result is False
def test_parametrize_without_completion_params(self):
"""Test that parametrize without completion_params is ignored."""
decorator = create_parametrize_decorator(
"other_param", ast.List(elts=[ast.Constant(value="value")], ctx=ast.Load())
)
result = _is_pytest_parametrize_with_completion_params(decorator)
assert result is False