Coverage for tests / test_tutorial / test_events / test_tutorial001.py: 100%

19 statements  

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

1import pytest 1abdc

2from fastapi import FastAPI 1abdc

3from fastapi.testclient import TestClient 1abdc

4from inline_snapshot import snapshot 1abdc

5 

6 

7@pytest.fixture(name="app", scope="module") 1abdc

8def get_app(): 1abdc

9 with pytest.warns(DeprecationWarning): 1abc

10 from docs_src.events.tutorial001_py310 import app 1abc

11 yield app 1abc

12 

13 

14def test_events(app: FastAPI): 1abdc

15 with TestClient(app) as client: 1efg

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

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

18 assert response.json() == {"name": "Fighters"} 1efg

19 

20 

21def test_openapi_schema(app: FastAPI): 1abdc

22 with TestClient(app) as client: 1hij

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

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

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

26 { 

27 "openapi": "3.1.0", 

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

29 "paths": { 

30 "/items/{item_id}": { 

31 "get": { 

32 "responses": { 

33 "200": { 

34 "description": "Successful Response", 

35 "content": {"application/json": {"schema": {}}}, 

36 }, 

37 "422": { 

38 "description": "Validation Error", 

39 "content": { 

40 "application/json": { 

41 "schema": { 

42 "$ref": "#/components/schemas/HTTPValidationError" 

43 } 

44 } 

45 }, 

46 }, 

47 }, 

48 "summary": "Read Items", 

49 "operationId": "read_items_items__item_id__get", 

50 "parameters": [ 

51 { 

52 "required": True, 

53 "schema": {"title": "Item Id", "type": "string"}, 

54 "name": "item_id", 

55 "in": "path", 

56 } 

57 ], 

58 } 

59 } 

60 }, 

61 "components": { 

62 "schemas": { 

63 "ValidationError": { 

64 "title": "ValidationError", 

65 "required": ["loc", "msg", "type"], 

66 "type": "object", 

67 "properties": { 

68 "ctx": {"title": "Context", "type": "object"}, 

69 "input": {"title": "Input"}, 

70 "loc": { 

71 "title": "Location", 

72 "type": "array", 

73 "items": { 

74 "anyOf": [ 

75 {"type": "string"}, 

76 {"type": "integer"}, 

77 ] 

78 }, 

79 }, 

80 "msg": {"title": "Message", "type": "string"}, 

81 "type": {"title": "Error Type", "type": "string"}, 

82 }, 

83 }, 

84 "HTTPValidationError": { 

85 "title": "HTTPValidationError", 

86 "type": "object", 

87 "properties": { 

88 "detail": { 

89 "title": "Detail", 

90 "type": "array", 

91 "items": { 

92 "$ref": "#/components/schemas/ValidationError" 

93 }, 

94 } 

95 }, 

96 }, 

97 } 

98 }, 

99 } 

100 )