11import json
22
3+ import pytest
4+
35
46def test_create_summary (test_app_with_db ):
57 response = test_app_with_db .post (
6- "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
8+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/ " })
79 )
810
911 assert response .status_code == 201
10- assert response .json ()["url" ] == "https://foo.bar"
12+ assert response .json ()["url" ] == "https://foo.bar/ "
1113
1214
1315def test_create_summaries_invalid_json (test_app ):
@@ -24,10 +26,16 @@ def test_create_summaries_invalid_json(test_app):
2426 ]
2527 }
2628
29+ response = test_app .post ("/summaries/" , data = json .dumps ({"url" : "invalid://url" }))
30+ assert response .status_code == 422
31+ assert (
32+ response .json ()["detail" ][0 ]["msg" ] == "URL scheme should be 'http' or 'https'"
33+ )
34+
2735
2836def test_read_summary (test_app_with_db ):
2937 response = test_app_with_db .post (
30- "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
38+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/ " })
3139 )
3240 summary_id = response .json ()["id" ]
3341
@@ -36,7 +44,7 @@ def test_read_summary(test_app_with_db):
3644
3745 response_dict = response .json ()
3846 assert response_dict ["id" ] == summary_id
39- assert response_dict ["url" ] == "https://foo.bar"
47+ assert response_dict ["url" ] == "https://foo.bar/ "
4048 assert response_dict ["summary" ]
4149 assert response_dict ["created_at" ]
4250
@@ -46,10 +54,24 @@ def test_read_summary_incorrect_id(test_app_with_db):
4654 assert response .status_code == 404
4755 assert response .json ()["detail" ] == "Summary not found"
4856
57+ response = test_app_with_db .get ("/summaries/0/" )
58+ assert response .status_code == 422
59+ assert response .json () == {
60+ "detail" : [
61+ {
62+ "ctx" : {"gt" : 0 },
63+ "input" : "0" ,
64+ "loc" : ["path" , "id" ],
65+ "msg" : "Input should be greater than 0" ,
66+ "type" : "greater_than" ,
67+ }
68+ ]
69+ }
70+
4971
5072def test_read_all_summaries (test_app_with_db ):
5173 response = test_app_with_db .post (
52- "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
74+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/ " })
5375 )
5476 summary_id = response .json ()["id" ]
5577
@@ -58,3 +80,132 @@ def test_read_all_summaries(test_app_with_db):
5880
5981 response_list = response .json ()
6082 assert len (list (filter (lambda d : d ["id" ] == summary_id , response_list ))) == 1
83+
84+
85+ def test_remove_summary (test_app_with_db ):
86+ response = test_app_with_db .post (
87+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/" })
88+ )
89+ summary_id = response .json ()["id" ]
90+
91+ response = test_app_with_db .delete (f"/summaries/{ summary_id } /" )
92+ assert response .status_code == 200
93+ assert response .json () == {"id" : summary_id , "url" : "https://foo.bar/" }
94+
95+
96+ def test_remove_summary_incorrect_id (test_app_with_db ):
97+ response = test_app_with_db .delete ("/summaries/999/" )
98+ assert response .status_code == 404
99+ assert response .json ()["detail" ] == "Summary not found"
100+
101+ response = test_app_with_db .delete ("/summaries/0/" )
102+ assert response .status_code == 422
103+ assert response .json () == {
104+ "detail" : [
105+ {
106+ "ctx" : {"gt" : 0 },
107+ "input" : "0" ,
108+ "loc" : ["path" , "id" ],
109+ "msg" : "Input should be greater than 0" ,
110+ "type" : "greater_than" ,
111+ }
112+ ]
113+ }
114+
115+
116+ def test_update_summary (test_app_with_db ):
117+ response = test_app_with_db .post (
118+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/" })
119+ )
120+ summary_id = response .json ()["id" ]
121+
122+ response = test_app_with_db .put (
123+ f"/summaries/{ summary_id } /" ,
124+ data = json .dumps ({"url" : "https://foo.bar/" , "summary" : "updated!" }),
125+ )
126+ assert response .status_code == 200
127+
128+ response_dict = response .json ()
129+ assert response_dict ["id" ] == summary_id
130+ assert response_dict ["url" ] == "https://foo.bar/"
131+ assert response_dict ["summary" ] == "updated!"
132+ assert response_dict ["created_at" ]
133+
134+
135+ @pytest .mark .parametrize (
136+ "summary_id, payload, status_code, detail" ,
137+ [
138+ [
139+ 999 ,
140+ {"url" : "https://foo.bar/" , "summary" : "updated!" },
141+ 404 ,
142+ "Summary not found" ,
143+ ],
144+ [
145+ 0 ,
146+ {"url" : "https://foo.bar/" , "summary" : "updated!" },
147+ 422 ,
148+ [
149+ {
150+ "type" : "greater_than" ,
151+ "loc" : ["path" , "id" ],
152+ "msg" : "Input should be greater than 0" ,
153+ "input" : "0" ,
154+ "ctx" : {"gt" : 0 },
155+ }
156+ ],
157+ ],
158+ [
159+ 1 ,
160+ {},
161+ 422 ,
162+ [
163+ {
164+ "type" : "missing" ,
165+ "loc" : ["body" , "url" ],
166+ "msg" : "Field required" ,
167+ "input" : {},
168+ },
169+ {
170+ "type" : "missing" ,
171+ "loc" : ["body" , "summary" ],
172+ "msg" : "Field required" ,
173+ "input" : {},
174+ },
175+ ],
176+ ],
177+ [
178+ 1 ,
179+ {"url" : "https://foo.bar/" },
180+ 422 ,
181+ [
182+ {
183+ "type" : "missing" ,
184+ "loc" : ["body" , "summary" ],
185+ "msg" : "Field required" ,
186+ "input" : {"url" : "https://foo.bar/" },
187+ }
188+ ],
189+ ],
190+ ],
191+ )
192+ def test_update_summary_invalid (
193+ test_app_with_db , summary_id , payload , status_code , detail
194+ ):
195+ response = test_app_with_db .put (
196+ f"/summaries/{ summary_id } /" , data = json .dumps (payload )
197+ )
198+ assert response .status_code == status_code
199+ print (response .json ()["detail" ])
200+ assert response .json ()["detail" ] == detail
201+
202+
203+ def test_update_summary_invalid_url (test_app ):
204+ response = test_app .put (
205+ "/summaries/1/" ,
206+ data = json .dumps ({"url" : "invalid://url" , "summary" : "updated!" }),
207+ )
208+ assert response .status_code == 422
209+ assert (
210+ response .json ()["detail" ][0 ]["msg" ] == "URL scheme should be 'http' or 'https'"
211+ )
0 commit comments