Skip to content

Commit

Permalink
Add support for 'None' on response data
Browse files Browse the repository at this point in the history
  • Loading branch information
avibhstarburst authored and hashhar committed Jan 31, 2024
1 parent ca38d29 commit 191cd79
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
23 changes: 23 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ def sample_get_response_data():
}


@pytest.fixture(scope="session")
def sample_get_response_data_none():
"""
This is the response to the second HTTP request (a GET) from an actual
Trino session. It is deliberately not truncated to document such response
and allow to use it for other tests. After doing the steps above, do:
::
>>> cur.fetchall()
"""
yield {
"id": "20210817_140827_00000_arvdv",
"nextUri": "coordinator:8080/v1/statement/20210817_140827_00000_arvdv/2",
"data": None,
"columns": [],
"taskDownloadUris": [],
"partialCancelUri": "http://localhost:8080/v1/stage/20210817_140827_00000_arvdv.0", # NOQA: E501
"stats": {},
"infoUri": "http://coordinator:8080/query.html?20210817_140827_00000_arvdv", # NOQA: E501
}


@pytest.fixture(scope="session")
def sample_get_error_response_data():
yield {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,32 @@ def test_trino_fetch_request(mock_requests, sample_get_response_data):
assert status.rows == sample_get_response_data["data"]


@mock.patch("trino.client.TrinoRequest.http")
def test_trino_fetch_request_data_none(mock_requests, sample_get_response_data_none):
mock_requests.Response.return_value.json.return_value = sample_get_response_data_none

req = TrinoRequest(
host="coordinator",
port=8080,
client_session=ClientSession(
user="test",
source="test",
catalog="test",
schema="test",
properties={},
),
http_scheme="http",
)

http_resp = TrinoRequest.http.Response()
http_resp.status_code = 200
status = req.process(http_resp)

assert status.next_uri == sample_get_response_data_none["nextUri"]
assert status.id == sample_get_response_data_none["id"]
assert status.rows == []


@mock.patch("trino.client.TrinoRequest.http")
def test_trino_fetch_error(mock_requests, sample_get_error_response_data):
mock_requests.Response.return_value.json.return_value = sample_get_error_response_data
Expand Down
4 changes: 3 additions & 1 deletion trino/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ def process(self, http_response) -> TrinoStatus:

self._next_uri = response.get("nextUri")

data = response.get("data") if response.get("data") else []

return TrinoStatus(
id=response["id"],
stats=response["stats"],
Expand All @@ -641,7 +643,7 @@ def process(self, http_response) -> TrinoStatus:
next_uri=self._next_uri,
update_type=response.get("updateType"),
update_count=response.get("updateCount"),
rows=response.get("data", []),
rows=data,
columns=response.get("columns"),
)

Expand Down

0 comments on commit 191cd79

Please sign in to comment.