|
| 1 | +from labthings import views, fields |
| 2 | +from labthings.schema import Schema |
| 3 | +from labthings.marshalling.args import use_args, use_body |
| 4 | + |
| 5 | + |
| 6 | +def test_use_body_string(app, client): |
| 7 | + class Index(views.MethodView): |
| 8 | + @use_body(fields.String(required=True)) |
| 9 | + def post(self, args): |
| 10 | + return args |
| 11 | + |
| 12 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 13 | + |
| 14 | + with client: |
| 15 | + res = client.post("/", data="string", content_type="application/json") |
| 16 | + assert res.data.decode() == "string" |
| 17 | + |
| 18 | + |
| 19 | +def test_use_body_no_data_error(app, client): |
| 20 | + class Index(views.MethodView): |
| 21 | + @use_body(fields.String(required=True)) |
| 22 | + def post(self, args): |
| 23 | + return args |
| 24 | + |
| 25 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 26 | + |
| 27 | + with client: |
| 28 | + res = client.post("/", content_type="application/json") |
| 29 | + assert res.status_code == 400 |
| 30 | + |
| 31 | + |
| 32 | +def test_use_body_no_data_missing(app, client): |
| 33 | + class Index(views.MethodView): |
| 34 | + @use_body(fields.String(missing="default")) |
| 35 | + def post(self, args): |
| 36 | + return args |
| 37 | + |
| 38 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 39 | + |
| 40 | + with client: |
| 41 | + res = client.post("/", content_type="application/json") |
| 42 | + assert res.data.decode() == "default" |
| 43 | + |
| 44 | + |
| 45 | +def test_use_body_validation_error(app, client): |
| 46 | + class Index(views.MethodView): |
| 47 | + @use_body(fields.String(required=True)) |
| 48 | + def post(self, args): |
| 49 | + return args |
| 50 | + |
| 51 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 52 | + |
| 53 | + with client: |
| 54 | + res = client.post("/", json={"foo": "bar"}, content_type="application/json") |
| 55 | + assert res.status_code == 400 |
| 56 | + |
| 57 | + |
| 58 | +def test_use_args_field(app, client): |
| 59 | + class Index(views.MethodView): |
| 60 | + @use_args(fields.String(required=True)) |
| 61 | + def post(self, args): |
| 62 | + return args |
| 63 | + |
| 64 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 65 | + |
| 66 | + with client: |
| 67 | + res = client.post("/", data="string", content_type="application/json") |
| 68 | + assert res.data.decode() == "string" |
| 69 | + |
| 70 | + |
| 71 | +def test_use_args_map(app, client): |
| 72 | + class Index(views.MethodView): |
| 73 | + @use_args({"foo": fields.String(required=True)}) |
| 74 | + def post(self, args): |
| 75 | + return args |
| 76 | + |
| 77 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 78 | + |
| 79 | + with client: |
| 80 | + res = client.post("/", json={"foo": "bar"}, content_type="application/json") |
| 81 | + assert res.json == {"foo": "bar"} |
| 82 | + |
| 83 | + |
| 84 | +def test_use_args_schema(app, client): |
| 85 | + class TestSchema(Schema): |
| 86 | + foo = fields.String(required=True) |
| 87 | + |
| 88 | + class Index(views.MethodView): |
| 89 | + @use_args(TestSchema()) |
| 90 | + def post(self, args): |
| 91 | + return args |
| 92 | + |
| 93 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 94 | + |
| 95 | + with client: |
| 96 | + res = client.post("/", json={"foo": "bar"}, content_type="application/json") |
| 97 | + assert res.json == {"foo": "bar"} |
0 commit comments