3
3
4
4
import json
5
5
6
+ import pytest
7
+
6
8
7
9
def test_create_summary (test_app_with_db ):
8
10
response = test_app_with_db .post (
@@ -26,6 +28,10 @@ def test_create_summaries_invalid_json(test_app):
26
28
]
27
29
}
28
30
31
+ response = test_app .post ("/summaries/" , data = json .dumps ({"url" : "invalid://url" }))
32
+ assert response .status_code == 422
33
+ assert response .json ()["detail" ][0 ]["msg" ] == "URL scheme not permitted"
34
+
29
35
30
36
def test_read_summary (test_app_with_db ):
31
37
response = test_app_with_db .post (
@@ -48,6 +54,19 @@ def test_read_summary_incorrect_id(test_app_with_db):
48
54
assert response .status_code == 404
49
55
assert response .json ()["detail" ] == "Summary not found"
50
56
57
+ response = test_app_with_db .get ("/summaries/0/" )
58
+ assert response .status_code == 422
59
+ assert response .json () == {
60
+ "detail" : [
61
+ {
62
+ "loc" : ["path" , "id" ],
63
+ "msg" : "ensure this value is greater than 0" ,
64
+ "type" : "value_error.number.not_gt" ,
65
+ "ctx" : {"limit_value" : 0 },
66
+ }
67
+ ]
68
+ }
69
+
51
70
52
71
def test_read_all_summaries (test_app_with_db ):
53
72
response = test_app_with_db .post (
@@ -60,3 +79,124 @@ def test_read_all_summaries(test_app_with_db):
60
79
61
80
response_list = response .json ()
62
81
assert len (list (filter (lambda d : d ["id" ] == summary_id , response_list ))) == 1
82
+
83
+
84
+ def test_remove_summary (test_app_with_db ):
85
+ response = test_app_with_db .post (
86
+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
87
+ )
88
+ summary_id = response .json ()["id" ]
89
+
90
+ response = test_app_with_db .delete (f"/summaries/{ summary_id } /" )
91
+ assert response .status_code == 200
92
+ assert response .json () == {"id" : summary_id , "url" : "https://foo.bar" }
93
+
94
+
95
+ def test_remove_summary_incorrect_id (test_app_with_db ):
96
+ response = test_app_with_db .delete ("/summaries/999/" )
97
+ assert response .status_code == 404
98
+ assert response .json ()["detail" ] == "Summary not found"
99
+
100
+ response = test_app_with_db .delete ("/summaries/0/" )
101
+ assert response .status_code == 422
102
+ assert response .json () == {
103
+ "detail" : [
104
+ {
105
+ "loc" : ["path" , "id" ],
106
+ "msg" : "ensure this value is greater than 0" ,
107
+ "type" : "value_error.number.not_gt" ,
108
+ "ctx" : {"limit_value" : 0 },
109
+ }
110
+ ]
111
+ }
112
+
113
+
114
+ def test_update_summary (test_app_with_db ):
115
+ response = test_app_with_db .post (
116
+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
117
+ )
118
+ summary_id = response .json ()["id" ]
119
+
120
+ response = test_app_with_db .put (
121
+ f"/summaries/{ summary_id } /" ,
122
+ data = json .dumps ({"url" : "https://foo.bar" , "summary" : "updated!" }),
123
+ )
124
+ assert response .status_code == 200
125
+
126
+ response_dict = response .json ()
127
+ assert response_dict ["id" ] == summary_id
128
+ assert response_dict ["url" ] == "https://foo.bar"
129
+ assert response_dict ["summary" ] == "updated!"
130
+ assert response_dict ["created_at" ]
131
+
132
+
133
+ @pytest .mark .parametrize (
134
+ "summary_id, payload, status_code, detail" ,
135
+ [
136
+ [
137
+ 999 ,
138
+ {"url" : "https://foo.bar" , "summary" : "updated!" },
139
+ 404 ,
140
+ "Summary not found" ,
141
+ ],
142
+ [
143
+ 0 ,
144
+ {"url" : "https://foo.bar" , "summary" : "updated!" },
145
+ 422 ,
146
+ [
147
+ {
148
+ "loc" : ["path" , "id" ],
149
+ "msg" : "ensure this value is greater than 0" ,
150
+ "type" : "value_error.number.not_gt" ,
151
+ "ctx" : {"limit_value" : 0 },
152
+ }
153
+ ],
154
+ ],
155
+ [
156
+ 1 ,
157
+ {},
158
+ 422 ,
159
+ [
160
+ {
161
+ "loc" : ["body" , "url" ],
162
+ "msg" : "field required" ,
163
+ "type" : "value_error.missing" ,
164
+ },
165
+ {
166
+ "loc" : ["body" , "summary" ],
167
+ "msg" : "field required" ,
168
+ "type" : "value_error.missing" ,
169
+ },
170
+ ],
171
+ ],
172
+ [
173
+ 1 ,
174
+ {"url" : "https://foo.bar" },
175
+ 422 ,
176
+ [
177
+ {
178
+ "loc" : ["body" , "summary" ],
179
+ "msg" : "field required" ,
180
+ "type" : "value_error.missing" ,
181
+ }
182
+ ],
183
+ ],
184
+ ],
185
+ )
186
+ def test_update_summary_invalid (
187
+ test_app_with_db , summary_id , payload , status_code , detail
188
+ ):
189
+ response = test_app_with_db .put (
190
+ f"/summaries/{ summary_id } /" , data = json .dumps (payload )
191
+ )
192
+ assert response .status_code == status_code
193
+ assert response .json ()["detail" ] == detail
194
+
195
+
196
+ def test_update_summary_invalid_url (test_app ):
197
+ response = test_app .put (
198
+ "/summaries/1/" ,
199
+ data = json .dumps ({"url" : "invalid://url" , "summary" : "updated!" }),
200
+ )
201
+ assert response .status_code == 422
202
+ assert response .json ()["detail" ][0 ]["msg" ] == "URL scheme not permitted"
0 commit comments