Passed
Push — mpeta ( 55bdfc...e243a9 )
by Konstantinos
01:29
created

test_phi.app_phi_function()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
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 phi_registry():
25
    from so_magic.data.features.phi import phi_registry
26
    return phi_registry
27
28
29
def test_initial_phi_registry(phi_registry):
30
    assert phi_registry.objects == {}
31
    assert 'any-key' not in phi_registry
32
33
34
def test_phi_creation(app_phi_function, phi_registry, registering_phi_at_the_same_key_error,
35
                      assert_function_properties_values):
36
37
    PhiFunction = app_phi_function
38
39
    @PhiFunction.register('pame')
40
    def ela(x):
41
        """ela Docstring"""
42
        return x + 1
43
44
    assert 'pame' in phi_registry
45
    assert_function_properties_values(ela, 'ela', 'ela Docstring')
46
47
    assert phi_registry.get('pame').__name__ == 'ela'
48
    assert phi_registry.get('pame').__doc__ == 'ela Docstring'
49
    test_value = 5
50
    assert phi_registry.get('pame')(test_value) == test_value + 1
51
52
    @PhiFunction.register('')
53
    def gg(x):
54
        return x * 2
55
    assert 'gg' in phi_registry
56
57
    @PhiFunction.register()
58
    def dd(x):
59
        return x * 2
60
    assert 'dd' in phi_registry
61
    assert phi_registry.get('dd')(4) == 8
62
63
    @PhiFunction.register('edw')
64
    class Edw:
65
        def __call__(self, data, **kwargs):
66
            return data + 5
67
    assert 'edw' in phi_registry
68
    assert phi_registry.get('edw')(3) == 8
69
70
    @PhiFunction.register('')
71
    class qw:
72
        def __call__(self, data, **kwargs):
73
            """Subtract 5 from the input number.
74
75
            Args:
76
                data ([type]): numerical value
77
78
            Returns:
79
                [type]: the result of the subtraction
80
            """
81
            return data - 5
82
    
83
    assert 'qw' in phi_registry
84
    assert phi_registry.get('qw')(3) == -2
85
86
    assert_function_properties_values(phi_registry.get('qw'), 'qw', qw.__call__.__doc__)
87
88
    @PhiFunction.register()
89
    class Nai:
90
        def __call__(self, data, **kwargs):
91
            """Subtract 1 to the input number."""            
92
            return data - 1
93
94
    assert 'Nai' in phi_registry
95
96
    assert_function_properties_values(phi_registry.get('Nai'), 'Nai', 'Subtract 1 to the input number.')
97
    
98
    assert phi_registry.get('Nai')(3) == 2
99
100
    def gg(x):
101
        return x
102
103
    test_value = 1
104
    assert gg(test_value) == test_value
105
    assert phi_registry.get('gg')(test_value) == test_value * 2
106
107
    with pytest.raises(registering_phi_at_the_same_key_error):
108
        PhiFunction.register('')(gg)
109
110
    assert phi_registry.get('gg')(test_value) == test_value * 2
111