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

16 statements  

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

1from fastapi.testclient import TestClient 1abcd

2from inline_snapshot import snapshot 1abcd

3 

4from docs_src.handling_errors.tutorial003_py310 import app 1abcd

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_get(): 1abcd

10 response = client.get("/unicorns/shinny") 1efg

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

12 assert response.json() == {"unicorn_name": "shinny"} 1efg

13 

14 

15def test_get_exception(): 1abcd

16 response = client.get("/unicorns/yolo") 1hij

17 assert response.status_code == 418, response.text 1hij

18 assert response.json() == { 1hij

19 "message": "Oops! yolo did something. There goes a rainbow..." 

20 } 

21 

22 

23def test_openapi_schema(): 1abcd

24 response = client.get("/openapi.json") 1klm

25 assert response.status_code == 200, response.text 1klm

26 assert response.json() == snapshot( 1klm

27 { 

28 "openapi": "3.1.0", 

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

30 "paths": { 

31 "/unicorns/{name}": { 

32 "get": { 

33 "responses": { 

34 "200": { 

35 "description": "Successful Response", 

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

37 }, 

38 "422": { 

39 "description": "Validation Error", 

40 "content": { 

41 "application/json": { 

42 "schema": { 

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

44 } 

45 } 

46 }, 

47 }, 

48 }, 

49 "summary": "Read Unicorn", 

50 "operationId": "read_unicorn_unicorns__name__get", 

51 "parameters": [ 

52 { 

53 "required": True, 

54 "schema": {"title": "Name", "type": "string"}, 

55 "name": "name", 

56 "in": "path", 

57 } 

58 ], 

59 } 

60 } 

61 }, 

62 "components": { 

63 "schemas": { 

64 "ValidationError": { 

65 "title": "ValidationError", 

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

67 "type": "object", 

68 "properties": { 

69 "loc": { 

70 "title": "Location", 

71 "type": "array", 

72 "items": { 

73 "anyOf": [{"type": "string"}, {"type": "integer"}] 

74 }, 

75 }, 

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

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

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

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

80 }, 

81 }, 

82 "HTTPValidationError": { 

83 "title": "HTTPValidationError", 

84 "type": "object", 

85 "properties": { 

86 "detail": { 

87 "title": "Detail", 

88 "type": "array", 

89 "items": { 

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

91 }, 

92 } 

93 }, 

94 }, 

95 } 

96 }, 

97 } 

98 )