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-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7 

8@pytest.fixture( 1abcd

9 name="client", 

10 params=[ 

11 pytest.param("tutorial004_py310"), 

12 ], 

13) 

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

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

16 

17 client = TestClient(mod.app) 1efghijk

18 return client 1efghijk

19 

20 

21def test_get_items(client: TestClient): 1abcd

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

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

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

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): 1abcd

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

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

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

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 )