diff --git a/solar/solar/system_log/change.py b/solar/solar/system_log/change.py index 69ae8cb6..f1ad5c5c 100644 --- a/solar/solar/system_log/change.py +++ b/solar/solar/system_log/change.py @@ -167,24 +167,32 @@ def _revert_remove(logitem): signals.connect(emmiter_obj, receiver_obj, {emitter_input: receiver_input}) +def _update_inputs_connections(res_obj, args, old_connections, new_connections): + res_obj.update(args) + + for emitter, _, receiver, _ in old_connections: + emmiter_obj = resource.load(emitter) + receiver_obj = resource.load(receiver) + signals.disconnect(emmiter_obj, receiver_obj) + + + for emitter, emitter_input, receiver, receiver_input in new_connections: + emmiter_obj = resource.load(emitter) + receiver_obj = resource.load(receiver) + signals.connect(emmiter_obj, receiver_obj, {emitter_input: receiver_input}) + + def _revert_update(logitem): """Revert of update should update inputs and connections """ res_obj = resource.load(logitem.res) commited = res_obj.load_commited() + args_to_update = dictdiffer.revert(logitem.diff, commited.inputs) - res_obj.update(args_to_update) - - for emitter, _, receiver, _ in commited.connections: - emmiter_obj = resource.load(emitter) - receiver_obj = resource.load(receiver) - signals.disconnect(emmiter_obj, receiver_obj) - connections = dictdiffer.revert(logitem.signals_diff, sorted(commited.connections)) - for emitter, emitter_input, receiver, receiver_input in connections: - emmiter_obj = resource.load(emitter) - receiver_obj = resource.load(receiver) - signals.connect(emmiter_obj, receiver_obj, {emitter_input: receiver_input}) + + _update_inputs_connections( + res_obj, args_to_update, commited.connections, connections) def _revert_run(logitem): @@ -200,11 +208,21 @@ def _discard_remove(item): resource_obj = resource.load(item.res) resource_obj.set_created() -_discard_update = _revert_update -_discard_run = _revert_run + +def _discard_update(item): + resource_obj = resource.load(item.res) + old_connections = resource_obj.connections + new_connections = dictdiffer.revert(item.signals_diff, old_connections) + args = dictdiffer.revert(item.diff, resource_obj.args) + _update_inputs_connections( + resource_obj, args, old_connections, new_connections) + +def _discard_run(item): + resource.load(item.res).remove(force=True) def discard_uids(uids): + staged_log = data.SL() for uid in uids: item = staged_log.get(uid) diff --git a/solar/solar/test/test_system_log_api.py b/solar/solar/test/test_system_log_api.py index a467060c..96cba082 100644 --- a/solar/solar/test/test_system_log_api.py +++ b/solar/solar/test/test_system_log_api.py @@ -178,5 +178,80 @@ def test_revert_create(): assert len(staged_log) == 1 for item in staged_log: operations.move_to_commited(item.log_action) - resources = orm.DBResource.load_all() - assert resources == [] + assert orm.DBResource.load_all() == [] + + +def test_discard_all_pending_changes_resources_created(): + res1 = orm.DBResource(id='test1', name='test1', base_path='x') + res1.save() + res1.add_input('a', 'str', '9') + + res2 = orm.DBResource(id='test2', name='test2', base_path='x') + res2.save() + res2.add_input('a', 'str', 0) + + staged_log = change.stage_changes() + assert len(staged_log) == 2 + + change.discard_all() + staged_log = change.stage_changes() + assert len(staged_log) == 0 + assert orm.DBResource.load_all() == [] + + +def test_discard_connection(): + res1 = orm.DBResource(id='test1', name='test1', base_path='x') + res1.save() + res1.add_input('a', 'str', '9') + + res2 = orm.DBResource(id='test2', name='test2', base_path='x') + res2.save() + res2.add_input('a', 'str', '0') + + staged_log = change.stage_changes() + for item in staged_log: + operations.move_to_commited(item.log_action) + + res1 = resource.load('test1') + res2 = resource.load('test2') + signals.connect(res1, res2) + staged_log = change.stage_changes() + assert len(staged_log) == 1 + assert res2.args == {'a': '9'} + change.discard_all() + assert res2.args == {'a': '0'} + assert len(change.stage_changes()) == 0 + + +def test_discard_removed(): + res1 = orm.DBResource(id='test1', name='test1', base_path='x') + res1.save() + res1.add_input('a', 'str', '9') + staged_log = change.stage_changes() + for item in staged_log: + operations.move_to_commited(item.log_action) + res1 = resource.load('test1') + res1.remove() + assert len(change.stage_changes()) == 1 + assert res1.to_be_removed() + + change.discard_all() + + assert len(change.stage_changes()) == 0 + assert not resource.load('test1').to_be_removed() + + +def test_discard_update(): + res1 = orm.DBResource(id='test1', name='test1', base_path='x') + res1.save() + res1.add_input('a', 'str', '9') + staged_log = change.stage_changes() + for item in staged_log: + operations.move_to_commited(item.log_action) + res1 = resource.load('test1') + res1.update({'a': '11'}) + assert len(change.stage_changes()) == 1 + assert res1.args == {'a': '11'} + + change.discard_all() + assert res1.args == {'a': '9'}