Coverage for tests / test_put_no_body.py: 100%

20 statements  

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

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8@app.put("/items/{item_id}") 1abcd

9def save_item_no_body(item_id: str): 1abcd

10 return {"item_id": item_id} 1efghij

11 

12 

13client = TestClient(app) 1abcd

14 

15 

16def test_put_no_body(): 1abcd

17 response = client.put("/items/foo") 1egi

18 assert response.status_code == 200, response.text 1egi

19 assert response.json() == {"item_id": "foo"} 1egi

20 

21 

22def test_put_no_body_with_body(): 1abcd

23 response = client.put("/items/foo", json={"name": "Foo"}) 1fhj

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

25 assert response.json() == {"item_id": "foo"} 1fhj

26 

27 

28def test_openapi_schema(): 1abcd

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

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

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

32 { 

33 "openapi": "3.1.0", 

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

35 "paths": { 

36 "/items/{item_id}": { 

37 "put": { 

38 "responses": { 

39 "200": { 

40 "description": "Successful Response", 

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

42 }, 

43 "422": { 

44 "description": "Validation Error", 

45 "content": { 

46 "application/json": { 

47 "schema": { 

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

49 } 

50 } 

51 }, 

52 }, 

53 }, 

54 "summary": "Save Item No Body", 

55 "operationId": "save_item_no_body_items__item_id__put", 

56 "parameters": [ 

57 { 

58 "required": True, 

59 "schema": {"title": "Item Id", "type": "string"}, 

60 "name": "item_id", 

61 "in": "path", 

62 } 

63 ], 

64 } 

65 } 

66 }, 

67 "components": { 

68 "schemas": { 

69 "ValidationError": { 

70 "title": "ValidationError", 

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

72 "type": "object", 

73 "properties": { 

74 "loc": { 

75 "title": "Location", 

76 "type": "array", 

77 "items": { 

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

79 }, 

80 }, 

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

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

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

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

85 }, 

86 }, 

87 "HTTPValidationError": { 

88 "title": "HTTPValidationError", 

89 "type": "object", 

90 "properties": { 

91 "detail": { 

92 "title": "Detail", 

93 "type": "array", 

94 "items": { 

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

96 }, 

97 } 

98 }, 

99 }, 

100 } 

101 }, 

102 } 

103 )