Skip to content

Commit c79914d

Browse files
author
Dani Reinón
committed
tests: add tests for left methods
1 parent 7ea2168 commit c79914d

File tree

2 files changed

+166
-26
lines changed

2 files changed

+166
-26
lines changed

tests/_async/test_request_builder.py

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Dict, List
22

33
import pytest
4-
from httpx import Response
4+
from httpx import Request, Response
55

66
from postgrest_py import AsyncRequestBuilder
77
from postgrest_py.base_request_builder import APIResponse
@@ -173,22 +173,55 @@ def prefer_header_without_count() -> str:
173173

174174
@pytest.fixture
175175
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+
)
177179

178180

179181
@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+
)
182193

183194

184195
@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:
186199
return Response(
187200
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+
),
192225
)
193226

194227

@@ -212,7 +245,7 @@ def test_parses_valid_response_data_and_count(
212245
assert result.count == count
213246

214247
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
216249
):
217250
assert (
218251
APIResponse.get_count_from_content_range_header(
@@ -222,7 +255,7 @@ def test_get_count_from_content_range_header_with_count(
222255
)
223256

224257
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
226259
):
227260
assert (
228261
APIResponse.get_count_from_content_range_header(
@@ -231,8 +264,45 @@ def test_get_count_from_content_range_header_without_count(
231264
is None
232265
)
233266

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):
235268
assert APIResponse.is_count_in_prefer_header(prefer_header_with_count)
236269

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):
238271
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

tests/_sync/test_request_builder.py

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Dict, List
22

33
import pytest
4-
from httpx import Response
4+
from httpx import Request, Response
55

66
from postgrest_py import SyncRequestBuilder
77
from postgrest_py.base_request_builder import APIResponse
@@ -173,22 +173,55 @@ def prefer_header_without_count() -> str:
173173

174174
@pytest.fixture
175175
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+
)
177179

178180

179181
@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+
)
182193

183194

184195
@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:
186199
return Response(
187200
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+
),
192225
)
193226

194227

@@ -212,7 +245,7 @@ def test_parses_valid_response_data_and_count(
212245
assert result.count == count
213246

214247
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
216249
):
217250
assert (
218251
APIResponse.get_count_from_content_range_header(
@@ -222,7 +255,7 @@ def test_get_count_from_content_range_header_with_count(
222255
)
223256

224257
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
226259
):
227260
assert (
228261
APIResponse.get_count_from_content_range_header(
@@ -231,8 +264,45 @@ def test_get_count_from_content_range_header_without_count(
231264
is None
232265
)
233266

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):
235268
assert APIResponse.is_count_in_prefer_header(prefer_header_with_count)
236269

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):
238271
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

Comments
 (0)