def test_bind_with_invalid_name(self): class Parent(Contract): pass field = fields.Field() self.assertRaises(ValueError, field.bind, None, Parent) self.assertRaises(ValueError, field.bind, '', Parent)
def test_custom_error_message(self): field = fields.Field(error_messages={'custom': 'custom fail'}) with self.assertRaises(ValidationError) as e: field._fail('custom') self.assertEqual(e.exception.messages, ['custom fail'])
def test_null_error_message(self): field = fields.Field() with self.assertRaises(ValidationError) as e: field._fail('null') self.assertEqual(e.exception.messages, ['This field may not be null.'])
def test_bind_with_invalid_parent(self): class InvalidContract: pass field = fields.Field() self.assertRaises(ValueError, field.bind, 'field1', None) self.assertRaises(ValueError, field.bind, 'field1', '') self.assertRaises(ValueError, field.bind, 'field1', 1) self.assertRaises(ValueError, field.bind, 'field1', InvalidContract)
def test_bind(self): class Parent(Contract): pass field = fields.Field() field.bind('field1', Parent) self.assertEqual(field.dump_to, 'field1') self.assertEqual(field.load_from, 'field1') self.assertEqual(field.name, 'field1') self.assertEqual(field.parent, Parent)
def test_dict_error_message(self): field = fields.Field(error_messages={ 'invalid': { 'message': 'error message', 'code': 123 } }) with self.assertRaises(ValidationError) as e: field._fail('invalid') self.assertEqual(e.exception.messages, [{ 'message': 'error message', 'code': 123 }])
def test_disallow_none(self): field = fields.Field(allow_none=False) self._load_raises(field, None, ['This field may not be null.'])
def test_allow_none(self): field = fields.Field(allow_none=True) self._load_equal(field, None, None)
def test_non_required(self): field = fields.Field(required=False) self._load_equal(field, missing, missing) self._load_equal(field, 'abc', 'abc')
def test_required(self): field = fields.Field(required=True) self._load_raises(field, missing, ['This field is required.']) self._load_equal(field, 'abc', 'abc')
def test_validator_returning_false(self): def validator(value): return False field = fields.Field(validators=[validator]) self._load_raises(field, 123, ['Invalid value.'])
def test_callable_default(self): field = fields.Field(default=lambda: 123) self._load_equal(field, missing, 123)
def test_default(self): field = fields.Field(default=123) self._load_equal(field, missing, 123)
def test_not_found_error_message(self): field = fields.Field() with self.assertRaises(AssertionError) as e: field._fail('not_found')
def test_validator_raising_error(self): def validator(value): raise ValueError() field = fields.Field(validators=[validator]) self.assertRaises(ValueError, field.load, 123, None)
def test_bypass_default_on_loading(self): field = fields.Field(default=123) self._load_equal(field, 456, 456)
def test_validator(self): def validator(value): pass field = fields.Field(validators=[validator]) self._load_equal(field, 123, 123)
def test_validator_returning_true(self): def validator(value): return True field = fields.Field(validators=[validator]) self._load_equal(field, 123, 123)