From b95bd5354610eff92a7dce909e679191d4e1f0b5 Mon Sep 17 00:00:00 2001 From: larkee Date: Tue, 9 Mar 2021 00:54:30 +1100 Subject: [PATCH 1/2] test: deflake autocommit sample test --- samples/samples/autocommit_test.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/samples/samples/autocommit_test.py b/samples/samples/autocommit_test.py index a98744968a..77aa2eea32 100644 --- a/samples/samples/autocommit_test.py +++ b/samples/samples/autocommit_test.py @@ -8,8 +8,6 @@ from google.api_core.exceptions import Aborted from google.cloud import spanner -from google.cloud.spanner_dbapi import connect -import mock import pytest from test_utils.retry import RetryErrors @@ -30,6 +28,10 @@ def unique_database_id(): DATABASE_ID = unique_database_id() +def drop_table(transaction, table_name): + transaction.execute_update("DROP TABLE {}".format(table_name)) + + @pytest.fixture(scope="module") def spanner_instance(): spanner_client = spanner.Client() @@ -53,13 +55,12 @@ def database(spanner_instance): @RetryErrors(exception=Aborted, max_tries=2) 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 + # Delete table if it exists for retry attempts. + table = database.table('Singers') + if table.exists(): + database.run_in_transaction(drop_table, 'Singers') + + 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 From 781ede0f5c1a3b6996cdc2993f0ae2c627c4ee45 Mon Sep 17 00:00:00 2001 From: larkee Date: Tue, 9 Mar 2021 09:26:07 +1100 Subject: [PATCH 2/2] test: fix DDL call --- samples/samples/autocommit_test.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/samples/samples/autocommit_test.py b/samples/samples/autocommit_test.py index 77aa2eea32..c9631516fa 100644 --- a/samples/samples/autocommit_test.py +++ b/samples/samples/autocommit_test.py @@ -28,10 +28,6 @@ def unique_database_id(): DATABASE_ID = unique_database_id() -def drop_table(transaction, table_name): - transaction.execute_update("DROP TABLE {}".format(table_name)) - - @pytest.fixture(scope="module") def spanner_instance(): spanner_client = spanner.Client() @@ -58,7 +54,8 @@ def test_enable_autocommit_mode(capsys, database): # Delete table if it exists for retry attempts. table = database.table('Singers') if table.exists(): - database.run_in_transaction(drop_table, 'Singers') + op = database.update_ddl(["DROP TABLE Singers"]) + op.result() autocommit.enable_autocommit_mode(INSTANCE_ID, DATABASE_ID) out, _ = capsys.readouterr()