Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial013.py: 100%

23 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_py39 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial013", 

13 "tutorial013_an", 

14 pytest.param("tutorial013_an_py39", marks=needs_py39), 

15 ], 

16) 

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

18 mod = importlib.import_module( 1abcdef

19 f"docs_src.query_params_str_validations.{request.param}" 

20 ) 

21 

22 client = TestClient(mod.app) 1abcdef

23 return client 1abcdef

24 

25 

26def test_multi_query_values(client: TestClient): 1abcdef

27 url = "/items/?q=foo&q=bar" 1ghijkl

28 response = client.get(url) 1ghijkl

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

30 assert response.json() == {"q": ["foo", "bar"]} 1ghijkl

31 

32 

33def test_query_no_values(client: TestClient): 1abcdef

34 url = "/items/" 1mnopqr

35 response = client.get(url) 1mnopqr

36 assert response.status_code == 200, response.text 1mnopqr

37 assert response.json() == {"q": []} 1mnopqr

38 

39 

40def test_openapi_schema(client: TestClient): 1abcdef

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

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

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

44 "openapi": "3.1.0", 

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

46 "paths": { 

47 "/items/": { 

48 "get": { 

49 "responses": { 

50 "200": { 

51 "description": "Successful Response", 

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

53 }, 

54 "422": { 

55 "description": "Validation Error", 

56 "content": { 

57 "application/json": { 

58 "schema": { 

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

60 } 

61 } 

62 }, 

63 }, 

64 }, 

65 "summary": "Read Items", 

66 "operationId": "read_items_items__get", 

67 "parameters": [ 

68 { 

69 "required": False, 

70 "schema": { 

71 "title": "Q", 

72 "type": "array", 

73 "items": {}, 

74 "default": [], 

75 }, 

76 "name": "q", 

77 "in": "query", 

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 }