Skip to content

Commit

Permalink
Add arg to understand, when we're in group
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaFaer committed Jul 30, 2019
1 parent 1b631b1 commit 01f4da1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion firestore/google/cloud/firestore_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def collection_group(self, collection_id):
)

collection = self.collection(collection_id)
return query.Query(collection, all_descendants=True)
return query.Query(collection, all_descendants=True, is_group=True)

def document(self, *document_path):
"""Get a reference to a document in a collection.
Expand Down
14 changes: 11 additions & 3 deletions firestore/google/cloud/firestore_v1/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def __init__(
start_at=None,
end_at=None,
all_descendants=False,
is_group=False,
):
self._parent = parent
self._projection = projection
Expand All @@ -146,6 +147,7 @@ def __init__(
self._start_at = start_at
self._end_at = end_at
self._all_descendants = all_descendants
self._is_group = is_group

def __eq__(self, other):
if not isinstance(other, self.__class__):
Expand Down Expand Up @@ -214,6 +216,7 @@ def select(self, field_paths):
start_at=self._start_at,
end_at=self._end_at,
all_descendants=self._all_descendants,
is_group=self._is_group,
)

def where(self, field_path, op_string, value):
Expand Down Expand Up @@ -283,6 +286,7 @@ def where(self, field_path, op_string, value):
start_at=self._start_at,
end_at=self._end_at,
all_descendants=self._all_descendants,
is_group=self._is_group,
)

@staticmethod
Expand Down Expand Up @@ -335,6 +339,7 @@ def order_by(self, field_path, direction=ASCENDING):
start_at=self._start_at,
end_at=self._end_at,
all_descendants=self._all_descendants,
is_group=self._is_group,
)

def limit(self, count):
Expand All @@ -361,6 +366,7 @@ def limit(self, count):
start_at=self._start_at,
end_at=self._end_at,
all_descendants=self._all_descendants,
is_group=self._is_group,
)

def offset(self, num_to_skip):
Expand Down Expand Up @@ -388,6 +394,7 @@ def offset(self, num_to_skip):
start_at=self._start_at,
end_at=self._end_at,
all_descendants=self._all_descendants,
is_group=self._is_group,
)

def _cursor_helper(self, document_fields, before, start):
Expand Down Expand Up @@ -419,9 +426,9 @@ def _cursor_helper(self, document_fields, before, start):
if isinstance(document_fields, tuple):
document_fields = list(document_fields)
elif isinstance(document_fields, document.DocumentSnapshot):
if (
document_fields.reference._path[:-1] != self._parent._path
and document_fields.reference.parent._path[-1] != self._parent._path[0]
if document_fields.reference._path[:-1] != self._parent._path and not (
self._is_group
and document_fields.reference.parent._path[-1] == self._parent._path[0]
):
raise ValueError(
"Cannot use snapshot from another collection as a cursor."
Expand All @@ -438,6 +445,7 @@ def _cursor_helper(self, document_fields, before, start):
"limit": self._limit,
"offset": self._offset,
"all_descendants": self._all_descendants,
"is_group": self._is_group,
}
if start:
query_kwargs["start_at"] = cursor_pair
Expand Down
2 changes: 0 additions & 2 deletions firestore/tests/unit/v1/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,10 @@ def test_start_after(self):

def test_start_after_snapshot(self):
from google.cloud.firestore_v1.query import Query
import types

client = _make_client()
parent_ref = self._make_one("parents", client=client).document("parent")

doc_fields = {"d": "foo"}
child_col = parent_ref.collection("children")
child_snapshot = child_col.document("child").get()

Expand Down

0 comments on commit 01f4da1

Please sign in to comment.