Content-Length: 429704 | pFad | http://github.com/chemelnucfin/google-cloud-python/commit/9f701bcb6f1b57cbfce5e81759aaf220f8b8e36e

3C Spanner: Fix database not found error, Closes #4071 · chemelnucfin/google-cloud-python@9f701bc · GitHub
Skip to content

Commit 9f701bc

Browse files
committed
Spanner: Fix database not found error, Closes googleapis#4071
1 parent a62931a commit 9f701bc

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

spanner/google/cloud/spanner_v1/database.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import threading
1919

2020
import google.auth.credentials
21+
from google.api_core import exceptions
2122
from google.gax.errors import GaxError
2223
from google.gax.grpc import exc_to_code
2324
from google.cloud.spanner_v1.gapic.spanner_client import SpannerClient
@@ -208,14 +209,7 @@ def create(self):
208209
options=options,
209210
)
210211
except GaxError as exc:
211-
if exc_to_code(exc.cause) == StatusCode.ALREADY_EXISTS:
212-
raise Conflict(self.name)
213-
elif exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
214-
raise NotFound('Instance not found: {name}'.format(
215-
name=self._instance.name,
216-
))
217-
raise
218-
212+
raise exceptions.from_grpc_error(exc.cause)
219213
return future
220214

221215
def exists(self):

spanner/tests/system/test_system.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
from google.cloud._helpers import UTC
3838
from google.cloud.exceptions import GrpcRendezvous
39+
from google.cloud.exceptions import NotFound
3940
from google.cloud.spanner_v1._helpers import TimestampWithNanoseconds
4041
from google.cloud.spanner import Client
4142
from google.cloud.spanner import KeyRange
@@ -282,6 +283,35 @@ def test_create_database(self):
282283
for database in Config.INSTANCE.list_databases()]
283284
self.assertIn(temp_db_id, database_ids)
284285

286+
def test_table_not_found(self):
287+
temp_db_id = 'temp_db' + unique_resource_id('_')
288+
289+
correct_table = 'MyTable'
290+
incorrect_table = 'NotMyTable'
291+
self.assertNotEqual(correct_table, incorrect_table)
292+
293+
create_table = (
294+
'CREATE TABLE {} (\n'
295+
' Id STRING(36) NOT NULL,\n'
296+
' Field1 STRING(36) NOT NULL\n'
297+
') PRIMARY KEY (Id)').format(correct_table)
298+
index = 'CREATE INDEX IDX ON {} (Field1)'.format(incorrect_table)
299+
300+
temp_db = Config.INSTANCE.database(
301+
temp_db_id,
302+
ddl_statements=[
303+
create_table,
304+
index,
305+
],
306+
)
307+
try:
308+
temp_db.create()
309+
except NotFound as exc:
310+
self.assertEqual(exc.args[0],
311+
'Table not found: {0}'.format(incorrect_table))
312+
except:
313+
self.fail()
314+
285315
def test_update_database_ddl(self):
286316
pool = BurstyPool()
287317
temp_db_id = 'temp_db' + unique_resource_id('_')

spanner/tests/unit/test_database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test___ne__(self):
284284
self.assertNotEqual(database1, database2)
285285

286286
def test_create_grpc_error(self):
287-
from google.gax.errors import GaxError
287+
from google.api_core.exceptions import GoogleAPICallError
288288

289289
client = _Client()
290290
api = client.database_admin_api = _FauxDatabaseAdminAPI(
@@ -293,7 +293,7 @@ def test_create_grpc_error(self):
293293
pool = _Pool()
294294
database = self._make_one(self.DATABASE_ID, instance, pool=pool)
295295

296-
with self.assertRaises(GaxError):
296+
with self.assertRaises(GoogleAPICallError):
297297
database.create()
298298

299299
(parent, create_statement, extra_statements,

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/chemelnucfin/google-cloud-python/commit/9f701bcb6f1b57cbfce5e81759aaf220f8b8e36e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy