4
4
# license that can be found in the LICENSE file or at
5
5
# https://developers.google.com/open-source/licenses/bsd
6
6
7
- import sys
8
- import unittest
9
- import os
10
-
11
- from mock_import import mock_import
12
7
from unittest import mock
8
+ from tests .unit .django_spanner .simple_test import SpannerSimpleTestClass
13
9
14
10
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 ):
42
12
def test_property_instance (self ):
43
- settings_dict = {"INSTANCE" : "instance" }
44
- db_wrapper = self ._make_one (settings_dict = settings_dict )
45
-
46
13
with mock .patch ("django_spanner.base.spanner" ) as mock_spanner :
47
14
mock_spanner .Client = mock_client = mock .MagicMock ()
48
15
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 )
51
18
52
- def test_property__nodb_connection (self ):
53
- db_wrapper = self ._make_one (None )
19
+ def test_property_nodb_connection (self ):
54
20
with self .assertRaises (NotImplementedError ):
55
- db_wrapper ._nodb_connection ()
21
+ self . db_wrapper ._nodb_connection ()
56
22
57
23
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 ()
60
25
61
26
self .assertEqual (params ["project" ], self .PROJECT )
62
27
self .assertEqual (params ["instance_id" ], self .INSTANCE_ID )
@@ -65,54 +30,50 @@ def test_get_connection_params(self):
65
30
self .assertEqual (params ["option" ], self .OPTIONS ["option" ])
66
31
67
32
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 ()
70
34
mock_database .connect = mock_connection = mock .MagicMock ()
71
35
conn_params = {"test_param" : "dummy" }
72
- db_wrapper .
10000
get_new_connection (conn_params )
36
+ self . db_wrapper .get_new_connection (conn_params )
73
37
mock_connection .assert_called_once_with (** conn_params )
74
38
75
39
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 ()
78
41
mock_connection .close = mock_close = mock .MagicMock ()
79
- db_wrapper .init_connection_state ()
42
+ self . db_wrapper .init_connection_state ()
80
43
mock_close .assert_called_once_with ()
81
44
82
45
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 ()
85
47
mock_connection .cursor = mock_cursor = mock .MagicMock ()
86
- db_wrapper .create_cursor ()
48
+ self . db_wrapper .create_cursor ()
87
49
mock_cursor .assert_called_once_with ()
88
50
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 ()
92
53
mock_connection .autocommit = False
93
- db_wrapper ._set_autocommit (True )
54
+ self . db_wrapper ._set_autocommit (True )
94
55
self .assertEqual (mock_connection .autocommit , True )
95
56
96
57
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 ())
102
60
103
- db_wrapper .connection = mock_connection = mock .MagicMock ()
61
+ self . db_wrapper .connection = mock_connection = mock .MagicMock ()
104
62
mock_connection .is_closed = True
105
- self .assertFalse (db_wrapper .is_usable ())
63
+ self .assertFalse (self . db_wrapper .is_usable ())
106
64
107
65
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
109
70
71
+ self .db_wrapper .connection = mock_connection = mock .MagicMock ()
110
72
mock_connection .cursor = mock .MagicMock (side_effect = Error )
111
- self .assertFalse (db_wrapper .is_usable ())
73
+ self .assertFalse (self . db_wrapper .is_usable ())
112
74
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 ()
116
77
mock_connection .cursor = mock_cursor = mock .MagicMock ()
117
- db_wrapper ._start_transaction_under_autocommit ()
78
+ self . db_wrapper ._start_transaction_under_autocommit ()
118
79
mock_cursor .assert_called_once_with ()
0 commit comments