Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: autocommit sample * remove unused imports * Remove samples/samples init * No package-relative imports in samples * fix check errors, rename test file * move ResultsChecksum() to test * use connection.Cursor in patch * Apply suggestions from code review Co-authored-by: Chris Kleinknecht <libc@google.com> * Update samples/samples/autocommit_test.py Co-authored-by: Chris Kleinknecht <libc@google.com> * lint fix Co-authored-by: Chris Kleinknecht <libc@google.com> Co-authored-by: Alex <7764119+AVaksman@users.noreply.github.com> Co-authored-by: Ilya Gurov <ilya.faer@mail.ru> Co-authored-by: larkee <31196561+larkee@users.noreply.github.com>
- Loading branch information
1 parent
9b8d472
commit 4ef793c
Showing
2 changed files
with
126 additions
and
0 deletions.
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,64 @@ | ||
# 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] | ||
|
||
connection = connect(instance_id, database_id) | ||
connection.autocommit = True | ||
print("Autocommit mode is enabled.") | ||
|
||
cursor = connection.cursor() | ||
|
||
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,62 @@ | ||
# 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 | ||
from google.cloud.spanner_dbapi import connect | ||
import mock | ||
import pytest | ||
|
||
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" | ||
|
||
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): | ||
connection = connect(INSTANCE_ID, DATABASE_ID) | ||
cursor = connection.cursor() | ||
|
||
with mock.patch( | ||
"google.cloud.spanner_dbapi.connection.Cursor", return_value=cursor, | ||
): | ||
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 |