Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial014.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_py39, needs_py310 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial014", 

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

14 "tutorial014_an", 

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

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

17 ], 

18) 

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

20 mod = importlib.import_module( 1abcdef

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

22 ) 

23 

24 client = TestClient(mod.app) 1abcdef

25 return client 1abcdef

26 

27 

28def test_hidden_query(client: TestClient): 1abcdef

29 response = client.get("/items?hidden_query=somevalue") 1ghijkl

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

31 assert response.json() == {"hidden_query": "somevalue"} 1ghijkl

32 

33 

34def test_no_hidden_query(client: TestClient): 1abcdef

35 response = client.get("/items") 1mnopqr

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

37 assert response.json() == {"hidden_query": "Not found"} 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 "summary": "Read Items", 

50 "operationId": "read_items_items__get", 

51 "responses": { 

52 "200": { 

53 "description": "Successful Response", 

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

55 }, 

56 "422": { 

57 "description": "Validation Error", 

58 "content": { 

59 "application/json": { 

60 "schema": { 

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

62 } 

63 } 

64 }, 

65 }, 

66 }, 

67 } 

68 } 

69 }, 

70 "components": { 

71 "schemas": { 

72 "HTTPValidationError": { 

73 "title": "HTTPValidationError", 

74 "type": "object", 

75 "properties": { 

76 "detail": { 

77 "title": "Detail", 

78 "type": "array", 

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

80 } 

81 }, 

82 }, 

83 "ValidationError": { 

84 "title": "ValidationError", 

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

86 "type": "object", 

87 "properties": { 

88 "loc": { 

89 "title": "Location", 

90 "type": "array", 

91 "items": { 

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

93 }, 

94 }, 

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

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

97 }, 

98 }, 

99 } 

100 }, 

101 }