Skip to content

Commit

Permalink
chore: write tests for csv()
Browse files Browse the repository at this point in the history
  • Loading branch information
anand2312 committed Sep 30, 2023
1 parent 8918a95 commit a6c4fa5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
31 changes: 30 additions & 1 deletion tests/_async/test_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
31 changes: 30 additions & 1 deletion tests/_sync/test_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit a6c4fa5

Please sign in to comment.