Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore: for queries ordered on '__name__', expand field values to full paths. #6829

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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