Skip to content

Commit fbb7a0c

Browse files
author
Joel Collins
committed
Coverage for server representations
1 parent 21e18f3 commit fbb7a0c

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

tests/test_server_representations.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from labthings.server import representations
2+
from flask import Flask, Response
3+
import pytest
4+
5+
6+
@pytest.fixture()
7+
def app(request):
8+
9+
app = Flask(__name__)
10+
11+
# pushes an application context manually
12+
ctx = app.app_context()
13+
ctx.push()
14+
15+
# bind the test life with the context through the
16+
request.addfinalizer(ctx.pop)
17+
return app
18+
19+
20+
@pytest.fixture()
21+
def debug_app(request):
22+
23+
app = Flask(__name__)
24+
app.debug = True
25+
26+
# pushes an application context manually
27+
ctx = app.app_context()
28+
ctx.push()
29+
30+
# bind the test life with the context through the
31+
request.addfinalizer(ctx.pop)
32+
return app
33+
34+
35+
@pytest.fixture()
36+
def app_context(app):
37+
with app.app_context():
38+
yield app
39+
40+
41+
@pytest.fixture()
42+
def app_context_debug(debug_app):
43+
with debug_app.app_context():
44+
yield debug_app
45+
46+
47+
@pytest.fixture
48+
def labthings_json_encoder():
49+
return representations.LabThingsJSONEncoder
50+
51+
52+
def test_encoder_default_exception(labthings_json_encoder):
53+
with pytest.raises(TypeError):
54+
labthings_json_encoder().default("")
55+
56+
57+
def test_encode_json(labthings_json_encoder):
58+
data = {
59+
"a": "String",
60+
"b": 5,
61+
"c": [10, 20, 30, 40, 50],
62+
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
63+
}
64+
assert (
65+
representations.encode_json(data, encoder=labthings_json_encoder)
66+
== '{"a": "String", "b": 5, "c": [10, 20, 30, 40, 50], "d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]}}\n'
67+
)
68+
69+
70+
def test_output_json(app_context):
71+
data = {
72+
"a": "String",
73+
"b": 5,
74+
"c": [10, 20, 30, 40, 50],
75+
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
76+
}
77+
78+
with app_context.test_request_context():
79+
response = representations.output_json(data, 200)
80+
assert isinstance(response, Response)
81+
assert response.status_code == 200
82+
assert (
83+
response.data
84+
== b'{"a": "String", "b": 5, "c": [10, 20, 30, 40, 50], "d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]}}\n'
85+
)
86+
87+
88+
def test_pretty_output_json(app_context_debug):
89+
data = {
90+
"a": "String",
91+
"b": 5,
92+
"c": [10, 20, 30, 40, 50],
93+
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
94+
}
95+
96+
with app_context_debug.test_request_context():
97+
response = representations.output_json(data, 200)
98+
assert isinstance(response, Response)
99+
assert response.status_code == 200
100+
assert (
101+
response.data
102+
== b'{\n "a": "String",\n "b": 5,\n "c": [\n 10,\n 20,\n 30,\n 40,\n 50\n ],\n "d": {\n "a": "String",\n "b": 5,\n "c": [\n 10,\n 20,\n 30,\n 40,\n 50\n ]\n }\n}\n'
103+
)

0 commit comments

Comments
 (0)