Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial012.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 "tutorial012", 

13 pytest.param("tutorial012_py39", marks=needs_py39), 

14 "tutorial012_an", 

15 pytest.param("tutorial012_an_py39", marks=needs_py39), 

16 ], 

17) 

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

19 mod = importlib.import_module( 1abcdef

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

21 ) 

22 

23 client = TestClient(mod.app) 1abcdef

24 return client 1abcdef

25 

26 

27def test_default_query_values(client: TestClient): 1abcdef

28 url = "/items/" 1ghijkl

29 response = client.get(url) 1ghijkl

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

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

32 

33 

34def test_multi_query_values(client: TestClient): 1abcdef

35 url = "/items/?q=baz&q=foobar" 1mnopqr

36 response = client.get(url) 1mnopqr

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

38 assert response.json() == {"q": ["baz", "foobar"]} 1mnopqr

39 

40 

41def test_openapi_schema(client: TestClient): 1abcdef

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

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

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

45 "openapi": "3.1.0", 

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

47 "paths": { 

48 "/items/": { 

49 "get": { 

50 "responses": { 

51 "200": { 

52 "description": "Successful Response", 

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

54 }, 

55 "422": { 

56 "description": "Validation Error", 

57 "content": { 

58 "application/json": { 

59 "schema": { 

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

61 } 

62 } 

63 }, 

64 }, 

65 }, 

66 "summary": "Read Items", 

67 "operationId": "read_items_items__get", 

68 "parameters": [ 

69 { 

70 "required": False, 

71 "schema": { 

72 "title": "Q", 

73 "type": "array", 

74 "items": {"type": "string"}, 

75 "default": ["foo", "bar"], 

76 }, 

77 "name": "q", 

78 "in": "query", 

79 } 

80 ], 

81 } 

82 } 

83 }, 

84 "components": { 

85 "schemas": { 

86 "ValidationError": { 

87 "title": "ValidationError", 

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

89 "type": "object", 

90 "properties": { 

91 "loc": { 

92 "title": "Location", 

93 "type": "array", 

94 "items": { 

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

96 }, 

97 }, 

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

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

100 }, 

101 }, 

102 "HTTPValidationError": { 

103 "title": "HTTPValidationError", 

104 "type": "object", 

105 "properties": { 

106 "detail": { 

107 "title": "Detail", 

108 "type": "array", 

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

110 } 

111 }, 

112 }, 

113 } 

114 }, 

115 }