Skip to content

Commit f22f11e

Browse files
committed
support tuples in pipelin_source.collection
1 parent 63b83b8 commit f22f11e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

google/cloud/firestore_v1/pipeline_source.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Generic, TypeVar, TYPE_CHECKING
1717
from google.cloud.firestore_v1 import pipeline_stages as stages
1818
from google.cloud.firestore_v1.base_pipeline import _BasePipeline
19+
from google.cloud.firestore_v1._helpers import DOCUMENT_PATH_DELIMITER
1920

2021
if TYPE_CHECKING:
2122
from google.cloud.firestore_v1.client import Client
@@ -44,15 +45,19 @@ def __init__(self, client: Client | AsyncClient):
4445
def _create_pipeline(self, source_stage):
4546
return self.client._pipeline_cls(self.client, source_stage)
4647

47-
def collection(self, path: str) -> PipelineType:
48+
def collection(self, path: str | tuple[str]) -> PipelineType:
4849
"""
4950
Creates a new Pipeline that operates on a specified Firestore collection.
5051
5152
Args:
52-
path: The path to the Firestore collection (e.g., "users")
53+
path: The path to the Firestore collection (e.g., "users"). Can either be:
54+
* A single ``/``-delimited path to a collection
55+
* A tuple of collection path segment
5356
Returns:
5457
a new pipeline instance targeting the specified collection
5558
"""
59+
if isinstance(path, tuple):
60+
path = DOCUMENT_PATH_DELIMITER.join(path)
5661
return self._create_pipeline(stages.Collection(path))
5762

5863
def collection_group(self, collection_id: str) -> PipelineType:

tests/unit/v1/test_base_query.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,8 @@ def test__query_pipeline_no_decendants(in_path, out_path):
20052005
from google.cloud.firestore_v1 import pipeline_stages
20062006

20072007
client = make_client()
2008-
query = client.collection(in_path)
2008+
collection = client.collection(in_path)
2009+
query = collection._query()
20092010
pipeline = query.pipeline()
20102011

20112012
assert len(pipeline.stages) == 1
@@ -2113,7 +2114,7 @@ def test__query_pipeline_order_sorts():
21132114
assert sort_stage.orders[1].order_dir == expr.Ordering.Direction.DESCENDING
21142115

21152116

2116-
def test__query_pipeline_cursor():
2117+
def test__query_pipeline_unsupported():
21172118
client = make_client()
21182119
query_start = client.collection("my_col").start_at({"field_a": "value"})
21192120
with pytest.raises(NotImplementedError, match="cursors"):

tests/unit/v1/test_pipeline_source.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ def test_collection(self):
4444
assert isinstance(first_stage, stages.Collection)
4545
assert first_stage.path == "/path"
4646

47+
def test_collection_w_tuple(self):
48+
instance = self._make_client().pipeline()
49+
ppl = instance.collection(("a", "b", "c"))
50+
assert isinstance(ppl, self._expected_pipeline_type)
51+
assert len(ppl.stages) == 1
52+
first_stage = ppl.stages[0]
53+
assert isinstance(first_stage, stages.Collection)
54+
assert first_stage.path == "/a/b/c"
55+
56+
4757
def test_collection_group(self):
4858
instance = self._make_client().pipeline()
4959
ppl = instance.collection_group("id")

0 commit comments

Comments
 (0)