Coverage for tests/test_tutorial/test_response_model/test_tutorial003_05.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from fastapi.testclient import TestClient 1abcdef

5 

6from ...utils import needs_py310 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial003_05", 

13 pytest.param("tutorial003_05_py310", marks=needs_py310), 

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abcdef

17 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abcdef

18 

19 client = TestClient(mod.app) 1abcdef

20 return client 1abcdef

21 

22 

23def test_get_portal(client: TestClient): 1abcdef

24 response = client.get("/portal") 1ghijkl

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

26 assert response.json() == {"message": "Here's your interdimensional portal."} 1ghijkl

27 

28 

29def test_get_redirect(client: TestClient): 1abcdef

30 response = client.get("/portal", params={"teleport": True}, follow_redirects=False) 1mnopqr

31 assert response.status_code == 307, response.text 1mnopqr

32 assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ" 1mnopqr

33 

34 

35def test_openapi_schema(client: TestClient): 1abcdef

36 response = client.get("/openapi.json") 1stuvwx

37 assert response.status_code == 200, response.text 1stuvwx

38 assert response.json() == { 1stuvwx

39 "openapi": "3.1.0", 

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

41 "paths": { 

42 "/portal": { 

43 "get": { 

44 "summary": "Get Portal", 

45 "operationId": "get_portal_portal_get", 

46 "parameters": [ 

47 { 

48 "required": False, 

49 "schema": { 

50 "title": "Teleport", 

51 "type": "boolean", 

52 "default": False, 

53 }, 

54 "name": "teleport", 

55 "in": "query", 

56 } 

57 ], 

58 "responses": { 

59 "200": { 

60 "description": "Successful Response", 

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

62 }, 

63 "422": { 

64 "description": "Validation Error", 

65 "content": { 

66 "application/json": { 

67 "schema": { 

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

69 } 

70 } 

71 }, 

72 }, 

73 }, 

74 } 

75 } 

76 }, 

77 "components": { 

78 "schemas": { 

79 "HTTPValidationError": { 

80 "title": "HTTPValidationError", 

81 "type": "object", 

82 "properties": { 

83 "detail": { 

84 "title": "Detail", 

85 "type": "array", 

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

87 } 

88 }, 

89 }, 

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 }, 

105 }, 

106 } 

107 }, 

108 }