Skip to content
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 #552

Closed
wants to merge 14 commits into from
69 changes: 69 additions & 0 deletions samples/samples/autocommit.py
@@ -0,0 +1,69 @@
# 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
c24t marked this conversation as resolved.
Show resolved Hide resolved


def enable_autocommit_mode(instance_id, database_id):
"""Enables autocommit mode."""
# [START enable_autocommit_mode]
c24t marked this conversation as resolved.
Show resolved Hide resolved
AlisskaPie marked this conversation as resolved.
Show resolved Hide resolved
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(
u"SingerId: {}, AlbumId: {}, AlbumTitle: {}".format(*cursor.fetchone())
)

connection.close()
# [END enable_autocommit_mode]
AlisskaPie marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
c24t marked this conversation as resolved.
Show resolved Hide resolved
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(
"Command {} did not match expected commands.".format(args.command)
AlisskaPie marked this conversation as resolved.
Show resolved Hide resolved
)
53 changes: 53 additions & 0 deletions samples/samples/test_autocommit.py
@@ -0,0 +1,53 @@
# 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.api_core.exceptions import DeadlineExceeded
from google.cloud import spanner
import pytest

import autocommit
c24t marked this conversation as resolved.
Show resolved Hide resolved


def unique_instance_id():
"""Creates a unique id for the database."""
return "test-instance-{}".format(uuid.uuid4().hex[:10])
AlisskaPie marked this conversation as resolved.
Show resolved Hide resolved


def unique_database_id():
"""Creates a unique id for the database."""
return "test-db-{}".format(uuid.uuid4().hex[:10])
AlisskaPie marked this conversation as resolved.
Show resolved Hide resolved


INSTANCE_ID = unique_instance_id()
DATABASE_ID = unique_database_id()


@pytest.fixture(scope="module")
def spanner_instance():
spanner_client = spanner.Client()
instance = spanner_client.instance(INSTANCE_ID)
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):
c24t marked this conversation as resolved.
Show resolved Hide resolved
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