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