Skip to content

Commit 5f2eaa2

Browse files
author
Joel Collins
committed
Improved LabThing coverage
1 parent 3a2aef8 commit 5f2eaa2

File tree

1 file changed

+118
-2
lines changed

1 file changed

+118
-2
lines changed

tests/test_server_labthing.py

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from labthings.server import labthing
44

5+
from labthings.server.view import View
56
from labthings.server.representations import LabThingsJSONEncoder
67
from labthings.server.names import EXTENSION_NAME
78
from labthings.server.extensions import BaseExtension
@@ -25,16 +26,58 @@ def test_init_app(app):
2526

2627
assert app.extensions.get(EXTENSION_NAME) == thing
2728
assert app.json_encoder == LabThingsJSONEncoder
29+
assert 400 in app.error_handler_spec.get(None)
30+
31+
32+
def test_init_app_no_error_formatter(app):
33+
thing = labthing.LabThing(format_flask_exceptions=False)
34+
thing.init_app(app)
35+
assert app.error_handler_spec == {}
36+
37+
38+
def test_add_view(thing, view_cls, client):
39+
thing.add_view(view_cls, "/index", endpoint="index")
40+
41+
with client as c:
42+
assert c.get("/index").data == b"GET"
43+
44+
45+
def test_add_view_endpoint_clash(thing, view_cls, client):
46+
thing.add_view(view_cls, "/index", endpoint="index")
47+
with pytest.raises(AssertionError):
48+
thing.add_view(view_cls, "/index2", endpoint="index")
49+
50+
51+
def test_view_decorator(thing, client):
52+
@thing.view("/index")
53+
class ViewClass(View):
54+
def get(self):
55+
return "GET"
56+
57+
with client as c:
58+
assert c.get("/index").data == b"GET"
59+
60+
61+
def test_add_view_action(thing, view_cls, client):
62+
view_cls.__apispec__ = {"_groups": ["actions"]}
63+
thing.add_view(view_cls, "/index", endpoint="index")
64+
assert view_cls in thing._action_views.values()
65+
66+
67+
def test_add_view_property(thing, view_cls, client):
68+
view_cls.__apispec__ = {"_groups": ["properties"]}
69+
thing.add_view(view_cls, "/index", endpoint="index")
70+
assert view_cls in thing._property_views.values()
2871

2972

3073
def test_init_app_early_views(app, view_cls, client):
3174
thing = labthing.LabThing()
32-
thing.add_view(view_cls, "/", endpoint="index")
75+
thing.add_view(view_cls, "/index", endpoint="index")
3376

3477
thing.init_app(app)
3578

3679
with client as c:
37-
assert c.get("/").status_code == 200
80+
assert c.get("/index").data == b"GET"
3881

3982

4083
def test_register_extension(thing):
@@ -132,3 +175,76 @@ def test_complete_url(thing):
132175
assert thing._complete_url("", "") == "/prefix"
133176
assert thing._complete_url("", "api") == "/prefix/api"
134177
assert thing._complete_url("/method", "api") == "/prefix/api/method"
178+
179+
180+
def test_url_for(thing, view_cls, app_ctx):
181+
with app_ctx.test_request_context():
182+
# Before added, should return no URL
183+
assert thing.url_for(view_cls) == ""
184+
# Add view
185+
thing.add_view(view_cls, "/index", endpoint="index")
186+
# Check URLs
187+
assert thing.url_for(view_cls, _external=False) == "/index"
188+
assert all(
189+
substring in thing.url_for(view_cls) for substring in ["http://", "/index"]
190+
)
191+
192+
193+
def test_owns_endpoint(thing, view_cls, app_ctx):
194+
assert not thing.owns_endpoint("index")
195+
thing.add_view(view_cls, "/index", endpoint="index")
196+
assert thing.owns_endpoint("index")
197+
198+
199+
def test_add_root_link(thing, view_cls, app_ctx, schemas_path):
200+
thing.add_root_link(view_cls, "rel")
201+
assert {
202+
"rel": "rel",
203+
"view": view_cls,
204+
"params": {},
205+
"kwargs": {},
206+
} in thing.thing_description._links
207+
208+
209+
def test_td_add_link_options(thing, view_cls):
210+
thing.add_root_link(
211+
view_cls, "rel", kwargs={"kwarg": "kvalue"}, params={"param": "pvalue"}
212+
)
213+
assert {
214+
"rel": "rel",
215+
"view": view_cls,
216+
"params": {"param": "pvalue"},
217+
"kwargs": {"kwarg": "kvalue"},
218+
} in thing.thing_description._links
219+
220+
221+
def test_root_rep(thing, app_ctx):
222+
with app_ctx.test_request_context():
223+
assert thing.root() == thing.thing_description.to_dict()
224+
225+
226+
def test_description(thing):
227+
assert thing.description == ""
228+
thing.description = "description"
229+
assert thing.description == "description"
230+
assert thing.spec.description == "description"
231+
232+
233+
def test_title(thing):
234+
assert thing.title == ""
235+
thing.title = "title"
236+
assert thing.title == "title"
237+
assert thing.spec.title == "title"
238+
239+
240+
def test_version(thing):
241+
assert thing.version == "0.0.0"
242+
thing.version = "x.x.x"
243+
assert thing.version == "x.x.x"
244+
assert thing.spec.version == "x.x.x"
245+
246+
247+
def test_socket_handler(thing, fake_websocket):
248+
ws = fake_websocket("", recieve_once=True)
249+
thing._socket_handler(ws)
250+
assert ws.response is None

0 commit comments

Comments
 (0)