| 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 |  |  | @pytest.fixture(params=[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     ['test-pd'],  # client backend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     ['pd'],  # so magic built in (default) backend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | def engine_backend(request, built_in_n_client_backends): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     return built_in_n_client_backends.backends[request.param[0]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | def test_delegate_sanity_check2(built_in_n_client_backends, engine_backend, tabular_operators, data_manager): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     dt_manager = data_manager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     assert dt_manager.engine.backend.id == 'pd' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     dt_manager.engine.backend = engine_backend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     assert dt_manager.engine.backend.id == engine_backend.id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     assert all(all(hasattr(getattr(engine_backend, operator_interface_name)(), required_method_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                    for required_method_name in required_methods) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                for operator_interface_name, required_methods in tabular_operators['required_methods']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | def test_client_backend(built_in_n_client_backends, tabular_operators): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     # just invoke all methods of each operator as a 'smoke test' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     assert all(all(getattr(built_in_n_client_backends.implementations['test-pd'][operator_interface_name](), m) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                    (*list([None] * tabular_operators['get_nb_args'](operator_interface_name, m))) == m | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                    for m in required_methods) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |                for operator_interface_name, required_methods in tabular_operators['required_methods']) | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |