Skip to content

Commit

Permalink
Merge branch '621-bug-move-extras_validation_into___before'
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Apr 16, 2013
2 parents 061fdc4 + 98e98f0 commit f75d21b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ckan/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def default_create_tag_schema():

def default_create_package_schema():
schema = {
'__before': [duplicate_extras_key, ignore],
'id': [empty],
'revision_id': [ignore],
'name': [not_empty, unicode, name_validator, package_name_validator],
Expand All @@ -139,7 +140,6 @@ def default_create_package_schema():
'tags': default_tags_schema(),
'tag_string': [ignore_missing, tag_string_convert],
'extras': default_extras_schema(),
'extras_validation': [duplicate_extras_key, ignore],
'save': [ignore],
'return_to': [ignore],
'relationships_as_object': default_relationship_schema(),
Expand Down
4 changes: 3 additions & 1 deletion ckan/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ def duplicate_extras_key(key, data, errors, context):
for extra_key in set(extras_keys):
extras_keys.remove(extra_key)
if extras_keys:
errors[key].append(_('Duplicate key "%s"') % extras_keys[0])
key_ = ('extras_validation',)
assert key_ not in errors
errors[key_] = [_('Duplicate key "%s"') % extras_keys[0]]

def group_name_validator(key, data, errors, context):
model = context['model']
Expand Down
36 changes: 34 additions & 2 deletions ckan/tests/logic/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,40 @@ def test_42_resource_search_accessible_via_get_request(self):
assert "index" in resource['description'].lower()
assert "json" in resource['format'].lower()

def test_package_create_duplicate_extras_error(self):
import ckan.tests
import paste.fixture
import pylons.test

# Posting a dataset dict to package_create containing two extras dicts
# with the same key, should return a Validation Error.
app = paste.fixture.TestApp(pylons.test.pylonsapp)
error = ckan.tests.call_action_api(app, 'package_create',
apikey=self.sysadmin_user.apikey, status=409,
name='foobar', extras=[{'key': 'foo', 'value': 'bar'},
{'key': 'foo', 'value': 'gar'}])
assert error['__type'] == 'Validation Error'
assert error['extras_validation'] == ['Duplicate key "foo"']

def test_package_update_duplicate_extras_error(self):
import ckan.tests
import paste.fixture
import pylons.test

# We need to create a package first, so that we can update it.
app = paste.fixture.TestApp(pylons.test.pylonsapp)
package = ckan.tests.call_action_api(app, 'package_create',
apikey=self.sysadmin_user.apikey, name='foobar')

# Posting a dataset dict to package_update containing two extras dicts
# with the same key, should return a Validation Error.
package['extras'] = [{'key': 'foo', 'value': 'bar'},
{'key': 'foo', 'value': 'gar'}]
error = ckan.tests.call_action_api(app, 'package_update',
apikey=self.sysadmin_user.apikey, status=409, **package)
assert error['__type'] == 'Validation Error'
assert error['extras_validation'] == ['Duplicate key "foo"']

class TestActionTermTranslation(WsgiAppCase):

@classmethod
Expand Down Expand Up @@ -1553,5 +1587,3 @@ def test_02_bulk_delete(self):

res = self.app.get('/api/action/package_search?q=*:*')
assert json.loads(res.body)['result']['count'] == 0


0 comments on commit f75d21b

Please sign in to comment.