示例#1
0
 def test_string_field_max_length_validator(self):
     ctx = mock_context()
     v = StringFieldMaxLengthValidator(80)
     field = base_fields.Field()
     field._attribute = 'bar'
     field._type = str
     badness = {
         'foo.bar': ['This should not exceed the expected string length.']
     }
     for (value, expected) in (
         ('A', {}),
         (5 * 'A', {}),
         (10 * 'A', {}),
         (40 * 'A', {}),
         (79 * 'A', {}),
         (80 * 'A', {}),
         (81 * 'A', badness),
         (85 * 'A', badness),
         (100 * 'A', badness),
         (200 * 'A', badness),
         (500 * 'A', badness),
     ):
         error_dict = {}
         v.find_errors(error_dict, ctx, 'foo', None, field, value)
         self.assertEqual(expected, error_dict)
示例#2
0
 def test_int_field_max_validator(self):
     ctx = mock_context()
     v = IntFieldMaxValidator(11)
     field = base_fields.Field()
     field._attribute = 'bar'
     field._type = int
     badness = {
         'foo.bar':
         ['This value should be less than or equal to the maximum.']
     }
     for (value, expected) in (('0', {}), ('5', {}), ('11', {}),
                               ('23', badness), ('90210', badness)):
         error_dict = {}
         v.find_errors(error_dict, ctx, 'foo', None, field, value)
         self.assertEqual(expected, error_dict)
示例#3
0
 def test_datetime_field_max_validator(self):
     ctx = mock_context()
     v = DatetimeFieldMaxValidator(now)
     field = base_fields.Field()
     field._attribute = 'bar'
     field._type = datetime
     badness = {
         'foo.bar':
         ['This value should be no later than the maximum datetime.']
     }
     for (value, expected) in ((ancient, {}), (long_ago, {}), (now, {}),
                               (later, badness), (too_late, badness)):
         error_dict = {}
         v.find_errors(error_dict, ctx, 'foo', None, field,
                       value.isoformat())
         self.assertEqual(expected, error_dict)
示例#4
0
 def test_int_field_range_validator(self):
     ctx = mock_context()
     v = IntFieldRangeValidator(4, 11)
     field = base_fields.Field()
     field._attribute = 'bar'
     field._type = int
     badness = {
         'foo.bar':
         ['This value should be within the allowed integer range.']
     }
     for (value, expected) in (('0', badness), ('3', badness), ('4', {}),
                               ('5', {}), ('10', {}), ('11', {}),
                               ('12', badness), ('23', badness), ('90210',
                                                                  badness)):
         error_dict = {}
         v.find_errors(error_dict, ctx, 'foo', None, field, value)
         self.assertEqual(expected, error_dict)
示例#5
0
 def test_string_field_zipcode_validator(self):
     ctx = mock_context()
     v = StringFieldZipcodeValidator()
     field = base_fields.Field()
     field._attribute = 'bar'
     field._type = str
     badness = {'foo.bar': ['This should be a zipcode.']}
     for (value, expected) in (
         ('02134', {}),
         ('01701', {}),
         ('01701-7627', {}),
         ('90210', {}),
         ('65536', {}),
         ('01234567890', badness),
         ('01234-567890', badness),
     ):
         error_dict = {}
         v.find_errors(error_dict, ctx, 'foo', None, field, value)
         self.assertEqual(expected, error_dict)