def test_field_translate_none(self): f = Field('f', required=True) self.assertRaises(ValidationException, f.translate, None, validate=True) resp = f.translate(None, validate=False) self.assertIsNone(resp)
def test_validate_required_success(self): """ Tests when the object is required and it is there """ f = Field('f', required=True) resp = validate_required(f, 1) self.assertEqual(resp, 1)
def test_validate_regex(self): """Success case for validate_regex""" regex = re.compile(r'[a-z]*something[a-z]*') f = Field('f') obj = 'dfgfgfsomethingdfgfgfs' resp = validate_regex(f, obj, regex) self.assertEqual(resp, obj)
def test_validate_regex_multiple_matches(self): """Tests that the validation succeeds if there is more than one match""" regex = re.compile(r'something') f = Field('f') obj = 'somethingsomethingsomething' resp = validate_regex(f, obj, regex) self.assertEqual(resp, obj)
def test_validate_required_skipped(self): """ Tests when the item is None and the validation is skipped because skip_required is True """ f = Field('f', required=True) resp = validate_required(f, None, skip_required=True) self.assertEqual(resp, None)
def test_validate_required_non_required_field(self): """ Tests the the field is validated if the field is not required and the object is None """ f = Field('f', required=False) resp = validate_required(f, None, skip_required=False) self.assertEqual(resp, None)
def test_basic_validation_none_object(self, validate_type): """Check that validate_type is not called when object is None""" f = Field('f') basic_validation(f, None) self.assertFalse(validate_type.called) # Also ensure that it is called basic_validation(f, object()) self.assertTrue(validate_type.called)
def test_validate_size_too_large(self): f = Field('blah') obj = 10 self.assertRaises(ValidationException, validate_size, f, obj, obj, maximum=9)
def test_validate_size_too_small(self): f = Field('blah') obj = 10 self.assertRaises(ValidationException, validate_size, f, obj, obj, minimum=11)
def __init__(self, name, indv_field=None, minimum=None, maximum=None, **kwargs): """ :param unicode name: The name of the field. :param BaseField indv_field: The field to use when translating and validating individual items in the list. """ self.indv_field = indv_field or Field('list') super(ListField, self).__init__(name, **kwargs) self.minimum = minimum self.maximum = maximum
def test_field_translate(self): f = Field('f') obj = object() resp = f.translate(obj) self.assertIs(obj, resp)
def test_validate_size(self): """Success case for validate size""" f = Field('blah') obj = 10 resp = validate_size(f, obj, obj, minimum=10, maximum=10) self.assertEqual(resp, obj)
def test_validate_type_failure(self): """Failure case for validate_type""" f = Field('f') obj = 'blah' self.assertRaises(ValidationException, validate_type, f, int, obj)
def test_validate_type(self): """Success case for validate_type""" f = Field('f') obj = object() resp = validate_type(f, object, obj) self.assertIs(resp, obj)
def test_validate_required_failure(self): """Tests that it fails when the object is None""" f = Field('f', required=True) self.assertRaises(ValidationException, validate_required, f, None)
def test_validate_regex_failure(self): """Failure case for validate_regex""" regex = re.compile(r'something') f = Field('f') obj = 'notthatsome' self.assertRaises(ValidationException, validate_regex, f, obj, regex)
def test_basic_validation_required_failure(self): """Ensure failure when object is None and required""" f = Field('f', required=True) self.assertRaises(ValidationException, basic_validation, f, None)
def test_basic_validation_skip_required(self): """Tests when skip_required is True and the object is None""" f = Field('f', required=True) resp = basic_validation(f, None, skip_required=True) self.assertIsNone(resp)
def test_basic_validation(self): """Success case for basic_validation""" f = Field('f', required=True) obj = object() resp = basic_validation(f, obj, skip_required=False) self.assertIs(resp, obj)