From 8823a84eb4b7a1ee608efd5dbe0ca730fc3ee149 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Tue, 20 Mar 2018 14:31:02 -0700 Subject: [PATCH 01/10] Added commit_timestamp samples. --- spanner/cloud-client/snippets.py | 148 +++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index 74a52b2d234..2d5d5ac2546 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -460,6 +460,138 @@ def read_only_transaction(instance_id, database_id): # [END spanner_read_only_transaction] +# [START spanner_create_table_with_timestamp_column] +def create_table_with_timestamp(instance_id, database_id): + """Creates a table with a COMMIT_TIMESTAMP column.""" + + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + database = instance.database(database_id) + + operation = database.update_ddl([ + """CREATE TABLE Performances ( + SingerId INT64 NOT NULL, + VenueId INT64 NOT NULL, + EventDate Date, + Revenue INT64, + LastUpdateTime TIMESTAMP NOT NULL OPTIONS(allow_commit_timestamp=true) + ) PRIMARY KEY (SingerId, VenueId, EventDate), + INTERLEAVE IN PARENT Singers ON DELETE CASCADE""" + ]) + + print('Waiting for operation to complete...') + operation.result() + + print('Created Performances table on database {} on instance {}'.format( + database_id, instance_id)) +# [END spanner_create_table_with_timestamp_column] + + +# [START spanner_insert_data_with_timestamp_column] +def insert_data_with_timestamp(instance_id, database_id): + """Inserts data with a COMMIT_TIMESTAMP field into a table. """ + + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + + database = instance.database(database_id) + + with database.batch() as batch: + batch.insert( + table='Performances', + columns=('SingerId', 'VenueId', 'EventDate', 'Revenue', 'LastUpdateTime',), + values=[ + (1, 4, "2017-10-05", 11000, spanner.COMMIT_TIMESTAMP), + (1, 19, "2017-11-02", 15000, spanner.COMMIT_TIMESTAMP), + (2, 42, "2017-12-23", 7000, spanner.COMMIT_TIMESTAMP)]) + + print('Inserted data.') +# [END spanner_create_table_with_timestamp_column] + + +# [START spanner_add_timestamp_column] +def add_timestamp_column(database_id, instance_id): + """Adds a new TIMESTAMP column to the Albums table in the example database.""" + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + + database = instance.database(database_id) + + operation = database.update_ddl( + "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP" + "OPTIONS (allow_commit_timestamp=true)") + + print('Waiting for operation to complete...') + operation.result() + + print('Altered table "Albums" on database {} on instance {}.'.format( + database_id, instance_id)) +# [END spanner_add_timestamp_column] + + +# [START spanner_update_data_with_timestamp_column] +def update_data_with_timestamp(database_id, instance_id): + """Updates Performances tables in the database with the COMMIT_TIMESTAMP column. + + This updates the `MarketingBudget` column which must be created before + running this sample. You can add the column by running the `add_column` + sample or by running this DDL statement against your database: + + ALTER TABLE Performances ADD COLUMN MarketingBudget INT64 + + In addition this update expects the LastUpdateTime column added by + applying this DDL statement against your database: + + ALTER TABLE Performances ADD COLUMN LastUpdateTime TIMESTAMP + OPTIONS (allow_commit_timestamp=true) + """ + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + + database = instance.database(database_id) + + with database.batch() as batch: + batch.update( + table='Albums', + columns=( + 'SingerId', 'AlbumId', 'MarketingBudget'), + values=[ + (1, 4, "2017-10-05", 11000, spanner.COMMIT_TIMESTAMP), + (1, 19, "2017-11-02", 15000, spanner.COMMIT_TIMESTAMP), + (2, 42, "2017-12-23", 7000, spanner.COMMIT_TIMESTAMP)]) + + print('Updated data.') +# [END spanner_update_data_with_timestamp_column] + + +# [START spanner_query_data_with_timestamp_column] +def query_data_with_timestamp(instance_id, database_id): + """Queries sample data from the database using SQL. + + This updates the `LastUpdateTime` column which must be created before + running this sample. You can add the column by running the + `add_timestamp_column` sample or by running this DDL statement + against your database: + + ALTER TABLE Performances ADD COLUMN LastUpdateTime TIMESTAMP + OPTIONS (allow_commit_timestamp=true) + + """ + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + + database = instance.database(database_id) + + with database.snapshot() as snapshot: + results = snapshot.execute_sql( + 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums' + 'FROM Albums ORDER BY LastUpdateTime DESC') + + for row in results: + print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row)) +# [END spanner_query_data_with_timestamp_column] + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, @@ -495,6 +627,12 @@ def read_only_transaction(instance_id, database_id): subparsers.add_parser('add_storing_index', help=add_storing_index.__doc__) subparsers.add_parser( 'read_data_with_storing_index', help=insert_data.__doc__) + subparsers.add_parser('create_table_with_timestamp', help=create_table_with_timestamp.__doc__) + subparsers.add_parser('insert_data_with_timestamp', help=insert_data_with_timestamp.__doc__) + subparsers.add_parser('add_timestamp_column', help=add_timestamp_column.__doc__) + subparsers.add_parser('update_data_with_timestamp', help=update_data_with_timestamp.__doc__) + subparsers.add_parser('query_data_with_timestamp', help=query_data_with_timestamp.__doc__) + args = parser.parse_args() @@ -530,3 +668,13 @@ def read_only_transaction(instance_id, database_id): add_storing_index(args.instance_id, args.database_id) elif args.command == 'read_data_with_storing_index': read_data_with_storing_index(args.instance_id, args.database_id) + elif args.command == 'create_table_with_timestamp': + create_table_with_timestamp(args.instance_id, args.database_id) + elif args.command == 'insert_data_with_timestamp': + insert_data_with_timestamp(args.instance_id, args.database_id) + elif args.command == 'add_timestamp_column': + add_timestamp_column(args.instance_id, args.database_id) + elif args.command == 'update_data_with_timestamp': + update_data_with_timestamp(args.instance_id, args.database_id) + elif args.command == 'query_data_with_timestamp': + query_data_with_timestamp(args.instance_id, args.database_id) From 6362b104687e64849deecef0fa9cbb32465a9be7 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Wed, 21 Mar 2018 08:56:31 -0700 Subject: [PATCH 02/10] Minor tweaks. --- spanner/cloud-client/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index 2d5d5ac2546..a1826d2ff34 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -518,8 +518,8 @@ def add_timestamp_column(database_id, instance_id): database = instance.database(database_id) operation = database.update_ddl( - "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP" - "OPTIONS (allow_commit_timestamp=true)") + 'ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP ' + 'OPTIONS(allow_commit_timestamp=true)') print('Waiting for operation to complete...') operation.result() From 1cbb517bbe60d2629b2310f76ee2d64de8be9ef9 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Wed, 21 Mar 2018 10:54:12 -0700 Subject: [PATCH 03/10] Added tests for new timestamp functions. --- spanner/cloud-client/snippets_test.py | 87 +++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 484c3bad075..7ccd1db0703 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -180,3 +180,90 @@ def _(): out, _ = capsys.readouterr() assert 'Forever Hold Your Peace' in out + + +def test_create_table_with_timestamp(temporary_database, capsys): + snippets.create_table_with_timestamp( + SPANNER_INSTANCE, + temporary_database.database_id) + + out, _ = capsys.readouterr() + + assert 'Performances' in out + + +def test_insert_data_with_timestamp(temporary_database, capsys): + snippets.create_table_with_timestamp( + SPANNER_INSTANCE, + temporary_database.database_id) + snippets.insert_data_with_timestamp( + SPANNER_INSTANCE, + temporary_database.database_id) + + out, _ = capsys.readouterr() + + assert 'Inserted data.' in out + + +@pytest.fixture(scope='module') +def temporary_database_with_timestamps(temporary_database): + snippets.create_table_with_timestamp( + SPANNER_INSTANCE, + temporary_database.database_id) + snippets.insert_data_with_timestamp( + SPANNER_INSTANCE, + temporary_database.database_id) + + yield temporary_database + + +def test_add_timestamp_column(temporary_database, capsys): + snippets.add_timestamp_column( + SPANNER_INSTANCE, + temporary_database_with_timestamps.database_id) + + out, _ = capsys.readouterr() + + assert 'Albums' in out + + +@pytest.fixture(scope='module') +def temporary_database_with_timestamps_column(temporary_database_with_timestamps): + snippets.add_timestamp_column( + SPANNER_INSTANCE, + temporary_database_with_timestamps.database_id) + + yield temporary_database + + +def test_update_data_with_timestamp(temporary_database_with_timestamps, capsys): + snippets.update_data_with_timestamp( + SPANNER_INSTANCE, + temporary_database_with_timestamps_column.database_id) + + out, _ = capsys.readouterr() + + assert 'Updated data.' in out + + +@pytest.fixture(scope='module') +def temporary_database_with_timestamps_data(temporary_database_with_timestamps): + snippets.add_timestamp_column( + SPANNER_INSTANCE, + temporary_database_with_timestamps.database_id) + + yield temporary_database + + +@pytest.mark.slow +def test_query_data_with_timestamp(temporary_database_with_timestamps_data, capsys): + @eventually_consistent.call + def _(): + snippets.query_data_with_timestamp( + SPANNER_INSTANCE, + temporary_database_with_timestamps_data.database_id) + + out, _ = capsys.readouterr() + + assert 'Updated data.' in out + From 70a0a6109b6e920e39a7a68b29b788b6525ba13f Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Wed, 21 Mar 2018 12:43:17 -0700 Subject: [PATCH 04/10] Minor fixes. --- spanner/cloud-client/snippets.py | 26 +++++++++++++------------- spanner/cloud-client/snippets_test.py | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index a1826d2ff34..aa515756031 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -510,16 +510,16 @@ def insert_data_with_timestamp(instance_id, database_id): # [START spanner_add_timestamp_column] -def add_timestamp_column(database_id, instance_id): +def add_timestamp_column(instance_id, database_id): """Adds a new TIMESTAMP column to the Albums table in the example database.""" spanner_client = spanner.Client() instance = spanner_client.instance(instance_id) database = instance.database(database_id) - operation = database.update_ddl( + operation = database.update_ddl([ 'ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP ' - 'OPTIONS(allow_commit_timestamp=true)') + 'OPTIONS(allow_commit_timestamp=true)']) print('Waiting for operation to complete...') operation.result() @@ -530,20 +530,20 @@ def add_timestamp_column(database_id, instance_id): # [START spanner_update_data_with_timestamp_column] -def update_data_with_timestamp(database_id, instance_id): +def update_data_with_timestamp(instance_id, database_id): """Updates Performances tables in the database with the COMMIT_TIMESTAMP column. This updates the `MarketingBudget` column which must be created before running this sample. You can add the column by running the `add_column` sample or by running this DDL statement against your database: - ALTER TABLE Performances ADD COLUMN MarketingBudget INT64 + ALTER TABLE Albums ADD COLUMN MarketingBudget INT64 In addition this update expects the LastUpdateTime column added by applying this DDL statement against your database: - ALTER TABLE Performances ADD COLUMN LastUpdateTime TIMESTAMP - OPTIONS (allow_commit_timestamp=true) + ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP + OPTIONS(allow_commit_timestamp=true) """ spanner_client = spanner.Client() instance = spanner_client.instance(instance_id) @@ -554,11 +554,11 @@ def update_data_with_timestamp(database_id, instance_id): batch.update( table='Albums', columns=( - 'SingerId', 'AlbumId', 'MarketingBudget'), + 'SingerId', 'AlbumId', 'MarketingBudget', 'LastUpdateTime'), values=[ - (1, 4, "2017-10-05", 11000, spanner.COMMIT_TIMESTAMP), - (1, 19, "2017-11-02", 15000, spanner.COMMIT_TIMESTAMP), - (2, 42, "2017-12-23", 7000, spanner.COMMIT_TIMESTAMP)]) + (1, 4, 11000, spanner.COMMIT_TIMESTAMP), + (1, 19, 15000, spanner.COMMIT_TIMESTAMP), + (2, 42, 7000, spanner.COMMIT_TIMESTAMP)]) print('Updated data.') # [END spanner_update_data_with_timestamp_column] @@ -584,8 +584,8 @@ def query_data_with_timestamp(instance_id, database_id): with database.snapshot() as snapshot: results = snapshot.execute_sql( - 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums' - 'FROM Albums ORDER BY LastUpdateTime DESC') + 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums ' + 'ORDER BY LastUpdateTime DESC') for row in results: print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row)) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 7ccd1db0703..53e756c5b3f 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -265,5 +265,5 @@ def _(): out, _ = capsys.readouterr() - assert 'Updated data.' in out + assert 'Go, Go, Go' in out From c38587f5ecccc953923e84ff4708d3759717614f Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Thu, 22 Mar 2018 08:25:56 -0700 Subject: [PATCH 05/10] Fixed region tag. --- spanner/cloud-client/snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index aa515756031..e09ff130f85 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -506,7 +506,7 @@ def insert_data_with_timestamp(instance_id, database_id): (2, 42, "2017-12-23", 7000, spanner.COMMIT_TIMESTAMP)]) print('Inserted data.') -# [END spanner_create_table_with_timestamp_column] +# [END spanner_insert_data_with_timestamp_column] # [START spanner_add_timestamp_column] From 72bf55f13e10002ab39adc2367e9921f6c042c74 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Mon, 26 Mar 2018 19:34:53 -0700 Subject: [PATCH 06/10] Fix style issues. --- spanner/cloud-client/snippets.py | 35 ++++++++++++++++++--------- spanner/cloud-client/snippets_test.py | 13 ++++++---- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/spanner/cloud-client/snippets.py b/spanner/cloud-client/snippets.py index e09ff130f85..e6a6f37cfcd 100644 --- a/spanner/cloud-client/snippets.py +++ b/spanner/cloud-client/snippets.py @@ -473,8 +473,9 @@ def create_table_with_timestamp(instance_id, database_id): SingerId INT64 NOT NULL, VenueId INT64 NOT NULL, EventDate Date, - Revenue INT64, - LastUpdateTime TIMESTAMP NOT NULL OPTIONS(allow_commit_timestamp=true) + Revenue INT64, + LastUpdateTime TIMESTAMP NOT NULL + OPTIONS(allow_commit_timestamp=true) ) PRIMARY KEY (SingerId, VenueId, EventDate), INTERLEAVE IN PARENT Singers ON DELETE CASCADE""" ]) @@ -499,7 +500,9 @@ def insert_data_with_timestamp(instance_id, database_id): with database.batch() as batch: batch.insert( table='Performances', - columns=('SingerId', 'VenueId', 'EventDate', 'Revenue', 'LastUpdateTime',), + columns=( + 'SingerId', 'VenueId', 'EventDate', + 'Revenue', 'LastUpdateTime',), values=[ (1, 4, "2017-10-05", 11000, spanner.COMMIT_TIMESTAMP), (1, 19, "2017-11-02", 15000, spanner.COMMIT_TIMESTAMP), @@ -511,7 +514,9 @@ def insert_data_with_timestamp(instance_id, database_id): # [START spanner_add_timestamp_column] def add_timestamp_column(instance_id, database_id): - """Adds a new TIMESTAMP column to the Albums table in the example database.""" + """ + Adds a new TIMESTAMP column to the Albums table in the example database. + """ spanner_client = spanner.Client() instance = spanner_client.instance(instance_id) @@ -531,7 +536,8 @@ def add_timestamp_column(instance_id, database_id): # [START spanner_update_data_with_timestamp_column] def update_data_with_timestamp(instance_id, database_id): - """Updates Performances tables in the database with the COMMIT_TIMESTAMP column. + """Updates Performances tables in the database with the COMMIT_TIMESTAMP + column. This updates the `MarketingBudget` column which must be created before running this sample. You can add the column by running the `add_column` @@ -592,7 +598,7 @@ def query_data_with_timestamp(instance_id, database_id): # [END spanner_query_data_with_timestamp_column] -if __name__ == '__main__': +if __name__ == '__main__': # noqa: C901 parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) @@ -627,12 +633,17 @@ def query_data_with_timestamp(instance_id, database_id): subparsers.add_parser('add_storing_index', help=add_storing_index.__doc__) subparsers.add_parser( 'read_data_with_storing_index', help=insert_data.__doc__) - subparsers.add_parser('create_table_with_timestamp', help=create_table_with_timestamp.__doc__) - subparsers.add_parser('insert_data_with_timestamp', help=insert_data_with_timestamp.__doc__) - subparsers.add_parser('add_timestamp_column', help=add_timestamp_column.__doc__) - subparsers.add_parser('update_data_with_timestamp', help=update_data_with_timestamp.__doc__) - subparsers.add_parser('query_data_with_timestamp', help=query_data_with_timestamp.__doc__) - + subparsers.add_parser( + 'create_table_with_timestamp', + help=create_table_with_timestamp.__doc__) + subparsers.add_parser( + 'insert_data_with_timestamp', help=insert_data_with_timestamp.__doc__) + subparsers.add_parser( + 'add_timestamp_column', help=add_timestamp_column.__doc__) + subparsers.add_parser( + 'update_data_with_timestamp', help=update_data_with_timestamp.__doc__) + subparsers.add_parser( + 'query_data_with_timestamp', help=query_data_with_timestamp.__doc__) args = parser.parse_args() diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 53e756c5b3f..ff56b349e6e 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -228,7 +228,8 @@ def test_add_timestamp_column(temporary_database, capsys): @pytest.fixture(scope='module') -def temporary_database_with_timestamps_column(temporary_database_with_timestamps): +def temporary_database_with_timestamps_column( + temporary_database_with_timestamps): snippets.add_timestamp_column( SPANNER_INSTANCE, temporary_database_with_timestamps.database_id) @@ -236,7 +237,8 @@ def temporary_database_with_timestamps_column(temporary_database_with_timestamps yield temporary_database -def test_update_data_with_timestamp(temporary_database_with_timestamps, capsys): +def test_update_data_with_timestamp( + temporary_database_with_timestamps_column, capsys): snippets.update_data_with_timestamp( SPANNER_INSTANCE, temporary_database_with_timestamps_column.database_id) @@ -247,7 +249,8 @@ def test_update_data_with_timestamp(temporary_database_with_timestamps, capsys): @pytest.fixture(scope='module') -def temporary_database_with_timestamps_data(temporary_database_with_timestamps): +def temporary_database_with_timestamps_data( + temporary_database_with_timestamps): snippets.add_timestamp_column( SPANNER_INSTANCE, temporary_database_with_timestamps.database_id) @@ -256,7 +259,8 @@ def temporary_database_with_timestamps_data(temporary_database_with_timestamps): @pytest.mark.slow -def test_query_data_with_timestamp(temporary_database_with_timestamps_data, capsys): +def test_query_data_with_timestamp( + temporary_database_with_timestamps_data, capsys): @eventually_consistent.call def _(): snippets.query_data_with_timestamp( @@ -266,4 +270,3 @@ def _(): out, _ = capsys.readouterr() assert 'Go, Go, Go' in out - From d164f8268f8aa605aee0c4b9f7946037ed278ec8 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Mon, 26 Mar 2018 20:26:38 -0700 Subject: [PATCH 07/10] Fix tests number 1. --- spanner/cloud-client/snippets_test.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index ff56b349e6e..4c0a3516d40 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -206,7 +206,7 @@ def test_insert_data_with_timestamp(temporary_database, capsys): @pytest.fixture(scope='module') -def temporary_database_with_timestamps(temporary_database): +def temp_database_with_timestamps(temporary_database): snippets.create_table_with_timestamp( SPANNER_INSTANCE, temporary_database.database_id) @@ -217,10 +217,10 @@ def temporary_database_with_timestamps(temporary_database): yield temporary_database -def test_add_timestamp_column(temporary_database, capsys): +def test_add_timestamp_column(temp_database_with_timestamps, capsys): snippets.add_timestamp_column( SPANNER_INSTANCE, - temporary_database_with_timestamps.database_id) + temp_database_with_timestamps.database_id) out, _ = capsys.readouterr() @@ -228,20 +228,19 @@ def test_add_timestamp_column(temporary_database, capsys): @pytest.fixture(scope='module') -def temporary_database_with_timestamps_column( - temporary_database_with_timestamps): +def temp_database_with_timestamps_column(temp_database_with_timestamps): snippets.add_timestamp_column( SPANNER_INSTANCE, - temporary_database_with_timestamps.database_id) + temp_database_with_timestamps.database_id) yield temporary_database def test_update_data_with_timestamp( - temporary_database_with_timestamps_column, capsys): + temp_database_with_timestamps_column, capsys): snippets.update_data_with_timestamp( SPANNER_INSTANCE, - temporary_database_with_timestamps_column.database_id) + temp_database_with_timestamps_column.database_id) out, _ = capsys.readouterr() @@ -249,23 +248,21 @@ def test_update_data_with_timestamp( @pytest.fixture(scope='module') -def temporary_database_with_timestamps_data( - temporary_database_with_timestamps): - snippets.add_timestamp_column( +def temp_database_with_timestamps_data(temp_database_with_timestamps_column): + snippets.update_data_with_timestamp( SPANNER_INSTANCE, - temporary_database_with_timestamps.database_id) + temp_database_with_timestamps_column.database_id) yield temporary_database @pytest.mark.slow -def test_query_data_with_timestamp( - temporary_database_with_timestamps_data, capsys): +def test_query_data_with_timestamp(temp_database_with_timestamps_data, capsys): @eventually_consistent.call def _(): snippets.query_data_with_timestamp( SPANNER_INSTANCE, - temporary_database_with_timestamps_data.database_id) + temp_database_with_timestamps_data.database_id) out, _ = capsys.readouterr() From c89dd148f7471331e99554ffa8beb5edddd767dd Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Tue, 27 Mar 2018 11:27:34 -0700 Subject: [PATCH 08/10] Remove fixture scope. --- spanner/cloud-client/snippets_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 4c0a3516d40..74ea06cedda 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -46,7 +46,7 @@ def test_create_database(spanner_instance): database.drop() -@pytest.fixture(scope='module') +@pytest.fixture() def temporary_database(spanner_instance): database_id = unique_database_id() snippets.create_database(SPANNER_INSTANCE, database_id) @@ -84,7 +84,7 @@ def test_read_stale_data(temporary_database, capsys): assert 'Total Junk' not in out -@pytest.fixture(scope='module') +@pytest.fixture() def temporary_database_with_column(temporary_database): snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id) yield temporary_database @@ -105,7 +105,7 @@ def test_query_data_with_new_column(temporary_database_with_column, capsys): assert 'MarketingBudget' in out -@pytest.fixture(scope='module') +@pytest.fixture() def temporary_database_with_indexes(temporary_database_with_column): snippets.add_index( SPANNER_INSTANCE, @@ -205,7 +205,7 @@ def test_insert_data_with_timestamp(temporary_database, capsys): assert 'Inserted data.' in out -@pytest.fixture(scope='module') +@pytest.fixture() def temp_database_with_timestamps(temporary_database): snippets.create_table_with_timestamp( SPANNER_INSTANCE, @@ -227,7 +227,7 @@ def test_add_timestamp_column(temp_database_with_timestamps, capsys): assert 'Albums' in out -@pytest.fixture(scope='module') +@pytest.fixture() def temp_database_with_timestamps_column(temp_database_with_timestamps): snippets.add_timestamp_column( SPANNER_INSTANCE, @@ -247,7 +247,7 @@ def test_update_data_with_timestamp( assert 'Updated data.' in out -@pytest.fixture(scope='module') +@pytest.fixture() def temp_database_with_timestamps_data(temp_database_with_timestamps_column): snippets.update_data_with_timestamp( SPANNER_INSTANCE, From a16d2ec18d45e7529b7ed5199c7babda855b9019 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Tue, 27 Mar 2018 12:13:35 -0700 Subject: [PATCH 09/10] Update requirements.txt. --- spanner/cloud-client/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spanner/cloud-client/requirements.txt b/spanner/cloud-client/requirements.txt index 06551559ee2..26b6dd27cf4 100644 --- a/spanner/cloud-client/requirements.txt +++ b/spanner/cloud-client/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-spanner==1.2.0 +google-cloud-spanner==1.3.0 futures==3.2.0; python_version < "3" From 8c94229a127a49f817478cca8c53440c7c48c499 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Tue, 27 Mar 2018 12:34:46 -0700 Subject: [PATCH 10/10] Replace module scope and use consecutive tests. --- spanner/cloud-client/snippets_test.py | 53 +++++---------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/spanner/cloud-client/snippets_test.py b/spanner/cloud-client/snippets_test.py index 74ea06cedda..bdedd70fe65 100644 --- a/spanner/cloud-client/snippets_test.py +++ b/spanner/cloud-client/snippets_test.py @@ -46,7 +46,7 @@ def test_create_database(spanner_instance): database.drop() -@pytest.fixture() +@pytest.fixture(scope='module') def temporary_database(spanner_instance): database_id = unique_database_id() snippets.create_database(SPANNER_INSTANCE, database_id) @@ -84,7 +84,7 @@ def test_read_stale_data(temporary_database, capsys): assert 'Total Junk' not in out -@pytest.fixture() +@pytest.fixture(scope='module') def temporary_database_with_column(temporary_database): snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id) yield temporary_database @@ -105,7 +105,7 @@ def test_query_data_with_new_column(temporary_database_with_column, capsys): assert 'MarketingBudget' in out -@pytest.fixture() +@pytest.fixture(scope='module') def temporary_database_with_indexes(temporary_database_with_column): snippets.add_index( SPANNER_INSTANCE, @@ -193,9 +193,6 @@ def test_create_table_with_timestamp(temporary_database, capsys): def test_insert_data_with_timestamp(temporary_database, capsys): - snippets.create_table_with_timestamp( - SPANNER_INSTANCE, - temporary_database.database_id) snippets.insert_data_with_timestamp( SPANNER_INSTANCE, temporary_database.database_id) @@ -205,64 +202,34 @@ def test_insert_data_with_timestamp(temporary_database, capsys): assert 'Inserted data.' in out -@pytest.fixture() -def temp_database_with_timestamps(temporary_database): - snippets.create_table_with_timestamp( - SPANNER_INSTANCE, - temporary_database.database_id) - snippets.insert_data_with_timestamp( - SPANNER_INSTANCE, - temporary_database.database_id) - - yield temporary_database - - -def test_add_timestamp_column(temp_database_with_timestamps, capsys): +def test_add_timestamp_column(temporary_database, capsys): snippets.add_timestamp_column( SPANNER_INSTANCE, - temp_database_with_timestamps.database_id) + temporary_database.database_id) out, _ = capsys.readouterr() assert 'Albums' in out -@pytest.fixture() -def temp_database_with_timestamps_column(temp_database_with_timestamps): - snippets.add_timestamp_column( - SPANNER_INSTANCE, - temp_database_with_timestamps.database_id) - - yield temporary_database - - -def test_update_data_with_timestamp( - temp_database_with_timestamps_column, capsys): +@pytest.mark.slow +def test_update_data_with_timestamp(temporary_database, capsys): snippets.update_data_with_timestamp( SPANNER_INSTANCE, - temp_database_with_timestamps_column.database_id) + temporary_database.database_id) out, _ = capsys.readouterr() assert 'Updated data.' in out -@pytest.fixture() -def temp_database_with_timestamps_data(temp_database_with_timestamps_column): - snippets.update_data_with_timestamp( - SPANNER_INSTANCE, - temp_database_with_timestamps_column.database_id) - - yield temporary_database - - @pytest.mark.slow -def test_query_data_with_timestamp(temp_database_with_timestamps_data, capsys): +def test_query_data_with_timestamp(temporary_database, capsys): @eventually_consistent.call def _(): snippets.query_data_with_timestamp( SPANNER_INSTANCE, - temp_database_with_timestamps_data.database_id) + temporary_database.database_id) out, _ = capsys.readouterr()