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

14 statements  

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

1import pytest 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5from docs_src.path_params.tutorial001_py310 import app 1abcd

6 

7client = TestClient(app) 1abcd

8 

9 

10@pytest.mark.parametrize( 1abcd

11 ("item_id", "expected_response"), 

12 [ 

13 (1, {"item_id": "1"}), 

14 ("alice", {"item_id": "alice"}), 

15 ], 

16) 

17def test_get_items(item_id, expected_response): 1abcd

18 response = client.get(f"/items/{item_id}") 1efg

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

20 assert response.json() == expected_response 1efg

21 

22 

23def test_openapi_schema(): 1abcd

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

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

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

27 { 

28 "openapi": "3.1.0", 

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

30 "paths": { 

31 "/items/{item_id}": { 

32 "get": { 

33 "operationId": "read_item_items__item_id__get", 

34 "parameters": [ 

35 { 

36 "in": "path", 

37 "name": "item_id", 

38 "required": True, 

39 "schema": { 

40 "title": "Item Id", 

41 }, 

42 }, 

43 ], 

44 "responses": { 

45 "200": { 

46 "content": { 

47 "application/json": { 

48 "schema": {}, 

49 }, 

50 }, 

51 "description": "Successful Response", 

52 }, 

53 "422": { 

54 "content": { 

55 "application/json": { 

56 "schema": { 

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

58 }, 

59 }, 

60 }, 

61 "description": "Validation Error", 

62 }, 

63 }, 

64 "summary": "Read Item", 

65 }, 

66 }, 

67 }, 

68 "components": { 

69 "schemas": { 

70 "HTTPValidationError": { 

71 "properties": { 

72 "detail": { 

73 "items": { 

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

75 }, 

76 "title": "Detail", 

77 "type": "array", 

78 }, 

79 }, 

80 "title": "HTTPValidationError", 

81 "type": "object", 

82 }, 

83 "ValidationError": { 

84 "properties": { 

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

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

87 "loc": { 

88 "items": { 

89 "anyOf": [ 

90 { 

91 "type": "string", 

92 }, 

93 { 

94 "type": "integer", 

95 }, 

96 ], 

97 }, 

98 "title": "Location", 

99 "type": "array", 

100 }, 

101 "msg": { 

102 "title": "Message", 

103 "type": "string", 

104 }, 

105 "type": { 

106 "title": "Error Type", 

107 "type": "string", 

108 }, 

109 }, 

110 "required": [ 

111 "loc", 

112 "msg", 

113 "type", 

114 ], 

115 "title": "ValidationError", 

116 "type": "object", 

117 }, 

118 }, 

119 }, 

120 } 

121 )