Skip to content

Commit b95ed9a

Browse files
authored
feat: allow 'Collection.where(__name__, in, [hello, world])' (#501)
Closes #421. Supersedes #496.
1 parent be39005 commit b95ed9a

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

packages/google-cloud-firestore/google/cloud/firestore_v1/base_collection.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,29 @@ def where(self, field_path: str, op_string: str, value) -> BaseQuery:
236236
field_path (str): A field path (``.``-delimited list of
237237
field names) for the field to filter on.
238238
op_string (str): A comparison operation in the form of a string.
239-
Acceptable values are ``<``, ``<=``, ``==``, ``>=``
240-
and ``>``.
239+
Acceptable values are ``<``, ``<=``, ``==``, ``>=``, ``>``,
240+
and ``in``.
241241
value (Any): The value to compare the field against in the filter.
242242
If ``value`` is :data:`None` or a NaN, then ``==`` is the only
243-
allowed operation.
243+
allowed operation. If ``op_string`` is ``in``, ``value``
244+
must be a sequence of values.
244245
245246
Returns:
246247
:class:`~google.cloud.firestore_v1.query.Query`:
247248
A filtered query.
248249
"""
250+
if field_path == "__name__" and op_string == "in":
251+
wrapped_names = []
252+
253+
for name in value:
254+
255+
if isinstance(name, str):
256+
name = self.document(name)
257+
258+
wrapped_names.append(name)
259+
260+
value = wrapped_names
261+
249262
query = self._query()
250263
return query.where(field_path, op_string, value)
251264

packages/google-cloud-firestore/tests/unit/v1/test_base_collection.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,45 @@ def test_basecollectionreference_where(mock_query):
217217
assert query == mock_query.where.return_value
218218

219219

220+
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
221+
def test_basecollectionreference_where_w___name___w_value_as_list_of_str(mock_query):
222+
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
223+
224+
with mock.patch.object(BaseCollectionReference, "_query") as _query:
225+
_query.return_value = mock_query
226+
227+
client = _make_client()
228+
collection = _make_base_collection_reference("collection", client=client)
229+
field_path = "__name__"
230+
op_string = "in"
231+
names = ["hello", "world"]
232+
233+
query = collection.where(field_path, op_string, names)
234+
235+
expected_refs = [collection.document(name) for name in names]
236+
mock_query.where.assert_called_once_with(field_path, op_string, expected_refs)
237+
assert query == mock_query.where.return_value
238+
239+
240+
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
241+
def test_basecollectionreference_where_w___name___w_value_as_list_of_docref(mock_query):
242+
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
243+
244+
with mock.patch.object(BaseCollectionReference, "_query") as _query:
245+
_query.return_value = mock_query
246+
247+
client = _make_client()
248+
collection = _make_base_collection_reference("collection", client=client)
249+
field_path = "__name__"
250+
op_string = "in"
251+
refs = [collection.document("hello"), collection.document("world")]
252+
253+
query = collection.where(field_path, op_string, refs)
254+
255+
mock_query.where.assert_called_once_with(field_path, op_string, refs)
256+
assert query == mock_query.where.return_value
257+
258+
220259
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
221260
def test_basecollectionreference_order_by(mock_query):
222261
from google.cloud.firestore_v1.base_query import BaseQuery

0 commit comments

Comments
 (0)