Skip to content

Commit

Permalink
Fix and test for include_total not working, because of the default() …
Browse files Browse the repository at this point in the history
…validator was broken.
  • Loading branch information
David Read authored and smotornyuk committed Jun 16, 2019
1 parent b8f0dad commit 0bc3ad6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ckan/lib/navl/validators.py
Expand Up @@ -78,7 +78,7 @@ def default(default_value):
def callable(key, data, errors, context):

value = data.get(key)
if not value or value is missing:
if value is None or value == '' or value is missing:
data[key] = default_value

return callable
Expand Down
15 changes: 0 additions & 15 deletions ckan/tests/legacy/lib/test_navl.py
Expand Up @@ -186,21 +186,6 @@ def test_basic_errors():

assert errors == {('__junk',): [u"The input field [('4', 1, '30')] was not expected."], ('1',): [u'Missing value'], ('__extras',): [u'The input field __extras was not expected.']}, errors

def test_default():
schema = {
"__junk": [ignore],
"__extras": [ignore, default("weee")],
"__before": [ignore],
"__after": [ignore],
"0": [default("default")],
"1": [default("default")],
}

converted_data, errors = validate_flattened(data, schema)

assert not errors
assert converted_data == {('1',): 'default', ('0',): '0 value'}, converted_data


def test_flatten():

Expand Down
36 changes: 33 additions & 3 deletions ckan/tests/lib/navl/test_validators.py
Expand Up @@ -8,6 +8,10 @@
import nose.tools

import ckan.tests.factories as factories
import ckan.lib.navl.validators as validators


eq_ = nose.tools.eq_


def returns_None(function):
Expand Down Expand Up @@ -222,7 +226,6 @@ def test_ignore_missing_with_value_missing(self):
'''
import ckan.lib.navl.dictization_functions as df
import ckan.lib.navl.validators as validators

for value in (None, df.missing, 'skip'):

Expand Down Expand Up @@ -251,8 +254,6 @@ def test_ignore_missing_with_a_value(self):
nothing.
'''
import ckan.lib.navl.validators as validators

key = ('key to be validated',)
data = factories.validator_data_dict()
data[key] = 'value to be validated'
Expand All @@ -265,3 +266,32 @@ def test_ignore_missing_with_a_value(self):
def call_validator(*args, **kwargs):
return validators.ignore_missing(*args, **kwargs)
call_validator(key=key, data=data, errors=errors, context={})


class TestDefault(object):
def test_key_doesnt_exist(self):
dict_ = {}
validators.default('default_value')('key', dict_, {}, {})
eq_(dict_, {'key': 'default_value'})

def test_value_is_none(self):
dict_ = {'key': None}
validators.default('default_value')('key', dict_, {}, {})
eq_(dict_, {'key': 'default_value'})

def test_value_is_empty_string(self):
dict_ = {'key': ''}
validators.default('default_value')('key', dict_, {}, {})
eq_(dict_, {'key': 'default_value'})

def test_value_is_false(self):
# False is a consciously set value, so should not be changed to the
# default
dict_ = {'key': False}
validators.default('default_value')('key', dict_, {}, {})
eq_(dict_, {'key': False})

def test_already_has_a_value(self):
dict_ = {'key': 'original'}
validators.default('default_value')('key', dict_, {}, {})
eq_(dict_, {'key': 'original'})
2 changes: 1 addition & 1 deletion ckanext/datastore/controller.py
Expand Up @@ -139,7 +139,7 @@ def result_page(offs, lim):
'offset': offs,
'sort': '_id',
'records_format': records_format,
'include_total': 'false', # XXX: default() is broken
'include_total': 'false',
})

result = result_page(offset, limit)
Expand Down
12 changes: 12 additions & 0 deletions ckanext/datastore/tests/test_search.py
Expand Up @@ -634,6 +634,18 @@ def test_search_is_unsuccessful_when_called_with_invalid_fields(self):
assert res_dict['success'] is False
assert res_dict['error'].get('fields') is not None, res_dict['error']

def test_search_without_total(self):
data = {'resource_id': self.data['resource_id'],
'include_total': False}
postparams = '%s=1' % json.dumps(data)
auth = {'Authorization': str(self.normal_user.apikey)}
res = self.app.post('/api/action/datastore_search', params=postparams,
extra_environ=auth)
res_dict = json.loads(res.body)
assert res_dict['success'] is True
result = res_dict['result']
assert 'total' not in result


class TestDatastoreFullTextSearch(DatastoreLegacyTestBase):
@classmethod
Expand Down

0 comments on commit 0bc3ad6

Please sign in to comment.