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

22 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7from ...utils import needs_py310 1abcd

8 

9 

10@pytest.fixture( 1abcd

11 name="client", 

12 params=[ 

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

14 pytest.param("tutorial014_an_py310", marks=needs_py310), 

15 ], 

16) 

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

18 mod = importlib.import_module( 1efghijklmnopqrstuvw

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

20 ) 

21 

22 client = TestClient(mod.app) 1efghijklmnopqrstuvw

23 return client 1efghijklmnopqrstuvw

24 

25 

26def test_hidden_query(client: TestClient): 1abcd

27 response = client.get("/items?hidden_query=somevalue") 1xyzABCD

28 assert response.status_code == 200, response.text 1xyzABCD

29 assert response.json() == {"hidden_query": "somevalue"} 1xyzABCD

30 

31 

32def test_no_hidden_query(client: TestClient): 1abcd

33 response = client.get("/items") 1EFGHIJK

34 assert response.status_code == 200, response.text 1EFGHIJK

35 assert response.json() == {"hidden_query": "Not found"} 1EFGHIJK

36 

37 

38def test_openapi_schema(client: TestClient): 1abcd

39 response = client.get("/openapi.json") 1LMNOPQR

40 assert response.status_code == 200, response.text 1LMNOPQR

41 assert response.json() == snapshot( 1LMNOPQR

42 { 

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/items/": { 

47 "get": { 

48 "summary": "Read Items", 

49 "operationId": "read_items_items__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 } 

67 } 

68 }, 

69 "components": { 

70 "schemas": { 

71 "HTTPValidationError": { 

72 "title": "HTTPValidationError", 

73 "type": "object", 

74 "properties": { 

75 "detail": { 

76 "title": "Detail", 

77 "type": "array", 

78 "items": { 

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

80 }, 

81 } 

82 }, 

83 }, 

84 "ValidationError": { 

85 "title": "ValidationError", 

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

87 "type": "object", 

88 "properties": { 

89 "loc": { 

90 "title": "Location", 

91 "type": "array", 

92 "items": { 

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

94 }, 

95 }, 

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

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

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

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

100 }, 

101 }, 

102 } 

103 }, 

104 } 

105 )