Debian Codegraph @

tests/test_tutorial/test_dependencies/test_tutorial008d.py

raw
1 import importlib
2 from types import ModuleType
3
4 import pytest
5 from fastapi.testclient import TestClient
6
7
8 @pytest.fixture(
9 name="mod",
10 params=[
11 pytest.param("tutorial008d_py310"),
12 pytest.param("tutorial008d_an_py310"),
13 ],
14 )
15 def get_mod(request: pytest.FixtureRequest):
16 mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
17
18 return mod
19
20
21 def test_get_no_item(mod: ModuleType):
22 client = TestClient(mod.app)
23 response = client.get("/items/foo")
24 assert response.status_code == 404, response.text
25 assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
26
27
28 def test_get(mod: ModuleType):
29 client = TestClient(mod.app)
30 response = client.get("/items/plumbus")
31 assert response.status_code == 200, response.text
32 assert response.json() == "plumbus"
33
34
35 def test_internal_error(mod: ModuleType):
36 client = TestClient(mod.app)
37 with pytest.raises(mod.InternalError) as exc_info:
38 client.get("/items/portal-gun")
39 assert (
40 exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick"
41 )
42
43
44 def test_internal_server_error(mod: ModuleType):
45 client = TestClient(mod.app, raise_server_exceptions=False)
46 response = client.get("/items/portal-gun")
47 assert response.status_code == 500, response.text
48 assert response.text == "Internal Server Error"