Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(samples): Add an example of using read_time in queries and get() #342

Merged
merged 6 commits into from Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion samples/snippets/snippets.py
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

import argparse
from datetime import datetime, timedelta, timezone
from pprint import pprint

from google.cloud import datastore # noqa: I100
Expand Down Expand Up @@ -56,11 +57,31 @@ def not_in_query(client):
return list(query.fetch())


def query_with_readtime(client):
# [START datastore_snapshot_read]
# Create a read time of 120 seconds in the past
read_time = datetime.now(timezone.utc) - timedelta(seconds=120)

# Fetch an entity at time read_time
task_key = client.key('Task', 'sampletask')
entity = client.get(task_key, read_time=read_time)

# Query Task entities at time read_time
query = client.query(kind="Task")
tasks = query.fetch(read_time=read_time, limit=10)
# [END datastore_snapshot_read]

results = list(tasks)
results.append(entity)

return results


def main(project_id):
client = datastore.Client(project_id)

for name, function in globals().items():
if name in ("main", "_preamble", "defaultdict") or not callable(function):
if name in ("main", "_preamble", "defaultdict", "datetime", "timezone", "timedelta") or not callable(function):
continue

print(name)
Expand Down
6 changes: 6 additions & 0 deletions samples/snippets/snippets_test.py
Expand Up @@ -66,3 +66,9 @@ def test_not_in_query(self, client):
tasks = snippets.not_in_query(client)
client.entities_to_delete.extend(tasks)
assert tasks is not None

@backoff.on_exception(backoff.expo, AssertionError, max_time=240)
def test_query_with_readtime(self, client):
tasks = snippets.query_with_readtime(client)
client.entities_to_delete.extend(tasks)
assert tasks is not None