44# license that can be found in the LICENSE file or at
55# https://developers.google.com/open-source/licenses/bsd
66
7- import sys
8- import unittest
9- import os
10-
11- from mock_import import mock_import
127from unittest import mock
8+ from tests .unit .django_spanner .simple_test import SpannerSimpleTestClass
139
1410
15- @mock_import ()
16- @unittest .skipIf (
17- sys .version_info < (3 , 6 ), reason = "Skipping Python versions <= 3.5"
18- )
19- class TestBase (unittest .TestCase ):
20- PROJECT = os .environ ["GOOGLE_CLOUD_PROJECT" ]
21- INSTANCE_ID = "instance_id"
22- DATABASE_ID = "database_id"
23- USER_AGENT = "django_spanner/2.2.0a1"
24- OPTIONS = {"option" : "dummy" }
25-
26- settings_dict = {
27- "PROJECT" : PROJECT ,
28- "INSTANCE" : INSTANCE_ID ,
29- "NAME" : DATABASE_ID ,
30- "user_agent" : USER_AGENT ,
31- "OPTIONS" : OPTIONS ,
32- }
33-
34- def _get_target_class (self ):
35- from django_spanner .base import DatabaseWrapper
36-
37- return DatabaseWrapper
38-
39- def _make_one (self , * args , ** kwargs ):
40- return self ._get_target_class ()(* args , ** kwargs )
41-
11+ class TestBase (SpannerSimpleTestClass ):
4212 def test_property_instance (self ):
43- settings_dict = {"INSTANCE" : "instance" }
44- db_wrapper = self ._make_one (settings_dict = settings_dict )
45-
4613 with mock .patch ("django_spanner.base.spanner" ) as mock_spanner :
4714 mock_spanner .Client = mock_client = mock .MagicMock ()
4815 mock_client ().instance = mock_instance = mock .MagicMock ()
49- _ = db_wrapper .instance
50- mock_instance .assert_called_once_with (settings_dict [ "INSTANCE" ] )
16+ _ = self . db_wrapper .instance
17+ mock_instance .assert_called_once_with (self . INSTANCE_ID )
5118
52- def test_property__nodb_connection (self ):
53- db_wrapper = self ._make_one (None )
19+ def test_property_nodb_connection (self ):
5420 with self .assertRaises (NotImplementedError ):
55- db_wrapper ._nodb_connection ()
21+ self . db_wrapper ._nodb_connection ()
5622
5723 def test_get_connection_params (self ):
58- db_wrapper = self ._make_one (self .settings_dict )
59- params = db_wrapper .get_connection_params ()
24+ params = self .db_wrapper .get_connection_params ()
6025
6126 self .assertEqual (params ["project" ], self .PROJECT )
6227 self .assertEqual (params ["instance_id" ], self .INSTANCE_ID )
@@ -65,54 +30,50 @@ def test_get_connection_params(self):
6530 self .assertEqual (params ["option" ], self .OPTIONS ["option" ])
6631
6732 def test_get_new_connection (self ):
68- db_wrapper = self ._make_one (self .settings_dict )
69- db_wrapper .Database = mock_database = mock .MagicMock ()
33+ self .db_wrapper .Database = mock_database = mock .MagicMock ()
7034 mock_database .connect = mock_connection = mock .MagicMock ()
7135 conn_params = {"test_param" : "dummy" }
72- db_wrapper .get_new_connection (conn_params )
36+ self . db_wrapper .get_new_connection (conn_params )
7337 mock_connection .assert_called_once_with (** conn_params )
7438
7539 def test_init_connection_state (self ):
76- db_wrapper = self ._make_one (self .settings_dict )
77- db_wrapper .connection = mock_connection = mock .MagicMock ()
40+ self .db_wrapper .connection = mock_connection = mock .MagicMock ()
7841 mock_connection .close = mock_close = mock .MagicMock ()
79- db_wrapper .init_connection_state ()
42+ self . db_wrapper .init_connection_state ()
8043 mock_close .assert_called_once_with ()
8144
8245 def test_create_cursor (self ):
83- db_wrapper = self ._make_one (self .settings_dict )
84- db_wrapper .connection = mock_connection = mock .MagicMock ()
46+ self .db_wrapper .connection = mock_connection = mock .MagicMock ()
8547 mock_connection .cursor = mock_cursor = mock .MagicMock ()
86- db_wrapper .create_cursor ()
48+ self . db_wrapper .create_cursor ()
8749 mock_cursor .assert_called_once_with ()
8850
89- def test__set_autocommit (self ):
90- db_wrapper = self ._make_one (self .settings_dict )
91- db_wrapper .connection = mock_connection = mock .MagicMock ()
51+ def test_set_autocommit (self ):
52+ self .db_wrapper .connection = mock_connection = mock .MagicMock ()
9253 mock_connection .autocommit = False
93- db_wrapper ._set_autocommit (True )
54+ self . db_wrapper ._set_autocommit (True )
9455 self .assertEqual (mock_connection .autocommit , True )
9556
9657 def test_is_usable (self ):
97- from google .cloud .spanner_dbapi .exceptions import Error
98-
99- db_wrapper = self ._make_one (self .settings_dict )
100- db_wrapper .connection = None
101- self .assertFalse (db_wrapper .is_usable ())
58+ self .db_wrapper .connection = None
59+ self .assertFalse (self .db_wrapper .is_usable ())
10260
103- db_wrapper .connection = mock_connection = mock .MagicMock ()
61+ self . db_wrapper .connection = mock_connection = mock .MagicMock ()
10462 mock_connection .is_closed = True
105- self .assertFalse (db_wrapper .is_usable ())
63+ self .assertFalse (self . db_wrapper .is_usable ())
10664
10765 mock_connection .is_closed = False
108- self .assertTrue (db_wrapper .is_usable ())
66+ self .assertTrue (self .db_wrapper .is_usable ())
67+
68+ def test_is_usable_with_error (self ):
69+ from google .cloud .spanner_dbapi .exceptions import Error
10970
71+ self .db_wrapper .connection = mock_connection = mock .MagicMock ()
11072 mock_connection .cursor = mock .MagicMock (side_effect = Error )
111- self .assertFalse (db_wrapper .is_usable ())
73+ self .assertFalse (self . db_wrapper .is_usable ())
11274
113- def test__start_transaction_under_autocommit (self ):
114- db_wrapper = self ._make_one (self .settings_dict )
115- db_wrapper .connection = mock_connection = mock .MagicMock ()
75+ def test_start_transaction_under_autocommit (self ):
76+ self .db_wrapper .connection = mock_connection = mock .MagicMock ()
11677 mock_connection .cursor = mock_cursor = mock .MagicMock ()
117- db_wrapper ._start_transaction_under_autocommit ()
78+ self . db_wrapper ._start_transaction_under_autocommit ()
11879 mock_cursor .assert_called_once_with ()
0 commit comments