| 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 | |||
| 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) | ||
| 132 |