Completed
Push — master ( db2bdb...52f8b4 )
by Amresh
01:56 queued 46s
created

fastest.test_compiler.test_writer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 32
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A create_test_case_content() 0 12 2
A build() 0 10 2
A create_test_case() 0 12 4
A write_tests_to_file() 0 2 1
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