Coverage for tests/test_filter_pydantic_sub_model/test_filter_pydantic_sub_model_pv1.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1abcde

2from fastapi.exceptions import ResponseValidationError 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5from ..utils import needs_pydanticv1 1abcde

6 

7 

8@pytest.fixture(name="client") 1abcde

9def get_client(): 1abcde

10 from .app_pv1 import app 1abcde

11 

12 client = TestClient(app) 1abcde

13 return client 1abcde

14 

15 

16@needs_pydanticv1 1abcde

17def test_filter_sub_model(client: TestClient): 1abcde

18 response = client.get("/model/modelA") 1abcde

19 assert response.status_code == 200, response.text 1abcde

20 assert response.json() == { 1abcde

21 "name": "modelA", 

22 "description": "model-a-desc", 

23 "model_b": {"username": "test-user"}, 

24 } 

25 

26 

27@needs_pydanticv1 1abcde

28def test_validator_is_cloned(client: TestClient): 1abcde

29 with pytest.raises(ResponseValidationError) as err: 1abcde

30 client.get("/model/modelX") 1abcde

31 assert err.value.errors() == [ 1abcde

32 { 

33 "loc": ("response", "name"), 

34 "msg": "name must end in A", 

35 "type": "value_error", 

36 } 

37 ] 

38 

39 

40@needs_pydanticv1 1abcde

41def test_openapi_schema(client: TestClient): 1abcde

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

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

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

45 "openapi": "3.1.0", 

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

47 "paths": { 

48 "/model/{name}": { 

49 "get": { 

50 "summary": "Get Model A", 

51 "operationId": "get_model_a_model__name__get", 

52 "parameters": [ 

53 { 

54 "required": True, 

55 "schema": {"title": "Name", "type": "string"}, 

56 "name": "name", 

57 "in": "path", 

58 } 

59 ], 

60 "responses": { 

61 "200": { 

62 "description": "Successful Response", 

63 "content": { 

64 "application/json": { 

65 "schema": {"$ref": "#/components/schemas/ModelA"} 

66 } 

67 }, 

68 }, 

69 "422": { 

70 "description": "Validation Error", 

71 "content": { 

72 "application/json": { 

73 "schema": { 

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

75 } 

76 } 

77 }, 

78 }, 

79 }, 

80 } 

81 } 

82 }, 

83 "components": { 

84 "schemas": { 

85 "HTTPValidationError": { 

86 "title": "HTTPValidationError", 

87 "type": "object", 

88 "properties": { 

89 "detail": { 

90 "title": "Detail", 

91 "type": "array", 

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

93 } 

94 }, 

95 }, 

96 "ModelA": { 

97 "title": "ModelA", 

98 "required": ["name", "model_b"], 

99 "type": "object", 

100 "properties": { 

101 "name": {"title": "Name", "type": "string"}, 

102 "description": {"title": "Description", "type": "string"}, 

103 "model_b": {"$ref": "#/components/schemas/ModelB"}, 

104 }, 

105 }, 

106 "ModelB": { 

107 "title": "ModelB", 

108 "required": ["username"], 

109 "type": "object", 

110 "properties": {"username": {"title": "Username", "type": "string"}}, 

111 }, 

112 "ValidationError": { 

113 "title": "ValidationError", 

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

115 "type": "object", 

116 "properties": { 

117 "loc": { 

118 "title": "Location", 

119 "type": "array", 

120 "items": { 

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

122 }, 

123 }, 

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

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

126 }, 

127 }, 

128 } 

129 }, 

130 }