|
| 1 | +import pytest |
| 2 | + |
| 3 | +from labthings.server.view import builder |
| 4 | + |
| 5 | + |
| 6 | +def test_property_of(app): |
| 7 | + obj = type("obj", (object,), {"property_name": "propertyValue"}) |
| 8 | + |
| 9 | + GeneratedClass = builder.property_of(obj, "property_name") |
| 10 | + |
| 11 | + assert callable(GeneratedClass.get) |
| 12 | + assert callable(GeneratedClass.post) |
| 13 | + |
| 14 | + app.add_url_rule("/", view_func=GeneratedClass.as_view("index")) |
| 15 | + |
| 16 | + with app.test_client() as c: |
| 17 | + assert c.get("/").data == b"propertyValue" |
| 18 | + assert c.post("/", data=b"newPropertyValue").data == b"newPropertyValue" |
| 19 | + assert c.get("/").data == b"newPropertyValue" |
| 20 | + |
| 21 | + |
| 22 | +def test_property_of_dict(app): |
| 23 | + obj = type( |
| 24 | + "obj", |
| 25 | + (object,), |
| 26 | + { |
| 27 | + "properties": { |
| 28 | + "property_name": "propertyValue", |
| 29 | + "property_name_2": "propertyValue2", |
| 30 | + } |
| 31 | + }, |
| 32 | + ) |
| 33 | + |
| 34 | + GeneratedClass = builder.property_of(obj, "properties") |
| 35 | + |
| 36 | + assert callable(GeneratedClass.get) |
| 37 | + assert callable(GeneratedClass.post) |
| 38 | + assert callable(GeneratedClass.put) |
| 39 | + |
| 40 | + app.add_url_rule("/", view_func=GeneratedClass.as_view("index")) |
| 41 | + |
| 42 | + with app.test_client() as c: |
| 43 | + assert ( |
| 44 | + c.get("/").data |
| 45 | + == b'{"property_name":"propertyValue","property_name_2":"propertyValue2"}\n' |
| 46 | + ) |
| 47 | + assert ( |
| 48 | + c.put("/", json={"property_name": "newPropertyValue"}).data |
| 49 | + == b'{"property_name":"newPropertyValue","property_name_2":"propertyValue2"}\n' |
| 50 | + ) |
| 51 | + assert ( |
| 52 | + c.post("/", json={"property_name": "newPropertyValue"}).data |
| 53 | + == b'{"property_name":"newPropertyValue"}\n' |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_property_of_readonly(): |
| 58 | + obj = type("obj", (object,), {"property_name": "propertyValue"}) |
| 59 | + |
| 60 | + GeneratedClass = builder.property_of(obj, "property_name", readonly=True) |
| 61 | + |
| 62 | + assert callable(GeneratedClass.get) |
| 63 | + assert not hasattr(GeneratedClass, "post") |
| 64 | + |
| 65 | + |
| 66 | +def test_property_of_name_description(): |
| 67 | + obj = type("obj", (object,), {"property_name": "propertyValue"}) |
| 68 | + |
| 69 | + GeneratedClass = builder.property_of( |
| 70 | + obj, "property_name", name="property_name", description="property description" |
| 71 | + ) |
| 72 | + |
| 73 | + assert GeneratedClass.__apispec__.get("description") == "property description" |
| 74 | + assert GeneratedClass.__apispec__.get("summary") == "property description" |
0 commit comments