Skip to content

Commit ddc0f43

Browse files
authored
For queries ordered on '__name__', expand field values to full paths. (#6829)
Closes #6793.
1 parent 9f09cd9 commit ddc0f43

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

packages/google-cloud-firestore/google/cloud/firestore_v1beta1/query.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,7 @@ def _normalize_projection(projection):
563563

564564
return projection
565565

566-
@staticmethod
567-
def _normalize_cursor(cursor, orders):
566+
def _normalize_cursor(self, cursor, orders):
568567
"""Helper: convert cursor to a list of values based on orders."""
569568
if cursor is None:
570569
return
@@ -593,11 +592,19 @@ def _normalize_cursor(cursor, orders):
593592
raise ValueError(msg)
594593

595594
_transform_bases = (transforms.Sentinel, transforms._ValueList)
596-
for field in document_fields:
595+
596+
for index, key_field in enumerate(zip(order_keys, document_fields)):
597+
key, field = key_field
598+
597599
if isinstance(field, _transform_bases):
598600
msg = _INVALID_CURSOR_TRANSFORM
599601
raise ValueError(msg)
600602

603+
if key == "__name__" and "/" not in field:
604+
document_fields[index] = "{}/{}/{}".format(
605+
self._client._database_string, "/".join(self._parent._path), field
606+
)
607+
601608
return document_fields, before
602609

603610
def _to_protobuf(self):

packages/google-cloud-firestore/tests/unit/test_query.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,36 @@ def test__normalize_cursor_as_dict_hit(self):
603603

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

606+
def test__normalize_cursor_w___name___w_slash(self):
607+
db_string = "projects/my-project/database/(default)"
608+
client = mock.Mock(spec=["_database_string"])
609+
client._database_string = db_string
610+
parent = mock.Mock(spec=["_path", "_client"])
611+
parent._client = client
612+
parent._path = ["C"]
613+
query = self._make_one(parent).order_by("__name__", "ASCENDING")
614+
expected = "{}/C/b".format(db_string)
615+
cursor = ([expected], True)
616+
617+
self.assertEqual(
618+
query._normalize_cursor(cursor, query._orders), ([expected], True)
619+
)
620+
621+
def test__normalize_cursor_w___name___wo_slash(self):
622+
db_string = "projects/my-project/database/(default)"
623+
client = mock.Mock(spec=["_database_string"])
624+
client._database_string = db_string
625+
parent = mock.Mock(spec=["_path", "_client"])
626+
parent._client = client
627+
parent._path = ["C"]
628+
query = self._make_one(parent).order_by("__name__", "ASCENDING")
629+
cursor = (["b"], True)
630+
expected = "{}/C/b".format(db_string)
631+
632+
self.assertEqual(
633+
query._normalize_cursor(cursor, query._orders), ([expected], True)
634+
)
635+
606636
def test__to_protobuf_all_fields(self):
607637
from google.protobuf import wrappers_pb2
608638
from google.cloud.firestore_v1beta1.gapic import enums

0 commit comments

Comments
 (0)