Skip to content

Commit

Permalink
For queries ordered on '__name__', expand field values to full paths. (
Browse files Browse the repository at this point in the history
…#6829)

Closes #6793.
  • Loading branch information
tseaver authored Dec 4, 2018
1 parent 6b327e9 commit 9c23445
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
13 changes: 10 additions & 3 deletions firestore/google/cloud/firestore_v1beta1/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,7 @@ def _normalize_projection(projection):

return projection

@staticmethod
def _normalize_cursor(cursor, orders):
def _normalize_cursor(self, cursor, orders):
"""Helper: convert cursor to a list of values based on orders."""
if cursor is None:
return
Expand Down Expand Up @@ -593,11 +592,19 @@ def _normalize_cursor(cursor, orders):
raise ValueError(msg)

_transform_bases = (transforms.Sentinel, transforms._ValueList)
for field in document_fields:

for index, key_field in enumerate(zip(order_keys, document_fields)):
key, field = key_field

if isinstance(field, _transform_bases):
msg = _INVALID_CURSOR_TRANSFORM
raise ValueError(msg)

if key == "__name__" and "/" not in field:
document_fields[index] = "{}/{}/{}".format(
self._client._database_string, "/".join(self._parent._path), field
)

return document_fields, before

def _to_protobuf(self):
Expand Down
30 changes: 30 additions & 0 deletions firestore/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,36 @@ def test__normalize_cursor_as_dict_hit(self):

self.assertEqual(query._normalize_cursor(cursor, query._orders), ([1], True))

def test__normalize_cursor_w___name___w_slash(self):
db_string = "projects/my-project/database/(default)"
client = mock.Mock(spec=["_database_string"])
client._database_string = db_string
parent = mock.Mock(spec=["_path", "_client"])
parent._client = client
parent._path = ["C"]
query = self._make_one(parent).order_by("__name__", "ASCENDING")
expected = "{}/C/b".format(db_string)
cursor = ([expected], True)

self.assertEqual(
query._normalize_cursor(cursor, query._orders), ([expected], True)
)

def test__normalize_cursor_w___name___wo_slash(self):
db_string = "projects/my-project/database/(default)"
client = mock.Mock(spec=["_database_string"])
client._database_string = db_string
parent = mock.Mock(spec=["_path", "_client"])
parent._client = client
parent._path = ["C"]
query = self._make_one(parent).order_by("__name__", "ASCENDING")
cursor = (["b"], True)
expected = "{}/C/b".format(db_string)

self.assertEqual(
query._normalize_cursor(cursor, query._orders), ([expected], True)
)

def test__to_protobuf_all_fields(self):
from google.protobuf import wrappers_pb2
from google.cloud.firestore_v1beta1.gapic import enums
Expand Down

0 comments on commit 9c23445

Please sign in to comment.