PageRenderTime 36ms CodeModel.GetById 25ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/validators/tests.py

https://code.google.com/p/mango-py/
Python | 161 lines | 152 code | 7 blank | 2 comment | 0 complexity | 7167bb6fc05a77f76ad4e2575d6e65d1 MD5 | raw file
  1# -*- coding: utf-8 -*-
  2import re
  3import types
  4from datetime import datetime, timedelta
  5
  6from django.core.exceptions import ValidationError
  7from django.core.validators import *
  8from django.utils.unittest import TestCase
  9
 10
 11NOW = datetime.now()
 12
 13TEST_DATA = (
 14    # (validator, value, expected),
 15    (validate_integer, '42', None),
 16    (validate_integer, '-42', None),
 17    (validate_integer, -42, None),
 18    (validate_integer, -42.5, None),
 19
 20    (validate_integer, None, ValidationError),
 21    (validate_integer, 'a', ValidationError),
 22
 23    (validate_email, 'email@here.com', None),
 24    (validate_email, 'weirder-email@here.and.there.com', None),
 25
 26    (validate_email, None, ValidationError),
 27    (validate_email, '', ValidationError),
 28    (validate_email, 'abc', ValidationError),
 29    (validate_email, 'a @x.cz', ValidationError),
 30    (validate_email, 'something@@somewhere.com', ValidationError),
 31
 32    (validate_slug, 'slug-ok', None),
 33    (validate_slug, 'longer-slug-still-ok', None),
 34    (validate_slug, '--------', None),
 35    (validate_slug, 'nohyphensoranything', None),
 36
 37    (validate_slug, '', ValidationError),
 38    (validate_slug, ' text ', ValidationError),
 39    (validate_slug, ' ', ValidationError),
 40    (validate_slug, 'some@mail.com', ValidationError),
 41    (validate_slug, 'ä? ???', ValidationError),
 42    (validate_slug, '\n', ValidationError),
 43
 44    (validate_ipv4_address, '1.1.1.1', None),
 45    (validate_ipv4_address, '255.0.0.0', None),
 46    (validate_ipv4_address, '0.0.0.0', None),
 47
 48    (validate_ipv4_address, '256.1.1.1', ValidationError),
 49    (validate_ipv4_address, '25.1.1.', ValidationError),
 50    (validate_ipv4_address, '25,1,1,1', ValidationError),
 51    (validate_ipv4_address, '25.1 .1.1', ValidationError),
 52
 53    (validate_comma_separated_integer_list, '1', None),
 54    (validate_comma_separated_integer_list, '1,2,3', None),
 55    (validate_comma_separated_integer_list, '1,2,3,', None),
 56
 57    (validate_comma_separated_integer_list, '', ValidationError),
 58    (validate_comma_separated_integer_list, 'a,b,c', ValidationError),
 59    (validate_comma_separated_integer_list, '1, 2, 3', ValidationError),
 60
 61    (MaxValueValidator(10), 10, None),
 62    (MaxValueValidator(10), -10, None),
 63    (MaxValueValidator(10), 0, None),
 64    (MaxValueValidator(NOW), NOW, None),
 65    (MaxValueValidator(NOW), NOW - timedelta(days=1), None),
 66
 67    (MaxValueValidator(0), 1, ValidationError),
 68    (MaxValueValidator(NOW), NOW + timedelta(days=1), ValidationError),
 69
 70    (MinValueValidator(-10), -10, None),
 71    (MinValueValidator(-10), 10, None),
 72    (MinValueValidator(-10), 0, None),
 73    (MinValueValidator(NOW), NOW, None),
 74    (MinValueValidator(NOW), NOW + timedelta(days=1), None),
 75
 76    (MinValueValidator(0), -1, ValidationError),
 77    (MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),
 78
 79    (MaxLengthValidator(10), '', None),
 80    (MaxLengthValidator(10), 10*'x', None),
 81
 82    (MaxLengthValidator(10), 15*'x', ValidationError),
 83
 84    (MinLengthValidator(10), 15*'x', None),
 85    (MinLengthValidator(10), 10*'x', None),
 86
 87    (MinLengthValidator(10), '', ValidationError),
 88
 89    (URLValidator(), 'http://www.djangoproject.com/', None),
 90    (URLValidator(), 'http://localhost/', None),
 91    (URLValidator(), 'http://example.com/', None),
 92    (URLValidator(), 'http://www.example.com/', None),
 93    (URLValidator(), 'http://www.example.com:8000/test', None),
 94    (URLValidator(), 'http://valid-with-hyphens.com/', None),
 95    (URLValidator(), 'http://subdomain.example.com/', None),
 96    (URLValidator(), 'http://200.8.9.10/', None),
 97    (URLValidator(), 'http://200.8.9.10:8000/test', None),
 98    (URLValidator(), 'http://valid-----hyphens.com/', None),
 99    (URLValidator(), 'http://example.com?something=value', None),
100    (URLValidator(), 'http://example.com/index.php?something=value&another=value2', None),
101
102    (URLValidator(), 'foo', ValidationError),
103    (URLValidator(), 'http://', ValidationError),
104    (URLValidator(), 'http://example', ValidationError),
105    (URLValidator(), 'http://example.', ValidationError),
106    (URLValidator(), 'http://.com', ValidationError),
107    (URLValidator(), 'http://invalid-.com', ValidationError),
108    (URLValidator(), 'http://-invalid.com', ValidationError),
109    (URLValidator(), 'http://inv-.alid-.com', ValidationError),
110    (URLValidator(), 'http://inv-.-alid.com', ValidationError),
111
112    (BaseValidator(True), True, None),
113    (BaseValidator(True), False, ValidationError),
114
115    (RegexValidator('.*'), '', None),
116    (RegexValidator(re.compile('.*')), '', None),
117    (RegexValidator('.*'), 'xxxxx', None),
118
119    (RegexValidator('x'), 'y', ValidationError),
120    (RegexValidator(re.compile('x')), 'y', ValidationError),
121)
122
123def create_simple_test_method(validator, expected, value, num):
124    if expected is not None and issubclass(expected, Exception):
125        test_mask = 'test_%s_raises_error_%d'
126        def test_func(self):
127            self.assertRaises(expected, validator, value)
128    else:
129        test_mask = 'test_%s_%d'
130        def test_func(self):
131            self.assertEqual(expected, validator(value))
132    if isinstance(validator, types.FunctionType):
133        val_name = validator.__name__
134    else:
135        val_name = validator.__class__.__name__
136    test_name = test_mask % (val_name, num)
137    return test_name, test_func
138
139# Dynamically assemble a test class with the contents of TEST_DATA
140
141class TestSimpleValidators(TestCase):
142    def test_single_message(self):
143        v = ValidationError('Not Valid')
144        self.assertEqual(str(v), "[u'Not Valid']")
145        self.assertEqual(repr(v), "ValidationError([u'Not Valid'])")
146
147    def test_message_list(self):
148        v = ValidationError(['First Problem', 'Second Problem'])
149        self.assertEqual(str(v), "[u'First Problem', u'Second Problem']")
150        self.assertEqual(repr(v), "ValidationError([u'First Problem', u'Second Problem'])")
151
152    def test_message_dict(self):
153        v = ValidationError({'first': 'First Problem'})
154        self.assertEqual(str(v), "{'first': 'First Problem'}")
155        self.assertEqual(repr(v), "ValidationError({'first': 'First Problem'})")
156
157test_counter = 0
158for validator, value, expected in TEST_DATA:
159    name, method = create_simple_test_method(validator, expected, value, test_counter)
160    setattr(TestSimpleValidators, name, method)
161    test_counter += 1