Passed
Pull Request — dev (#32)
by Konstantinos
04:02 queued 02:26
created

client_pandas_tabular_implementation()   B

Complexity

Conditions 1

Size

Total Lines 60
Code Lines 39

Duplication

Lines 21
Ratio 35 %

Importance

Changes 0
Metric Value
cc 1
eloc 39
nop 0
dl 21
loc 60
rs 8.9439
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 client_pandas_tabular_implementation():
6
    from so_magic.data.interfaces import TabularRetriever, TabularIterator, TabularMutator
7
    
8 View Code Duplication
    class TestPDTabularRetrieverDelegate(TabularRetriever):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
        """The observation object is the same as the one you return from 'from_json_lines'"""
10
11
        @classmethod
12
        def column(cls, identifier, data):
13
            return data.observations[identifier]
14
15
        def row(self, identifier, data):
16
            return data.observations.loc(identifier)
17
18
        @classmethod
19
        def nb_columns(cls, data):
20
            return len(data.observations.columns)
21
22
        @classmethod
23
        def nb_rows(cls, data):
24
            return len(data.observations)
25
26
        @classmethod
27
        def get_numerical_attributes(cls, data):
28
            return data.observations._get_numeric_data().columns.values
29
30
31
    class TestPDTabularIteratorDelegate(TabularIterator):
32
        """The observation object is the same as the one your return from 'from_json_lines'"""
33
34
        def columnnames(self, data):
35
            return list(data.observations.columns)
36
37
        @classmethod
38
        def iterrows(cls, data):
39
            return iter(data.observations.iterrows())
40
41
        @classmethod
42
        def itercolumns(cls, data):
43
            return iter(data.observations[column] for column in data.observations.columns)
44
45
46
    class TestPDTabularMutatorDelegate(TabularMutator):
47
48
        @classmethod
49
        def add_column(cls, datapoints, values, new_attribute, **kwargs):
50
            datapoints.observations[new_attribute] = values
51
52
53
    BACKEND = {
54
        'backend_id': 'test-pd',
55
        'backend_name': 'test-pandas',
56
        'interfaces': [
57
            TestPDTabularRetrieverDelegate,
58
            TestPDTabularIteratorDelegate,
59
            TestPDTabularMutatorDelegate,
60
        ]
61
    }
62
63
    return BACKEND
64
65
66
@pytest.fixture
67
def engine_backends(client_pandas_tabular_implementation):
68
    CLIENT_BACKENDS = [
69
        client_pandas_tabular_implementation,
70
    ]
71
    from so_magic.data.backend.panda_handling.df_backend import magic_backends
72
73
    backends = magic_backends()
74
75
    backends.add(*CLIENT_BACKENDS)
76
    return backends
77
78
79
def test_delegate_sanity_check(engine_backends, data_manager):
80
    dt_manager = data_manager()
81
    # assert that the data engine initial (default) backend is "pandas-backend"
82
    # could need to change in the future if we give the client the option to initialize the engine with a backend of their preference
83
    assert dt_manager.engine.backend.id == 'pd'
84
    for backend_id, _backend_implementation in engine_backends:
85
        dt_manager.engine.backend = engine_backends.backends[backend_id]
86
        assert dt_manager.engine.backend.id == backend_id
87