Skip to content

Commit ad150c1

Browse files
author
Joel Collins
committed
Started thing description coverage
1 parent bab208a commit ad150c1

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

tests/test_server_spec_td.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import pytest
2+
3+
import os
4+
import json
5+
import jsonschema
6+
from labthings.server import fields
7+
from labthings.server.spec import td
8+
9+
10+
@pytest.fixture
11+
def thing_description(thing):
12+
return thing.thing_description
13+
14+
15+
def validate_thing_description(thing_description, app_ctx, schemas_path):
16+
schema = json.load(open(os.path.join(schemas_path, "w3_wot_td_v1.json"), "r"))
17+
jsonschema.Draft7Validator.check_schema(schema)
18+
19+
with app_ctx.test_request_context():
20+
td_json = thing_description.to_dict()
21+
assert td_json
22+
23+
jsonschema.validate(instance=td_json, schema=schema)
24+
25+
26+
def test_find_schema_for_view_readonly():
27+
class ViewClass:
28+
def get(self):
29+
pass
30+
31+
ViewClass.get.__apispec__ = {"_schema": {200: "schema"}}
32+
assert td.find_schema_for_view(ViewClass) == "schema"
33+
34+
35+
def test_find_schema_for_view_writeonly_post():
36+
class ViewClass:
37+
def post(self):
38+
pass
39+
40+
ViewClass.post.__apispec__ = {"_params": "params"}
41+
assert td.find_schema_for_view(ViewClass) == "params"
42+
43+
44+
def test_find_schema_for_view_writeonly_put():
45+
class ViewClass:
46+
def put(self):
47+
pass
48+
49+
ViewClass.put.__apispec__ = {"_params": "params"}
50+
assert td.find_schema_for_view(ViewClass) == "params"
51+
52+
53+
def test_find_schema_for_view_none():
54+
class ViewClass:
55+
pass
56+
57+
assert td.find_schema_for_view(ViewClass) == {}
58+
59+
60+
def test_td_init(thing_description, app_ctx, schemas_path):
61+
assert thing_description
62+
63+
validate_thing_description(thing_description, app_ctx, schemas_path)
64+
65+
66+
def test_td_add_link(thing_description, view_cls, app_ctx, schemas_path):
67+
thing_description.add_link(view_cls, "rel")
68+
assert {
69+
"rel": "rel",
70+
"view": view_cls,
71+
"params": {},
72+
"kwargs": {},
73+
} in thing_description._links
74+
75+
validate_thing_description(thing_description, app_ctx, schemas_path)
76+
77+
78+
def test_td_add_link_options(thing_description, view_cls):
79+
thing_description.add_link(
80+
view_cls, "rel", kwargs={"kwarg": "kvalue"}, params={"param": "pvalue"}
81+
)
82+
assert {
83+
"rel": "rel",
84+
"view": view_cls,
85+
"params": {"param": "pvalue"},
86+
"kwargs": {"kwarg": "kvalue"},
87+
} in thing_description._links
88+
89+
90+
def test_td_links(thing_description, app_ctx, view_cls):
91+
thing_description.add_link(
92+
view_cls, "rel", kwargs={"kwarg": "kvalue"}, params={"param": "pvalue"}
93+
)
94+
95+
with app_ctx.test_request_context():
96+
assert {"rel": "rel", "href": "", "kwarg": "kvalue"} in (
97+
thing_description.links
98+
)
99+
100+
101+
def test_td_action(app, thing_description, view_cls, app_ctx, schemas_path):
102+
app.add_url_rule("/", view_func=view_cls.as_view("index"))
103+
rules = app.url_map._rules_by_endpoint["index"]
104+
105+
thing_description.action(rules, view_cls)
106+
107+
with app_ctx.test_request_context():
108+
assert "index" in thing_description.to_dict().get("actions")
109+
validate_thing_description(thing_description, app_ctx, schemas_path)
110+
111+
112+
def test_td_action_with_schema(app, thing_description, view_cls, app_ctx, schemas_path):
113+
view_cls.post.__apispec__ = {"_params": {"integer": fields.Int()}}
114+
115+
app.add_url_rule("/", view_func=view_cls.as_view("index"))
116+
rules = app.url_map._rules_by_endpoint["index"]
117+
118+
thing_description.action(rules, view_cls)
119+
120+
with app_ctx.test_request_context():
121+
assert "index" in thing_description.to_dict().get("actions")
122+
assert thing_description.to_dict().get("actions").get("index").get("input") == {
123+
"type": "object",
124+
"properties": {"integer": {"type": "integer", "format": "int32"}},
125+
}
126+
validate_thing_description(thing_description, app_ctx, schemas_path)

0 commit comments

Comments
 (0)