Skip to content

Commit 8490acb

Browse files
committed
Added more unit tests
1 parent b9f9fc9 commit 8490acb

File tree

2 files changed

+110
-15
lines changed

2 files changed

+110
-15
lines changed

tests/test_openapi.py

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,120 @@
1010
from labthings import fields, schema
1111
from labthings.actions.thread import ActionThread
1212
from labthings.extensions import BaseExtension
13+
from labthings.schema import LogRecordSchema, Schema
1314
from labthings.views import ActionView, PropertyView, EventView
1415
from marshmallow import validate
16+
import apispec
1517
from apispec.utils import validate_spec
18+
from apispec.ext.marshmallow import MarshmallowPlugin
19+
from labthings.apispec import utilities
20+
import yaml
21+
1622

1723
def test_openapi(thing):
1824
"""Make an example Thing and check its openapi description validates"""
25+
1926
class TestAction(ActionView):
2027
args = {"n": fields.Integer()}
28+
2129
def post(self):
2230
return "POST"
31+
2332
thing.add_view(TestAction, "TestAction")
2433

2534
class TestProperty(PropertyView):
2635
schema = {"count": fields.Integer()}
36+
2737
def get(self):
2838
return 1
39+
2940
def post(self, args):
3041
pass
42+
3143
thing.add_view(TestProperty, "TestProperty")
3244

3345
class TestFieldProperty(PropertyView):
3446
schema = fields.String(validate=validate.OneOf(["one", "two"]))
47+
3548
def get(self):
3649
return "one"
50+
3751
def post(self, args):
3852
pass
53+
3954
thing.add_view(TestFieldProperty, "TestFieldProperty")
40-
41-
55+
4256
thing.spec.to_yaml()
43-
validate_spec(thing.spec)
57+
validate_spec(thing.spec)
58+
59+
60+
def test_ensure_schema_field_instance():
61+
ret = utilities.ensure_schema(fields.Integer())
62+
assert ret == {"type": "integer"}
63+
64+
65+
def test_ensure_schema_field_class():
66+
ret = utilities.ensure_schema(fields.Integer)
67+
assert ret == {"type": "integer"}
68+
69+
70+
def test_ensure_schema_class():
71+
ret = utilities.ensure_schema(LogRecordSchema)
72+
assert isinstance(ret, Schema)
73+
74+
75+
def test_ensure_schema_instance():
76+
ret = utilities.ensure_schema(LogRecordSchema())
77+
assert isinstance(ret, Schema)
78+
79+
80+
def test_ensure_schema_dict():
81+
ret = utilities.ensure_schema(
82+
{
83+
"count": fields.Integer(),
84+
"name": fields.String(),
85+
}
86+
)
87+
assert isinstance(ret, Schema)
88+
89+
90+
def test_ensure_schema_none():
91+
assert utilities.ensure_schema(None) is None
92+
93+
94+
def test_ensure_schema_error():
95+
with pytest.raises(TypeError):
96+
utilities.ensure_schema(Exception)
97+
98+
99+
def test_get_marshmallow_plugin(spec):
100+
p = utilities.get_marshmallow_plugin(spec)
101+
assert isinstance(p, MarshmallowPlugin)
102+
103+
104+
def test_get_marchmallow_plugin_empty():
105+
spec = apispec.APISpec("test", "0", "3.0")
106+
with pytest.raises(Exception):
107+
utilities.get_marshmallow_plugin(spec)
108+
109+
110+
def dict_is_openapi(d):
111+
for k in ["paths", "components"]:
112+
assert k in d.keys()
113+
assert d["openapi"].startswith("3.0")
114+
return True
115+
116+
117+
def test_openapi_json_endpoint(thing):
118+
c = thing.app.test_client()
119+
r = c.get("/docs/openapi")
120+
assert r.status_code == 200
121+
assert dict_is_openapi(r.get_json())
122+
123+
124+
def test_openapi_yaml_endpoint(thing):
125+
c = thing.app.test_client()
126+
127+
r = c.get("/docs/openapi.yaml")
128+
assert r.status_code == 200
129+
assert dict_is_openapi(yaml.safe_load(r.data))

tests/test_utilities.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
@pytest.fixture
88
def example_class():
99
class ExampleClass:
10-
"""
11-
First line of class docstring.
12-
Second line of class docstring.
10+
"""First line of class docstring.
11+
12+
Third line of class docstring.
1313
"""
1414

1515
def class_method(self):
16-
"""
17-
First line of class method docstring.
18-
Second line of class method docstring.
16+
"""First line of class method docstring.
17+
18+
Third line of class method docstring.
1919
"""
2020
return self
2121

@@ -28,17 +28,26 @@ def class_method_no_docstring(self):
2828
def test_get_docstring(example_class):
2929
assert (
3030
utilities.get_docstring(example_class)
31-
== "First line of class docstring. Second line of class docstring. "
31+
== "First line of class docstring. Third line of class docstring. "
3232
)
3333
assert (
3434
utilities.get_docstring(example_class, remove_newlines=False)
35-
== "First line of class docstring.\nSecond line of class docstring."
35+
== "First line of class docstring.\n\nThird line of class docstring."
3636
)
37-
38-
assert utilities.get_docstring(example_class.class_method) == (
39-
"First line of class method docstring. Second line of class method docstring. "
37+
assert (
38+
utilities.get_docstring(example_class.class_method)
39+
== "First line of class method docstring. Third line of class method docstring. "
40+
)
41+
assert (
42+
utilities.get_docstring(example_class.class_method, remove_summary=True)
43+
== "Third line of class method docstring. "
44+
)
45+
assert (
46+
utilities.get_docstring(
47+
example_class.class_method, remove_newlines=False, remove_summary=True
48+
).strip()
49+
== "Third line of class method docstring."
4050
)
41-
4251
assert utilities.get_docstring(example_class.class_method_no_docstring) == ""
4352

4453

0 commit comments

Comments
 (0)