forked from ckan/ckan
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ckan#1894] Fix convert_to_extras to allow use with normal extras
`convert_to_extras` was adding the custom fields in an unflattened way during validation to the object that would be later unflattened. This worked fine if there were only `convert_to_extras` fields, but not if you were also providing actual extras, eg data_dict = { 'name': 'test-dataset', 'custom_text': 'Hi', 'custom_text2': 'Hi2', 'extras': [ {'key': 'proper_extra', 'value': 'Bye'}, ] } This fixes the `convert_to_extras` output and adds new converters test for it.
- Loading branch information
Showing
2 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import nose | ||
|
||
from ckan import model | ||
import ckan.plugins as p | ||
import ckan.lib.plugins as lib_plugins | ||
from ckan.lib.navl.dictization_functions import validate | ||
|
||
|
||
eq_ = nose.tools.eq_ | ||
|
||
|
||
class TestConvertToExtras(object): | ||
|
||
@classmethod | ||
def setup_class(cls): | ||
p.load('example_idatasetform') | ||
|
||
@classmethod | ||
def teardown_class(cls): | ||
p.unload('example_idatasetform') | ||
|
||
def test_convert_to_extras_field_gets_stored_as_extra(self): | ||
|
||
data_dict = { | ||
'name': 'test-dataset', | ||
'custom_text': 'Hi', | ||
} | ||
|
||
context = { | ||
'model': model, | ||
'session': model.Session, | ||
} | ||
|
||
package_plugin = lib_plugins.lookup_package_plugin('dataset') | ||
schema = package_plugin.create_package_schema() | ||
|
||
data, errors = validate(data_dict, schema, context) | ||
|
||
assert 'extras' in data | ||
eq_(len(data['extras']), 1) | ||
eq_(data['extras'][0]['key'], 'custom_text') | ||
eq_(data['extras'][0]['value'], 'Hi') | ||
|
||
def test_convert_to_extras_field_can_be_combined_with_a_proper_extra(self): | ||
|
||
data_dict = { | ||
'name': 'test-dataset', | ||
'custom_text': 'Hi', | ||
'extras': [ | ||
{'key': 'proper_extra', 'value': 'Bye'}, | ||
|
||
] | ||
} | ||
|
||
context = { | ||
'model': model, | ||
'session': model.Session, | ||
} | ||
|
||
package_plugin = lib_plugins.lookup_package_plugin('dataset') | ||
schema = package_plugin.create_package_schema() | ||
|
||
data, errors = validate(data_dict, schema, context) | ||
|
||
assert 'extras' in data | ||
eq_(len(data['extras']), 2) | ||
eq_(sorted([e['key'] for e in data['extras']]), | ||
['custom_text', 'proper_extra']) | ||
eq_(sorted([e['value'] for e in data['extras']]), | ||
['Bye', 'Hi']) | ||
|
||
def test_convert_to_extras_field_can_be_combined_with_more_extras(self): | ||
|
||
data_dict = { | ||
'name': 'test-dataset', | ||
'custom_text': 'Hi', | ||
'extras': [ | ||
{'key': 'proper_extra', 'value': 'Bye'}, | ||
{'key': 'proper_extra2', 'value': 'Bye2'}, | ||
] | ||
} | ||
|
||
context = { | ||
'model': model, | ||
'session': model.Session, | ||
} | ||
|
||
package_plugin = lib_plugins.lookup_package_plugin('dataset') | ||
schema = package_plugin.create_package_schema() | ||
|
||
data, errors = validate(data_dict, schema, context) | ||
|
||
assert 'extras' in data | ||
eq_(len(data['extras']), 3) | ||
eq_(sorted([e['key'] for e in data['extras']]), | ||
['custom_text', 'proper_extra', 'proper_extra2']) | ||
eq_(sorted([e['value'] for e in data['extras']]), | ||
['Bye', 'Bye2', 'Hi']) |