From 9f701bcb6f1b57cbfce5e81759aaf220f8b8e36e 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 | 10 ++----- spanner/tests/system/test_system.py | 30 +++++++++++++++++++++ spanner/tests/unit/test_database.py | 4 +-- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/spanner/google/cloud/spanner_v1/database.py b/spanner/google/cloud/spanner_v1/database.py index a72e250119174..fe57e4e981cd3 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,7 @@ 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 - + raise exceptions.from_grpc_error(exc.cause) return future def exists(self): diff --git a/spanner/tests/system/test_system.py b/spanner/tests/system/test_system.py index c8dc5df70baee..0e5127220cd55 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,35 @@ 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('_') + + correct_table = 'MyTable' + incorrect_table = 'NotMyTable' + self.assertNotEqual(correct_table, incorrect_table) + + create_table = ( + 'CREATE TABLE {} (\n' + ' Id STRING(36) NOT NULL,\n' + ' Field1 STRING(36) NOT NULL\n' + ') PRIMARY KEY (Id)').format(correct_table) + index = 'CREATE INDEX IDX ON {} (Field1)'.format(incorrect_table) + + temp_db = Config.INSTANCE.database( + temp_db_id, + ddl_statements=[ + create_table, + index, + ], + ) + try: + temp_db.create() + except NotFound as exc: + self.assertEqual(exc.args[0], + 'Table not found: {0}'.format(incorrect_table)) + except: + self.fail() + def test_update_database_ddl(self): pool = BurstyPool() temp_db_id = 'temp_db' + unique_resource_id('_') diff --git a/spanner/tests/unit/test_database.py b/spanner/tests/unit/test_database.py index 316ca307574ba..97c9fe3b0e85f 100644 --- a/spanner/tests/unit/test_database.py +++ b/spanner/tests/unit/test_database.py @@ -284,7 +284,7 @@ def test___ne__(self): self.assertNotEqual(database1, database2) def test_create_grpc_error(self): - from google.gax.errors import GaxError + from google.api_core.exceptions import GoogleAPICallError client = _Client() api = client.database_admin_api = _FauxDatabaseAdminAPI( @@ -293,7 +293,7 @@ def test_create_grpc_error(self): pool = _Pool() database = self._make_one(self.DATABASE_ID, instance, pool=pool) - with self.assertRaises(GaxError): + with self.assertRaises(GoogleAPICallError): database.create() (parent, create_statement, extra_statements,