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

23 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 "tutorial009_py310", 

12 ], 

13) 

14def get_client(request: pytest.FixtureRequest): 1abdc

15 mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}") 1abc

16 

17 client = TestClient(mod.app) 1abc

18 return client 1abc

19 

20 

21def test_post_body(client: TestClient): 1abdc

22 data = {"2": 2.2, "3": 3.3} 1efg

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

24 assert response.status_code == 200, response.text 1efg

25 assert response.json() == data 1efg

26 

27 

28def test_post_invalid_body(client: TestClient): 1abdc

29 data = {"foo": 2.2, "3": 3.3} 1hij

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

31 assert response.status_code == 422, response.text 1hij

32 assert response.json() == { 1hij

33 "detail": [ 

34 { 

35 "type": "int_parsing", 

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

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

38 "input": "foo", 

39 } 

40 ] 

41 } 

42 

43 

44def test_openapi_schema(client: TestClient): 1abdc

45 response = client.get("/openapi.json") 1klm

46 assert response.status_code == 200, response.text 1klm

47 assert response.json() == snapshot( 1klm

48 { 

49 "openapi": "3.1.0", 

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

51 "paths": { 

52 "/index-weights/": { 

53 "post": { 

54 "responses": { 

55 "200": { 

56 "description": "Successful Response", 

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

58 }, 

59 "422": { 

60 "description": "Validation Error", 

61 "content": { 

62 "application/json": { 

63 "schema": { 

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

65 } 

66 } 

67 }, 

68 }, 

69 }, 

70 "summary": "Create Index Weights", 

71 "operationId": "create_index_weights_index_weights__post", 

72 "requestBody": { 

73 "content": { 

74 "application/json": { 

75 "schema": { 

76 "title": "Weights", 

77 "type": "object", 

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

79 } 

80 } 

81 }, 

82 "required": True, 

83 }, 

84 } 

85 } 

86 }, 

87 "components": { 

88 "schemas": { 

89 "ValidationError": { 

90 "title": "ValidationError", 

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

92 "type": "object", 

93 "properties": { 

94 "loc": { 

95 "title": "Location", 

96 "type": "array", 

97 "items": { 

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

99 }, 

100 }, 

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

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

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

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

105 }, 

106 }, 

107 "HTTPValidationError": { 

108 "title": "HTTPValidationError", 

109 "type": "object", 

110 "properties": { 

111 "detail": { 

112 "title": "Detail", 

113 "type": "array", 

114 "items": { 

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

116 }, 

117 } 

118 }, 

119 }, 

120 } 

121 }, 

122 } 

123 )