From bdf0e7884ffcde40d83b92e587e6eaab91a93c36 Mon Sep 17 00:00:00 2001 From: iamarnavgarg Date: Sun, 10 Nov 2019 09:56:29 +0530 Subject: [PATCH] Incorporated the changes suggested by @amercader --- ckan/logic/validators.py | 19 +++++++----------- ckan/tests/logic/test_validators.py | 31 +++++++++-------------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/ckan/logic/validators.py b/ckan/logic/validators.py index dd835642013..a7436d160df 100644 --- a/ckan/logic/validators.py +++ b/ckan/logic/validators.py @@ -1,4 +1,5 @@ -g encoding: utf-8 + +# encoding: utf-8 import collections import datetime @@ -859,19 +860,13 @@ def email_validator(value, context): raise Invalid(_('Email {email} is not a valid format').format(email=value)) return value -def one_of(value,context): +def one_of(list_of_value): ''' Implementation of OneOf method for Python Library Formencode ''' + def callable(value): + if value not in list_of_values: + raise Invalid(_('Value must be one of {}'.format(list_of_value))) + return value - if (value == '' or value == None): - raise Invalid(_('A value must be provided')) - - else: - if value: - for x in context: - if (x == value): - return value - - raise Invalid(_('value not found') diff --git a/ckan/tests/logic/test_validators.py b/ckan/tests/logic/test_validators.py index c9cebe56a03..f3a894c10b1 100644 --- a/ckan/tests/logic/test_validators.py +++ b/ckan/tests/logic/test_validators.py @@ -1,4 +1,5 @@ -g encoding: utf-8 + +# encoding: utf-8 '''Unit tests for ckan/logic/validators.py. @@ -669,29 +670,15 @@ def call_validator(*args, **kwargs): class TestOneOfValidator(object): - def test_val_true(self): - val = 1 - cont = [1,2,3,4] - y = validators.one_of(val,cont) - if y is not None: - self.assert_equals(y,1) - - def test_val_false(self): - val = 0 - cont = [1,2,3,4] - y = validators.one_of(val,cont) - self.assertIs(y,False) - - def test_val(self): - val = '' + def test_val_in_list(self): cont = [1,2,3,4] - y = validators.one_of(val,cont) - self.assertIs(y,False) + func = validators.one_of(cont) + assert_equals(func(1),1) - def test_val_none(self): - val = None + @raises_Invalid + def test_val_not_in_list(self): cont = [1,2,3,4] - y = validators.one_of(val,cont) - self.assertIs(y,False) + y = validators.one_of(cont) + raises_invalid(func)(5) # TODO: Need to test when you are not providing owner_org and the validator queries for the dataset with package_show