Skip to content

Commit

Permalink
[#4042] update extras only for deleted resource
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk authored and amercader committed Mar 12, 2018
1 parent e064de7 commit 165dc2f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ckanext/datastore/plugin.py
Expand Up @@ -193,7 +193,7 @@ def after_delete(self, context, resources):
'resource_id': res.id,
})
res.extras['datastore_active'] = False
res_query.update(
res_query.filter_by(id=res.id).update(
{'extras': res.extras}, synchronize_session=False)

# IDatastore
Expand Down
39 changes: 39 additions & 0 deletions ckanext/datastore/tests/test_delete.py
Expand Up @@ -125,6 +125,45 @@ def test_datastore_deleted_during_resource_deletion(self):
NotFound, helpers.call_action, 'datastore_search',
resource_id=resource_id)

def test_datastore_deleted_during_resource_only_for_deleted_resource(self):
package = factories.Dataset()
data = {
'boo%k': 'crime',
'author': ['tolstoy', 'dostoevsky'],
'package_id': package['id']
}

result_1 = helpers.call_action(
'datastore_create', resource=data.copy())
resource_id_1 = result_1['resource_id']

result_2 = helpers.call_action(
'datastore_create', resource=data.copy())
resource_id_2 = result_2['resource_id']

res_1 = model.Resource.get(resource_id_1)
res_2 = model.Resource.get(resource_id_2)

# `synchronize_session=False` and session cache requires
# refreshing objects
model.Session.refresh(res_1)
model.Session.refresh(res_2)
assert res_1.extras['datastore_active']
assert res_2.extras['datastore_active']

helpers.call_action('resource_delete', id=resource_id_1)

assert_raises(
NotFound, helpers.call_action, 'datastore_search',
resource_id=resource_id_1)
assert_raises(
NotFound, helpers.call_action, 'resource_show',
id=resource_id_1)
model.Session.refresh(res_1)
model.Session.refresh(res_2)
assert not res_1.extras['datastore_active']
assert res_2.extras['datastore_active']

def test_delete_invalid_resource_id(self):
postparams = '%s=1' % json.dumps({'resource_id': 'bad'})
auth = {'Authorization': str(self.sysadmin_user.apikey)}
Expand Down

0 comments on commit 165dc2f

Please sign in to comment.