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

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

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_path_operation(): 1abcd

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

11 assert response.status_code == 200 1efg

12 assert response.json() == {"client_host": "testclient", "item_id": "foo"} 1efg

13 

14 

15def test_openapi(): 1abcd

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

17 assert response.status_code == 200 1hij

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

19 { 

20 "info": { 

21 "title": "FastAPI", 

22 "version": "0.1.0", 

23 }, 

24 "openapi": "3.1.0", 

25 "paths": { 

26 "/items/{item_id}": { 

27 "get": { 

28 "operationId": "read_root_items__item_id__get", 

29 "parameters": [ 

30 { 

31 "in": "path", 

32 "name": "item_id", 

33 "required": True, 

34 "schema": { 

35 "title": "Item Id", 

36 "type": "string", 

37 }, 

38 }, 

39 ], 

40 "responses": { 

41 "200": { 

42 "content": { 

43 "application/json": { 

44 "schema": {}, 

45 }, 

46 }, 

47 "description": "Successful Response", 

48 }, 

49 "422": { 

50 "content": { 

51 "application/json": { 

52 "schema": { 

53 "$ref": "#/components/schemas/HTTPValidationError", 

54 }, 

55 }, 

56 }, 

57 "description": "Validation Error", 

58 }, 

59 }, 

60 "summary": "Read Root", 

61 }, 

62 }, 

63 }, 

64 "components": { 

65 "schemas": { 

66 "HTTPValidationError": { 

67 "properties": { 

68 "detail": { 

69 "items": { 

70 "$ref": "#/components/schemas/ValidationError", 

71 }, 

72 "title": "Detail", 

73 "type": "array", 

74 }, 

75 }, 

76 "title": "HTTPValidationError", 

77 "type": "object", 

78 }, 

79 "ValidationError": { 

80 "properties": { 

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

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

83 "loc": { 

84 "items": { 

85 "anyOf": [ 

86 { 

87 "type": "string", 

88 }, 

89 { 

90 "type": "integer", 

91 }, 

92 ], 

93 }, 

94 "title": "Location", 

95 "type": "array", 

96 }, 

97 "msg": { 

98 "title": "Message", 

99 "type": "string", 

100 }, 

101 "type": { 

102 "title": "Error Type", 

103 "type": "string", 

104 }, 

105 }, 

106 "required": [ 

107 "loc", 

108 "msg", 

109 "type", 

110 ], 

111 "title": "ValidationError", 

112 "type": "object", 

113 }, 

114 }, 

115 }, 

116 } 

117 )