Skip to content

Commit

Permalink
Improved LabThing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 17, 2020
1 parent 3a2aef8 commit 5f2eaa2
Showing 1 changed file with 118 additions and 2 deletions.
120 changes: 118 additions & 2 deletions tests/test_server_labthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from labthings.server import labthing

from labthings.server.view import View
from labthings.server.representations import LabThingsJSONEncoder
from labthings.server.names import EXTENSION_NAME
from labthings.server.extensions import BaseExtension
Expand All @@ -25,16 +26,58 @@ def test_init_app(app):

assert app.extensions.get(EXTENSION_NAME) == thing
assert app.json_encoder == LabThingsJSONEncoder
assert 400 in app.error_handler_spec.get(None)


def test_init_app_no_error_formatter(app):
thing = labthing.LabThing(format_flask_exceptions=False)
thing.init_app(app)
assert app.error_handler_spec == {}


def test_add_view(thing, view_cls, client):
thing.add_view(view_cls, "/index", endpoint="index")

with client as c:
assert c.get("/index").data == b"GET"


def test_add_view_endpoint_clash(thing, view_cls, client):
thing.add_view(view_cls, "/index", endpoint="index")
with pytest.raises(AssertionError):
thing.add_view(view_cls, "/index2", endpoint="index")


def test_view_decorator(thing, client):
@thing.view("/index")
class ViewClass(View):
def get(self):
return "GET"

with client as c:
assert c.get("/index").data == b"GET"


def test_add_view_action(thing, view_cls, client):
view_cls.__apispec__ = {"_groups": ["actions"]}
thing.add_view(view_cls, "/index", endpoint="index")
assert view_cls in thing._action_views.values()


def test_add_view_property(thing, view_cls, client):
view_cls.__apispec__ = {"_groups": ["properties"]}
thing.add_view(view_cls, "/index", endpoint="index")
assert view_cls in thing._property_views.values()


def test_init_app_early_views(app, view_cls, client):
thing = labthing.LabThing()
thing.add_view(view_cls, "/", endpoint="index")
thing.add_view(view_cls, "/index", endpoint="index")

thing.init_app(app)

with client as c:
assert c.get("/").status_code == 200
assert c.get("/index").data == b"GET"


def test_register_extension(thing):
Expand Down Expand Up @@ -132,3 +175,76 @@ def test_complete_url(thing):
assert thing._complete_url("", "") == "/prefix"
assert thing._complete_url("", "api") == "/prefix/api"
assert thing._complete_url("/method", "api") == "/prefix/api/method"


def test_url_for(thing, view_cls, app_ctx):
with app_ctx.test_request_context():
# Before added, should return no URL
assert thing.url_for(view_cls) == ""
# Add view
thing.add_view(view_cls, "/index", endpoint="index")
# Check URLs
assert thing.url_for(view_cls, _external=False) == "/index"
assert all(
substring in thing.url_for(view_cls) for substring in ["http://", "/index"]
)


def test_owns_endpoint(thing, view_cls, app_ctx):
assert not thing.owns_endpoint("index")
thing.add_view(view_cls, "/index", endpoint="index")
assert thing.owns_endpoint("index")


def test_add_root_link(thing, view_cls, app_ctx, schemas_path):
thing.add_root_link(view_cls, "rel")
assert {
"rel": "rel",
"view": view_cls,
"params": {},
"kwargs": {},
} in thing.thing_description._links


def test_td_add_link_options(thing, view_cls):
thing.add_root_link(
view_cls, "rel", kwargs={"kwarg": "kvalue"}, params={"param": "pvalue"}
)
assert {
"rel": "rel",
"view": view_cls,
"params": {"param": "pvalue"},
"kwargs": {"kwarg": "kvalue"},
} in thing.thing_description._links


def test_root_rep(thing, app_ctx):
with app_ctx.test_request_context():
assert thing.root() == thing.thing_description.to_dict()


def test_description(thing):
assert thing.description == ""
thing.description = "description"
assert thing.description == "description"
assert thing.spec.description == "description"


def test_title(thing):
assert thing.title == ""
thing.title = "title"
assert thing.title == "title"
assert thing.spec.title == "title"


def test_version(thing):
assert thing.version == "0.0.0"
thing.version = "x.x.x"
assert thing.version == "x.x.x"
assert thing.spec.version == "x.x.x"


def test_socket_handler(thing, fake_websocket):
ws = fake_websocket("", recieve_once=True)
thing._socket_handler(ws)
assert ws.response is None

0 comments on commit 5f2eaa2

Please sign in to comment.