Passed
Pull Request — dev (#32)
by Konstantinos
22:13 queued 01:54
created

test_phi.test_phi_creation()   B

Complexity

Conditions 2

Size

Total Lines 82
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 54
nop 2
dl 0
loc 82
rs 8.5054
c 0
b 0
f 0

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 pytest
2
3
4
@pytest.fixture
5
def registering_phi_at_the_same_key_error():
6
    from so_magic.utils.registry import ObjectRegistryError
7
    return ObjectRegistryError
8
9
10
@pytest.fixture
11
def app_phi_function(somagic):
12
    return somagic._data_manager.phi_class
13
14
15
@pytest.fixture
16
def assert_function_properties_values():
17
    def _f(func, expected_name: str, expected_doc: str):
18
        assert func.__name__ == expected_name
19
        assert func.__doc__ == expected_doc
20
    return _f
21
22
23
@pytest.fixture
24
def assert_class_properties_values():
25
    def _f(class_obj, expected_name: str, expected_doc: str):
26
        assert class_obj.__name__ == expected_name
27
        assert class_obj.__call__.__doc__ == expected_doc
28
    return _f
29
30
31
@pytest.fixture
32
def phi_registry(somagic):
33
    """Magic Phi function registry, listening to registration operations at runtime."""
34
    return somagic._data_manager.built_phis
35
36
37
@pytest.fixture
38
def test_phi():
39
    return {
40
        'f1': lambda x: x + 1,
41
        'f2': lambda x: x + 2,
42
        'f3': lambda x: x + 3,
43
        'f4': lambda x: x + 4,
44
        'f5': lambda x: x + 5,
45
        'f6': lambda x: x + 6,
46
    }
47
48
49
def test_initial_phi_registry(phi_registry):
50
    assert phi_registry.registry == {}
51
    assert 'any-key' not in phi_registry
52
53
54
@pytest.fixture(params=[
55
    (['pame'], 'f1', 'pame'),
56
    ([''], 'f2', 'f2_func'),
57
    ([], 'f3', 'f3_func'),
58
])
59
def phi_function_def(request, phi_registry, app_phi_function, test_phi):
60
    fname = f'{request.param[1]}_func'
61
62
    exec(f'def {fname}(x):\n'
63
         f'    """ela Docstring"""\n'
64
         f'    return x + {request.param[1][1]}')
65
66
    user_function = locals()[fname]
67
    app_phi_function.register(*request.param[0])(user_function)
68
69
    return {
70
        'func': user_function,
71
        'expected_key_in_registry': request.param[2],
72
        'user_defined_function_name': f'{request.param[1]}_func',
73
        'user_function_code_id': request.param[1],
74
    }
75
76
77
def test_sc1(phi_function_def, phi_registry, assert_function_properties_values, test_phi):
78
    assert phi_function_def['expected_key_in_registry'] in phi_registry
79
    assert_function_properties_values(phi_function_def['func'], f"{phi_function_def['user_defined_function_name']}",
80
                                      'ela Docstring')
81
    assert_function_properties_values(phi_registry.get(phi_function_def['expected_key_in_registry']),
82
                                      phi_function_def['user_defined_function_name'], 'ela Docstring')
83
    assert phi_registry.get(phi_function_def['expected_key_in_registry'])(1) == \
84
           test_phi[phi_function_def['user_function_code_id']](1)
85
86
87
@pytest.fixture(params=[
88
    (['edw'], 'f1', 'edw'),
89
    ([''], 'f2', 'f2'),
90
    ([], 'f3', 'f3'),
91
])
92
def phi_class_def(request, phi_registry, app_phi_function, test_phi):
93
94
    def __call__(self, data, **kwargs):
95
        """Add a small integer to the input number.
96
97
        Args:
98
            data ([type]): numerical value
99
100
        Returns:
101
            [type]: the result of the addition
102
        """
103
        return test_phi[(request.param[1])](data)
104
105
    class_obj = type(request.param[1], (object,), {
106
        '__call__': __call__,
107
    })
108
    app_phi_function.register(*request.param[0])(class_obj)
109
110
    return {
111
        'func': class_obj,
112
        'expected_key_in_registry': request.param[2],
113
        'expected_phi_logic': request.param[1],
114
    }
115
116
117
def test_sc2(phi_class_def, phi_registry, assert_class_properties_values, test_phi):
118
    assert phi_class_def['expected_key_in_registry'] in phi_registry
119
    assert_class_properties_values(phi_class_def['func'], phi_class_def['expected_phi_logic'],
120
                                   phi_class_def['func'].__call__.__doc__)
121
    assert_class_properties_values(phi_registry.get(phi_class_def['expected_key_in_registry']),
122
                                   phi_class_def['expected_phi_logic'], phi_class_def['func'].__call__.__doc__)
123
    assert phi_registry.get(phi_class_def['expected_key_in_registry'])(1) == \
124
           test_phi[phi_class_def['expected_phi_logic']](1)
125
126
127
#     def gg(x):
128
#         return x
129
#
130
#     test_value = 1
131
#     assert gg(test_value) == test_value
132
#     assert phi_registry.get('gg')(test_value) == test_value * 2
133
#
134
#     with pytest.raises(registering_phi_at_the_same_key_error):
135
#         PhiFunction.register('')(gg)
136
#
137
#     assert phi_registry.get('gg')(test_value) == test_value * 2
138