Skip to content

Commit a0ec226

Browse files
author
Joel Collins
committed
Improved coverage
1 parent f363982 commit a0ec226

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

tests/test_server_view_builder.py

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import pytest
2+
13
from labthings.server.view import builder
24
from labthings.server import fields
35

6+
from labthings.server.semantics.base import Semantic
7+
48

59
def test_property_of_no_schema(app, client):
610
obj = type("obj", (object,), {"property_name": "propertyValue"})
@@ -77,6 +81,50 @@ def test_property_of_name_description():
7781
assert GeneratedClass.summary == "property description"
7882

7983

84+
def test_property_of_semtype_string():
85+
obj = type("obj", (object,), {"property_name": "propertyValue"})
86+
GeneratedClass = builder.property_of(
87+
obj, "property_name", name="property_name", semtype="SemanticType"
88+
)
89+
90+
assert GeneratedClass.semtype == "SemanticType"
91+
92+
93+
def test_property_of_semtype_semantic():
94+
obj = type("obj", (object,), {"property_name": "propertyValue"})
95+
96+
semantic_annotation = Semantic()
97+
98+
GeneratedClass = builder.property_of(
99+
obj, "property_name", name="property_name", semtype=semantic_annotation
100+
)
101+
102+
assert GeneratedClass.semtype == "Semantic"
103+
104+
105+
def test_property_of_semtype_invalid():
106+
obj = type("obj", (object,), {"property_name": "propertyValue"})
107+
108+
semantic_annotation = object
109+
110+
with pytest.raises(TypeError):
111+
GeneratedClass = builder.property_of(
112+
obj, "property_name", name="property_name", semtype=semantic_annotation
113+
)
114+
115+
116+
def test_action_from(debug_app, debug_client):
117+
def f():
118+
return "response"
119+
120+
GeneratedClass = builder.action_from(f)
121+
debug_app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))
122+
123+
with debug_client as c:
124+
response = c.post("/").json
125+
assert "status" in response
126+
127+
80128
def test_action_from_with_args(app, client):
81129
def f(arg1, arg2=0):
82130
return {"arg1": arg1, "arg2": arg2}
@@ -93,19 +141,20 @@ def f(arg1, arg2=0):
93141
assert response["input"] == input_json
94142

95143

96-
def test_action_from(debug_app, debug_client):
144+
def test_action_from_with_schema(app, client):
97145
def f():
98146
return "response"
99147

100-
GeneratedClass = builder.action_from(f)
101-
debug_app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))
148+
GeneratedClass = builder.action_from(f, schema=fields.String())
149+
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))
102150

103-
with debug_client as c:
151+
with client as c:
104152
response = c.post("/").json
105153
assert "status" in response
154+
assert response["output"] == "response"
106155

107156

108-
def test_action_from_options(app):
157+
def test_action_from_with_options(app):
109158
def f():
110159
return "response"
111160

@@ -118,6 +167,36 @@ def f():
118167
)
119168

120169

170+
def test_action_from_semtype_string():
171+
def f():
172+
return "response"
173+
174+
GeneratedClass = builder.action_from(f, semtype="SemanticType")
175+
176+
assert GeneratedClass.semtype == "SemanticType"
177+
178+
179+
def test_action_from_semtype_semantic():
180+
def f():
181+
return "response"
182+
183+
semantic_annotation = Semantic()
184+
185+
GeneratedClass = builder.action_from(f, semtype=semantic_annotation)
186+
187+
assert GeneratedClass.semtype == "Semantic"
188+
189+
190+
def test_action_from_semtype_invalid():
191+
def f():
192+
return "response"
193+
194+
semantic_annotation = object
195+
196+
with pytest.raises(TypeError):
197+
GeneratedClass = builder.action_from(f, semtype=semantic_annotation)
198+
199+
121200
def test_static_from(app, client, app_ctx, static_path):
122201

123202
GeneratedClass = builder.static_from(static_path,)

0 commit comments

Comments
 (0)