Skip to content

Commit c4d9c24

Browse files
authored
feat(datastore): adds query filter OR (GoogleCloudPlatform#9315)
* feat(datastore): adds query filter OR * missed import * moar missed import * lint * lint * per reviewer * lint * per reviewer * per reviewer * linter
1 parent 1bef4ce commit c4d9c24

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"""
16+
Builds a union (OR) query filter.
17+
18+
See https://cloud.google.com/python/docs/reference/datastore/latest before running code.
19+
"""
20+
21+
22+
# [START datastore_query_filter_or]
23+
from google.cloud import datastore
24+
from google.cloud.datastore import query
25+
26+
27+
def query_filter_or(project_id: str) -> None:
28+
"""Builds a union of two queries (OR) filter.
29+
30+
Arguments:
31+
project_id: your Google Cloud Project ID
32+
"""
33+
client = datastore.Client(project=project_id)
34+
35+
or_query = client.query(kind="Task")
36+
or_filter = query.Or(
37+
[
38+
query.PropertyFilter("description", "=", "Buy milk"),
39+
query.PropertyFilter("description", "=", "Feed cats"),
40+
]
41+
)
42+
43+
or_query.add_filter(filter=or_filter)
44+
45+
results = or_query.fetch()
46+
for result in results:
47+
print(result["description"])
48+
# [END datastore_query_filter_or]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
from google.cloud import datastore
18+
import pytest
19+
20+
from query_filter_or import query_filter_or
21+
22+
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
24+
25+
@pytest.fixture()
26+
def entities():
27+
client = datastore.Client(project=PROJECT_ID)
28+
29+
task_key = client.key("Task")
30+
task1 = datastore.Entity(key=task_key)
31+
task1["description"] = "Buy milk"
32+
client.put(task1)
33+
34+
task_key2 = client.key("Task")
35+
task2 = datastore.Entity(key=task_key2)
36+
task2["description"] = "Feed cats"
37+
client.put(task2)
38+
39+
yield entities
40+
41+
client.delete(task1)
42+
client.delete(task2)
43+
44+
45+
def test_query_filter_or(capsys, entities):
46+
query_filter_or(project_id=PROJECT_ID)
47+
out, _ = capsys.readouterr()
48+
assert "Feed cats" in out
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-datastore==2.7.1
1+
google-cloud-datastore==2.15.0

0 commit comments

Comments
 (0)