From cd8585999b0252c59905bd8f29a72599c20ba715 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Fri, 4 Sep 2020 12:16:32 +0100 Subject: [PATCH] Removed tests for deprecated code --- src/labthings/utilities.py | 2 +- tests/conftest.py | 23 ++++- tests/test_apispec_apispec.py | 46 ---------- tests/test_apispec_converter.py | 20 ----- tests/test_apispec_utilities.py | 143 -------------------------------- tests/test_default_views.py | 57 ------------- tests/test_labthing.py | 43 ++-------- tests/test_semantics.py | 99 ---------------------- tests/test_td.py | 21 ----- tests/test_utilities.py | 16 ++-- tests/test_views.py | 11 --- 11 files changed, 39 insertions(+), 442 deletions(-) delete mode 100644 tests/test_apispec_apispec.py delete mode 100644 tests/test_apispec_converter.py delete mode 100644 tests/test_apispec_utilities.py delete mode 100644 tests/test_semantics.py diff --git a/src/labthings/utilities.py b/src/labthings/utilities.py index 2e45f4c5..4b89b715 100644 --- a/src/labthings/utilities.py +++ b/src/labthings/utilities.py @@ -81,7 +81,7 @@ def description_from_view(view_class): summary = get_summary(view_class) methods = [] - for method_key in http_method_funcs: + for method_key in ("get", "post", "put"): if hasattr(view_class, method_key): methods.append(method_key.upper()) diff --git a/tests/conftest.py b/tests/conftest.py index e7597fad..207cb591 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,7 @@ from labthings import LabThing from labthings.actions import Pool from labthings.json import encode_json -from labthings.views import View +from labthings.views import View, ActionView, PropertyView class Helpers: @@ -190,6 +190,27 @@ def delete(self): return ViewClass +@pytest.fixture +def action_view_cls(): + class ActionViewClass(ActionView): + def post(self): + return "POST" + + return ActionViewClass + + +@pytest.fixture +def property_view_cls(): + class PropertyViewClass(PropertyView): + def get(self): + return "GET" + + def put(self): + return "PUT" + + return PropertyViewClass + + @pytest.fixture def spec(): return APISpec( diff --git a/tests/test_apispec_apispec.py b/tests/test_apispec_apispec.py deleted file mode 100644 index 1834147a..00000000 --- a/tests/test_apispec_apispec.py +++ /dev/null @@ -1,46 +0,0 @@ -from labthings import fields -from labthings.apispec import apispec -from labthings.views import PropertyView, View - - -def test_rule_to_apispec_path(app, spec): - class Index(View): - """Index docstring""" - - def get(self): - return "GET" - - def post(self): - return "POST" - - app.add_url_rule("/path", view_func=Index.as_view("index")) - rule = app.url_map._rules_by_endpoint["index"][0] - - assert apispec.rule_to_apispec_path(rule, Index, spec)["path"] == "/path" - for method in ("get", "post"): - assert method in apispec.rule_to_apispec_path(rule, Index, spec)["operations"] - - -def test_rule_to_apispec_path_params(app, spec): - class Index(View): - """Index docstring""" - - def get(self): - return "GET" - - def post(self): - return "POST" - - app.add_url_rule("/path//", view_func=Index.as_view("index")) - rule = app.url_map._rules_by_endpoint["index"][0] - - ops = apispec.rule_to_apispec_path(rule, Index, spec)["operations"] - for method in ("get", "post"): - assert ops[method]["parameters"] == [ - { - "in": "path", - "name": "id", - "required": True, - "schema": {"type": "string"}, - } - ] diff --git a/tests/test_apispec_converter.py b/tests/test_apispec_converter.py deleted file mode 100644 index f6f5dff3..00000000 --- a/tests/test_apispec_converter.py +++ /dev/null @@ -1,20 +0,0 @@ -from labthings import fields -from labthings.apispec.converter import ExtendedOpenAPIConverter - - -class TestField(fields.Field): - def _jsonschema_type_mapping(self): - """ """ - return {"type": "string"} - - -def test_jsonschema_type_mapping(spec): - converter = ExtendedOpenAPIConverter("3.0.2", lambda _: None, spec) - - assert converter.jsonschema_type_mapping(TestField()) == {"type": "string"} - - -def test_jsonschema_type_mapping_missing(spec): - converter = ExtendedOpenAPIConverter("3.0.2", lambda _: None, spec) - - assert converter.jsonschema_type_mapping(fields.Field()) == {} diff --git a/tests/test_apispec_utilities.py b/tests/test_apispec_utilities.py deleted file mode 100644 index 4986e92e..00000000 --- a/tests/test_apispec_utilities.py +++ /dev/null @@ -1,143 +0,0 @@ -import pytest -from marshmallow import fields - -from labthings.apispec import utilities - - -def test_convert_schema_none(spec): - assert not utilities.convert_to_schema_or_json(None, spec) - - -def test_convert_schema_schema(spec): - from marshmallow import Schema - - schema = Schema() - schema.integer = fields.Int() - assert utilities.convert_to_schema_or_json(schema, spec) is schema - - -def test_convert_schema_map(spec): - schema = {"integer": fields.Int()} - assert utilities.convert_to_schema_or_json(schema, spec) == { - "type": "object", - "properties": {"integer": {"type": "integer", "format": "int32"}}, - } - - -def test_convert_schema_field(spec): - schema = fields.Int() - assert utilities.convert_to_schema_or_json(schema, spec) == { - "type": "integer", - "format": "int32", - } - - -def test_convert_schema_invalid(spec): - schema = object() - - with pytest.raises(TypeError): - utilities.convert_to_schema_or_json(schema, spec) - - -def test_map_to_schema_nested(spec): - schema = {"submap": {"integer": fields.Int()}} - - assert utilities.map_to_properties(schema, spec) == { - "type": "object", - "properties": { - "submap": { - "type": "object", - "properties": {"integer": {"type": "integer", "format": "int32"}}, - } - }, - } - - -def test_map_to_schema_json(spec): - schema = {"key": "value"} - - assert utilities.map_to_properties(schema, spec) == { - "type": "object", - "properties": {"key": "value"}, - } - - -def test_schema_to_json(spec): - from marshmallow import Schema - - UserSchema = Schema.from_dict({"name": fields.Str(), "email": fields.Email()}) - - assert utilities.schema_to_json(UserSchema(), spec) == { - "type": "object", - "properties": { - "name": {"type": "string"}, - "email": {"type": "string", "format": "email"}, - }, - } - - -def test_schema_to_json_json_in(spec): - from marshmallow import Schema - - input_dict = { - "type": "object", - "properties": { - "name": {"type": "string"}, - "email": {"type": "string", "format": "email"}, - }, - } - - assert utilities.schema_to_json(input_dict, spec) == input_dict - - -def test_recursive_expand_refs(spec): - from marshmallow import Schema - - UserSchema = Schema.from_dict({"name": fields.Str(), "email": fields.Email()}) - TestParentSchema = Schema.from_dict({"author": fields.Nested(UserSchema)}) - - assert utilities.schema_to_json(TestParentSchema(), spec) == { - "type": "object", - "properties": { - "author": { - "type": "object", - "properties": { - "email": {"type": "string", "format": "email"}, - "name": {"type": "string"}, - }, - } - }, - } - - -def test_recursive_expand_refs_schema_in(spec): - from marshmallow import Schema - - UserSchema = Schema.from_dict({"name": fields.Str(), "email": fields.Email()}) - - user_schema_instance = UserSchema() - assert ( - utilities.recursive_expand_refs(user_schema_instance, spec) - == user_schema_instance - ) - - -### TODO: Test expand_refs - - -def test_expand_refs_no_refs(spec): - input_dict = { - "type": "object", - "properties": { - "name": {"type": "string"}, - "email": {"type": "string", "format": "email"}, - }, - } - - assert utilities.expand_refs(input_dict, spec) == input_dict - - -def test_expand_refs_missing_schema(spec): - input_dict = {"$ref": "MissingRef"} - - assert utilities.expand_refs(input_dict, spec) == input_dict diff --git a/tests/test_default_views.py b/tests/test_default_views.py index 41549c73..8616b2a7 100644 --- a/tests/test_default_views.py +++ b/tests/test_default_views.py @@ -91,60 +91,3 @@ def task_func(): def test_action_kill_missing(thing_client): with thing_client as c: assert c.delete("/actions/missing_id").status_code == 404 - - -### DEPRECATED: LEGACY TASK VIEW - - -def test_tasks_list(thing_client): - def task_func(): - pass - - task_obj = current_labthing().actions.spawn(task_func) - - with thing_client as c: - response = c.get("/tasks").json - ids = [task.get("id") for task in response] - assert str(task_obj.id) in ids - - -def test_task_representation(thing_client): - def task_func(): - pass - - task_obj = current_labthing().actions.spawn(task_func) - task_id = str(task_obj.id) - - with thing_client as c: - response = c.get(f"/tasks/{task_id}").json - assert response - - -def test_task_representation_missing(thing_client): - with thing_client as c: - assert c.get("/tasks/missing_id").status_code == 404 - - -def test_task_kill(thing_client): - def task_func(): - while not current_action().stopping: - time.sleep(0) - - task_obj = current_labthing().actions.spawn(task_func) - task_id = str(task_obj.id) - - # Wait for task to start - task_obj.started.wait() - assert task_id in current_labthing().actions.to_dict() - - # Send a DELETE request to terminate the task - with thing_client as c: - response = c.delete(f"/tasks/{task_id}") - assert response.status_code == 200 - # Test task was stopped - assert task_obj._status == "stopped" - - -def test_task_kill_missing(thing_client): - with thing_client as c: - assert c.delete("/tasks/missing_id").status_code == 404 diff --git a/tests/test_labthing.py b/tests/test_labthing.py index e50ba1a1..84eae52a 100644 --- a/tests/test_labthing.py +++ b/tests/test_labthing.py @@ -13,12 +13,6 @@ def test_init_types(): assert thing.types == types -def test_init_types_invalid(): - types = ["org;labthings;test"] - with pytest.raises(ValueError): - LabThing(types=types) - - def test_init_app(app): thing = LabThing() thing.init_app(app) @@ -59,16 +53,16 @@ def get(self): assert c.get("/index").data == b'"GET"\n' -def test_add_view_action(thing, view_cls, client): - view_cls.tags = ["actions"] - thing.add_view(view_cls, "/index", endpoint="index") - assert view_cls in thing._action_views.values() +def test_add_view_action(thing, action_view_cls, client): + action_view_cls.tags = ["actions"] + thing.add_view(action_view_cls, "/index", endpoint="index") + assert action_view_cls in thing._action_views.values() -def test_add_view_property(thing, view_cls, client): - view_cls.tags = ["properties"] - thing.add_view(view_cls, "/index", endpoint="index") - assert view_cls in thing._property_views.values() +def test_add_view_property(thing, property_view_cls, client): + property_view_cls.tags = ["properties"] + thing.add_view(property_view_cls, "/index", endpoint="index") + assert property_view_cls in thing._property_views.values() def test_init_app_early_views(app, view_cls, client): @@ -239,24 +233,3 @@ def test_version(thing): thing.version = "x.x.x" assert thing.version == "x.x.x" assert thing.spec.version == "x.x.x" - - -def test_build_property(thing): - obj = type("obj", (object,), {"property_name": "propertyValue"}) - - thing.build_property(obj, "property_name") - # -1 index for last view added - # 1 index for URL tuple - assert "/properties/type/property_name" in thing.views[-1][1] - - -def test_build_action(thing): - def f(): - return "response" - - obj = type("obj", (object,), {"f": f}) - - thing.build_action(obj, "f") - # -1 index for last view added - # 1 index for URL tuple - assert "/actions/type/f" in thing.views[-1][1] diff --git a/tests/test_semantics.py b/tests/test_semantics.py deleted file mode 100644 index 8edcc0e5..00000000 --- a/tests/test_semantics.py +++ /dev/null @@ -1,99 +0,0 @@ -import pytest - -from labthings import semantics -from labthings.views import View, op - - -@pytest.fixture -def thing_description(thing): - return thing.thing_description - - -def test_moz_BooleanProperty(helpers, app, thing_description, app_ctx, schemas_path): - @semantics.moz.BooleanProperty() - class Index(View): - @op.readproperty - def get(self): - return "GET" - - app.add_url_rule("/", view_func=Index.as_view("index")) - rules = app.url_map._rules_by_endpoint["index"] - - thing_description.property(rules, Index) - - with app_ctx.test_request_context(): - assert ( - thing_description.to_dict()["properties"]["index"]["@type"] - == "BooleanProperty" - ) - assert thing_description.to_dict()["properties"]["index"]["type"] == "boolean" - helpers.validate_thing_description(thing_description, app_ctx, schemas_path) - - -def test_moz_LevelProperty(helpers, app, thing_description, app_ctx, schemas_path): - @semantics.moz.LevelProperty(0, 100) - class Index(View): - @op.readproperty - def get(self): - return "GET" - - app.add_url_rule("/", view_func=Index.as_view("index")) - rules = app.url_map._rules_by_endpoint["index"] - - thing_description.property(rules, Index) - - with app_ctx.test_request_context(): - assert ( - thing_description.to_dict()["properties"]["index"]["@type"] - == "LevelProperty" - ) - assert thing_description.to_dict()["properties"]["index"]["type"] == "number" - assert thing_description.to_dict()["properties"]["index"]["format"] == "integer" - assert thing_description.to_dict()["properties"]["index"]["minimum"] == 0 - assert thing_description.to_dict()["properties"]["index"]["maximum"] == 100 - helpers.validate_thing_description(thing_description, app_ctx, schemas_path) - - -def test_moz_BrightnessProperty(helpers, app, thing_description, app_ctx, schemas_path): - @semantics.moz.BrightnessProperty() - class Index(View): - @op.readproperty - def get(self): - return "GET" - - app.add_url_rule("/", view_func=Index.as_view("index")) - rules = app.url_map._rules_by_endpoint["index"] - - thing_description.property(rules, Index) - - with app_ctx.test_request_context(): - assert ( - thing_description.to_dict()["properties"]["index"]["@type"] - == "BrightnessProperty" - ) - assert thing_description.to_dict()["properties"]["index"]["type"] == "number" - assert thing_description.to_dict()["properties"]["index"]["format"] == "integer" - assert thing_description.to_dict()["properties"]["index"]["minimum"] == 0 - assert thing_description.to_dict()["properties"]["index"]["maximum"] == 100 - helpers.validate_thing_description(thing_description, app_ctx, schemas_path) - - -def test_moz_OnOffProperty(helpers, app, thing_description, app_ctx, schemas_path): - @semantics.moz.OnOffProperty() - class Index(View): - @op.readproperty - def get(self): - return "GET" - - app.add_url_rule("/", view_func=Index.as_view("index")) - rules = app.url_map._rules_by_endpoint["index"] - - thing_description.property(rules, Index) - - with app_ctx.test_request_context(): - assert ( - thing_description.to_dict()["properties"]["index"]["@type"] - == "OnOffProperty" - ) - assert thing_description.to_dict()["properties"]["index"]["type"] == "boolean" - helpers.validate_thing_description(thing_description, app_ctx, schemas_path) diff --git a/tests/test_td.py b/tests/test_td.py index cddc0753..4b723fbb 100644 --- a/tests/test_td.py +++ b/tests/test_td.py @@ -79,27 +79,6 @@ def post(self): with app_ctx.test_request_context(): assert "index" in thing_description.to_dict().get("actions") - - assert thing_description.to_dict().get("actions").get("index") == { - "title": "Index", - "description": "", - "links": [{"href": "/"}], - "safe": False, - "idempotent": False, - "forms": [ - { - "op": "invokeaction", - "htv:methodName": "POST", - "href": "/", - "contentType": "application/json", - } - ], - "input": { - "type": "object", - "properties": {"integer": {"type": "number", "format": "integer"}}, - }, - "@type": "ToggleAction", - } helpers.validate_thing_description(thing_description, app_ctx, schemas_path) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index 5529bb27..fecd6856 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -189,10 +189,10 @@ def post(self): """POST summary""" return "POST" - assert utilities.description_from_view(Index) == { - "methods": ["GET", "POST"], - "description": "Class summary", - } + description = utilities.description_from_view(Index) + assert "POST" in description["methods"] + assert "GET" in description["methods"] + assert description["description"] == "Class summary" def test_description_from_view_summary_from_method(app): @@ -205,10 +205,10 @@ def post(self): """POST summary""" return "POST" - assert utilities.description_from_view(Index) == { - "methods": ["GET", "POST"], - "description": "GET summary", - } + description = utilities.description_from_view(Index) + assert "POST" in description["methods"] + assert "GET" in description["methods"] + assert description["description"] == "GET summary" def test_view_class_from_endpoint(app): diff --git a/tests/test_views.py b/tests/test_views.py index 34b60455..9d985fe0 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -158,17 +158,6 @@ def get(self): assert Index().get_value() == {"json": "body"} -def test_action_view_get_responses(): - class Index(views.ActionView): - def post(self): - return {} - - responses = Index.get_apispec().get("post").get("responses") - assert 201 in responses - assert "application/json" in responses[201]["content"] - assert "schema" in responses[201]["content"]["application/json"] - - def test_action_view_stop(app): class Index(views.ActionView): default_stop_timeout = 0