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