diff --git a/tests/_async/test_request_builder.py b/tests/_async/test_request_builder.py index 6bb3f4e5..dfbf0bf2 100644 --- a/tests/_async/test_request_builder.py +++ b/tests/_async/test_request_builder.py @@ -3,7 +3,7 @@ import pytest from httpx import Request, Response -from postgrest import AsyncRequestBuilder +from postgrest import AsyncRequestBuilder, AsyncSingleRequestBuilder from postgrest.base_request_builder import APIResponse, SingleAPIResponse from postgrest.types import CountMethod from postgrest.utils import AsyncClient @@ -36,6 +36,12 @@ def test_select_with_count(self, request_builder: AsyncRequestBuilder): assert builder.http_method == "HEAD" assert builder.json == {} + def test_select_as_csv(self, request_builder: AsyncRequestBuilder): + builder = request_builder.select("*").csv() + + assert builder.headers["Accept"] == "text/csv" + assert isinstance(builder, AsyncSingleRequestBuilder) + class TestInsert: def test_insert(self, request_builder: AsyncRequestBuilder): @@ -146,6 +152,11 @@ def test_explain_options(self, request_builder: AsyncRequestBuilder): ) +@pytest.fixture +def csv_api_response() -> str: + return "id,name\n1,foo\n" + + @pytest.fixture def api_response_with_error() -> Dict[str, Any]: return { @@ -281,6 +292,15 @@ def request_response_with_single_data( ) +@pytest.fixture +def request_response_with_csv_data(csv_api_response: str) -> Response: + return Response( + status_code=200, + text=csv_api_response, + request=Request(method="GET", url="http://example.com"), + ) + + class TestApiResponse: def test_response_raises_when_api_error( self, api_response_with_error: Dict[str, Any] @@ -374,3 +394,12 @@ def test_single_from_http_request_response_constructor( assert isinstance(result.data, dict) assert result.data == single_api_response assert result.count == 2 + + def test_single_with_csv_data( + self, request_response_with_csv_data: Response, csv_api_response: str + ): + result = SingleAPIResponse.from_http_request_response( + request_response_with_csv_data + ) + assert isinstance(result.data, str) + assert result.data == csv_api_response diff --git a/tests/_sync/test_request_builder.py b/tests/_sync/test_request_builder.py index 5d0ecd25..5d8773e9 100644 --- a/tests/_sync/test_request_builder.py +++ b/tests/_sync/test_request_builder.py @@ -3,7 +3,7 @@ import pytest from httpx import Request, Response -from postgrest import SyncRequestBuilder +from postgrest import SyncRequestBuilder, SyncSingleRequestBuilder from postgrest.base_request_builder import APIResponse, SingleAPIResponse from postgrest.types import CountMethod from postgrest.utils import SyncClient @@ -36,6 +36,12 @@ def test_select_with_count(self, request_builder: SyncRequestBuilder): assert builder.http_method == "HEAD" assert builder.json == {} + def test_select_as_csv(self, request_builder: SyncRequestBuilder): + builder = request_builder.select("*").csv() + + assert builder.headers["Accept"] == "text/csv" + assert isinstance(builder, SyncSingleRequestBuilder) + class TestInsert: def test_insert(self, request_builder: SyncRequestBuilder): @@ -146,6 +152,11 @@ def test_explain_options(self, request_builder: SyncRequestBuilder): ) +@pytest.fixture +def csv_api_response() -> str: + return "id,name\n1,foo\n" + + @pytest.fixture def api_response_with_error() -> Dict[str, Any]: return { @@ -281,6 +292,15 @@ def request_response_with_single_data( ) +@pytest.fixture +def request_response_with_csv_data(csv_api_response: str) -> Response: + return Response( + status_code=200, + text=csv_api_response, + request=Request(method="GET", url="http://example.com"), + ) + + class TestApiResponse: def test_response_raises_when_api_error( self, api_response_with_error: Dict[str, Any] @@ -374,3 +394,12 @@ def test_single_from_http_request_response_constructor( assert isinstance(result.data, dict) assert result.data == single_api_response assert result.count == 2 + + def test_single_with_csv_data( + self, request_response_with_csv_data: Response, csv_api_response: str + ): + result = SingleAPIResponse.from_http_request_response( + request_response_with_csv_data + ) + assert isinstance(result.data, str) + assert result.data == csv_api_response