Skip to content

Commit e1e630a

Browse files
Add more complete checks for fallback server
1 parent 204e35a commit e1e630a

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[run]
2-
concurrency = multiprocessing
2+
concurrency = multiprocessing, thread
33
parallel = true
44
sigterm = true

tests/test_fallback.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from fastapi.testclient import TestClient
2+
from labthings_fastapi.server import server_from_config
3+
from labthings_fastapi.server.fallback import app
4+
5+
6+
def test_fallback_empty():
7+
with TestClient(app) as client:
8+
response = client.get("/")
9+
html = response.text
10+
# test that something when wrong is shown
11+
assert "Something went wrong" in html
12+
assert "No logging info available" in html
13+
14+
15+
def test_fallback_with_config():
16+
app.labthings_config = {"hello": "goodbye"}
17+
with TestClient(app) as client:
18+
response = client.get("/")
19+
html = response.text
20+
assert "Something went wrong" in html
21+
assert "No logging info available" in html
22+
assert '"hello": "goodbye"' in html
23+
24+
25+
def test_fallback_with_error():
26+
app.labthings_error = RuntimeError("Custom error message")
27+
with TestClient(app) as client:
28+
response = client.get("/")
29+
html = response.text
30+
assert "Something went wrong" in html
31+
assert "No logging info available" in html
32+
assert "RuntimeError" in html
33+
assert "Custom error message" in html
34+
35+
36+
def test_fallback_with_server():
37+
config = {
38+
"things": {
39+
"thing1": "labthings_fastapi.example_things:MyThing",
40+
"thing2": {
41+
"class": "labthings_fastapi.example_things:MyThing",
42+
"kwargs": {},
43+
},
44+
}
45+
}
46+
app.labthings_server = server_from_config(config)
47+
with TestClient(app) as client:
48+
response = client.get("/")
49+
html = response.text
50+
assert "Something went wrong" in html
51+
assert "No logging info available" in html
52+
assert "thing1/" in html
53+
assert "thing2/" in html
54+
55+
56+
def test_fallback_with_log():
57+
app.log_history = "Fake log conetent"
58+
with TestClient(app) as client:
59+
response = client.get("/")
60+
html = response.text
61+
assert "Something went wrong" in html
62+
assert "No logging info available" not in html
63+
assert "<p>Logging info</p>" in html
64+
assert "Fake log conetent" in html

tests/test_server_cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def test_invalid_thing():
131131
with raises(ImportError):
132132
check_serve_from_cli(["-j", config_json])
133133

134+
134135
def test_fallback():
135136
"""test the fallback option
136137

0 commit comments

Comments
 (0)