|
3 | 3 | import pytest
|
4 | 4 | from httpx import Request, Response
|
5 | 5 |
|
6 |
| -from postgrest import AsyncRequestBuilder |
| 6 | +from postgrest import AsyncRequestBuilder, AsyncSingleRequestBuilder |
7 | 7 | from postgrest.base_request_builder import APIResponse, SingleAPIResponse
|
8 | 8 | from postgrest.types import CountMethod
|
9 | 9 | from postgrest.utils import AsyncClient
|
@@ -36,6 +36,12 @@ def test_select_with_count(self, request_builder: AsyncRequestBuilder):
|
36 | 36 | assert builder.http_method == "HEAD"
|
37 | 37 | assert builder.json == {}
|
38 | 38 |
|
| 39 | + def test_select_as_csv(self, request_builder: AsyncRequestBuilder): |
| 40 | + builder = request_builder.select("*").csv() |
| 41 | + |
| 42 | + assert builder.headers["Accept"] == "text/csv" |
| 43 | + assert isinstance(builder, AsyncSingleRequestBuilder) |
| 44 | + |
39 | 45 |
|
40 | 46 | class TestInsert:
|
41 | 47 | def test_insert(self, request_builder: AsyncRequestBuilder):
|
@@ -146,6 +152,11 @@ def test_explain_options(self, request_builder: AsyncRequestBuilder):
|
146 | 152 | )
|
147 | 153 |
|
148 | 154 |
|
| 155 | +@pytest.fixture |
| 156 | +def csv_api_response() -> str: |
| 157 | + return "id,name\n1,foo\n" |
| 158 | + |
| 159 | + |
149 | 160 | @pytest.fixture
|
150 | 161 | def api_response_with_error() -> Dict[str, Any]:
|
151 | 162 | return {
|
@@ -281,6 +292,15 @@ def request_response_with_single_data(
|
281 | 292 | )
|
282 | 293 |
|
283 | 294 |
|
| 295 | +@pytest.fixture |
| 296 | +def request_response_with_csv_data(csv_api_response: str) -> Response: |
| 297 | + return Response( |
| 298 | + status_code=200, |
| 299 | + text=csv_api_response, |
| 300 | + request=Request(method="GET", url="http://example.com"), |
| 301 | + ) |
| 302 | + |
| 303 | + |
284 | 304 | class TestApiResponse:
|
285 | 305 | def test_response_raises_when_api_error(
|
286 | 306 | self, api_response_with_error: Dict[str, Any]
|
@@ -374,3 +394,12 @@ def test_single_from_http_request_response_constructor(
|
374 | 394 | assert isinstance(result.data, dict)
|
375 | 395 | assert result.data == single_api_response
|
376 | 396 | assert result.count == 2
|
| 397 | + |
| 398 | + def test_single_with_csv_data( |
| 399 | + self, request_response_with_csv_data: Response, csv_api_response: str |
| 400 | + ): |
| 401 | + result = SingleAPIResponse.from_http_request_response( |
| 402 | + request_response_with_csv_data |
| 403 | + ) |
| 404 | + assert isinstance(result.data, str) |
| 405 | + assert result.data == csv_api_response |
0 commit comments