Coverage for tests/test_tutorial/test_handling_errors/test_tutorial006.py: 100%

20 statements  

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

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.handling_errors.tutorial006 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_get_validation_error(): 1abcde

10 response = client.get("/items/foo") 1abcde

11 assert response.status_code == 422, response.text 1abcde

12 assert response.json() == IsDict( 1abcde

13 { 

14 "detail": [ 

15 { 

16 "type": "int_parsing", 

17 "loc": ["path", "item_id"], 

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

19 "input": "foo", 

20 } 

21 ] 

22 } 

23 ) | IsDict( 

24 # TODO: remove when deprecating Pydantic v1 

25 { 

26 "detail": [ 

27 { 

28 "loc": ["path", "item_id"], 

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

30 "type": "type_error.integer", 

31 } 

32 ] 

33 } 

34 ) 

35 

36 

37def test_get_http_error(): 1abcde

38 response = client.get("/items/3") 1abcde

39 assert response.status_code == 418, response.text 1abcde

40 assert response.json() == {"detail": "Nope! I don't like 3."} 1abcde

41 

42 

43def test_get(): 1abcde

44 response = client.get("/items/2") 1abcde

45 assert response.status_code == 200, response.text 1abcde

46 assert response.json() == {"item_id": 2} 1abcde

47 

48 

49def test_openapi_schema(): 1abcde

50 response = client.get("/openapi.json") 1abcde

51 assert response.status_code == 200, response.text 1abcde

52 assert response.json() == { 1abcde

53 "openapi": "3.1.0", 

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

55 "paths": { 

56 "/items/{item_id}": { 

57 "get": { 

58 "responses": { 

59 "200": { 

60 "description": "Successful Response", 

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

62 }, 

63 "422": { 

64 "description": "Validation Error", 

65 "content": { 

66 "application/json": { 

67 "schema": { 

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

69 } 

70 } 

71 }, 

72 }, 

73 }, 

74 "summary": "Read Item", 

75 "operationId": "read_item_items__item_id__get", 

76 "parameters": [ 

77 { 

78 "required": True, 

79 "schema": {"title": "Item Id", "type": "integer"}, 

80 "name": "item_id", 

81 "in": "path", 

82 } 

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 }, 

104 }, 

105 "HTTPValidationError": { 

106 "title": "HTTPValidationError", 

107 "type": "object", 

108 "properties": { 

109 "detail": { 

110 "title": "Detail", 

111 "type": "array", 

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

113 } 

114 }, 

115 }, 

116 } 

117 }, 

118 }