|
4 | 4 | import json
|
5 | 5 | import jsonschema
|
6 | 6 | from labthings.server import fields
|
| 7 | +from labthings.server.view import View |
7 | 8 | from labthings.server.spec import td
|
8 | 9 |
|
9 | 10 |
|
@@ -124,3 +125,67 @@ def test_td_action_with_schema(app, thing_description, view_cls, app_ctx, schema
|
124 | 125 | "properties": {"integer": {"type": "integer", "format": "int32"}},
|
125 | 126 | }
|
126 | 127 | validate_thing_description(thing_description, app_ctx, schemas_path)
|
| 128 | + |
| 129 | + |
| 130 | +def test_td_property(app, thing_description, app_ctx, schemas_path): |
| 131 | + class Index(View): |
| 132 | + def get(self): |
| 133 | + return "GET" |
| 134 | + |
| 135 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 136 | + rules = app.url_map._rules_by_endpoint["index"] |
| 137 | + |
| 138 | + thing_description.property(rules, Index) |
| 139 | + |
| 140 | + with app_ctx.test_request_context(): |
| 141 | + assert "index" in thing_description.to_dict().get("properties") |
| 142 | + validate_thing_description(thing_description, app_ctx, schemas_path) |
| 143 | + |
| 144 | + |
| 145 | +def test_td_property_with_schema(app, thing_description, app_ctx, schemas_path): |
| 146 | + class Index(View): |
| 147 | + def get(self): |
| 148 | + return "GET" |
| 149 | + |
| 150 | + Index.__apispec__ = {"_propertySchema": {"integer": fields.Int()}} |
| 151 | + |
| 152 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 153 | + rules = app.url_map._rules_by_endpoint["index"] |
| 154 | + |
| 155 | + thing_description.property(rules, Index) |
| 156 | + |
| 157 | + with app_ctx.test_request_context(): |
| 158 | + assert "index" in thing_description.to_dict().get("properties") |
| 159 | + validate_thing_description(thing_description, app_ctx, schemas_path) |
| 160 | + |
| 161 | + |
| 162 | +def test_td_property_with_url_param(app, thing_description, app_ctx, schemas_path): |
| 163 | + class Index(View): |
| 164 | + def get(self): |
| 165 | + return "GET" |
| 166 | + |
| 167 | + app.add_url_rule("/path/<int:id>/", view_func=Index.as_view("index")) |
| 168 | + rules = app.url_map._rules_by_endpoint["index"] |
| 169 | + |
| 170 | + thing_description.property(rules, Index) |
| 171 | + |
| 172 | + with app_ctx.test_request_context(): |
| 173 | + assert "index" in thing_description.to_dict().get("properties") |
| 174 | + validate_thing_description(thing_description, app_ctx, schemas_path) |
| 175 | + |
| 176 | + |
| 177 | +def test_td_property_write_only(app, thing_description, app_ctx, schemas_path): |
| 178 | + class Index(View): |
| 179 | + def post(self): |
| 180 | + return "POST" |
| 181 | + |
| 182 | + Index.__apispec__ = {"_propertySchema": fields.Int()} |
| 183 | + |
| 184 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 185 | + rules = app.url_map._rules_by_endpoint["index"] |
| 186 | + |
| 187 | + thing_description.property(rules, Index) |
| 188 | + |
| 189 | + with app_ctx.test_request_context(): |
| 190 | + assert "index" in thing_description.to_dict().get("properties") |
| 191 | + validate_thing_description(thing_description, app_ctx, schemas_path) |
0 commit comments