New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: missing method implementation and potential bug in the DatabaseWrapper
class
#545
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
35d2e68
fix: implementation of `init_connection_state()` + potential bug in `…
mf2199 6496a18
Merge branch 'master' into django-spanner-base
mf2199 5a8ac4e
chore: implementing suggested changes
mf2199 83f6217
Merge branch 'master' into django-spanner-base
mf2199 0faab71
test: unit test coverage for `base.py`
mf2199 14ebac9
chore: [temporarily] skipping Python-3.5
mf2199 35b207e
Use django 2.2 in nox tests
c24t 29652ac
Merge branch 'master' into django-spanner-base
c24t File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
import sys | ||
import unittest | ||
|
||
from mock_import import mock_import | ||
from unittest import mock | ||
|
||
|
||
@mock_import() | ||
@unittest.skipIf(sys.version_info < (3, 6), reason="Skipping Python 3.5") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can safely drop 3.5 in tests since we already decided not to support it elsewhere. |
||
class TestBase(unittest.TestCase): | ||
PROJECT = "project" | ||
INSTANCE_ID = "instance_id" | ||
DATABASE_ID = "database_id" | ||
USER_AGENT = "django_spanner/2.2.0a1" | ||
OPTIONS = {"option": "dummy"} | ||
|
||
settings_dict = { | ||
"PROJECT": PROJECT, | ||
"INSTANCE": INSTANCE_ID, | ||
"NAME": DATABASE_ID, | ||
"user_agent": USER_AGENT, | ||
"OPTIONS": OPTIONS, | ||
} | ||
|
||
def _get_target_class(self): | ||
from django_spanner.base import DatabaseWrapper | ||
|
||
return DatabaseWrapper | ||
|
||
def _make_one(self, *args, **kwargs): | ||
return self._get_target_class()(*args, **kwargs) | ||
|
||
def test_property_instance(self): | ||
settings_dict = {"INSTANCE": "instance"} | ||
db_wrapper = self._make_one(settings_dict=settings_dict) | ||
|
||
with mock.patch("django_spanner.base.spanner") as mock_spanner: | ||
mock_spanner.Client = mock_client = mock.MagicMock() | ||
mock_client().instance = mock_instance = mock.MagicMock() | ||
_ = db_wrapper.instance | ||
mock_instance.assert_called_once_with(settings_dict["INSTANCE"]) | ||
|
||
def test_property__nodb_connection(self): | ||
db_wrapper = self._make_one(None) | ||
with self.assertRaises(NotImplementedError): | ||
db_wrapper._nodb_connection() | ||
|
||
def test_get_connection_params(self): | ||
db_wrapper = self._make_one(self.settings_dict) | ||
params = db_wrapper.get_connection_params() | ||
|
||
self.assertEqual(params["project"], self.PROJECT) | ||
self.assertEqual(params["instance_id"], self.INSTANCE_ID) | ||
self.assertEqual(params["database_id"], self.DATABASE_ID) | ||
self.assertEqual(params["user_agent"], self.USER_AGENT) | ||
self.assertEqual(params["option"], self.OPTIONS["option"]) | ||
|
||
def test_get_new_connection(self): | ||
db_wrapper = self._make_one(self.settings_dict) | ||
db_wrapper.Database = mock_database = mock.MagicMock() | ||
mock_database.connect = mock_connect = mock.MagicMock() | ||
conn_params = {"test_param": "dummy"} | ||
db_wrapper.get_new_connection(conn_params) | ||
mock_connect.assert_called_once_with(**conn_params) | ||
|
||
def test_init_connection_state(self): | ||
db_wrapper = self._make_one(self.settings_dict) | ||
db_wrapper.connection = mock_connection = mock.MagicMock() | ||
mock_connection.close = mock_close = mock.MagicMock() | ||
db_wrapper.init_connection_state() | ||
mock_close.assert_called_once_with() | ||
|
||
def test_create_cursor(self): | ||
db_wrapper = self._make_one(self.settings_dict) | ||
db_wrapper.connection = mock_connection = mock.MagicMock() | ||
mock_connection.cursor = mock_cursor = mock.MagicMock() | ||
db_wrapper.create_cursor() | ||
mock_cursor.assert_called_once_with() | ||
|
||
def test__set_autocommit(self): | ||
db_wrapper = self._make_one(self.settings_dict) | ||
db_wrapper.connection = mock_connection = mock.MagicMock() | ||
mock_connection.autocommit = False | ||
db_wrapper._set_autocommit(True) | ||
self.assertEqual(mock_connection.autocommit, True) | ||
|
||
def test_is_usable(self): | ||
from google.cloud.spanner_dbapi.exceptions import Error | ||
|
||
db_wrapper = self._make_one(self.settings_dict) | ||
db_wrapper.connection = None | ||
self.assertFalse(db_wrapper.is_usable()) | ||
|
||
db_wrapper.connection = mock_connection = mock.MagicMock() | ||
mock_connection.is_closed = True | ||
self.assertFalse(db_wrapper.is_usable()) | ||
|
||
mock_connection.is_closed = False | ||
self.assertTrue(db_wrapper.is_usable()) | ||
|
||
mock_connection.cursor = mock.MagicMock(side_effect=Error) | ||
self.assertFalse(db_wrapper.is_usable()) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW this odd "import x as Database; Database = Database" pattern seems to come from sqlite (https://github.com/django/django/blob/e893c0ad8b0b5b0a1e5be3345c287044868effc4/django/db/backends/sqlite3/base.py#L156), and is copied in all the other built in backends.