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

15 statements  

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

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5from docs_src.cookie_params.tutorial001_an import app 1abcde

6 

7 

8@pytest.mark.parametrize( 1abcde

9 "path,cookies,expected_status,expected_response", 

10 [ 

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

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

13 ( 

14 "/items", 

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

16 200, 

17 {"ads_id": "ads_track"}, 

18 ), 

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

20 ], 

21) 

22def test(path, cookies, expected_status, expected_response): 1abcde

23 client = TestClient(app, cookies=cookies) 1abcde

24 response = client.get(path) 1abcde

25 assert response.status_code == expected_status 1abcde

26 assert response.json() == expected_response 1abcde

27 

28 

29def test_openapi_schema(): 1abcde

30 client = TestClient(app) 1abcde

31 response = client.get("/openapi.json") 1abcde

32 assert response.status_code == 200 1abcde

33 assert response.json() == { 1abcde

34 "openapi": "3.1.0", 

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

36 "paths": { 

37 "/items/": { 

38 "get": { 

39 "responses": { 

40 "200": { 

41 "description": "Successful Response", 

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

43 }, 

44 "422": { 

45 "description": "Validation Error", 

46 "content": { 

47 "application/json": { 

48 "schema": { 

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

50 } 

51 } 

52 }, 

53 }, 

54 }, 

55 "summary": "Read Items", 

56 "operationId": "read_items_items__get", 

57 "parameters": [ 

58 { 

59 "required": False, 

60 "schema": IsDict( 

61 { 

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

63 "title": "Ads Id", 

64 } 

65 ) 

66 | IsDict( 

67 # TODO: remove when deprecating Pydantic v1 

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

69 ), 

70 "name": "ads_id", 

71 "in": "cookie", 

72 } 

73 ], 

74 } 

75 } 

76 }, 

77 "components": { 

78 "schemas": { 

79 "ValidationError": { 

80 "title": "ValidationError", 

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

82 "type": "object", 

83 "properties": { 

84 "loc": { 

85 "title": "Location", 

86 "type": "array", 

87 "items": { 

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

89 }, 

90 }, 

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

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

93 }, 

94 }, 

95 "HTTPValidationError": { 

96 "title": "HTTPValidationError", 

97 "type": "object", 

98 "properties": { 

99 "detail": { 

100 "title": "Detail", 

101 "type": "array", 

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

103 } 

104 }, 

105 }, 

106 } 

107 }, 

108 }