Passed
Push — mpeta ( 8bcafa...508e7a )
by Konstantinos
101:05 queued 99:27
created

test_delegate.test_client_backend()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import inspect
2
import pytest
3
4
5
@pytest.fixture
6
def client_pandas_tabular_implementation():
7
    """Client code that defines an Engine Backend"""
8
    from so_magic.data.interfaces import TabularRetriever, TabularIterator, TabularMutator
9
    
10
    class TestPDTabularRetrieverDelegate(TabularRetriever):
11
12
        @classmethod
13
        def column(cls, identifier, data):
14
            return inspect.currentframe().f_code.co_name
15
16
        def row(self, identifier, data):
17
            return inspect.currentframe().f_code.co_name
18
19
        @classmethod
20
        def nb_columns(cls, data):
21
            return inspect.currentframe().f_code.co_name
22
23
        @classmethod
24
        def nb_rows(cls, data):
25
            return inspect.currentframe().f_code.co_name
26
27
        @classmethod
28
        def get_numerical_attributes(cls, data):
29
            return inspect.currentframe().f_code.co_name
30
31
    class TestPDTabularIteratorDelegate(TabularIterator):
32
33
        def columnnames(self, data):
34
            return inspect.currentframe().f_code.co_name
35
36
        @classmethod
37
        def iterrows(cls, data):
38
            return inspect.currentframe().f_code.co_name
39
40
        @classmethod
41
        def itercolumns(cls, data):
42
            return inspect.currentframe().f_code.co_name
43
44
    class TestPDTabularMutatorDelegate(TabularMutator):
45
46
        @classmethod
47
        def add_column(cls, datapoints, values, new_attribute, **kwargs):
48
            return inspect.currentframe().f_code.co_name
49
50
    BACKEND = {
51
        'backend_id': 'test-pd',
52
        'backend_name': 'test-pandas',
53
        'interfaces': [
54
            TestPDTabularRetrieverDelegate,
55
            TestPDTabularIteratorDelegate,
56
            TestPDTabularMutatorDelegate,
57
        ]
58
    }
59
60
    return BACKEND
61
62
63
@pytest.fixture
64
def built_in_n_client_backends(built_in_backends, client_pandas_tabular_implementation):
65
    CLIENT_BACKENDS = [
66
        client_pandas_tabular_implementation,
67
    ]
68
69
    built_in_backends.add(*CLIENT_BACKENDS)
70
    return built_in_backends
71
72
73
def test_delegate_sanity_check(built_in_n_client_backends, tabular_operators, data_manager):
74
    dt_manager = data_manager()
75
    # assert that the data engine initial (default) backend is "pandas-backend"
76
    # could need to change in the future if we give the client the option to initialize the engine with a backend of
77
    # their preference
78
    assert dt_manager.engine.backend.id == 'pd'
79
80
    for backend_id, implementations_data in built_in_n_client_backends:
81
        dt_manager.engine.backend = built_in_n_client_backends.backends[backend_id]
82
        assert dt_manager.engine.backend.id == backend_id
83
        assert all(all(required_method_name in dir(implementations_data[operator_interface_name])
84
                       for required_method_name in required_methods)
85
                   for operator_interface_name, required_methods in tabular_operators['required_methods'])
86
87
88
def test_client_backend(built_in_n_client_backends, tabular_operators):
89
90
    for operator_interface_name, required_methods in tabular_operators['required_methods']:
91
        for m in required_methods:
92
            nb_args = tabular_operators['get_nb_args'](operator_interface_name, m)
93
            # we have to initialize an instance out of an operator class like we do in the 'observations_command' method in the Backend
94
            # class (eg cls.retriever(), cls.iterator(), cls.mutator())
95
            assert getattr(built_in_n_client_backends.implementations['test-pd'][operator_interface_name](), m)(*list([None] * nb_args)) == m
96
97
        # assert all(getattr(built_in_n_client_backends.implementations['test-pd'][operator_interface_name], method)(None, None) == method for method in required_methods)
98