|
| 1 | +import pytest |
| 2 | + |
| 3 | +from labthings.views import View, op |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def thing_description(thing): |
| 8 | + return thing.thing_description |
| 9 | + |
| 10 | + |
| 11 | +def test_op_readproperty(helpers, app, thing_description, app_ctx, schemas_path): |
| 12 | + class Index(View): |
| 13 | + @op.readproperty |
| 14 | + def get(self): |
| 15 | + return "GET" |
| 16 | + |
| 17 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 18 | + rules = app.url_map._rules_by_endpoint["index"] |
| 19 | + |
| 20 | + thing_description.property(rules, Index) |
| 21 | + |
| 22 | + with app_ctx.test_request_context(): |
| 23 | + assert "index" in thing_description.to_dict()["properties"] |
| 24 | + assert ( |
| 25 | + thing_description.to_dict()["properties"]["index"]["forms"][0]["op"] |
| 26 | + == "readproperty" |
| 27 | + ) |
| 28 | + |
| 29 | + assert ( |
| 30 | + thing_description.to_dict()["properties"]["index"]["forms"][0][ |
| 31 | + "htv:methodName" |
| 32 | + ] |
| 33 | + == "GET" |
| 34 | + ) |
| 35 | + helpers.validate_thing_description(thing_description, app_ctx, schemas_path) |
| 36 | + |
| 37 | + |
| 38 | +def test_op_writeproperty(helpers, app, thing_description, app_ctx, schemas_path): |
| 39 | + class Index(View): |
| 40 | + @op.writeproperty |
| 41 | + def put(self): |
| 42 | + return "PUT" |
| 43 | + |
| 44 | + app.add_url_rule("/", view_func=Index.as_view("index")) |
| 45 | + rules = app.url_map._rules_by_endpoint["index"] |
| 46 | + |
| 47 | + thing_description.property(rules, Index) |
| 48 | + |
| 49 | + with app_ctx.test_request_context(): |
| 50 | + assert "index" in thing_description.to_dict()["properties"] |
| 51 | + assert ( |
| 52 | + thing_description.to_dict()["properties"]["index"]["forms"][0]["op"] |
| 53 | + == "writeproperty" |
| 54 | + ) |
| 55 | + |
| 56 | + assert ( |
| 57 | + thing_description.to_dict()["properties"]["index"]["forms"][0][ |
| 58 | + "htv:methodName" |
| 59 | + ] |
| 60 | + == "PUT" |
| 61 | + ) |
| 62 | + helpers.validate_thing_description(thing_description, app_ctx, schemas_path) |
0 commit comments