1
|
|
|
import os |
2
|
|
|
from fastest.constants import Keys, Sys |
3
|
|
|
from fastest.test_compiler.compile_tests import add_imports_for_test_case, create_test_class |
4
|
|
|
from fastest.bodies.test_case_template_builder import create_naive_test_case |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
def create_test_case_content(function_object, imports, contents): |
8
|
|
|
""" |
9
|
|
|
:param function_object: dict |
10
|
|
|
:param imports: set |
11
|
|
|
:param contents: list |
12
|
|
|
:return: tuple |
13
|
|
|
""" |
14
|
|
|
for example in function_object.get(Keys.TESTS, {}).get(Keys.EXAMPLES, []): |
15
|
|
|
contents.append(create_naive_test_case(function_object, example)) |
16
|
|
|
|
17
|
|
|
imports = add_imports_for_test_case(function_object[Keys.TESTS], imports) |
18
|
|
|
return imports, contents |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def create_test_case(function_objects, deps_import, root_module_name): |
22
|
|
|
imports = set() |
23
|
|
|
contents = [] |
24
|
|
|
|
25
|
|
|
for function_object in function_objects: |
26
|
|
|
if not isinstance(function_object, dict): |
27
|
|
|
continue |
28
|
|
|
if function_object[Keys.TESTS] is None: |
29
|
|
|
continue |
30
|
|
|
imports, contents = create_test_class(imports, contents, deps_import, function_object, root_module_name) |
31
|
|
|
imports, contents = create_test_case_content(function_object, imports, contents) |
32
|
|
|
return imports, contents |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def write_tests_to_file(fp, imports, contents): |
36
|
|
|
return fp.write(''.join(sorted(list(imports)) + contents)) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def build(function_objects, src_file_path, base_path): |
40
|
|
|
last_file = -1 |
41
|
|
|
test_file_name = 'test__' + src_file_path.split(Sys.SLASH)[last_file] |
42
|
|
|
deps_import = src_file_path.replace(base_path + Sys.SLASH, '').replace(Sys.SLASH, '.').replace('.py', '') |
43
|
|
|
root_module_name = deps_import.split('.')[-1] |
44
|
|
|
test_file_path = os.path.join(base_path, Keys.TEST, test_file_name) |
45
|
|
|
|
46
|
|
|
with open(test_file_path, 'w+') as fp: |
47
|
|
|
imports, contents = create_test_case(function_objects, deps_import, root_module_name) |
48
|
|
|
write_tests_to_file(fp, imports, contents) |
49
|
|
|
|