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

21 statements  

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

1import importlib 1abdc

2from types import ModuleType 1abdc

3 

4import pytest 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8from ...utils import needs_py310 1abdc

9 

10 

11@pytest.fixture( 1abdc

12 name="mod", 

13 params=[ 

14 pytest.param("tutorial001_py310", marks=needs_py310), 

15 pytest.param("tutorial001_an_py310", marks=needs_py310), 

16 ], 

17) 

18def get_mod(request: pytest.FixtureRequest): 1abdc

19 mod = importlib.import_module(f"docs_src.cookie_params.{request.param}") 1abc

20 

21 return mod 1abc

22 

23 

24@pytest.mark.parametrize( 1abdc

25 "path,cookies,expected_status,expected_response", 

26 [ 

27 ("/items", None, 200, {"ads_id": None}), 

28 ("/items", {"ads_id": "ads_track"}, 200, {"ads_id": "ads_track"}), 

29 ( 

30 "/items", 

31 {"ads_id": "ads_track", "session": "cookiesession"}, 

32 200, 

33 {"ads_id": "ads_track"}, 

34 ), 

35 ("/items", {"session": "cookiesession"}, 200, {"ads_id": None}), 

36 ], 

37) 

38def test(path, cookies, expected_status, expected_response, mod: ModuleType): 1abdc

39 client = TestClient(mod.app, cookies=cookies) 1efg

40 response = client.get(path) 1efg

41 assert response.status_code == expected_status 1efg

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

43 

44 

45def test_openapi_schema(mod: ModuleType): 1abdc

46 client = TestClient(mod.app) 1hij

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

48 assert response.status_code == 200 1hij

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

50 { 

51 "openapi": "3.1.0", 

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

53 "paths": { 

54 "/items/": { 

55 "get": { 

56 "responses": { 

57 "200": { 

58 "description": "Successful Response", 

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

60 }, 

61 "422": { 

62 "description": "Validation Error", 

63 "content": { 

64 "application/json": { 

65 "schema": { 

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

67 } 

68 } 

69 }, 

70 }, 

71 }, 

72 "summary": "Read Items", 

73 "operationId": "read_items_items__get", 

74 "parameters": [ 

75 { 

76 "required": False, 

77 "schema": { 

78 "anyOf": [{"type": "string"}, {"type": "null"}], 

79 "title": "Ads Id", 

80 }, 

81 "name": "ads_id", 

82 "in": "cookie", 

83 } 

84 ], 

85 } 

86 } 

87 }, 

88 "components": { 

89 "schemas": { 

90 "ValidationError": { 

91 "title": "ValidationError", 

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

93 "type": "object", 

94 "properties": { 

95 "loc": { 

96 "title": "Location", 

97 "type": "array", 

98 "items": { 

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

100 }, 

101 }, 

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

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

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

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

106 }, 

107 }, 

108 "HTTPValidationError": { 

109 "title": "HTTPValidationError", 

110 "type": "object", 

111 "properties": { 

112 "detail": { 

113 "title": "Detail", 

114 "type": "array", 

115 "items": { 

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

117 }, 

118 } 

119 }, 

120 }, 

121 } 

122 }, 

123 } 

124 )