-
Notifications
You must be signed in to change notification settings - Fork 4
/
pytest_data_test.py
188 lines (133 loc) · 5.39 KB
/
pytest_data_test.py
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
# -*- coding: utf-8 -*-
import pytest
from pytest_data import get_data, use_data
def test_only_default():
data = _get_data({'a': 1})
assert data == {'a': 1}
def test_module():
data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30})
assert data == {'a': 1, 'b': 20, 'c': 30}
def test_cls():
data = _get_data({'a': 1, 'b': 2}, cls={'b': 20, 'c': 30})
assert data == {'a': 1, 'b': 20, 'c': 30}
def test_function():
data = _get_data({'a': 1, 'b': 2}, function={'b': 20, 'c': 30})
assert data == {'a': 1, 'b': 20, 'c': 30}
def test_module_and_cls():
data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30}, cls={'c': 300, 'd': 400})
assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 400}
def test_module_and_function():
data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30}, function={'c': 300, 'd': 400})
assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 400}
def test_cls_and_function():
data = _get_data({'a': 1, 'b': 2}, cls={'b': 20, 'c': 30}, function={'c': 300, 'd': 400})
assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 400}
def test_module_and_cls_and_function():
data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30}, cls={'c': 300, 'd': 400}, function={'d': 4000, 'e': 5000})
assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 4000, 'e': 5000}
def test_list_only_default():
data = _get_data([{'a': 1}])
assert data == [{'a': 1}]
def test_list_mix_raises_module():
with pytest.raises(ValueError):
_get_data({'a': 1}, module=[{'b': 20}])
with pytest.raises(ValueError):
_get_data([{'a': 1}], module={'b': 20})
def test_list_mix_raises_cls():
with pytest.raises(ValueError):
_get_data({'a': 1}, cls=[{'b': 20}])
with pytest.raises(ValueError):
_get_data([{'a': 1}], cls={'b': 20})
def test_list_mix_raises_function():
with pytest.raises(ValueError):
_get_data({'a': 1}, function=[{'b': 20}])
with pytest.raises(ValueError):
_get_data([{'a': 1}], function={'b': 20})
def test_list_module():
data = _get_data([{'a': 1, 'b': 2}], module=[{'b': 20, 'c': 30}])
assert data == [{'a': 1, 'b': 20, 'c': 30}]
def test_list_cls():
data = _get_data([{'a': 1, 'b': 2}], cls=[{'b': 20, 'c': 30}])
assert data == [{'a': 1, 'b': 20, 'c': 30}]
def test_list_function():
data = _get_data([{'a': 1, 'b': 2}], function=[{'b': 20, 'c': 30}])
assert data == [{'a': 1, 'b': 20, 'c': 30}]
def test_list_more_values_module():
data = _get_data([{'a': 1, 'b': 2}], module=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}])
assert data == [{'a': 1, 'b': 20, 'c': 30}, {'a': 1, 'b': 21, 'c': 31}]
def test_list_more_values_cls():
data = _get_data([{'a': 1, 'b': 2}], cls=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}])
assert data == [{'a': 1, 'b': 20, 'c': 30}, {'a': 1, 'b': 21, 'c': 31}]
def test_list_more_values_function():
data = _get_data([{'a': 1, 'b': 2}], function=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}])
assert data == [{'a': 1, 'b': 20, 'c': 30}, {'a': 1, 'b': 21, 'c': 31}]
def test_list_more_values_module_and_cls():
data = _get_data(
[{'a': 1, 'b': 2}],
module=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}],
cls=[{'c': 300, 'd': 400}],
)
assert data == [
{'a': 1, 'b': 20, 'c': 300, 'd': 400},
{'a': 1, 'b': 21, 'c': 300, 'd': 400},
]
def test_list_more_values_module_and_function():
data = _get_data(
[{'a': 1, 'b': 2}],
module=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}, {'b': 22, 'c': 32}],
function=[{'c': 300, 'd': 400}, {'c': 301, 'd': 401}],
)
assert data == [
{'a': 1, 'b': 20, 'c': 300, 'd': 400},
{'a': 1, 'b': 21, 'c': 301, 'd': 401},
{'a': 1, 'b': 22, 'c': 300, 'd': 400},
]
def test_list_more_values_cls_and_function():
data = _get_data(
[{'a': 10, 'b': 20}, {'a': 11, 'b': 21}],
cls=[{'b': 200, 'c': 300}],
function=[{'c': 3000, 'd': 4000}, {'c': 3001, 'd': 4001}],
)
assert data == [
{'a': 10, 'b': 200, 'c': 3000, 'd': 4000},
{'a': 11, 'b': 200, 'c': 3001, 'd': 4001},
]
def test_list_more_values_module_cls_and_function():
data = _get_data(
[{'a': 10, 'b': 20}, {'a': 11, 'b': 21}],
module=[{'b': 200, 'c': 300}, {'b': 201, 'c': 301}, {'b': 202, 'c': 302}],
cls=[{'c': 3000, 'd': 4000}],
function=[{'d': 40000, 'e': 50000}, {'d': 40001, 'e': 50001}],
)
assert data == [
{'a': 10, 'b': 200, 'c': 3000, 'd': 40000, 'e': 50000},
{'a': 11, 'b': 201, 'c': 3000, 'd': 40001, 'e': 50001},
{'a': 10, 'b': 202, 'c': 3000, 'd': 40000, 'e': 50000},
]
def _get_data(default, module=None, cls=None, function=None):
class request:
class module:
if module:
foo = module
class cls:
if cls:
foo = cls
class function:
if function:
foo = function
return get_data(request, 'foo', default)
def test_use_data_func_stays_same():
def test_func():
pass
res_func = use_data(foo=42)(test_func)
assert res_func == test_func
def test_use_data_func_has_data():
def test_func():
pass
res_func = use_data(foo=42)(test_func)
assert res_func.foo == 42
def test_use_data_can_be_used_only_with_tests():
def func():
pass
with pytest.raises(AssertionError):
use_data(foo=42)(func)