def test_revert_create(): res = DBResource.from_dict('test1', {'name': 'test1', 'base_path': 'x', 'state': RESOURCE_STATE.created.name, 'meta_inputs': {'a': {'value': None, 'schema': 'str'}}}) res.inputs['a'] = '9' res.save_lazy() ModelMeta.save_all_lazy() staged_log = change.stage_changes() assert len(staged_log) == 1 logitem = staged_log[0] operations.move_to_commited(logitem.log_action) assert logitem.diff == [['add', '', [['a', '9']]]] commited = CommitedResource.get('test1') assert commited.inputs == {'a': '9'} change.revert(logitem.uid) staged_log = change.stage_changes() assert len(staged_log) == 1 for item in staged_log: operations.move_to_commited(item.log_action) assert resource.load_all() == []
def move_to_commited(log_action, *args, **kwargs): sl = data.SL() item = next((i for i in sl if i.log_action == log_action), None) if item: resource_obj = resource.load(item.resource) commited = CommitedResource.get_or_create(item.resource) updated = resource_obj.db_obj.updated if item.action == CHANGES.remove.name: resource_obj.delete() commited.state = resource.RESOURCE_STATE.removed.name else: resource_obj.set_operational() commited.state = resource.RESOURCE_STATE.operational.name commited.base_path = item.base_path updated = resource_obj.db_obj.updated # required to update `updated` field resource_obj.db_obj.save() commited.inputs = patch(item.diff, commited.inputs) # TODO fix TagsWrp to return list # commited.tags = resource_obj.tags sorted_connections = sorted(commited.connections) commited.connections = patch(item.connections_diff, sorted_connections) commited.save() item.log = 'history' item.state = 'success' item.updated = updated item.save()
def test_revert_create(): res = DBResource.from_dict( "test1", { "name": "test1", "base_path": "x", "state": RESOURCE_STATE.created.name, "meta_inputs": {"a": {"value": None, "schema": "str"}}, }, ) res.inputs["a"] = "9" res.save_lazy() ModelMeta.save_all_lazy() staged_log = change.stage_changes() assert len(staged_log) == 1 logitem = staged_log[0] operations.move_to_commited(logitem.log_action) assert logitem.diff == [["add", "", [["a", "9"]]]] commited = CommitedResource.get("test1") assert commited.inputs == {"a": "9"} change.revert(logitem.uid) staged_log = change.stage_changes() assert len(staged_log) == 1 for item in staged_log: operations.move_to_commited(item.log_action) assert resource.load_all() == []
def _revert_remove(logitem): """Resource should be created with all previous connections""" commited = CommitedResource.get(logitem.resource) args = dictdiffer.revert(logitem.diff, commited.inputs) connections = dictdiffer.revert( logitem.connections_diff, sorted(commited.connections)) resource.Resource(logitem.resource, logitem.base_path, args=_get_args_to_update(args, connections), tags=commited.tags) 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})
def test_revert_removal(): res = create_resource('test1') res.inputs['a'] = '9' res.save_lazy() commited = CommitedResource.from_dict('test1', { 'inputs': { 'a': '9' }, 'state': 'operational' }) commited.save_lazy() resource_obj = resource.load(res.name) resource_obj.remove() ModelMeta.save_all_lazy() log_item = change.create_remove(resource_obj.name) log_item.save() uid = log_item.uid assert log_item.diff == [['remove', '', [['a', '9']]]] operations.commit_log_item(log_item) ModelMeta.save_all_lazy() with mock.patch.object(repository.Repository, 'read_meta') as mread: mread.return_value = { 'input': { 'a': { 'schema': 'str!' } }, 'id': 'mocked' } with mock.patch.object(repository.Repository, 'get_path') as mpath: mpath.return_value = 'x' change.revert(uid) ModelMeta.save_all_lazy() resource_obj = resource.load('test1') assert resource_obj.args == { 'a': '9', 'location_id': '', 'transports_id': '' }
def test_revert_removal(): res = DBResource.from_dict('test1', {'name': 'test1', 'base_path': 'x', 'state': RESOURCE_STATE.created.name, 'meta_inputs': {'a': {'value': None, 'schema': 'str'}}}) res.inputs['a'] = '9' res.save_lazy() commited = CommitedResource.from_dict('test1', {'inputs': {'a': '9'}, 'state': 'operational'}) commited.save_lazy() resource_obj = resource.load(res.name) resource_obj.remove() ModelMeta.save_all_lazy() changes = change.stage_changes() assert len(changes) == 1 assert changes[0].diff == [['remove', '', [['a', '9']]]] operations.move_to_commited(changes[0].log_action) clear_cache() assert DBResource._c.obj_cache == {} # assert DBResource.bucket.get('test1').siblings == [] with mock.patch.object(repository.Repository, 'read_meta') as mread: mread.return_value = { 'input': {'a': {'schema': 'str!'}}, 'id': 'mocked' } with mock.patch.object(repository.Repository, 'get_path') as mpath: mpath.return_value = 'x' change.revert(changes[0].uid) ModelMeta.save_all_lazy() # assert len(DBResource.bucket.get('test1').siblings) == 1 resource_obj = resource.load('test1') assert resource_obj.args == { 'a': '9', 'location_id': '', 'transports_id': '' }
def test_revert_create(): res = create_resource('test1') res.db_obj.inputs['a'] = '9' logitem = change.create_run(res) assert logitem.diff == [['add', '', [['a', '9']]]] uid = logitem.uid operations.commit_log_item(logitem) commited = CommitedResource.get('test1') assert commited.inputs == {'a': '9'} change.revert(uid) ModelMeta.save_all_lazy() staged_log = change.staged_log() assert len(staged_log) == 1 for item in staged_log: operations.commit_log_item(item) assert resource.load_all() == []
def commit_log_item(item): resource_obj = resource.load(item.resource) commited = CommitedResource.get_or_create(item.resource) if item.action == CHANGES.remove.name: resource_obj.delete() commited.state = resource.RESOURCE_STATE.removed.name else: resource_obj.set_operational() commited.state = resource.RESOURCE_STATE.operational.name commited.base_path = item.base_path resource_obj.db_obj.save_lazy() commited.inputs = patch(item.diff, commited.inputs) # TODO fix TagsWrp to return list # commited.tags = resource_obj.tags sorted_connections = sorted(commited.connections) commited.connections = patch(item.connections_diff, sorted_connections) commited.save_lazy() item.to_history().save_lazy() item.delete()
def test_revert_removal(): res = DBResource.from_dict('test1', {'name': 'test1', 'base_path': 'x', 'state': RESOURCE_STATE.created.name, 'meta_inputs': {'a': {'value': None, 'schema': 'str'}}}) res.inputs['a'] = '9' res.save_lazy() commited = CommitedResource.from_dict('test1', {'inputs': {'a': '9'}, 'state': 'operational'}) commited.save_lazy() resource_obj = resource.load(res.name) resource_obj.remove() ModelMeta.save_all_lazy() changes = change.stage_changes() assert len(changes) == 1 assert changes[0].diff == [['remove', '', [['a', '9']]]] operations.move_to_commited(changes[0].log_action) ModelMeta.session_start() assert DBResource._c.obj_cache == {} assert DBResource.bucket.get('test1').siblings == [] with mock.patch.object(resource, 'read_meta') as mread: mread.return_value = { 'input': {'a': {'schema': 'str!'}}, 'id': 'mocked' } change.revert(changes[0].uid) ModelMeta.save_all_lazy() assert len(DBResource.bucket.get('test1').siblings) == 1 resource_obj = resource.load('test1') assert resource_obj.args == { 'a': '9', 'location_id': '', 'transports_id': '' }
def test_revert_create(): res = create_resource('test1') res.inputs['a'] = '9' res.save_lazy() logitem = change.create_run(res.name) assert logitem.diff == [['add', '', [['a', '9']]]] uid = logitem.uid operations.commit_log_item(logitem) commited = CommitedResource.get('test1') assert commited.inputs == {'a': '9'} change.revert(uid) ModelMeta.save_all_lazy() staged_log = change.staged_log() assert len(staged_log) == 1 for item in staged_log: operations.commit_log_item(item) assert resource.load_all() == []
def test_revert_removal(): res = create_resource('test1') res.inputs['a'] = '9' res.save_lazy() commited = CommitedResource.from_dict('test1', {'inputs': {'a': '9'}, 'state': 'operational'}) commited.save_lazy() resource_obj = resource.load(res.name) resource_obj.remove() ModelMeta.save_all_lazy() log_item = change.create_remove(resource_obj.name) log_item.save() uid = log_item.uid assert log_item.diff == [['remove', '', [['a', '9']]]] operations.commit_log_item(log_item) ModelMeta.save_all_lazy() with mock.patch.object(repository.Repository, 'read_meta') as mread: mread.return_value = { 'input': {'a': {'schema': 'str!'}}, 'id': 'mocked' } with mock.patch.object(repository.Repository, 'get_path') as mpath: mpath.return_value = 'x' change.revert(uid) ModelMeta.save_all_lazy() resource_obj = resource.load('test1') assert resource_obj.args == { 'a': '9', 'location_id': '', 'transports_id': '' }
def test_revert_removal(): res = DBResource.from_dict( "test1", { "name": "test1", "base_path": "x", "state": RESOURCE_STATE.created.name, "meta_inputs": {"a": {"value": None, "schema": "str"}}, }, ) res.inputs["a"] = "9" res.save_lazy() commited = CommitedResource.from_dict("test1", {"inputs": {"a": "9"}, "state": "operational"}) commited.save_lazy() resource_obj = resource.load(res.name) resource_obj.remove() ModelMeta.save_all_lazy() changes = change.stage_changes() assert len(changes) == 1 assert changes[0].diff == [["remove", "", [["a", "9"]]]] operations.move_to_commited(changes[0].log_action) ModelMeta.session_start() assert DBResource._c.obj_cache == {} assert DBResource.bucket.get("test1").siblings == [] with mock.patch.object(resource, "read_meta") as mread: mread.return_value = {"input": {"a": {"schema": "str!"}}, "id": "mocked"} change.revert(changes[0].uid) ModelMeta.save_all_lazy() assert len(DBResource.bucket.get("test1").siblings) == 1 resource_obj = resource.load("test1") assert resource_obj.args == {"a": "9", "location_id": "", "transports_id": ""}
def load_commited(self): return CommitedResource.get_or_create(self.name)
def clear_history(): LogItem.delete_all() CommitedResource.delete_all()