|
| 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 |
0 commit comments