Added tests for URLField and DecimalField
This commit is contained in:
parent
4451843a39
commit
5b2dbfe007
@ -57,23 +57,36 @@ class StringField(BaseField):
|
||||
value = re.compile(regex % value, flags)
|
||||
return value
|
||||
|
||||
|
||||
class URLField(StringField):
|
||||
"""A field that validates input as a URL.
|
||||
"""
|
||||
|
||||
def __init__(self, verify_exists=True, **kwargs):
|
||||
URL_REGEX = re.compile(
|
||||
r'^https?://'
|
||||
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|'
|
||||
r'localhost|'
|
||||
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
|
||||
r'(?::\d+)?'
|
||||
r'(?:/?|[/?]\S+)$', re.IGNORECASE
|
||||
)
|
||||
|
||||
def __init__(self, verify_exists=False, **kwargs):
|
||||
self.verify_exists = verify_exists
|
||||
super(URLField, self).__init__(**kwargs)
|
||||
|
||||
def validate(self, value):
|
||||
import urllib2
|
||||
if not URLField.URL_REGEX.match(value):
|
||||
raise ValidationError('Invalid URL: %s' % value)
|
||||
|
||||
if self.verify_exists:
|
||||
import urllib2
|
||||
try:
|
||||
request = urllib2.Request(value)
|
||||
response = urllib2.urlopen(request)
|
||||
except Exception, e:
|
||||
raise ValidationError('This URL appears to be invalid: %s' % e)
|
||||
message = 'This URL appears to be a broken link: %s' % e
|
||||
raise ValidationError(message)
|
||||
|
||||
|
||||
class IntField(BaseField):
|
||||
@ -148,7 +161,7 @@ class DecimalField(BaseField):
|
||||
if self.min_value is not None and value < self.min_value:
|
||||
raise ValidationError('Decimal value is too small')
|
||||
|
||||
if self.max_value is not None and vale > self.max_value:
|
||||
if self.max_value is not None and value > self.max_value:
|
||||
raise ValidationError('Decimal value is too large')
|
||||
|
||||
|
||||
|
@ -79,6 +79,19 @@ class FieldTest(unittest.TestCase):
|
||||
|
||||
person.name = 'Shorter name'
|
||||
person.validate()
|
||||
|
||||
def test_url_validation(self):
|
||||
"""Ensure that URLFields validate urls properly.
|
||||
"""
|
||||
class Link(Document):
|
||||
url = URLField()
|
||||
|
||||
link = Link()
|
||||
link.url = 'google'
|
||||
self.assertRaises(ValidationError, link.validate)
|
||||
|
||||
link.url = 'http://www.google.com:8080'
|
||||
link.validate()
|
||||
|
||||
def test_int_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to int fields.
|
||||
@ -114,6 +127,26 @@ class FieldTest(unittest.TestCase):
|
||||
person.height = 4.0
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
def test_decimal_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to decimal fields.
|
||||
"""
|
||||
class Person(Document):
|
||||
height = DecimalField(min_value=Decimal('0.1'),
|
||||
max_value=Decimal('3.5'))
|
||||
|
||||
person = Person()
|
||||
person.height = Decimal('1.89')
|
||||
person.validate()
|
||||
|
||||
person.height = '2.0'
|
||||
person.validate()
|
||||
person.height = 0.01
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
person.height = Decimal('0.01')
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
person.height = Decimal('4.0')
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
def test_boolean_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to boolean fields.
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user