def test_validation_works(self): self.data["validation_required"] = "123" with self.assertRaises(ValidationError) as ctx: compat.dump(self.schema, self.data) # it's some sort of date error self.assertIn("'str' object has no attribute 'isoformat'", ctx.exception.messages[0])
def test_validation_works(self): self.data["validation_required"] = "123" with self.assertRaises(ValidationError) as ctx: compat.dump(self.schema, self.data) # it's some sort of date error self.assertIn( "Not a valid datetime", ctx.exception.messages["validation_required"][0] )
def test_serialize(self): data = compat.dump(StringList(), {"foos": ["bar"]}) self.assertEqual(data["foos"], "bar") data = compat.dump(StringList(), {"foos": ["bar", "baz"]}) self.assertEqual(data["foos"], "bar,baz") data = compat.dump(IntegerList(), {"foos": [1, 2]}) self.assertEqual(data["foos"], "1,2")
def test_serialize_errors(self): with self.assertRaises(ValidationError) as ctx: compat.dump(IntegerList(), {"foos": [42, "two"]}) self.assertEqual( ctx.exception.messages, { # Element 1 in our list should produce error: "foos": {1: ["Not a valid integer."]} }, )
def test_serialize_errors(self): with self.assertRaises(ValidationError) as ctx: compat.dump(IntegerList(), {"foos": [1, "two"]}) self.assertEqual( ctx.exception.messages, { # Marshmallow's fields.List formats the dump errors differently # than load :shrug: "foos": ["Not a valid integer."] }, )
def marshal(data, schema): """ Dumps an object with the given marshmallow.Schema. :raises: marshmallow.ValidationError if the given data fails validation of the schema. """ schema = normalize_schema(schema) return compat.dump(schema=schema, data=data)
def test_value_optional_missing(self): del self.data["value_optional"] with self.assertRaises(ValidationError) as ctx: compat.dump(self.schema, self.data) self.assertIn("value_optional", ctx.exception.messages)
def test_required_none(self): self.data["value_required"] = None with self.assertRaises(ValidationError) as ctx: compat.dump(self.schema, self.data) self.assertIn("value_required", ctx.exception.messages)
def test_required_failed_validate(self): self.data["one_of_validation"] = "c" with self.assertRaises(ValidationError) as ctx: compat.dump(self.schema, self.data) self.assertIn("one_of_validation", ctx.exception.messages)
def test_serialize_errors(self): with self.assertRaises(ValidationError) as ctx: compat.dump(IntegerList(), {"foos": [42, "two"]}) self.assertEqual(ctx.exception.messages, ["invalid literal for int() with base 10: 'two'"])