create_naive_test_case()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 57
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 57
rs 9.65
c 0
b 0
f 0
cc 3
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import re
2
import uuid
3
from fastest.constants import Keys, Content
4
5
6
def case_generator(uuid_val=None):
7
    """
8
    ----
9
    examples:
10
    1) case_generator('a55eff11-ed51-ecb37-ccba') -> 'A55EFF11ED'
11
    ----
12
    Use uuid to create test-case unique name
13
    :return:
14
    """
15
    uuid_val = uuid_val if uuid_val is not None else str(uuid.uuid4())
16
    return str(uuid_val).upper().replace("-", "")[0:10]
17
18
19
def get_empty_of_type(input_type):
20
    """
21
    Return an empty form of a given type
22
    ----
23
    examples:
24
25
    1) get_empty_of_type('str') -> "''"
26
    2) get_empty_of_type('???') -> None
27
    ----
28
    :param input_type: str
29
    :return: str
30
    """
31
    empty_type = {
32
        'str': '\'\'',
33
        'int': '0',
34
        'list': '[]',
35
        'dict': '{}',
36
        '': '\'\''
37
    }
38
39
    return empty_type.get(input_type)
40
41
42
def get_params_list(params):
43
    """
44
    ----
45
    examples:
46
47
    1) get_params_list(['str', 'str', 'list', 'dict']) -> ["''", "''", '[]', '{}']
48
    2) get_params_list(['str', '???']) -> ["''"]
49
    ----
50
    :param params: list
51
    :return: list
52
    """
53
    return [
54
        get_empty_of_type(param)
55
        for param in params
56
        if param in ['str', 'int', 'list', 'dict']
57
    ]
58
59
60
def create_assertion_test(function_object):
61
    """
62
    Create assertion test cases by embedding into the template strings
63
    if examples are present in the docstrings
64
    -----
65
    examples:
66
67
    @need
68
    from fastest.constants import TestBodies
69
    @end
70
71
    @let
72
    function_object_1 = {
73
        'tests': {
74
            'variables': ['a = 5']
75
        }
76
    }
77
78
    function_object_2 = {'tests': {'variables': []}}
79
80
    params = ['str', 'str']
81
    @end
82
83
    1) create_assertion_test(function_object_1) -> TestBodies.ASSERTION_TEST_1
84
    2) create_assertion_test(function_object_2) -> ''
85
    -----
86
    :param function_object: dict
87
    :return: str
88
    """
89
    template = ''
90
    if function_object.get(Keys.TESTS, {}).get(Keys.VARIABLES):
91
        for variable in function_object.get(Keys.TESTS, {}).get(Keys.VARIABLES, []):
92
            template += Content.VARIABLES_TEMPLATE.format(variables=variable)
93
    return template
94
95
96
def create_naive_test_case(function_object, test, test_id=None):
97
    """
98
    Create test cases from the assertions, docstring params and return types
99
    ----
100
    examples:
101
102
    @need
103
    from fastest.constants import TestBodies
104
    @end
105
106
    @let
107
    function_object = {
108
        'tests': {
109
            'return': 'str',
110
            'variables': ['a = 5']
111
        },
112
        'name': 'function_1'
113
    }
114
115
    test = {
116
        'from': 'function_1',
117
        'expect': '2'
118
    }
119
120
    exception_test = {
121
        'from': 'function_2(None)',
122
        'exception': 'TypeError'
123
    }
124
125
    test_id = 'a55eff11-ed51-ecb37-ccba'
126
    @end
127
    1) create_naive_test_case(function_object, test, test_id) -> TestBodies.NAIVE_TEST_RESULT
128
    2) create_naive_test_case(function_object, exception_test, test_id) -> TestBodies.EXCEPTION_TEST_RESULT
129
    ----
130
    :param function_object: dict
131
    :param test: dict
132
    :param test_id: str
133
    :return: str
134
    """
135
    test_template = Content.TEST_CASE_TEMPLATE.format(
136
        function_name=function_object.get(Keys.NAME),
137
        case_id=case_generator(test_id),
138
    )
139
140
    test_template += create_assertion_test(function_object)
141
142
    if test.get(Keys.EXPECT):
143
        test_template += Content.ASSERTION_TEMPLATE.format(function=test.get(Keys.FROM), value=test.get(Keys.EXPECT))
144
    elif test.get(Keys.EXCEPTION):
145
        args = re.search(r'\((.*)\)', test.get(Keys.FROM))
146
        print('arguments: ', args.group(1))
147
        test_template += Content.EXCEPTION_TEMPLATE.format(
148
            function=function_object.get(Keys.NAME),
149
            value=test.get(Keys.EXCEPTION),
150
            args=args.group(1)
151
        )
152
    return test_template
153