Skip to content

Commit

Permalink
Client now handles the response content correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASHuenchuleo committed Dec 6, 2022
1 parent 876c5b7 commit 0f4691a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion alerce/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _request(
handle_error(resp, response_format)

if response_format == "csv":
return ResultCsv(resp, format=result_format)
return ResultCsv(resp.content, format=result_format)
if response_field and result_format != "json" and result_format != "csv":
return ResultJson(resp.json()[response_field], format=result_format)
return ResultJson(resp.json(), format=result_format)
16 changes: 8 additions & 8 deletions tests/test_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def mock_result(encoding):
return result.decode(encoding)

mock_request.return_value.status_code = 200
mock_request.return_value.decode = mock_result
mock_request.return_value.content.decode = mock_result
query = "SELECT * from objects;"
r = alerce.send_query(query)

Expand All @@ -36,7 +36,7 @@ def mock_result(encoding):
return result.decode(encoding)

mock_request.return_value.status_code = 200
mock_request.return_value.decode = mock_result
mock_request.return_value.content.decode = mock_result
query = "SELECT * from objects where oid=-1;"
r = alerce.send_query(query)
assert r == result.decode("utf-8")
Expand Down Expand Up @@ -84,7 +84,7 @@ def mock_result(encoding):
return result.decode(encoding)

mock_request.return_value.status_code = 200
mock_request.return_value.decode = mock_result
mock_request.return_value.content.decode = mock_result
query = "SELECT * FROM objects;"
r = alerce.send_query(query, format="csv")
assert r == result.decode("utf-8")
Expand All @@ -98,7 +98,7 @@ def mock_result(encoding):
return result.decode(encoding)

mock_request.return_value.status_code = 200
mock_request.return_value.decode = mock_result
mock_request.return_value.content.decode = mock_result
query = "SELECT * FROM objects;"
r = alerce.send_query(query, format="pandas")
assert isinstance(r, pd.DataFrame)
Expand All @@ -111,7 +111,7 @@ def mock_result(encoding):
return "mjd\n2\n1\n"

mock_request.return_value.status_code = 200
mock_request.return_value.decode = mock_result
mock_request.return_value.content.decode = mock_result
sort = "mjd"
r = alerce.send_query("", format="pandas", sort=sort)
assert r.mjd.iloc[0] < r.mjd.iloc[1]
Expand All @@ -123,7 +123,7 @@ def mock_result(encoding):
return "oid,oid,mjd\ntest,test,1\n"

mock_request.return_value.status_code = 200
mock_request.return_value.decode = mock_result
mock_request.return_value.content.decode = mock_result
index = "oid"
r = alerce.send_query("", format="pandas", index=index)
assert r.index.name == index
Expand All @@ -132,7 +132,7 @@ def mock_result(encoding):
@patch.object(Session, "request")
def test_query_format_votable(mock_request):
mock_request.return_value.status_code = 200
mock_request.return_value.decode.return_value = "mjd,oid,oid\n1,5,5\n"
mock_request.return_value.content.decode.return_value = "mjd,oid,oid\n1,5,5\n"
r = alerce.send_query("", format="votable")
columns = ["mjd", "oid", "oid_1"]
expected_table = Table([[1], [5], [5]], names=columns)
Expand All @@ -148,7 +148,7 @@ def test_query_format_votable(mock_request):
@patch.object(Session, "request")
def test_query_format_error(mock_request):
mock_request.return_value.status_code = 200
mock_request.return_value.decode.return_value = "mjd,oid,oid\n1,5,5\n2,6,6\n"
mock_request.return_value.content.decode.return_value = "mjd,oid,oid\n1,5,5\n2,6,6\n"
r = alerce.send_query("", format="json")
expected_result = '[{"mjd":1,"oid":5,"oid_1":5},{"mjd":2,"oid":6,"oid_1":6}]'
assert r == expected_result
Expand Down

0 comments on commit 0f4691a

Please sign in to comment.