1
1
import json
2
2
3
+ import pytest
4
+
3
5
4
6
def test_create_summary (test_app_with_db ):
5
7
response = test_app_with_db .post (
6
- "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
8
+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/ " })
7
9
)
8
10
9
11
assert response .status_code == 201
10
- assert response .json ()["url" ] == "https://foo.bar"
12
+ assert response .json ()["url" ] == "https://foo.bar/ "
11
13
12
14
13
15
def test_create_summaries_invalid_json (test_app ):
@@ -24,10 +26,16 @@ def test_create_summaries_invalid_json(test_app):
24
26
]
25
27
}
26
28
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
+
27
35
28
36
def test_read_summary (test_app_with_db ):
29
37
response = test_app_with_db .post (
30
- "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
38
+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/ " })
31
39
)
32
40
summary_id = response .json ()["id" ]
33
41
@@ -36,7 +44,7 @@ def test_read_summary(test_app_with_db):
36
44
37
45
response_dict = response .json ()
38
46
assert response_dict ["id" ] == summary_id
39
- assert response_dict ["url" ] == "https://foo.bar"
47
+ assert response_dict ["url" ] == "https://foo.bar/ "
40
48
assert response_dict ["summary" ]
41
49
assert response_dict ["created_at" ]
42
50
@@ -46,10 +54,24 @@ def test_read_summary_incorrect_id(test_app_with_db):
46
54
assert response .status_code == 404
47
55
assert response .json ()["detail" ] == "Summary not found"
48
56
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
+
49
71
50
72
def test_read_all_summaries (test_app_with_db ):
51
73
response = test_app_with_db .post (
52
- "/summaries/" , data = json .dumps ({"url" : "https://foo.bar" })
74
+ "/summaries/" , data = json .dumps ({"url" : "https://foo.bar/ " })
53
75
)
54
76
summary_id = response .json ()["id" ]
55
77
@@ -58,3 +80,132 @@ def test_read_all_summaries(test_app_with_db):
58
80
59
81
response_list = response .json ()
60
82
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