Skip to content

Commit

Permalink
Incorporated the changes suggested by @amercader
Browse files Browse the repository at this point in the history
  • Loading branch information
iamarnavgarg committed Nov 10, 2019
1 parent 47e87a1 commit bdf0e78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
19 changes: 7 additions & 12 deletions ckan/logic/validators.py
@@ -1,4 +1,5 @@
g encoding: utf-8

# encoding: utf-8

import collections
import datetime
Expand Down Expand Up @@ -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')

31 changes: 9 additions & 22 deletions ckan/tests/logic/test_validators.py
@@ -1,4 +1,5 @@
g encoding: utf-8

# encoding: utf-8

'''Unit tests for ckan/logic/validators.py.
Expand Down Expand Up @@ -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

0 comments on commit bdf0e78

Please sign in to comment.