| Conditions | 1 |
| Total Lines | 75 |
| Code Lines | 52 |
| 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 |
||
| 52 | def test_data_manager(test_json_data): |
||
| 53 | import types |
||
| 54 | from green_magic.utils.commands import Command |
||
| 55 | from green_magic.data.backend.panda_handling.df_backend import PDTabularIterator, PDTabularRetriever, PDTabularReporter |
||
| 56 | from green_magic.data.features.phi import PhiFunction |
||
| 57 | |||
| 58 | DataEngine.new('test_pd') |
||
| 59 | assert 'test_pd' in DataEngine.subclasses |
||
| 60 | assert hasattr(DataEngine, 'state') |
||
| 61 | assert hasattr(DataEngine, 'registry') |
||
| 62 | |||
| 63 | data_api = DataManager(CommandsManager(), Backend(DataEngine.create('test_pd'))) |
||
| 64 | # make the datapoint_manager listen to newly created Datapoints objects events |
||
| 65 | assert hasattr(data_api, 'commands_manager') |
||
| 66 | assert hasattr(data_api.commands_manager, 'command') |
||
| 67 | assert hasattr(data_api.backend, 'datapoints_manager') |
||
| 68 | |||
| 69 | data_api.backend.engine.__class__.datapoints_factory.subject.attach(data_api.backend.datapoints_manager) |
||
| 70 | DataEngine.test_pd.command_factory.attach(data_api.commands_manager.command.accumulator) |
||
| 71 | PhiFunction.subject.attach(data_api.phis) |
||
| 72 | |||
| 73 | assert data_api.backend.engine.__class__.datapoints_factory not in PhiFunction.subject._observers |
||
| 74 | |||
| 75 | # test runtime command registration |
||
| 76 | import pandas as pd |
||
| 77 | @DataEngine.test_pd.dec() |
||
| 78 | def observations(file_path): |
||
| 79 | return pd.read_json(file_path, lines=True) |
||
| 80 | |||
| 81 | @DataEngine.test_pd.dec() |
||
| 82 | def add_attribute(_datapoints, values, new_attribute): |
||
| 83 | print("CORRECT") |
||
| 84 | _datapoints.observations[new_attribute] = values |
||
| 85 | |||
| 86 | assert type(DataEngine.test_pd) == type(DataEngine) |
||
| 87 | assert type(DataEngine.test_pd.registry) == dict |
||
| 88 | assert type(DataEngine.test_pd._commands) == dict |
||
| 89 | assert 'observations' in DataEngine.test_pd.registry |
||
| 90 | assert 'observations' in DataEngine.test_pd._commands |
||
| 91 | assert 'observations' not in DataEngine.registry |
||
| 92 | |||
| 93 | assert type(DataEngine.test_pd._commands['observations']) == Command |
||
| 94 | |||
| 95 | cmd = DataEngine.test_pd._commands['observations'] |
||
| 96 | # cmd = data_api.command.observations |
||
| 97 | cmd.args = [test_json_data['file_path']] |
||
| 98 | |||
| 99 | assert type(cmd._receiver) == types.FunctionType |
||
| 100 | assert cmd._method == '__call__' |
||
| 101 | assert cmd.args == [test_json_data['file_path']] |
||
| 102 | |||
| 103 | from green_magic.utils.commands import Invoker, CommandHistory |
||
| 104 | |||
| 105 | DataEngine.test_pd.retriever = PDTabularRetriever |
||
| 106 | DataEngine.test_pd.iterator = PDTabularIterator |
||
| 107 | DataEngine.test_pd.reporter = PDTabularReporter |
||
| 108 | |||
| 109 | inv = Invoker(CommandHistory()) |
||
| 110 | inv.execute_command(cmd) |
||
| 111 | |||
| 112 | datapoints = data_api.backend.datapoints_manager.datapoints |
||
| 113 | assert len(datapoints) == test_json_data['nb_lines'] |
||
| 114 | print(datapoints.attributes) |
||
| 115 | |||
| 116 | assert set(datapoints.attributes) == test_json_data['attributes'] |
||
| 117 | |||
| 118 | assert 'add_attribute' in DataEngine.test_pd.registry |
||
| 119 | |||
| 120 | cmd1 = DataEngine.test_pd._commands['add_attribute'] |
||
| 121 | # cmd1 = data_api.command.observations |
||
| 122 | cmd1.args = [datapoints, [_ for _ in range(1, len(datapoints) + 1)], 'test_attr'] |
||
| 123 | |||
| 124 | cmd1.execute() |
||
| 125 | |||
| 126 | assert set(datapoints.attributes) == set(_ for _ in list(test_json_data['attributes']) + ['test_attr']) |
||
| 127 |