Skip to content

Commit e3837f9

Browse files
committed
Add samples for IN, NOT_IN, and != operators.
Add additiona tests for IN, NOT_IN, and != samples.
1 parent b9e9cec commit e3837f9

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

samples/requirements-test.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
backoff==1.11.1; python_version < "3.7"
2+
backoff==2.0.0; python_version >= "3.7"
3+
pytest==7.0.1
4+
flaky==3.7.0

samples/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-datastore==2.4.0

samples/snippets.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Copyright 2022 Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import argparse
15+
from collections import defaultdict
16+
import datetime
17+
from pprint import pprint
18+
19+
import google.cloud.exceptions
20+
from google.cloud import datastore # noqa: I100
21+
22+
def _preamble():
23+
# [START datastore_size_coloration_query]
24+
from google.cloud import datastore
25+
26+
# For help authenticating your client, visit
27+
# https://cloud.google.com/docs/authentication/getting-started
28+
client = datastore.Client()
29+
30+
# [END datastore_size_coloration_query]
31+
assert client is not None
32+
33+
def in_query(client):
34+
# [START datastore_in_query]
35+
query = client.query(kind="Task")
36+
query.add_filter("tag", "IN", ["learn", "study"])
37+
# [END datastore_in_query]
38+
39+
return list(query.fetch())
40+
41+
def not_equals_query(client):
42+
# [START datastore_not_equals_query]
43+
query = client.query(kind="Task")
44+
query.add_filter("category", "!=", "work")
45+
# [END datastore_not_equals_query]
46+
47+
return list(query.fetch())
48+
49+
def not_equals_query(client):
50+
# [START datastore_not_in_query]
51+
query = client.query(kind="Task")
52+
query.add_filter("category", "NOT_IN", ["work", "chores", "school"])
53+
# [END datastore_not_in_query]
54+
55+
return list(query.fetch())
56+
57+
58+
def eq_query_sorted(client):
59+
# [START datastore_eq_query_sorted]
60+
query = client.query(kind="Task")
61+
query.add_filter("tag", "=", "learn")
62+
# Ordering on equality filters are ignored
63+
query.order = ['tag']
64+
# [END datastore_eq_query_sorted]
65+
66+
return list(query.fetch())
67+
68+
def in_query_sorted(client):
69+
# [START datastore_in_query_sorted]
70+
query = client.query(kind="Task")
71+
query.add_filter("tag", "IN", ["learn", "study"])
72+
# Ordering on equality filters are ignored
73+
query.order = ['tag']
74+
# [END datastore_in_query_sorted]
75+
76+
return list(query.fetch())
77+
78+
def main(project_id):
79+
client = datastore.Client(project_id)
80+
81+
for name, function in globals().items():
82+
if name in ("main", "_preamble", "defaultdict") or not callable(function):
83+
continue
84+
85+
print(name)
86+
pprint(function(client))
87+
print("\n-----------------\n")
88+
89+
90+
if __name__ == "__main__":
91+
parser = argparse.ArgumentParser(
92+
description="Demonstrates datastore API operations."
93+
)
94+
parser.add_argument("project_id", help="Your cloud project ID.")
95+
96+
args = parser.parse_args()
97+
98+
main(args.project_id)

samples/snippets_test.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Copyright 2022 Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import os
15+
16+
import backoff
17+
from google.cloud import datastore
18+
import pytest
19+
20+
import snippets
21+
22+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
24+
25+
class CleanupClient(datastore.Client):
26+
def __init__(self, *args, **kwargs):
27+
super(CleanupClient, self).__init__(*args, **kwargs)
28+
self.entities_to_delete = []
29+
self.keys_to_delete = []
30+
31+
def cleanup(self):
32+
with self.batch():
33+
self.delete_multi(
34+
list(set([x.key for x in self.entities_to_delete if x]))
35+
+ list(set(self.keys_to_delete))
36+
)
37+
38+
39+
@pytest.yield_fixture
40+
def client():
41+
client = CleanupClient(PROJECT)
42+
yield client
43+
client.cleanup()
44+
45+
46+
@pytest.mark.flaky
47+
class TestDatastoreSnippets:
48+
# These tests mostly just test the absence of exceptions.
49+
50+
@backoff.on_exception(backoff.expo, AssertionError, max_time=240)
51+
def test_in_query(self, client):
52+
tasks = snippets.in_query(client)
53+
client.entities_to_delete.extend(tasks)
54+
assert tasks
55+
56+
@backoff.on_exception(backoff.expo, AssertionError, max_time=240)
57+
def test_not_equals_query(self, client):
58+
tasks = snippets.not_equals_query(client)
59+
client.entities_to_delete.extend(tasks)
60+
assert tasks
61+
62+
@backoff.on_exception(backoff.expo, AssertionError, max_time=240)
63+
def test_not_in_query(self, client):
64+
tasks = snippets.not_in_query(client)
65+
client.entities_to_delete.extend(tasks)
66+
assert tasks
67+
68+
@backoff.on_exception(backoff.expo, AssertionError, max_time=240)
69+
def test_eq_query_sorted(self, client):
70+
tasks = snippets.eq_query_sorted(client)
71+
client.entities_to_delete.extend(tasks)
72+
assert tasks
73+
74+
@backoff.on_exception(backoff.expo, AssertionError, max_time=240)
75+
def test_in_query_sorted(self, client):
76+
tasks = snippets.in_query_sorted(client)
77+
client.entities_to_delete.extend(tasks)
78+
assert tasks

0 commit comments

Comments
 (0)