Skip to content

Commit c4ed416

Browse files
author
Joel Collins
committed
Server schema coverage
1 parent 8e0447f commit c4ed416

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tests/test_server_schema.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from labthings.server import schema
2+
from labthings.server import fields
3+
4+
from labthings.core.tasks.thread import TaskThread
5+
from labthings.server.extensions import BaseExtension
6+
7+
8+
def test_schema_json(app_ctx):
9+
test_schema = schema.Schema.from_dict({"i": fields.Int(), "s": fields.String(),})()
10+
11+
obj = type("obj", (object,), {"i": 5, "s": "string"})
12+
13+
with app_ctx.test_request_context():
14+
assert test_schema.jsonify(obj).data == b'{"i":5,"s":"string"}\n'
15+
16+
17+
def test_schema_many(app_ctx):
18+
test_schema = schema.Schema.from_dict({"i": fields.Int(), "s": fields.String(),})(
19+
many=True
20+
)
21+
22+
obj1 = type("obj1", (object,), {"i": 5, "s": "string1"})
23+
obj2 = type("obj2", (object,), {"i": 5, "s": "string2"})
24+
objs = [obj1, obj2]
25+
26+
with app_ctx.test_request_context():
27+
assert (
28+
test_schema.jsonify(objs).data
29+
== b'[{"i":5,"s":"string1"},{"i":5,"s":"string2"}]\n'
30+
)
31+
32+
33+
def test_schema_json_many(app_ctx):
34+
test_schema = schema.Schema.from_dict({"i": fields.Int(), "s": fields.String(),})()
35+
36+
obj1 = type("obj1", (object,), {"i": 5, "s": "string1"})
37+
obj2 = type("obj2", (object,), {"i": 5, "s": "string2"})
38+
objs = [obj1, obj2]
39+
40+
with app_ctx.test_request_context():
41+
assert (
42+
test_schema.jsonify(objs, many=True).data
43+
== b'[{"i":5,"s":"string1"},{"i":5,"s":"string2"}]\n'
44+
)
45+
46+
47+
def test_field_schema(app_ctx):
48+
test_schema = schema.FieldSchema(fields.String())
49+
50+
assert test_schema.serialize(5) == "5"
51+
assert test_schema.dump(5) == "5"
52+
assert test_schema.deserialize("string") == "string"
53+
assert test_schema.jsonify(5).data == b'"5"\n'
54+
55+
56+
def test_task_schema(app_ctx):
57+
test_schema = schema.TaskSchema()
58+
test_task_thread = TaskThread()
59+
60+
with app_ctx.test_request_context():
61+
d = test_schema.dump(test_task_thread)
62+
assert isinstance(d, dict)
63+
assert "data" in d
64+
assert "links" in d
65+
assert isinstance(d.get("links"), dict)
66+
assert "self" in d.get("links")
67+
assert d.get("function") == "None(args=(), kwargs={})"
68+
69+
70+
def test_extension_schema(app_ctx):
71+
test_schema = schema.ExtensionSchema()
72+
test_extension = BaseExtension("org.labthings.tests.extension")
73+
74+
with app_ctx.test_request_context():
75+
d = test_schema.dump(test_extension)
76+
assert isinstance(d, dict)
77+
assert "pythonName" in d
78+
assert d.get("pythonName") == "org.labthings.tests.extension"
79+
assert "links" in d
80+
assert isinstance(d.get("links"), dict)

0 commit comments

Comments
 (0)