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

16 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi.testclient import TestClient 1efgh

2from inline_snapshot import snapshot 1efgh

3 

4from docs_src.events.tutorial003_py310 import ( 1efgh

5 app, 

6 fake_answer_to_everything_ml_model, 

7 ml_models, 

8) 

9 

10 

11def test_events(): 1efgh

12 assert not ml_models, "ml_models should be empty" 1abcd

13 with TestClient(app) as client: 1abcd

14 assert ml_models["answer_to_everything"] == fake_answer_to_everything_ml_model 1abcd

15 response = client.get("/predict", params={"x": 2}) 1abcd

16 assert response.status_code == 200, response.text 1abcd

17 assert response.json() == {"result": 84.0} 1abcd

18 assert not ml_models, "ml_models should be empty" 1abcd

19 

20 

21def test_openapi_schema(): 1efgh

22 with TestClient(app) as client: 1ijkl

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

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

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

26 { 

27 "openapi": "3.1.0", 

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

29 "paths": { 

30 "/predict": { 

31 "get": { 

32 "summary": "Predict", 

33 "operationId": "predict_predict_get", 

34 "parameters": [ 

35 { 

36 "required": True, 

37 "schema": {"title": "X", "type": "number"}, 

38 "name": "x", 

39 "in": "query", 

40 } 

41 ], 

42 "responses": { 

43 "200": { 

44 "description": "Successful Response", 

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

46 }, 

47 "422": { 

48 "description": "Validation Error", 

49 "content": { 

50 "application/json": { 

51 "schema": { 

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

53 } 

54 } 

55 }, 

56 }, 

57 }, 

58 } 

59 } 

60 }, 

61 "components": { 

62 "schemas": { 

63 "HTTPValidationError": { 

64 "title": "HTTPValidationError", 

65 "type": "object", 

66 "properties": { 

67 "detail": { 

68 "title": "Detail", 

69 "type": "array", 

70 "items": { 

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

72 }, 

73 } 

74 }, 

75 }, 

76 "ValidationError": { 

77 "title": "ValidationError", 

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

79 "type": "object", 

80 "properties": { 

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

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

83 "loc": { 

84 "title": "Location", 

85 "type": "array", 

86 "items": { 

87 "anyOf": [ 

88 {"type": "string"}, 

89 {"type": "integer"}, 

90 ] 

91 }, 

92 }, 

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

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

95 }, 

96 }, 

97 } 

98 }, 

99 } 

100 )