1
|
|
|
import uuid |
2
|
|
|
from fastest.constants import Keys, Content |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
def case_generator(uuid_val=None): |
6
|
|
|
""" |
7
|
|
|
---- |
8
|
|
|
examples: |
9
|
|
|
1) case_generator('a55eff11-ed51-ecb37-ccba') -> 'A55EFF11ED' |
10
|
|
|
---- |
11
|
|
|
Use uuid to create test-case unique name |
12
|
|
|
:return: |
13
|
|
|
""" |
14
|
|
|
uuid_val = uuid_val if uuid_val is not None else str(uuid.uuid4()) |
15
|
|
|
return str(uuid_val).upper().replace("-", "")[0:10] |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def get_empty_of_type(input_type): |
19
|
|
|
""" |
20
|
|
|
Return an empty form of a given type |
21
|
|
|
---- |
22
|
|
|
examples: |
23
|
|
|
|
24
|
|
|
1) get_empty_of_type('str') -> "''" |
25
|
|
|
2) get_empty_of_type('???') -> None |
26
|
|
|
---- |
27
|
|
|
:param input_type: str |
28
|
|
|
:return: str |
29
|
|
|
""" |
30
|
|
|
empty_type = { |
31
|
|
|
'str': '\'\'', |
32
|
|
|
'int': '0', |
33
|
|
|
'list': '[]', |
34
|
|
|
'dict': '{}' |
35
|
|
|
} |
36
|
|
|
return empty_type.get(input_type) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def get_params_list(params): |
40
|
|
|
""" |
41
|
|
|
---- |
42
|
|
|
examples: |
43
|
|
|
1) get_params_list(['str', 'str', 'list', 'dict']) -> ["''", "''", '[]', '{}'] |
44
|
|
|
2) get_params_list(['str', '???']) -> ["''"] |
45
|
|
|
---- |
46
|
|
|
:param params: [] |
47
|
|
|
:return: [] |
48
|
|
|
""" |
49
|
|
|
params = [ |
50
|
|
|
get_empty_of_type(param) |
51
|
|
|
for param in params |
52
|
|
|
] |
53
|
|
|
return [ |
54
|
|
|
param for param in params |
55
|
|
|
if param is not None |
56
|
|
|
] |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def create_type_test_case(function_object, params): |
60
|
|
|
""" |
61
|
|
|
Create test case for checking types of function |
62
|
|
|
----- |
63
|
|
|
examples: |
64
|
|
|
|
65
|
|
|
@need |
66
|
|
|
from fastest.constants import TestBodies |
67
|
|
|
@end |
68
|
|
|
|
69
|
|
|
@let |
70
|
|
|
function_object = { |
71
|
|
|
'tests': { |
72
|
|
|
'return': 'str' |
73
|
|
|
}, |
74
|
|
|
'name': 'function_1' |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
params = ['str', 'str'] |
78
|
|
|
@end |
79
|
|
|
|
80
|
|
|
1) create_type_test_case(function_object, params) -> TestBodies.TYPE_TEST_CASE_1 |
81
|
|
|
----- |
82
|
|
|
:param function_object: dict |
83
|
|
|
:param params: list |
84
|
|
|
:return: str |
85
|
|
|
""" |
86
|
|
|
empty_param_call = '{}({})'.format(function_object[Keys.NAME], ', '.join(params)) |
87
|
|
|
return Content.TYPE_ASSERT_TEMPLATE.format( |
88
|
|
|
function=empty_param_call, value=function_object[Keys.TESTS][Keys.RETURN] |
89
|
|
|
) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def create_type_test_case_if_params(function_object, params): |
93
|
|
|
""" |
94
|
|
|
Create type test case if there is info over params and return |
95
|
|
|
---- |
96
|
|
|
examples: |
97
|
|
|
|
98
|
|
|
@need |
99
|
|
|
from fastest.constants import TestBodies |
100
|
|
|
@end |
101
|
|
|
|
102
|
|
|
@let |
103
|
|
|
function_object = { |
104
|
|
|
'tests': { |
105
|
|
|
'return': 'str' |
106
|
|
|
}, |
107
|
|
|
'name': 'function_1' |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
params = ['str', 'str'] |
111
|
|
|
@end |
112
|
|
|
|
113
|
|
|
1) create_type_test_case_if_params(function_object, params) -> TestBodies.TYPE_TEST_CASE_2 |
114
|
|
|
---- |
115
|
|
|
:param function_object: dict |
116
|
|
|
:param params: list |
117
|
|
|
:return: str |
118
|
|
|
""" |
119
|
|
|
return create_type_test_case(function_object, params)\ |
120
|
|
|
if is_type_test_ready(function_object, params)\ |
121
|
|
|
else '' |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
def is_type_test_ready(function_object, params): |
125
|
|
|
""" |
126
|
|
|
if a function has params and return type specified, return True |
127
|
|
|
else False |
128
|
|
|
---- |
129
|
|
|
examples: |
130
|
|
|
|
131
|
|
|
@let |
132
|
|
|
function_object_1 = { |
133
|
|
|
'tests': { |
134
|
|
|
'return': 'str' |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
function_object_2 = {'tests': {}} |
139
|
|
|
|
140
|
|
|
params = ['str', 'str'] |
141
|
|
|
@end |
142
|
|
|
|
143
|
|
|
1) is_type_test_ready(function_object_1, params) -> True |
144
|
|
|
2) is_type_test_ready(function_object_2, params) -> False |
145
|
|
|
3) is_type_test_ready(function_object_1, []) -> False |
146
|
|
|
---- |
147
|
|
|
:param function_object: dict |
148
|
|
|
:param params: list |
149
|
|
|
:return: bool |
150
|
|
|
""" |
151
|
|
|
return_type = function_object.get(Keys.TESTS, {}).get(Keys.RETURN) |
152
|
|
|
return bool(return_type and len(params) > 0) |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
def create_assertion_test(function_object): |
156
|
|
|
""" |
157
|
|
|
Create assertion test cases by embedding into the template strings |
158
|
|
|
if examples are present in the docstrings |
159
|
|
|
----- |
160
|
|
|
examples: |
161
|
|
|
|
162
|
|
|
@need |
163
|
|
|
from fastest.constants import TestBodies |
164
|
|
|
@end |
165
|
|
|
|
166
|
|
|
@let |
167
|
|
|
function_object_1 = { |
168
|
|
|
'tests': { |
169
|
|
|
'variables': ['a = 5'] |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
function_object_2 = {'tests': {'variables': []}} |
174
|
|
|
|
175
|
|
|
params = ['str', 'str'] |
176
|
|
|
@end |
177
|
|
|
|
178
|
|
|
1) create_assertion_test(function_object_1) -> TestBodies.ASSERTION_TEST_1 |
179
|
|
|
2) create_assertion_test(function_object_2) -> '' |
180
|
|
|
----- |
181
|
|
|
:param function_object: dict |
182
|
|
|
:return: str |
183
|
|
|
""" |
184
|
|
|
template = '' |
185
|
|
|
if function_object[Keys.TESTS][Keys.VARIABLES]: |
186
|
|
|
for variable in function_object[Keys.TESTS][Keys.VARIABLES]: |
187
|
|
|
template += Content.VARIABLES_TEMPLATE.format(variables=variable) |
188
|
|
|
return template |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
def create_naive_test_case(function_object, test, test_id=None): |
192
|
|
|
""" |
193
|
|
|
Create test cases from the assertions, docstring params and return types |
194
|
|
|
---- |
195
|
|
|
examples: |
196
|
|
|
|
197
|
|
|
@need |
198
|
|
|
from fastest.constants import TestBodies |
199
|
|
|
@end |
200
|
|
|
|
201
|
|
|
@let |
202
|
|
|
function_object = { |
203
|
|
|
'tests': { |
204
|
|
|
'return': 'str', |
205
|
|
|
'variables': ['a = 5'] |
206
|
|
|
}, |
207
|
|
|
'name': 'function_1' |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
test = { |
211
|
|
|
'from': 'function_1', |
212
|
|
|
'expect': '2' |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
test_id = 'a55eff11-ed51-ecb37-ccba' |
216
|
|
|
@end |
217
|
|
|
|
218
|
|
|
1) create_naive_test_case(function_object, test, test_id) -> TestBodies.NAIVE_TEST_RESULT |
219
|
|
|
---- |
220
|
|
|
:param function_object: dict |
221
|
|
|
:param test: dict |
222
|
|
|
:param test_id: str |
223
|
|
|
:return: str |
224
|
|
|
""" |
225
|
|
|
test_template = Content.TEST_CASE_TEMPLATE.format( |
226
|
|
|
function_name=function_object[Keys.NAME], |
227
|
|
|
case_id=case_generator(test_id), |
228
|
|
|
) |
229
|
|
|
|
230
|
|
|
params = get_params_list(function_object) |
231
|
|
|
|
232
|
|
|
test_template += create_type_test_case_if_params(function_object, params) |
233
|
|
|
test_template += create_assertion_test(function_object) |
234
|
|
|
test_template += Content.ASSERTION_TEMPLATE.format(function=test[Keys.FROM], value=test[Keys.EXPECT]) |
235
|
|
|
return test_template |
236
|
|
|
|