Coverage for tests/test_tutorial/test_body_nested_models/test_tutorial009_py39.py: 100%

26 statements  

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

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

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

9def get_client(): 1eabcd

10 from docs_src.body_nested_models.tutorial009_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_post_body(client: TestClient): 1eabcd

18 data = {"2": 2.2, "3": 3.3} 1abcd

19 response = client.post("/index-weights/", json=data) 1abcd

20 assert response.status_code == 200, response.text 1abcd

21 assert response.json() == data 1abcd

22 

23 

24@needs_py39 1eabcd

25def test_post_invalid_body(client: TestClient): 1eabcd

26 data = {"foo": 2.2, "3": 3.3} 1abcd

27 response = client.post("/index-weights/", json=data) 1abcd

28 assert response.status_code == 422, response.text 1abcd

29 assert response.json() == IsDict( 1abcd

30 { 

31 "detail": [ 

32 { 

33 "type": "int_parsing", 

34 "loc": ["body", "foo", "[key]"], 

35 "msg": "Input should be a valid integer, unable to parse string as an integer", 

36 "input": "foo", 

37 } 

38 ] 

39 } 

40 ) | IsDict( 

41 # TODO: remove when deprecating Pydantic v1 

42 { 

43 "detail": [ 

44 { 

45 "loc": ["body", "__key__"], 

46 "msg": "value is not a valid integer", 

47 "type": "type_error.integer", 

48 } 

49 ] 

50 } 

51 ) 

52 

53 

54@needs_py39 1eabcd

55def test_openapi_schema(client: TestClient): 1eabcd

56 response = client.get("/openapi.json") 1abcd

57 assert response.status_code == 200, response.text 1abcd

58 assert response.json() == { 1abcd

59 "openapi": "3.1.0", 

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

61 "paths": { 

62 "/index-weights/": { 

63 "post": { 

64 "responses": { 

65 "200": { 

66 "description": "Successful Response", 

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

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 "summary": "Create Index Weights", 

81 "operationId": "create_index_weights_index_weights__post", 

82 "requestBody": { 

83 "content": { 

84 "application/json": { 

85 "schema": { 

86 "title": "Weights", 

87 "type": "object", 

88 "additionalProperties": {"type": "number"}, 

89 } 

90 } 

91 }, 

92 "required": True, 

93 }, 

94 } 

95 } 

96 }, 

97 "components": { 

98 "schemas": { 

99 "ValidationError": { 

100 "title": "ValidationError", 

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

102 "type": "object", 

103 "properties": { 

104 "loc": { 

105 "title": "Location", 

106 "type": "array", 

107 "items": { 

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

109 }, 

110 }, 

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

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

113 }, 

114 }, 

115 "HTTPValidationError": { 

116 "title": "HTTPValidationError", 

117 "type": "object", 

118 "properties": { 

119 "detail": { 

120 "title": "Detail", 

121 "type": "array", 

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

123 } 

124 }, 

125 }, 

126 } 

127 }, 

128 }