From f5420310a8eb7ca39d6cea18cc9f7351fa8b2bc9 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Fri, 8 Dec 2017 11:33:30 -0800 Subject: [PATCH] Spanner: Fix database not found error, Closes #4071 --- spanner/google/cloud/spanner_v1/database.py | 11 +++----- spanner/tests/system/test_system.py | 28 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/spanner/google/cloud/spanner_v1/database.py b/spanner/google/cloud/spanner_v1/database.py index a72e250119174..dfdeda1d8a298 100644 --- a/spanner/google/cloud/spanner_v1/database.py +++ b/spanner/google/cloud/spanner_v1/database.py @@ -18,6 +18,7 @@ import threading import google.auth.credentials +from google.api_core import exceptions from google.gax.errors import GaxError from google.gax.grpc import exc_to_code from google.cloud.spanner_v1.gapic.spanner_client import SpannerClient @@ -208,14 +209,8 @@ def create(self): options=options, ) except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.ALREADY_EXISTS: - raise Conflict(self.name) - elif exc_to_code(exc.cause) == StatusCode.NOT_FOUND: - raise NotFound('Instance not found: {name}'.format( - name=self._instance.name, - )) - raise - + exception = exceptions.from_grpc_error(exc.cause) + raise exception return future def exists(self): diff --git a/spanner/tests/system/test_system.py b/spanner/tests/system/test_system.py index c8dc5df70baee..2833bba7ad338 100644 --- a/spanner/tests/system/test_system.py +++ b/spanner/tests/system/test_system.py @@ -36,6 +36,7 @@ from google.cloud._helpers import UTC from google.cloud.exceptions import GrpcRendezvous +from google.cloud.exceptions import NotFound from google.cloud.spanner_v1._helpers import TimestampWithNanoseconds from google.cloud.spanner import Client from google.cloud.spanner import KeyRange @@ -282,6 +283,33 @@ def test_create_database(self): for database in Config.INSTANCE.list_databases()] self.assertIn(temp_db_id, database_ids) + def test_table_not_found(self): + temp_db_id = 'temp_db' + unique_resource_id('_') + + table_name1 = 'MyTable' + table_name2 = 'NotMyTable' + self.assertNotEqual(table_name1, table_name2) + + create_table = ( + 'CREATE TABLE {} (\n' + ' Id STRING(36) NOT NULL,\n' + ' Field1 STRING(36) NOT NULL\n' + ') PRIMARY KEY (Id)') + create_table = create_table.format(table_name1) + create_index = 'CREATE INDEX IDX ON {} (Field1)'.format(table_name2) + + temp_db = Config.INSTANCE.database( + temp_db_id, + ddl_statements=[ + create_table, + create_index, + ], + ) + with self.assertRaisesRegexp(NotFound, + '404 Table not found: ' + + table_name2): + temp_db.create() + def test_update_database_ddl(self): pool = BurstyPool() temp_db_id = 'temp_db' + unique_resource_id('_')