예제 #1
0
 def test_validation_error_response(self):
     """
     Test that a validation error response is returned when
     object validation fails.
     """
     
     v = schema.validators
     class PersonSchema(schema.ObjectSchema):
         first_name = v.String()
         last_name = v.String()
         
     schema.bind_schema("Person", PersonSchema)
     
     res = self.post("/person", self.person_cls(1, "smith"))
     self.assertEqual(res.status_code, 400)
     self.assertIn("Expected str got int instead.", res.data)
예제 #2
0
 def test_validation_error(self):
     
     v = schema.validators
     
     class PersonSchema(schema.ObjectSchema):
         first_name = v.String()
         last_name = v.String()
 
     schema.bind_schema("Person", PersonSchema)
     
     request = get_request(self.person_cls(1, "smith"))    
     with self.assertRaises(JsonWebBadRequest) as c:
         person = request.json
         
     error = c.exception
     self.assertIn("Error validating object", str(error))
     self.assertEqual("Expected str got int instead.", str(error.extra["fields"]["first_name"]))
예제 #3
0
    def test_validation_error_response(self):
        """
        Test that a validation error response is returned when
        object validation fails.
        """

        v = schema.validators

        class PersonSchema(schema.ObjectSchema):
            first_name = v.String()
            last_name = v.String()

        schema.bind_schema("Person", PersonSchema)

        res = self.post("/person", self.person_cls(1, "smith"))
        self.assertEqual(res.status_code, 400)
        self.assertIn("Expected str got int instead.", res.data)
예제 #4
0
    def test_validation_error(self):

        v = schema.validators

        class PersonSchema(schema.ObjectSchema):
            first_name = v.String()
            last_name = v.String()

        schema.bind_schema("Person", PersonSchema)

        request = get_request(self.person_cls(1, "smith"))
        with self.assertRaises(JsonWebBadRequest) as c:
            person = request.json

        error = c.exception
        self.assertIn("Error validating object", str(error))
        self.assertEqual("Expected str got int instead.",
                         str(error.extra["fields"]["first_name"]))
예제 #5
0
        self.name = name
        if things:
            self.things = things


# JsonWeb Schemas

v = schema.validators
class ThingSchema(schema.ObjectSchema):
    name = v.String(32)

class WidgetSchema(schema.ObjectSchema):
    name = v.String(max_len=80)
    things = v.List(v.EnsureType(Thing))
    
schema.bind_schema("Widget", WidgetSchema)
schema.bind_schema("Thing", ThingSchema)


# Views

@app.route("/widget", methods=["POST"])
@json_view(expects=Widget)
def create_widget():
    widget = request.json
    db.session.add(widget)
    db.session.commit()
    return {"status": "ok"}


@app.route("/widget/<int:widget_id>")