Coverage for tests / test_tutorial / test_extra_models / test_tutorial004.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 pytest.param("tutorial004_py310"), 

12 ], 

13) 

14def get_client(request: pytest.FixtureRequest): 1abdc

15 mod = importlib.import_module(f"docs_src.extra_models.{request.param}") 1abc

16 

17 client = TestClient(mod.app) 1abc

18 return client 1abc

19 

20 

21def test_get_items(client: TestClient): 1abdc

22 response = client.get("/items/") 1efg

23 assert response.status_code == 200, response.text 1efg

24 assert response.json() == [ 1efg

25 {"name": "Foo", "description": "There comes my hero"}, 

26 {"name": "Red", "description": "It's my aeroplane"}, 

27 ] 

28 

29 

30def test_openapi_schema(client: TestClient): 1abdc

31 response = client.get("/openapi.json") 1hij

32 assert response.status_code == 200, response.text 1hij

33 assert response.json() == snapshot( 1hij

34 { 

35 "openapi": "3.1.0", 

36 "info": {"title": "FastAPI", "version": "0.1.0"}, 

37 "paths": { 

38 "/items/": { 

39 "get": { 

40 "responses": { 

41 "200": { 

42 "description": "Successful Response", 

43 "content": { 

44 "application/json": { 

45 "schema": { 

46 "title": "Response Read Items Items Get", 

47 "type": "array", 

48 "items": { 

49 "$ref": "#/components/schemas/Item" 

50 }, 

51 } 

52 } 

53 }, 

54 } 

55 }, 

56 "summary": "Read Items", 

57 "operationId": "read_items_items__get", 

58 } 

59 } 

60 }, 

61 "components": { 

62 "schemas": { 

63 "Item": { 

64 "title": "Item", 

65 "required": ["name", "description"], 

66 "type": "object", 

67 "properties": { 

68 "name": {"title": "Name", "type": "string"}, 

69 "description": {"title": "Description", "type": "string"}, 

70 }, 

71 } 

72 } 

73 }, 

74 } 

75 )