Skip to content

Commit

Permalink
Add spanner stale data sample [(googleapis#1107)](GoogleCloudPlatform…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored Sep 12, 2017
1 parent a8ce453 commit 211387d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ def read_data(instance_id, database_id):
print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row))


def read_stale_data(instance_id, database_id):
"""Reads sample data from the database. The data is exactly 10 seconds
stale."""
import datetime

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
staleness = datetime.timedelta(seconds=10)

with database.snapshot(exact_staleness=staleness) as snapshot:
keyset = spanner.KeySet(all_=True)
results = snapshot.read(
table='Albums',
columns=('SingerId', 'AlbumId', 'AlbumTitle',),
keyset=keyset)

for row in results:
print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row))


def query_data_with_new_column(instance_id, database_id):
"""Queries sample data from the database using SQL.
Expand Down Expand Up @@ -424,6 +445,7 @@ def read_only_transaction(instance_id, database_id):
subparsers.add_parser('insert_data', help=insert_data.__doc__)
subparsers.add_parser('query_data', help=query_data.__doc__)
subparsers.add_parser('read_data', help=read_data.__doc__)
subparsers.add_parser('read_stale_data', help=read_stale_data.__doc__)
subparsers.add_parser('add_column', help=add_column.__doc__)
subparsers.add_parser('update_data', help=update_data.__doc__)
subparsers.add_parser(
Expand Down Expand Up @@ -454,6 +476,8 @@ def read_only_transaction(instance_id, database_id):
query_data(args.instance_id, args.database_id)
elif args.command == 'read_data':
read_data(args.instance_id, args.database_id)
elif args.command == 'read_stale_data':
read_stale_data(args.instance_id, args.database_id)
elif args.command == 'add_column':
add_column(args.instance_id, args.database_id)
elif args.command == 'update_data':
Expand Down
11 changes: 11 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def test_read_data(temporary_database, capsys):
assert 'Total Junk' in out


def test_read_stale_data(temporary_database, capsys):
snippets.read_stale_data(SPANNER_INSTANCE, temporary_database.database_id)

out, _ = capsys.readouterr()

# It shouldn't be in the output because it was *just* inserted by the
# temporary database fixture and this sample reads 10 seconds into the
# past.
assert 'Total Junk' not in out


@pytest.fixture(scope='module')
def temporary_database_with_column(temporary_database):
snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id)
Expand Down

0 comments on commit 211387d

Please sign in to comment.