Completed
Push — master ( a5b230...649ac2 )
by Amresh
02:28
created

fastest.compiler.compile_tests   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 45
dl 0
loc 63
rs 10
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A create_test_class() 0 8 2
A write_tests_to_file() 0 2 1
A create_test_case() 0 14 4
A create_test_case_content() 0 5 2
A add_imports_for_test_case() 0 6 3
A build() 0 11 2
1
import os
2
from fastest.bodies import f
3
from fastest.constants import CONTENT, KEYS, SYS
4
5
6
7
8
def add_imports_for_test_case(test, imports):
9
    if test[KEYS.IMPORTS] is None:
10
        return imports
11
    for import_statement in test[KEYS.IMPORTS]:
12
        imports.add(import_statement)
13
    return imports
14
15
def create_test_class(imports, contents, deps_import, function_object, root_module_name):
16
    if len(function_object[KEYS.TESTS]) == 0:
17
        return None
18
19
    imports.add(CONTENT.IMPORT_UNITTEST)
20
    imports.add(CONTENT.DEPS_IMPORT_TEMPLATE.format(deps_import, function_object[KEYS.NAME]))
21
    contents.append(CONTENT.CLASS_CREATE_TEMPLATE.format(root_module_name, function_object[KEYS.NAME]))
22
    return imports, contents
23
24
25
def create_test_case_content(function_object, imports, contents):
26
    for example in function_object[KEYS.TESTS][KEYS.EXAMPLES]:
27
        contents.append(f.create_naive_test_case(function_object, example))
28
    imports = add_imports_for_test_case(function_object[KEYS.TESTS], imports)
29
    return imports, contents
30
31
32
def create_test_case(function_objects, deps_import, root_module_name):
33
    imports = set()
34
    contents = []
35
36
    for function_object in function_objects:
37
        if function_object is None:
38
            continue
39
40
        if function_object[KEYS.TESTS] is None:
41
            continue
42
43
        imports, contents = create_test_class(imports, contents, deps_import, function_object, root_module_name)
44
        imports, contents = create_test_case_content(function_object, imports, contents)
45
    return imports, contents
46
47
48
def write_tests_to_file(fp, imports, contents):
49
    return fp.write(''.join(sorted(list(imports)) + contents))
50
51
52
def build(function_objects, src_file_path, base_path):
53
    last_file = -1
54
    test_file_name = src_file_path.split(SYS.SLASH)[last_file].replace('.py', SYS.TEST_FILE_ENDING)
55
    deps_import = src_file_path.replace(base_path + SYS.SLASH, '').replace(SYS.SLASH, '.').replace('.py', '')
56
    root_module_name = deps_import.split('.')[-1]
57
    test_file_path = os.path.join(base_path, KEYS.TEST, test_file_name)
58
59
60
    with open(test_file_path, 'w+') as fp:
61
        imports, contents = create_test_case(function_objects, deps_import, root_module_name)
62
        write_tests_to_file(fp, imports, contents)
63