Skip to content

Commit

Permalink
Update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny1612 committed Nov 2, 2023
1 parent a32dd63 commit 96e6e15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,15 @@ def insert_data(instance_id, database_id):
# [END spanner_insert_data]


# [START spanner_batch_write]
# [START spanner_batch_write_at_least_once]
def batch_write(instance_id, database_id):
"""Inserts sample data into the given database via BatchWrite API.
The database and table must already exist and can be created using
`create_database`.
"""
from google.rpc.code_pb2 import OK

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
Expand All @@ -429,7 +431,7 @@ def batch_write(instance_id, database_id):
table="Singers",
columns=("SingerId", "FirstName", "LastName"),
values=[
(17, "Marc", "Richards"),
(17, "Marc", ""),
(18, "Catalina", "Smith"),
],
)
Expand All @@ -443,12 +445,21 @@ def batch_write(instance_id, database_id):
)

for response in groups.batch_write():
print(response)

print("Inserted data.")
if response.status.code == OK:
print(
"Mutation group indexes {} have been applied with commit timestamp {}".format(
response.indexes, response.commit_timestamp
)
)
else:
print(
"Mutation group indexes {} could not be applied with error {}".format(
response.indexes, response.status
)
)


# [END spanner_batch_write]
# [END spanner_batch_write_at_least_once]


# [START spanner_delete_data]
Expand Down
2 changes: 1 addition & 1 deletion samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_insert_data(capsys, instance_id, sample_database):
def test_batch_write(capsys, instance_id, sample_database):
snippets.batch_write(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "Inserted data" in out
assert "could not be applied with error" not in out


@pytest.mark.dependency(depends=["insert_data"])
Expand Down

0 comments on commit 96e6e15

Please sign in to comment.