Skip to content

Commit a00574f

Browse files
authored
Merge pull request #48 from marklogic/feature/rows-fix
rows.query now returns empty array on no matches
2 parents ba77347 + 480f1d0 commit a00574f

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

marklogic/internal/eval.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from decimal import Decimal
44
from marklogic.documents import Document
5+
from marklogic.internal.util import response_has_no_content
56
from requests import Response
67
from requests_toolbelt.multipart.decoder import MultipartDecoder
78

@@ -18,9 +19,7 @@ def process_multipart_mixed_response(response: Response) -> list:
1819
:param response: The original multipart/mixed response from a call to a
1920
MarkLogic server.
2021
"""
21-
22-
# The presence of this header indicates that the call returned an empty sequence.
23-
if "Content-Length" in response.headers:
22+
if response_has_no_content(response):
2423
return []
2524

2625
parts = MultipartDecoder.from_response(response).parts

marklogic/internal/util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from requests import Response
2+
3+
4+
def response_has_no_content(response: Response) -> bool:
5+
return response.headers.get("Content-Length") == "0"

marklogic/rows.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22
from requests import Session
3+
from marklogic.internal.util import response_has_no_content
4+
35

46
"""
57
Defines classes to simplify usage of the REST rows service defined at
@@ -82,6 +84,8 @@ def query(
8284

8385
response = self._session.post(path, headers=headers, data=data, **kwargs)
8486
if response.ok and not return_response:
87+
if response_has_no_content(response):
88+
return []
8589
return (
8690
response.json()
8791
if graphql

tests/test_rows.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def test_dsl_default(client):
1111
verify_four_musicians_are_returned_in_json(data, "test.musician.lastName")
1212

1313

14+
def test_no_rows_returned(client):
15+
query = 'op.fromView("test", "musician").where(op.eq(op.col("lastName"), "Smith"))'
16+
results = client.rows.query(query)
17+
assert [] == results
18+
19+
1420
def test_dsl_default_return_response(client):
1521
response = client.rows.query(dsl_query, return_response=True)
1622
assert 200 == response.status_code

0 commit comments

Comments
 (0)