Add tests for discard changes

This commit is contained in:
Dmitry Shulyak 2015-09-28 13:06:57 +03:00
parent 32cbbaeed8
commit 5d677264ff
2 changed files with 108 additions and 15 deletions

View File

@ -167,24 +167,32 @@ def _revert_remove(logitem):
signals.connect(emmiter_obj, receiver_obj, {emitter_input: receiver_input}) 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): def _revert_update(logitem):
"""Revert of update should update inputs and connections """Revert of update should update inputs and connections
""" """
res_obj = resource.load(logitem.res) res_obj = resource.load(logitem.res)
commited = res_obj.load_commited() commited = res_obj.load_commited()
args_to_update = dictdiffer.revert(logitem.diff, commited.inputs) 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)) connections = dictdiffer.revert(logitem.signals_diff, sorted(commited.connections))
for emitter, emitter_input, receiver, receiver_input in connections:
emmiter_obj = resource.load(emitter) _update_inputs_connections(
receiver_obj = resource.load(receiver) res_obj, args_to_update, commited.connections, connections)
signals.connect(emmiter_obj, receiver_obj, {emitter_input: receiver_input})
def _revert_run(logitem): def _revert_run(logitem):
@ -200,11 +208,21 @@ def _discard_remove(item):
resource_obj = resource.load(item.res) resource_obj = resource.load(item.res)
resource_obj.set_created() 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): def discard_uids(uids):
staged_log = data.SL() staged_log = data.SL()
for uid in uids: for uid in uids:
item = staged_log.get(uid) item = staged_log.get(uid)

View File

@ -178,5 +178,80 @@ def test_revert_create():
assert len(staged_log) == 1 assert len(staged_log) == 1
for item in staged_log: for item in staged_log:
operations.move_to_commited(item.log_action) operations.move_to_commited(item.log_action)
resources = orm.DBResource.load_all() assert orm.DBResource.load_all() == []
assert resources == []
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'}