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
feat: autocommit sample #172
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
beb5bdf
feat: autocommit sample
AlisskaPie adc05b0
remove unused imports
AlisskaPie 1659802
Merge branch 'master' into autocommit_samples
c24t 1de5c29
Remove samples/samples init
c24t 85885f5
No package-relative imports in samples
c24t f8a6c80
fix check errors, rename test file
AlisskaPie 1fb7019
move ResultsChecksum() to test
AlisskaPie 09c5246
use connection.Cursor in patch
AlisskaPie f8d4d4c
Merge branch 'master' into autocommit_samples
AVaksman 0e1e66c
Merge branch 'master' into autocommit_samples
c24t cab9f08
Apply suggestions from code review
f1353a2
Update samples/samples/autocommit_test.py
4e5465b
lint fix
12bdf95
Merge branch 'master' into autocommit_samples
c24t 447d96f
Merge branch 'master' into autocommit_samples
c24t 5fb4326
Merge branch 'master' into autocommit_samples
larkee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
import argparse | ||
|
||
from google.cloud.spanner_dbapi import connect | ||
|
||
|
||
def enable_autocommit_mode(instance_id, database_id): | ||
"""Enables autocommit mode.""" | ||
# [START spanner_enable_autocommit_mode] | ||
from google.cloud.spanner_dbapi.checksum import ResultsChecksum | ||
|
||
connection = connect(instance_id, database_id) | ||
connection.autocommit = True | ||
print("Autocommit mode is enabled.") | ||
|
||
cursor = connection.cursor() | ||
cursor._checksum = ResultsChecksum() | ||
|
||
cursor.execute( | ||
"""CREATE TABLE Singers ( | ||
SingerId INT64 NOT NULL, | ||
FirstName STRING(1024), | ||
LastName STRING(1024), | ||
SingerInfo BYTES(MAX) | ||
) PRIMARY KEY (SingerId)""" | ||
) | ||
|
||
cursor.execute( | ||
"""INSERT INTO Singers (SingerId, FirstName, LastName) VALUES | ||
(12, 'Melissa', 'Garcia'), | ||
(13, 'Russell', 'Morales'), | ||
(14, 'Jacqueline', 'Long'), | ||
(15, 'Dylan', 'Shaw')""" | ||
) | ||
|
||
cursor.execute("""SELECT * FROM Singers WHERE SingerId = 13""") | ||
|
||
print("SingerId: {}, AlbumId: {}, AlbumTitle: {}".format(*cursor.fetchone())) | ||
|
||
connection.close() | ||
# [END spanner_enable_autocommit_mode] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
parser.add_argument("instance_id", help="Your Cloud Spanner instance ID.") | ||
parser.add_argument( | ||
"--database-id", | ||
help="Your Cloud Spanner database ID.", | ||
default="example_db", | ||
) | ||
subparsers = parser.add_subparsers(dest="command") | ||
subparsers.add_parser("enable_autocommit_mode", help=enable_autocommit_mode.__doc__) | ||
args = parser.parse_args() | ||
if args.command == "enable_autocommit_mode": | ||
enable_autocommit_mode(args.instance_id, args.database_id) | ||
else: | ||
print(f"Command {args.command} did not match expected commands.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
import uuid | ||
|
||
from google.cloud import spanner | ||
import pytest | ||
IlyaFaer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import autocommit | ||
|
||
|
||
def unique_instance_id(): | ||
"""Creates a unique id for the database.""" | ||
return f"test-instance-{uuid.uuid4().hex[:10]}" | ||
|
||
|
||
def unique_database_id(): | ||
"""Creates a unique id for the database.""" | ||
return f"test-db-{uuid.uuid4().hex[:10]}" | ||
|
||
|
||
INSTANCE_ID = unique_instance_id() | ||
DATABASE_ID = unique_database_id() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def spanner_instance(): | ||
spanner_client = spanner.Client() | ||
config_name = f"{spanner_client.project_name}/instanceConfigs/regional-us-central1" | ||
IlyaFaer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
instance = spanner_client.instance(INSTANCE_ID, config_name) | ||
op = instance.create() | ||
op.result(120) # block until completion | ||
yield instance | ||
instance.delete() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def database(spanner_instance): | ||
"""Creates a temporary database that is removed after testing.""" | ||
db = spanner_instance.database(DATABASE_ID) | ||
db.create() | ||
yield db | ||
db.drop() | ||
|
||
|
||
def test_enable_autocommit_mode(capsys, database): | ||
autocommit.enable_autocommit_mode(INSTANCE_ID, DATABASE_ID) | ||
out, _ = capsys.readouterr() | ||
assert "Autocommit mode is enabled." in out | ||
assert "SingerId: 13, AlbumId: Russell, AlbumTitle: Morales" in out |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does the user need to initialise a private variable? Can this not be initialised as part of the cursor factory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review! And sorry for the delayed answer.
I've made an update and placed this part in the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not quite what I meant. The sample now looks as I would expect but I'm unclear as to why you still need to set
cursor._checksum
in the test. Is there a reason that_checksum
defaults toNone
? Could it be updated toResultsChecksum()
instead?