Coverage for tests / test_tutorial / test_dependencies / test_tutorial011.py: 100%

18 statements  

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

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 "tutorial011_py310", 

12 pytest.param("tutorial011_an_py310"), 

13 ], 

14) 

15def get_client(request: pytest.FixtureRequest): 1abdc

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

17 

18 client = TestClient(mod.app) 1abc

19 return client 1abc

20 

21 

22@pytest.mark.parametrize( 1abdc

23 "path,expected_status,expected_response", 

24 [ 

25 ( 

26 "/query-checker/", 

27 200, 

28 {"fixed_content_in_query": False}, 

29 ), 

30 ( 

31 "/query-checker/?q=qwerty", 

32 200, 

33 {"fixed_content_in_query": False}, 

34 ), 

35 ( 

36 "/query-checker/?q=foobar", 

37 200, 

38 {"fixed_content_in_query": True}, 

39 ), 

40 ], 

41) 

42def test_get(path, expected_status, expected_response, client: TestClient): 1abdc

43 response = client.get(path) 1efg

44 assert response.status_code == expected_status 1efg

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

46 

47 

48def test_openapi_schema(client: TestClient): 1abdc

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

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

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

52 { 

53 "openapi": "3.1.0", 

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

55 "paths": { 

56 "/query-checker/": { 

57 "get": { 

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 "summary": "Read Query Check", 

75 "operationId": "read_query_check_query_checker__get", 

76 "parameters": [ 

77 { 

78 "required": False, 

79 "schema": { 

80 "type": "string", 

81 "default": "", 

82 "title": "Q", 

83 }, 

84 "name": "q", 

85 "in": "query", 

86 }, 

87 ], 

88 } 

89 } 

90 }, 

91 "components": { 

92 "schemas": { 

93 "ValidationError": { 

94 "title": "ValidationError", 

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

96 "type": "object", 

97 "properties": { 

98 "loc": { 

99 "title": "Location", 

100 "type": "array", 

101 "items": { 

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

103 }, 

104 }, 

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

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

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

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

109 }, 

110 }, 

111 "HTTPValidationError": { 

112 "title": "HTTPValidationError", 

113 "type": "object", 

114 "properties": { 

115 "detail": { 

116 "title": "Detail", 

117 "type": "array", 

118 "items": { 

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

120 }, 

121 } 

122 }, 

123 }, 

124 } 

125 }, 

126 } 

127 )