Skip to content

Commit 0b46c37

Browse files
telpirionnicain
andauthored
feat(firestore): adds OR filter query (GoogleCloudPlatform#9769)
* feat(firestore): adds OR filter query * feat(firestore): more test * lint * fix * lint * update * fixed tests * region tag mismatch fix * lint * switched bulkwriter to transaction * moar test fix * test fix * test fix * test fix * chore: refactor test fixture logic to explicitly use all fixtures, and add retry fixture components with backoff (GoogleCloudPlatform#9812) * chore: update for linting --------- Co-authored-by: nicain <[email protected]>
1 parent d29a860 commit 0b46c37

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START firestore_query_filter_or]
16+
from google.cloud import firestore
17+
from google.cloud.firestore_v1.base_query import FieldFilter, Or
18+
19+
20+
def query_or_composite_filter(project_id: str) -> None:
21+
# Instantiate the Firestore client
22+
client = firestore.Client(project=project_id)
23+
col_ref = client.collection("users")
24+
25+
filter_1 = FieldFilter(u"birthYear", u"==", 1906)
26+
filter_2 = FieldFilter(u"birthYear", u"==", 1912)
27+
28+
# Create the union filter of the two filters (queries)
29+
or_filter = Or(filters=[filter_1, filter_2])
30+
31+
# Execute the query
32+
docs = col_ref.where(filter=or_filter).stream()
33+
34+
print("Documents found:")
35+
for doc in docs:
36+
print(f"ID: {doc.id}")
37+
# [END firestore_query_filter_or]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import backoff
18+
from google.cloud import firestore
19+
import pytest
20+
21+
from query_filter_or import query_or_composite_filter
22+
23+
os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['FIRESTORE_PROJECT']
24+
PROJECT_ID = os.environ['FIRESTORE_PROJECT']
25+
26+
27+
@pytest.fixture(scope="module")
28+
def data():
29+
return {
30+
u'aturing': {u'birthYear': 1912},
31+
u'cbabbage': {u'birthYear': 1791},
32+
u'ghopper': {u'birthYear': 1906},
33+
u'alovelace': {u'birthYear': 1815},
34+
}
35+
36+
37+
@backoff.on_exception(backoff.expo, Exception, max_tries=3)
38+
def create_document(key, val, collection):
39+
collection.document(key).set(val)
40+
41+
42+
def create_document_collection(data, collection):
43+
for key, val in data.items():
44+
create_document(key, val, collection)
45+
46+
47+
@backoff.on_exception(backoff.expo, Exception, max_tries=3)
48+
def delete_document(key, collection):
49+
collection.document(key).delete()
50+
51+
52+
def delete_document_collection(data, collection):
53+
for key in data:
54+
delete_document(key, collection)
55+
56+
57+
@backoff.on_exception(backoff.expo, Exception, max_tries=3)
58+
def test_query_or_composite_filter(capsys, data):
59+
client = firestore.Client(project=PROJECT_ID)
60+
collection = client.collection('users')
61+
62+
try:
63+
create_document_collection(data, collection)
64+
query_or_composite_filter(PROJECT_ID)
65+
finally:
66+
delete_document_collection(data, collection)
67+
68+
out, _ = capsys.readouterr()
69+
assert 'aturing' in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
backoff==2.2.1
12
pytest==7.0.1
23
flaky==3.7.0

0 commit comments

Comments
 (0)