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

17 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.tutorial001_py310 import app 1abcd

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_get_item(): 1abcd

10 response = client.get("/items/foo") 1hij

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

12 assert response.json() == {"item": "The Foo Wrestlers"} 1hij

13 

14 

15def test_get_item_not_found(): 1abcd

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

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

18 assert response.headers.get("x-error") is None 1efg

19 assert response.json() == {"detail": "Item not found"} 1efg

20 

21 

22def test_openapi_schema(): 1abcd

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

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

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

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 Item", 

49 "operationId": "read_item_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 "loc": { 

69 "title": "Location", 

70 "type": "array", 

71 "items": { 

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

73 }, 

74 }, 

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

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

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

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

79 }, 

80 }, 

81 "HTTPValidationError": { 

82 "title": "HTTPValidationError", 

83 "type": "object", 

84 "properties": { 

85 "detail": { 

86 "title": "Detail", 

87 "type": "array", 

88 "items": { 

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

90 }, 

91 } 

92 }, 

93 }, 

94 } 

95 }, 

96 } 

97 )