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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@needs_py39 1eabcd

9@pytest.mark.parametrize( 1eabcd

10 "path,cookies,expected_status,expected_response", 

11 [ 

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

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

14 ( 

15 "/items", 

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

17 200, 

18 {"ads_id": "ads_track"}, 

19 ), 

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

21 ], 

22) 

23def test(path, cookies, expected_status, expected_response): 1eabcd

24 from docs_src.cookie_params.tutorial001_an_py39 import app 1abcd

25 

26 client = TestClient(app, cookies=cookies) 1abcd

27 response = client.get(path) 1abcd

28 assert response.status_code == expected_status 1abcd

29 assert response.json() == expected_response 1abcd

30 

31 

32@needs_py39 1eabcd

33def test_openapi_schema(): 1eabcd

34 from docs_src.cookie_params.tutorial001_an_py39 import app 1abcd

35 

36 client = TestClient(app) 1abcd

37 response = client.get("/openapi.json") 1abcd

38 assert response.status_code == 200 1abcd

39 assert response.json() == { 1abcd

40 "openapi": "3.1.0", 

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

42 "paths": { 

43 "/items/": { 

44 "get": { 

45 "responses": { 

46 "200": { 

47 "description": "Successful Response", 

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

49 }, 

50 "422": { 

51 "description": "Validation Error", 

52 "content": { 

53 "application/json": { 

54 "schema": { 

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

56 } 

57 } 

58 }, 

59 }, 

60 }, 

61 "summary": "Read Items", 

62 "operationId": "read_items_items__get", 

63 "parameters": [ 

64 { 

65 "required": False, 

66 "schema": IsDict( 

67 { 

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

69 "title": "Ads Id", 

70 } 

71 ) 

72 | IsDict( 

73 # TODO: remove when deprecating Pydantic v1 

74 {"title": "Ads Id", "type": "string"} 

75 ), 

76 "name": "ads_id", 

77 "in": "cookie", 

78 } 

79 ], 

80 } 

81 } 

82 }, 

83 "components": { 

84 "schemas": { 

85 "ValidationError": { 

86 "title": "ValidationError", 

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

88 "type": "object", 

89 "properties": { 

90 "loc": { 

91 "title": "Location", 

92 "type": "array", 

93 "items": { 

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

95 }, 

96 }, 

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

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

99 }, 

100 }, 

101 "HTTPValidationError": { 

102 "title": "HTTPValidationError", 

103 "type": "object", 

104 "properties": { 

105 "detail": { 

106 "title": "Detail", 

107 "type": "array", 

108 "items": {"$ref": "#/components/schemas/ValidationError"}, 

109 } 

110 }, 

111 }, 

112 } 

113 }, 

114 }