1
1
from typing import Any , Dict , List
2
2
3
3
import pytest
4
- from httpx import Response
4
+ from httpx import Request , Response
5
5
6
6
from postgrest_py import AsyncRequestBuilder
7
7
from postgrest_py .base_request_builder import APIResponse
@@ -173,22 +173,55 @@ def prefer_header_without_count() -> str:
173
173
174
174
@pytest .fixture
175
175
def request_response_without_prefer_header () -> Response :
176
- return Response (status_code = 200 )
176
+ return Response (
177
+ status_code = 200 , request = Request (method = "GET" , url = "http://example.com" )
178
+ )
177
179
178
180
179
181
@pytest .fixture
180
- def request_response_with_prefer_header_without_count () -> Response :
181
- return Response (status_code = 200 , headers = {"prefer" : prefer_header_without_count ()})
182
+ def request_response_with_prefer_header_without_count (
183
+ prefer_header_without_count : str ,
184
+ ) -> Response :
185
+ return Response (
186
+ status_code = 200 ,
187
+ request = Request (
188
+ method = "GET" ,
189
+ url = "http://example.com" ,
190
+ headers = {"prefer" : prefer_header_without_count },
191
+ ),
192
+ )
182
193
183
194
184
195
@pytest .fixture
185
- def request_response_with_prefer_header_with_count_and_content_range () -> Response :
196
+ def request_response_with_prefer_header_with_count_and_content_range (
197
+ prefer_header_with_count : str , content_range_header_with_count : str
198
+ ) -> Response :
186
199
return Response (
187
200
status_code = 200 ,
188
- headers = {
189
- "prefer" : prefer_header_with_count (),
190
- "content-range" : content_range_header_with_count (),
191
- },
201
+ headers = {"content-range" : content_range_header_with_count },
202
+ request = Request (
203
+ method = "GET" ,
204
+ url = "http://example.com" ,
205
+ headers = {"prefer" : prefer_header_with_count },
206
+ ),
207
+ )
208
+
209
+
210
+ @pytest .fixture
211
+ def request_response_with_data (
212
+ prefer_header_with_count : str ,
213
+ content_range_header_with_count : str ,
214
+ api_response : List [Dict [str , Any ]],
215
+ ) -> Response :
216
+ return Response (
217
+ status_code = 200 ,
218
+ headers = {"content-range" : content_range_header_with_count },
219
+ json = api_response ,
220
+ request = Request (
221
+ method = "GET" ,
222
+ url = "http://example.com" ,
223
+ headers = {"prefer" : prefer_header_with_count },
224
+ ),
192
225
)
193
226
194
227
@@ -212,7 +245,7 @@ def test_parses_valid_response_data_and_count(
212
245
assert result .count == count
213
246
214
247
def test_get_count_from_content_range_header_with_count (
215
- self , content_range_header_with_count
248
+ self , content_range_header_with_count : str
216
249
):
217
250
assert (
218
251
APIResponse .get_count_from_content_range_header (
@@ -222,7 +255,7 @@ def test_get_count_from_content_range_header_with_count(
222
255
)
223
256
224
257
def test_get_count_from_content_range_header_without_count (
225
- self , content_range_header_without_count
258
+ self , content_range_header_without_count : str
226
259
):
227
260
assert (
228
261
APIResponse .get_count_from_content_range_header (
@@ -231,8 +264,45 @@ def test_get_count_from_content_range_header_without_count(
231
264
is None
232
265
)
233
266
234
- def test_is_count_in_prefer_header_true (self , prefer_header_with_count ):
267
+ def test_is_count_in_prefer_header_true (self , prefer_header_with_count : str ):
235
268
assert APIResponse .is_count_in_prefer_header (prefer_header_with_count )
236
269
237
- def test_is_count_in_prefer_header_false (self , prefer_header_without_count ):
270
+ def test_is_count_in_prefer_header_false (self , prefer_header_without_count : str ):
238
271
assert not APIResponse .is_count_in_prefer_header (prefer_header_without_count )
272
+
273
+ def test_get_count_from_http_request_response_without_prefer_header (
274
+ self , request_response_without_prefer_header : Response
275
+ ):
276
+ assert (
277
+ APIResponse .get_count_from_http_request_response (
278
+ request_response_without_prefer_header
279
+ )
280
+ is None
281
+ )
282
+
283
+ def test_get_count_from_http_request_response_with_prefer_header_without_count (
284
+ self , request_response_with_prefer_header_without_count : Response
285
+ ):
286
+ assert (
287
+ APIResponse .get_count_from_http_request_response (
288
+ request_response_with_prefer_header_without_count
289
+ )
290
+ is None
291
+ )
292
+
293
+ def test_get_count_from_http_request_response_with_count_and_content_range (
294
+ self , request_response_with_prefer_header_with_count_and_content_range : Response
295
+ ):
296
+ assert (
297
+ APIResponse .get_count_from_http_request_response (
298
+ request_response_with_prefer_header_with_count_and_content_range
299
+ )
300
+ == 2
301
+ )
302
+
303
+ def test_from_http_request_response_constructor (
304
+ self , request_response_with_data : Response , api_response : List [Dict [str , Any ]]
305
+ ):
306
+ result = APIResponse .from_http_request_response (request_response_with_data )
307
+ assert result .data == api_response
308
+ assert result .count == 2
0 commit comments