Skip to content

samples: add tagging samples #605

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,73 @@ def create_client_with_query_options(instance_id, database_id):
# [END spanner_create_client_with_query_options]


def set_transaction_tag(instance_id, database_id):
"""Executes a transaction with a transaction tag."""
# [START spanner_set_transaction_tag]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

def update_venues(transaction):
# Sets the request tag to "app=concert,env=dev,action=update".
# This request tag will only be set on this request.
transaction.execute_update(
"UPDATE Venues SET Capacity = CAST(Capacity/4 AS INT64) WHERE OutdoorVenue = false",
request_options={"request_tag": "app=concert,env=dev,action=update"}
)
print("Venue capacities updated.")

# Sets the request tag to "app=concert,env=dev,action=insert".
# This request tag will only be set on this request.
transaction.execute_update(
"INSERT INTO Venues (VenueId, VenueName, Capacity, OutdoorVenue, LastUpdateTime) "
"VALUES (@venueId, @venueName, @capacity, @outdoorVenue, PENDING_COMMIT_TIMESTAMP())",
params={
"venueId": 81,
"venueName": "Venue 81",
"capacity": 1440,
"outdoorVenue": True
},
param_types={
"venueId": param_types.INT64,
"venueName": param_types.STRING,
"capacity": param_types.INT64,
"outdoorVenue": param_types.BOOL
},
request_options={"request_tag": "app=concert,env=dev,action=insert"}
)
print("New venue inserted.")

database.run_in_transaction(
update_venues, transaction_tag="app=concert,env=dev"
)

# [END spanner_set_transaction_tag]


def set_request_tag(instance_id, database_id):
"""Executes a snapshot read with a request tag."""
# [START spanner_set_request_tag]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

with database.snapshot() as snapshot:
results = snapshot.execute_sql(
"SELECT SingerId, AlbumId, AlbumTitle FROM Albums",
request_options={"request_tag": "app=concert,env=dev,action=select"}
)

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

# [END spanner_set_request_tag]


if __name__ == "__main__": # noqa: C901
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand Down
15 changes: 15 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,18 @@ def test_create_client_with_query_options(capsys, instance_id, sample_database):
assert "VenueId: 4, VenueName: Venue 4, LastUpdateTime:" in out
assert "VenueId: 19, VenueName: Venue 19, LastUpdateTime:" in out
assert "VenueId: 42, VenueName: Venue 42, LastUpdateTime:" in out


@pytest.mark.dependency(depends=["insert_datatypes_data"])
def test_set_transaction_tag(capsys, instance_id, sample_database):
snippets.set_transaction_tag(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "Venue capacities updated." in out
assert "New venue inserted." in out


@pytest.mark.dependency(depends=["insert_data"])
def test_set_request_tag(capsys, instance_id, sample_database):
snippets.set_request_tag(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk" in out