async def test__validation_error_required(self): field = fields.Integer() with self.assertRaises(exceptions.ValidationError) as context: await field.validate(None) self.assertEqual(context.exception.error, fields.Field.errors['required'])
async def test_validate__invalid_item(self): field = fields.Array(default=['j'], field=fields.Integer()) with self.assertRaises(exceptions.ValidationError) as context: await field.validate(None) self.assertEqual(context.exception.error, {0: fields.Integer.errors['invalid']})
async def test__validation_error(self): field = fields.Integer() with self.assertRaises(exceptions.ValidationError) as context: await field.validate('a') self.assertEqual(context.exception.error, fields.Integer.errors['invalid'])
async def test_validate__max_value(self): field = fields.Integer(default=10, max_value=9) with self.assertRaises(exceptions.ValidationError) as context: await field.validate(None) self.assertEqual(context.exception.error, fields.Numeric.errors['max_value'].format(field))
async def test_validation__validators(self): async def validator(value): raise exceptions.ValidationError(value) field = fields.Integer(validators=[validator]) with self.assertRaises(exceptions.ValidationError) as context: await field.validate(1) self.assertEqual(context.exception.error, 1)
async def test_deserialize__schema_error(self): field = fields.Map(schema={'id': fields.Integer()}) with self.assertRaises(exceptions.ValidationError) as context: await field.deserialize({'id': 'i'}) self.assertEqual( {'id': fields.Integer.errors['invalid']}, context.exception.error, )
async def test_deserialize__schema(self): field = fields.Map(schema={'id': fields.Integer()}) self.assertEqual({'id': 1}, await field.deserialize({'id': '1'}))
async def test_serialize__with_field(self): field = fields.Array(field=fields.Integer()) self.assertEqual([1], await field.serialize([1]))
async def test_validate(self): field = fields.Array(field=fields.Integer()) self.assertEqual([10], await field.validate(['10']))
def test_callable_default(self): field = fields.Integer(default=lambda: 1 + 1) self.assertEqual(2, field.default)
async def test_validate(self): field = fields.Integer() self.assertEqual(10, await field.validate('10'))