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-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 pytest.param("tutorial001_py310"), 

12 pytest.param("tutorial002_py310"), 

13 ], 

14) 

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

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

17 

18 client = TestClient(mod.app) 1abc

19 return client 1abc

20 

21 

22def test_create_item(client: TestClient): 1abdc

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

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

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

26 

27 

28def test_openapi_schema(client: TestClient): 1abdc

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

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

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

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 )