Coverage for tests / test_tutorial / test_response_status_code / test_tutorial001_tutorial002.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7 

8@pytest.fixture( 1abcd

9 name="client", 

10 params=[ 

11 pytest.param("tutorial001_py310"), 

12 pytest.param("tutorial002_py310"), 

13 ], 

14) 

15def get_client(request: pytest.FixtureRequest): 1abcd

16 mod = importlib.import_module(f"docs_src.response_status_code.{request.param}") 1efghijklmnopqr

17 

18 client = TestClient(mod.app) 1efghijklmnopqr

19 return client 1efghijklmnopqr

20 

21 

22def test_create_item(client: TestClient): 1abcd

23 response = client.post("/items/", params={"name": "Test Item"}) 1stuvwxy

24 assert response.status_code == 201, response.text 1stuvwxy

25 assert response.json() == {"name": "Test Item"} 1stuvwxy

26 

27 

28def test_openapi_schema(client: TestClient): 1abcd

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

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

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

32 { 

33 "openapi": "3.1.0", 

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

35 "paths": { 

36 "/items/": { 

37 "post": { 

38 "parameters": [ 

39 { 

40 "name": "name", 

41 "in": "query", 

42 "required": True, 

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

44 } 

45 ], 

46 "summary": "Create Item", 

47 "operationId": "create_item_items__post", 

48 "responses": { 

49 "201": { 

50 "description": "Successful Response", 

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

52 }, 

53 "422": { 

54 "description": "Validation Error", 

55 "content": { 

56 "application/json": { 

57 "schema": { 

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

59 } 

60 } 

61 }, 

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 )