| Conditions | 2 | 
| Total Lines | 82 | 
| Code Lines | 54 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | import pytest | ||
| 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 assert_class_properties_values(): | ||
| 25 | def _f(class_obj, expected_name: str, expected_doc: str): | ||
| 26 | assert class_obj.__name__ == expected_name | ||
| 27 | assert class_obj.__call__.__doc__ == expected_doc | ||
| 28 | return _f | ||
| 29 | |||
| 30 | |||
| 31 | @pytest.fixture | ||
| 32 | def phi_registry(somagic): | ||
| 33 | """Magic Phi function registry, listening to registration operations at runtime.""" | ||
| 34 | return somagic._data_manager.built_phis | ||
| 35 | |||
| 36 | |||
| 37 | @pytest.fixture | ||
| 38 | def test_phi(): | ||
| 39 |     return { | ||
| 40 | 'f1': lambda x: x + 1, | ||
| 41 | 'f2': lambda x: x + 2, | ||
| 42 | 'f3': lambda x: x + 3, | ||
| 43 | 'f4': lambda x: x + 4, | ||
| 44 | 'f5': lambda x: x + 5, | ||
| 45 | 'f6': lambda x: x + 6, | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | def test_initial_phi_registry(phi_registry): | ||
| 50 |     assert phi_registry.registry == {} | ||
| 51 | assert 'any-key' not in phi_registry | ||
| 52 | |||
| 53 | |||
| 54 | @pytest.fixture(params=[ | ||
| 55 | (['pame'], 'f1', 'pame'), | ||
| 56 | ([''], 'f2', 'f2_func'), | ||
| 57 | ([], 'f3', 'f3_func'), | ||
| 58 | ]) | ||
| 59 | def phi_function_def(request, phi_registry, app_phi_function, test_phi): | ||
| 60 |     fname = f'{request.param[1]}_func' | ||
| 61 | |||
| 62 |     exec(f'def {fname}(x):\n' | ||
| 63 | f' """ela Docstring"""\n' | ||
| 64 |          f'    return x + {request.param[1][1]}') | ||
| 65 | |||
| 66 | user_function = locals()[fname] | ||
| 67 | app_phi_function.register(*request.param[0])(user_function) | ||
| 68 | |||
| 69 |     return { | ||
| 70 | 'func': user_function, | ||
| 71 | 'expected_key_in_registry': request.param[2], | ||
| 72 |         'user_defined_function_name': f'{request.param[1]}_func', | ||
| 73 | 'user_function_code_id': request.param[1], | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | def test_sc1(phi_function_def, phi_registry, assert_function_properties_values, test_phi): | ||
| 78 | assert phi_function_def['expected_key_in_registry'] in phi_registry | ||
| 79 |     assert_function_properties_values(phi_function_def['func'], f"{phi_function_def['user_defined_function_name']}", | ||
| 80 | 'ela Docstring') | ||
| 81 | assert_function_properties_values(phi_registry.get(phi_function_def['expected_key_in_registry']), | ||
| 82 | phi_function_def['user_defined_function_name'], 'ela Docstring') | ||
| 83 | assert phi_registry.get(phi_function_def['expected_key_in_registry'])(1) == \ | ||
| 84 | test_phi[phi_function_def['user_function_code_id']](1) | ||
| 85 | |||
| 86 | |||
| 87 | @pytest.fixture(params=[ | ||
| 88 | (['edw'], 'f1', 'edw'), | ||
| 89 | ([''], 'f2', 'f2'), | ||
| 90 | ([], 'f3', 'f3'), | ||
| 91 | ]) | ||
| 92 | def phi_class_def(request, phi_registry, app_phi_function, test_phi): | ||
| 93 | |||
| 94 | def __call__(self, data, **kwargs): | ||
| 95 | """Add a small integer to the input number. | ||
| 96 | |||
| 97 | Args: | ||
| 138 |