|
| 1 | +import pytest |
| 2 | + |
| 3 | +from marshmallow import Schema as _Schema |
| 4 | +from flask import make_response |
| 5 | + |
| 6 | +from labthings.server.schema import Schema |
| 7 | +from labthings.server import fields |
| 8 | +from labthings.core.tasks.thread import TaskThread |
| 9 | + |
| 10 | +from labthings.server import decorators |
| 11 | + |
| 12 | + |
| 13 | +def common_task_test(marshaled_task: dict): |
| 14 | + assert isinstance(marshaled_task, dict) |
| 15 | + assert isinstance(marshaled_task.get("id"), str) |
| 16 | + assert marshaled_task.get("function") == "None(args=(), kwargs={})" |
| 17 | + assert marshaled_task.get("status") == "idle" |
| 18 | + |
| 19 | + |
| 20 | +def test_marshal_with_ma_schema(): |
| 21 | + def func(): |
| 22 | + obj = type("obj", (object,), {"integer": 1}) |
| 23 | + return obj |
| 24 | + |
| 25 | + schema = _Schema.from_dict({"integer": fields.Int()})() |
| 26 | + wrapped_func = decorators.marshal_with(schema)(func) |
| 27 | + |
| 28 | + assert wrapped_func() == ({"integer": 1}, 200, {}) |
| 29 | + |
| 30 | + |
| 31 | +def test_marshal_with_dict_schema(): |
| 32 | + def func(): |
| 33 | + obj = type("obj", (object,), {"integer": 1}) |
| 34 | + return obj |
| 35 | + |
| 36 | + schema = {"integer": fields.Int()} |
| 37 | + wrapped_func = decorators.marshal_with(schema)(func) |
| 38 | + |
| 39 | + assert wrapped_func() == ({"integer": 1}, 200, {}) |
| 40 | + |
| 41 | + |
| 42 | +def test_marshal_with_field_schema(): |
| 43 | + def func(): |
| 44 | + return 1 |
| 45 | + |
| 46 | + schema = fields.String() |
| 47 | + wrapped_func = decorators.marshal_with(schema)(func) |
| 48 | + |
| 49 | + assert wrapped_func() == ("1", 200, {}) |
| 50 | + |
| 51 | + |
| 52 | +def test_marshal_with_response_tuple_field_schema(app_ctx): |
| 53 | + def func(): |
| 54 | + return ("response", 200, {}) |
| 55 | + |
| 56 | + schema = fields.String() |
| 57 | + wrapped_func = decorators.marshal_with(schema)(func) |
| 58 | + |
| 59 | + with app_ctx.test_request_context(): |
| 60 | + assert wrapped_func() == ("response", 200, {}) |
| 61 | + |
| 62 | + |
| 63 | +def test_marshal_with_response_field_schema(app_ctx): |
| 64 | + def func(): |
| 65 | + return make_response("response", 200) |
| 66 | + |
| 67 | + schema = fields.String() |
| 68 | + wrapped_func = decorators.marshal_with(schema)(func) |
| 69 | + |
| 70 | + with app_ctx.test_request_context(): |
| 71 | + assert wrapped_func().data == b"response" |
| 72 | + |
| 73 | + |
| 74 | +def test_marshal_with_invalid_schema(): |
| 75 | + def func(): |
| 76 | + return 1 |
| 77 | + |
| 78 | + schema = object() |
| 79 | + with pytest.raises(TypeError): |
| 80 | + decorators.marshal_with(schema)(func) |
| 81 | + |
| 82 | + |
| 83 | +def test_marshal_task(app_ctx): |
| 84 | + def func(): |
| 85 | + return TaskThread() |
| 86 | + |
| 87 | + wrapped_func = decorators.marshal_task(func) |
| 88 | + |
| 89 | + with app_ctx.test_request_context(): |
| 90 | + out = wrapped_func() |
| 91 | + common_task_test(out[0]) |
| 92 | + |
| 93 | + |
| 94 | +def test_marshal_task_response_tuple(app_ctx): |
| 95 | + def func(): |
| 96 | + return (TaskThread(), 201, {}) |
| 97 | + |
| 98 | + wrapped_func = decorators.marshal_task(func) |
| 99 | + |
| 100 | + with app_ctx.test_request_context(): |
| 101 | + out = wrapped_func() |
| 102 | + common_task_test(out[0]) |
| 103 | + |
| 104 | + |
| 105 | +def test_marshal_task_response_invalid(app_ctx): |
| 106 | + def func(): |
| 107 | + return object() |
| 108 | + |
| 109 | + wrapped_func = decorators.marshal_task(func) |
| 110 | + |
| 111 | + with app_ctx.test_request_context(), pytest.raises(TypeError): |
| 112 | + wrapped_func() |
0 commit comments