Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 103
fix(db_api): move connection validation into a separate method#543
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86ba731
fix(db_api): move connection validation into a separate method
c1ee8eb
add more tests
541c30b
add test for an exception
6905779
add exception docstring
1492d68
Merge remote-tracking branch 'origin/main' into connection_validate
b6e2cd7
Merge branch 'main' into connection_validate
larkee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -28,7 +28,7 @@ | ||
| from google.cloud.spanner_dbapi.checksum import _compare_checksums | ||
| from google.cloud.spanner_dbapi.checksum import ResultsChecksum | ||
| from google.cloud.spanner_dbapi.cursor import Cursor | ||
| from google.cloud.spanner_dbapi.exceptions import InterfaceError | ||
| from google.cloud.spanner_dbapi.exceptions import InterfaceError, OperationalError | ||
| from google.cloud.spanner_dbapi.version import DEFAULT_USER_AGENT | ||
| from google.cloud.spanner_dbapi.version import PY_VERSION | ||
| @@ -349,6 +349,30 @@ def run_statement(self, statement, retried=False): | ||
| ResultsChecksum() if retried else statement.checksum, | ||
| ) | ||
| def validate(self): | ||
| """ | ||
| Execute a minimal request to check if the connection | ||
| is valid and the related database is reachable. | ||
| Raise an exception in case if the connection is closed, | ||
| invalid, target database is not found, or the request result | ||
| is incorrect. | ||
| :raises: :class:`InterfaceError`: if this connection is closed. | ||
| :raises: :class:`OperationalError`: if the request result is incorrect. | ||
| :raises: :class:`google.cloud.exceptions.NotFound`: if the linked instance | ||
| or database doesn't exist. | ||
| """ | ||
| self._raise_if_closed() | ||
| with self.database.snapshot() as snapshot: | ||
| result = list(snapshot.execute_sql("SELECT 1")) | ||
| if result != [[1]]: | ||
| raise OperationalError( | ||
| "The checking query (SELECT 1) returned an unexpected result: %s. " | ||
| "Expected: [[1]]" % result | ||
| ) | ||
| def __enter__(self): | ||
| return self | ||
| @@ -399,9 +423,6 @@ def connect( | ||
| :rtype: :class:`google.cloud.spanner_dbapi.connection.Connection` | ||
| :returns: Connection object associated with the given Google Cloud Spanner | ||
| resource. | ||
| :raises: :class:`ValueError` in case of given instance/database | ||
| doesn't exist. | ||
| """ | ||
| client_info = ClientInfo( | ||
| @@ -418,14 +439,7 @@ def connect( | ||
| ) | ||
| instance = client.instance(instance_id) | ||
| if not instance.exists(): | ||
| raise ValueError("instance '%s' does not exist." % instance_id) | ||
| database = instance.database(database_id, pool=pool) | ||
| if not database.exists(): | ||
| raise ValueError("database '%s' does not exist." % database_id) | ||
| conn = Connection(instance, database) | ||
IlyaFaer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| conn = Connection(instance, instance.database(database_id, pool=pool)) | ||
| if pool is not None: | ||
| conn._own_pool = False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -624,3 +624,80 @@ def test_retry_transaction_w_empty_response(self): | ||
| compare_mock.assert_called_with(checksum, retried_checkum) | ||
| run_mock.assert_called_with(statement, retried=True) | ||
| def test_validate_ok(self): | ||
| def exit_func(self, exc_type, exc_value, traceback): | ||
| pass | ||
| connection = self._make_connection() | ||
| # mock snapshot context manager | ||
| snapshot_obj = mock.Mock() | ||
| snapshot_obj.execute_sql = mock.Mock(return_value=[[1]]) | ||
| snapshot_ctx = mock.Mock() | ||
| snapshot_ctx.__enter__ = mock.Mock(return_value=snapshot_obj) | ||
| snapshot_ctx.__exit__ = exit_func | ||
| snapshot_method = mock.Mock(return_value=snapshot_ctx) | ||
| connection.database.snapshot = snapshot_method | ||
| connection.validate() | ||
| snapshot_obj.execute_sql.assert_called_once_with("SELECT 1") | ||
| def test_validate_fail(self): | ||
IlyaFaer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| from google.cloud.spanner_dbapi.exceptions import OperationalError | ||
| def exit_func(self, exc_type, exc_value, traceback): | ||
| pass | ||
| connection = self._make_connection() | ||
| # mock snapshot context manager | ||
| snapshot_obj = mock.Mock() | ||
| snapshot_obj.execute_sql = mock.Mock(return_value=[[3]]) | ||
| snapshot_ctx = mock.Mock() | ||
| snapshot_ctx.__enter__ = mock.Mock(return_value=snapshot_obj) | ||
| snapshot_ctx.__exit__ = exit_func | ||
| snapshot_method = mock.Mock(return_value=snapshot_ctx) | ||
| connection.database.snapshot = snapshot_method | ||
| with self.assertRaises(OperationalError): | ||
| connection.validate() | ||
| snapshot_obj.execute_sql.assert_called_once_with("SELECT 1") | ||
| def test_validate_error(self): | ||
| from google.cloud.exceptions import NotFound | ||
| def exit_func(self, exc_type, exc_value, traceback): | ||
| pass | ||
| connection = self._make_connection() | ||
| # mock snapshot context manager | ||
| snapshot_obj = mock.Mock() | ||
| snapshot_obj.execute_sql = mock.Mock(side_effect=NotFound("Not found")) | ||
| snapshot_ctx = mock.Mock() | ||
| snapshot_ctx.__enter__ = mock.Mock(return_value=snapshot_obj) | ||
| snapshot_ctx.__exit__ = exit_func | ||
| snapshot_method = mock.Mock(return_value=snapshot_ctx) | ||
| connection.database.snapshot = snapshot_method | ||
| with self.assertRaises(NotFound): | ||
| connection.validate() | ||
| snapshot_obj.execute_sql.assert_called_once_with("SELECT 1") | ||
| def test_validate_closed(self): | ||
| from google.cloud.spanner_dbapi.exceptions import InterfaceError | ||
| connection = self._make_connection() | ||
| connection.close() | ||
| with self.assertRaises(InterfaceError): | ||
| connection.validate() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.