From 689bf87f202fdb5ad57a3fcd359b6f781c3a2227 Mon Sep 17 00:00:00 2001 From: Astha Mohta <35952883+asthamohta@users.noreply.github.com> Date: Tue, 25 Apr 2023 13:31:05 +0530 Subject: [PATCH] Revert "feat: enable instance-level connection (#931)" This reverts commit d6963e2142d880e94c6f3e9eb27ed1ac310bd1d0. --- google/cloud/spanner_dbapi/connection.py | 22 +++------ google/cloud/spanner_dbapi/cursor.py | 8 ---- tests/unit/spanner_dbapi/test_connection.py | 50 ++------------------- tests/unit/spanner_dbapi/test_cursor.py | 31 ------------- 4 files changed, 9 insertions(+), 102 deletions(-) diff --git a/google/cloud/spanner_dbapi/connection.py b/google/cloud/spanner_dbapi/connection.py index a50e48804b..d251e0f62a 100644 --- a/google/cloud/spanner_dbapi/connection.py +++ b/google/cloud/spanner_dbapi/connection.py @@ -83,7 +83,7 @@ class Connection: should end a that a new one should be started when the next statement is executed. """ - def __init__(self, instance, database=None, read_only=False): + def __init__(self, instance, database, read_only=False): self._instance = instance self._database = database self._ddl_statements = [] @@ -242,8 +242,6 @@ def _session_checkout(self): :rtype: :class:`google.cloud.spanner_v1.session.Session` :returns: Cloud Spanner session object ready to use. """ - if self.database is None: - raise ValueError("Database needs to be passed for this operation") if not self._session: self._session = self.database._pool.get() @@ -254,8 +252,6 @@ def _release_session(self): The session will be returned into the sessions pool. """ - if self.database is None: - raise ValueError("Database needs to be passed for this operation") self.database._pool.put(self._session) self._session = None @@ -372,7 +368,7 @@ def close(self): if self.inside_transaction: self._transaction.rollback() - if self._own_pool and self.database: + if self._own_pool: self.database._pool.clear() self.is_closed = True @@ -382,8 +378,6 @@ def commit(self): This method is non-operational in autocommit mode. """ - if self.database is None: - raise ValueError("Database needs to be passed for this operation") self._snapshot = None if self._autocommit: @@ -426,8 +420,6 @@ def cursor(self): @check_not_closed def run_prior_DDL_statements(self): - if self.database is None: - raise ValueError("Database needs to be passed for this operation") if self._ddl_statements: ddl_statements = self._ddl_statements self._ddl_statements = [] @@ -482,8 +474,6 @@ def validate(self): :raises: :class:`google.cloud.exceptions.NotFound`: if the linked instance or database doesn't exist. """ - if self.database is None: - raise ValueError("Database needs to be passed for this operation") with self.database.snapshot() as snapshot: result = list(snapshot.execute_sql("SELECT 1")) if result != [[1]]: @@ -502,7 +492,7 @@ def __exit__(self, etype, value, traceback): def connect( instance_id, - database_id=None, + database_id, project=None, credentials=None, pool=None, @@ -515,7 +505,7 @@ def connect( :param instance_id: The ID of the instance to connect to. :type database_id: str - :param database_id: (Optional) The ID of the database to connect to. + :param database_id: The ID of the database to connect to. :type project: str :param project: (Optional) The ID of the project which owns the @@ -567,9 +557,7 @@ def connect( raise ValueError("project in url does not match client object project") instance = client.instance(instance_id) - conn = Connection( - instance, instance.database(database_id, pool=pool) if database_id else None - ) + conn = Connection(instance, instance.database(database_id, pool=pool)) if pool is not None: conn._own_pool = False diff --git a/google/cloud/spanner_dbapi/cursor.py b/google/cloud/spanner_dbapi/cursor.py index 91bccedd4c..ac3888f35d 100644 --- a/google/cloud/spanner_dbapi/cursor.py +++ b/google/cloud/spanner_dbapi/cursor.py @@ -228,8 +228,6 @@ def execute(self, sql, args=None): :type args: list :param args: Additional parameters to supplement the SQL query. """ - if self.connection.database is None: - raise ValueError("Database needs to be passed for this operation") self._itr = None self._result_set = None self._row_count = _UNSET_COUNT @@ -303,8 +301,6 @@ def executemany(self, operation, seq_of_params): :param seq_of_params: Sequence of additional parameters to run the query with. """ - if self.connection.database is None: - raise ValueError("Database needs to be passed for this operation") self._itr = None self._result_set = None self._row_count = _UNSET_COUNT @@ -448,8 +444,6 @@ def _handle_DQL_with_snapshot(self, snapshot, sql, params): self._row_count = _UNSET_COUNT def _handle_DQL(self, sql, params): - if self.connection.database is None: - raise ValueError("Database needs to be passed for this operation") sql, params = parse_utils.sql_pyformat_args_to_spanner(sql, params) if self.connection.read_only and not self.connection.autocommit: # initiate or use the existing multi-use snapshot @@ -490,8 +484,6 @@ def list_tables(self): def run_sql_in_snapshot(self, sql, params=None, param_types=None): # Some SQL e.g. for INFORMATION_SCHEMA cannot be run in read-write transactions # hence this method exists to circumvent that limit. - if self.connection.database is None: - raise ValueError("Database needs to be passed for this operation") self.connection.run_prior_DDL_statements() with self.connection.database.snapshot() as snapshot: diff --git a/tests/unit/spanner_dbapi/test_connection.py b/tests/unit/spanner_dbapi/test_connection.py index 7a0ac9e687..b077c1feba 100644 --- a/tests/unit/spanner_dbapi/test_connection.py +++ b/tests/unit/spanner_dbapi/test_connection.py @@ -169,14 +169,6 @@ def test__session_checkout(self, mock_database): connection._session_checkout() self.assertEqual(connection._session, "db_session") - def test__session_checkout_database_error(self): - from google.cloud.spanner_dbapi import Connection - - connection = Connection(INSTANCE) - - with pytest.raises(ValueError): - connection._session_checkout() - @mock.patch("google.cloud.spanner_v1.database.Database") def test__release_session(self, mock_database): from google.cloud.spanner_dbapi import Connection @@ -190,13 +182,6 @@ def test__release_session(self, mock_database): pool.put.assert_called_once_with("session") self.assertIsNone(connection._session) - def test__release_session_database_error(self): - from google.cloud.spanner_dbapi import Connection - - connection = Connection(INSTANCE) - with pytest.raises(ValueError): - connection._release_session() - def test_transaction_checkout(self): from google.cloud.spanner_dbapi import Connection @@ -309,14 +294,6 @@ def test_commit(self, mock_warn): AUTOCOMMIT_MODE_WARNING, UserWarning, stacklevel=2 ) - def test_commit_database_error(self): - from google.cloud.spanner_dbapi import Connection - - connection = Connection(INSTANCE) - - with pytest.raises(ValueError): - connection.commit() - @mock.patch.object(warnings, "warn") def test_rollback(self, mock_warn): from google.cloud.spanner_dbapi import Connection @@ -370,13 +347,6 @@ def test_run_prior_DDL_statements(self, mock_database): with self.assertRaises(InterfaceError): connection.run_prior_DDL_statements() - def test_run_prior_DDL_statements_database_error(self): - from google.cloud.spanner_dbapi import Connection - - connection = Connection(INSTANCE) - with pytest.raises(ValueError): - connection.run_prior_DDL_statements() - def test_as_context_manager(self): connection = self._make_connection() with connection as conn: @@ -796,14 +766,6 @@ def test_validate_error(self): snapshot_obj.execute_sql.assert_called_once_with("SELECT 1") - def test_validate_database_error(self): - from google.cloud.spanner_dbapi import Connection - - connection = Connection(INSTANCE) - - with pytest.raises(ValueError): - connection.validate() - def test_validate_closed(self): from google.cloud.spanner_dbapi.exceptions import InterfaceError @@ -954,14 +916,16 @@ def test_request_priority(self): sql, params, param_types=param_types, request_options=None ) - def test_custom_client_connection(self): + @mock.patch("google.cloud.spanner_v1.Client") + def test_custom_client_connection(self, mock_client): from google.cloud.spanner_dbapi import connect client = _Client() connection = connect("test-instance", "test-database", client=client) self.assertTrue(connection.instance._client == client) - def test_invalid_custom_client_connection(self): + @mock.patch("google.cloud.spanner_v1.Client") + def test_invalid_custom_client_connection(self, mock_client): from google.cloud.spanner_dbapi import connect client = _Client() @@ -973,12 +937,6 @@ def test_invalid_custom_client_connection(self): client=client, ) - def test_connection_wo_database(self): - from google.cloud.spanner_dbapi import connect - - connection = connect("test-instance") - self.assertTrue(connection.database is None) - def exit_ctx_func(self, exc_type, exc_value, traceback): """Context __exit__ method mock.""" diff --git a/tests/unit/spanner_dbapi/test_cursor.py b/tests/unit/spanner_dbapi/test_cursor.py index f744fc769f..79ed898355 100644 --- a/tests/unit/spanner_dbapi/test_cursor.py +++ b/tests/unit/spanner_dbapi/test_cursor.py @@ -163,13 +163,6 @@ def test_execute_attribute_error(self): with self.assertRaises(AttributeError): cursor.execute(sql="SELECT 1") - def test_execute_database_error(self): - connection = self._make_connection(self.INSTANCE) - cursor = self._make_one(connection) - - with self.assertRaises(ValueError): - cursor.execute(sql="SELECT 1") - def test_execute_autocommit_off(self): from google.cloud.spanner_dbapi.utils import PeekIterator @@ -614,16 +607,6 @@ def test_executemany_insert_batch_aborted(self): ) self.assertIsInstance(connection._statements[0][1], ResultsChecksum) - @mock.patch("google.cloud.spanner_v1.Client") - def test_executemany_database_error(self, mock_client): - from google.cloud.spanner_dbapi import connect - - connection = connect("test-instance") - cursor = connection.cursor() - - with self.assertRaises(ValueError): - cursor.executemany("""SELECT * FROM table1 WHERE "col1" = @a1""", ()) - @unittest.skipIf( sys.version_info[0] < 3, "Python 2 has an outdated iterator definition" ) @@ -771,13 +754,6 @@ def test_handle_dql_priority(self): sql, None, None, request_options=RequestOptions(priority=1) ) - def test_handle_dql_database_error(self): - connection = self._make_connection(self.INSTANCE) - cursor = self._make_one(connection) - - with self.assertRaises(ValueError): - cursor._handle_DQL("sql", params=None) - def test_context(self): connection = self._make_connection(self.INSTANCE, self.DATABASE) cursor = self._make_one(connection) @@ -838,13 +814,6 @@ def test_run_sql_in_snapshot(self): mock_snapshot.execute_sql.return_value = results self.assertEqual(cursor.run_sql_in_snapshot("sql"), list(results)) - def test_run_sql_in_snapshot_database_error(self): - connection = self._make_connection(self.INSTANCE) - cursor = self._make_one(connection) - - with self.assertRaises(ValueError): - cursor.run_sql_in_snapshot("sql") - def test_get_table_column_schema(self): from google.cloud.spanner_dbapi.cursor import ColumnDetails from google.cloud.spanner_dbapi import _helpers pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy