Coverage for tests/test_tutorial/test_async_sql_databases/test_tutorial001.py: 100%

27 statements  

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

1import pytest 1abcde

2from fastapi import FastAPI 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5from ...utils import needs_pydanticv1 1abcde

6 

7 

8@pytest.fixture(name="app", scope="module") 1abcde

9def get_app(): 1abcde

10 with pytest.warns(DeprecationWarning): 1abcde

11 from docs_src.async_sql_databases.tutorial001 import app 1abcde

12 yield app 1abcde

13 

14 

15# TODO: pv2 add version with Pydantic v2 

16@needs_pydanticv1 1abcde

17def test_create_read(app: FastAPI): 1abcde

18 with TestClient(app) as client: 1abcde

19 note = {"text": "Foo bar", "completed": False} 1abcde

20 response = client.post("/notes/", json=note) 1abcde

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

22 data = response.json() 1abcde

23 assert data["text"] == note["text"] 1abcde

24 assert data["completed"] == note["completed"] 1abcde

25 assert "id" in data 1abcde

26 response = client.get("/notes/") 1abcde

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

28 assert data in response.json() 1abcde

29 

30 

31def test_openapi_schema(app: FastAPI): 1abcde

32 with TestClient(app) as client: 1abcde

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

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

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

36 "openapi": "3.1.0", 

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

38 "paths": { 

39 "/notes/": { 

40 "get": { 

41 "responses": { 

42 "200": { 

43 "description": "Successful Response", 

44 "content": { 

45 "application/json": { 

46 "schema": { 

47 "title": "Response Read Notes Notes Get", 

48 "type": "array", 

49 "items": { 

50 "$ref": "#/components/schemas/Note" 

51 }, 

52 } 

53 } 

54 }, 

55 } 

56 }, 

57 "summary": "Read Notes", 

58 "operationId": "read_notes_notes__get", 

59 }, 

60 "post": { 

61 "responses": { 

62 "200": { 

63 "description": "Successful Response", 

64 "content": { 

65 "application/json": { 

66 "schema": {"$ref": "#/components/schemas/Note"} 

67 } 

68 }, 

69 }, 

70 "422": { 

71 "description": "Validation Error", 

72 "content": { 

73 "application/json": { 

74 "schema": { 

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

76 } 

77 } 

78 }, 

79 }, 

80 }, 

81 "summary": "Create Note", 

82 "operationId": "create_note_notes__post", 

83 "requestBody": { 

84 "content": { 

85 "application/json": { 

86 "schema": {"$ref": "#/components/schemas/NoteIn"} 

87 } 

88 }, 

89 "required": True, 

90 }, 

91 }, 

92 } 

93 }, 

94 "components": { 

95 "schemas": { 

96 "NoteIn": { 

97 "title": "NoteIn", 

98 "required": ["text", "completed"], 

99 "type": "object", 

100 "properties": { 

101 "text": {"title": "Text", "type": "string"}, 

102 "completed": {"title": "Completed", "type": "boolean"}, 

103 }, 

104 }, 

105 "Note": { 

106 "title": "Note", 

107 "required": ["id", "text", "completed"], 

108 "type": "object", 

109 "properties": { 

110 "id": {"title": "Id", "type": "integer"}, 

111 "text": {"title": "Text", "type": "string"}, 

112 "completed": {"title": "Completed", "type": "boolean"}, 

113 }, 

114 }, 

115 "ValidationError": { 

116 "title": "ValidationError", 

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

118 "type": "object", 

119 "properties": { 

120 "loc": { 

121 "title": "Location", 

122 "type": "array", 

123 "items": { 

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

125 }, 

126 }, 

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

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

129 }, 

130 }, 

131 "HTTPValidationError": { 

132 "title": "HTTPValidationError", 

133 "type": "object", 

134 "properties": { 

135 "detail": { 

136 "title": "Detail", 

137 "type": "array", 

138 "items": { 

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

140 }, 

141 } 

142 }, 

143 }, 

144 } 

145 }, 

146 }