@@ -169,6 +169,14 @@ def test__session_checkout(self, mock_database):
169169 connection ._session_checkout ()
170170 self .assertEqual (connection ._session , "db_session" )
171171
172+ def test__session_checkout_database_error (self ):
173+ from google .cloud .spanner_dbapi import Connection
174+
175+ connection = Connection (INSTANCE )
176+
177+ with pytest .raises (ValueError ):
178+ connection ._session_checkout ()
179+
172180 @mock .patch ("google.cloud.spanner_v1.database.Database" )
173181 def test__release_session (self , mock_database ):
174182 from google .cloud .spanner_dbapi import Connection
@@ -182,6 +190,13 @@ def test__release_session(self, mock_database):
182190 pool .put .assert_called_once_with ("session" )
183191 self .assertIsNone (connection ._session )
184192
193+ def test__release_session_database_error (self ):
194+ from google .cloud .spanner_dbapi import Connection
195+
196+ connection = Connection (INSTANCE )
197+ with pytest .raises (ValueError ):
198+ connection ._release_session ()
199+
185200 def test_transaction_checkout (self ):
186201 from google .cloud .spanner_dbapi import Connection
187202
@@ -294,6 +309,14 @@ def test_commit(self, mock_warn):
294309 AUTOCOMMIT_MODE_WARNING , UserWarning , stacklevel = 2
295310 )
296311
312+ def test_commit_database_error (self ):
313+ from google .cloud .spanner_dbapi import Connection
314+
315+ connection = Connection (INSTANCE )
316+
317+ with pytest .raises (ValueError ):
318+ connection .commit ()
319+
297320 @mock .patch .object (warnings , "warn" )
298321 def test_rollback (self , mock_warn ):
299322 from google .cloud .spanner_dbapi import Connection
@@ -347,6 +370,13 @@ def test_run_prior_DDL_statements(self, mock_database):
347370 with self .assertRaises (InterfaceError ):
348371 connection .run_prior_DDL_statements ()
349372
373+ def test_run_prior_DDL_statements_database_error (self ):
374+ from google .cloud .spanner_dbapi import Connection
375+
376+ connection = Connection (INSTANCE )
377+ with pytest .raises (ValueError ):
378+ connection .run_prior_DDL_statements ()
379+
350380 def test_as_context_manager (self ):
351381 connection = self ._make_connection ()
352382 with connection as conn :
@@ -766,6 +796,14 @@ def test_validate_error(self):
766796
767797 snapshot_obj .execute_sql .assert_called_once_with ("SELECT 1" )
768798
799+ def test_validate_database_error (self ):
800+ from google .cloud .spanner_dbapi import Connection
801+
802+ connection = Connection (INSTANCE )
803+
804+ with pytest .raises (ValueError ):
805+ connection .validate ()
806+
769807 def test_validate_closed (self ):
770808 from google .cloud .spanner_dbapi .exceptions import InterfaceError
771809
@@ -916,16 +954,14 @@ def test_request_priority(self):
916954 sql , params , param_types = param_types , request_options = None
917955 )
918956
919- @mock .patch ("google.cloud.spanner_v1.Client" )
920- def test_custom_client_connection (self , mock_client ):
957+ def test_custom_client_connection (self ):
921958 from google .cloud .spanner_dbapi import connect
922959
923960 client = _Client ()
924961 connection = connect ("test-instance" , "test-database" , client = client )
925962 self .assertTrue (connection .instance ._client == client )
926963
927- @mock .patch ("google.cloud.spanner_v1.Client" )
928- def test_invalid_custom_client_connection (self , mock_client ):
964+ def test_invalid_custom_client_connection (self ):
929965 from google .cloud .spanner_dbapi import connect
930966
931967 client = _Client ()
@@ -937,6 +973,12 @@ def test_invalid_custom_client_connection(self, mock_client):
937973 client = client ,
938974 )
939975
976+ def test_connection_wo_database (self ):
977+ from google .cloud .spanner_dbapi import connect
978+
979+ connection = connect ("test-instance" )
980+ self .assertTrue (connection .database is None )
981+
940982
941983def exit_ctx_func (self , exc_type , exc_value , traceback ):
942984 """Context __exit__ method mock."""
0 commit comments