1919from datetime import datetime , timedelta
2020
2121import mock
22+ from google .cloud .spanner_v1 import pool as MUT
2223from google .cloud .spanner_v1 import _opentelemetry_tracing
24+ from google .cloud .spanner_v1 import ExecuteSqlRequest
25+ from google .cloud .spanner_v1 import BatchCreateSessionsResponse
26+ from google .cloud .spanner_v1 import Session
27+ from google .cloud .spanner_v1 import SpannerClient
28+ from google .cloud .spanner_v1 .database import Database
29+ from google .cloud .spanner_v1 .pool import AbstractSessionPool
30+ from google .cloud .spanner_v1 .pool import SessionCheckout
31+ from google .cloud .spanner_v1 .pool import FixedSizePool
32+ from google .cloud .spanner_v1 .pool import BurstyPool
33+ from google .cloud .spanner_v1 .pool import PingingPool
34+ from google .cloud .spanner_v1 .transaction import Transaction
35+ from google .cloud .exceptions import NotFound
36+ from google .cloud ._testing import _Monkey
2337from google .cloud .spanner_v1 ._helpers import (
2438 _metadata_with_request_id ,
2539 _metadata_with_request_id_and_req_id ,
4054
4155
4256def _make_database (name = "name" ):
43- from google .cloud .spanner_v1 .database import Database
4457
4558 return mock .create_autospec (Database , instance = True )
4659
4760
4861def _make_session ():
49- from google .cloud .spanner_v1 .database import Session
5062
5163 return mock .create_autospec (Session , instance = True )
5264
5365
5466class TestAbstractSessionPool (unittest .TestCase ):
5567 def _getTargetClass (self ):
56- from google .cloud .spanner_v1 .pool import AbstractSessionPool
5768
5869 return AbstractSessionPool
5970
@@ -129,7 +140,6 @@ def test__new_session_w_database_role(self):
129140 self .assertEqual (new_session .database_role , database_role )
130141
131142 def test_session_wo_kwargs (self ):
132- from google .cloud .spanner_v1 .pool import SessionCheckout
133143
134144 pool = self ._make_one ()
135145 checkout = pool .session ()
@@ -139,7 +149,6 @@ def test_session_wo_kwargs(self):
139149 self .assertEqual (checkout ._kwargs , {})
140150
141151 def test_session_w_kwargs (self ):
142- from google .cloud .spanner_v1 .pool import SessionCheckout
143152
144153 pool = self ._make_one ()
145154 checkout = pool .session (foo = "bar" )
@@ -164,7 +173,6 @@ class TestFixedSizePool(OpenTelemetryBase):
164173 enrich_with_otel_scope (BASE_ATTRIBUTES )
165174
166175 def _getTargetClass (self ):
167- from google .cloud .spanner_v1 .pool import FixedSizePool
168176
169177 return FixedSizePool
170178
@@ -559,7 +567,6 @@ class TestBurstyPool(OpenTelemetryBase):
559567 enrich_with_otel_scope (BASE_ATTRIBUTES )
560568
561569 def _getTargetClass (self ):
562- from google .cloud .spanner_v1 .pool import BurstyPool
563570
564571 return BurstyPool
565572
@@ -850,7 +857,6 @@ class TestPingingPool(OpenTelemetryBase):
850857 enrich_with_otel_scope (BASE_ATTRIBUTES )
851858
852859 def _getTargetClass (self ):
853- from google .cloud .spanner_v1 .pool import PingingPool
854860
855861 return PingingPool
856862
@@ -946,8 +952,6 @@ def test_get_hit_no_ping(self, mock_region):
946952 )
947953 def test_get_hit_w_ping (self , mock_region ):
948954 import datetime
949- from google .cloud ._testing import _Monkey
950- from google .cloud .spanner_v1 import pool as MUT
951955
952956 pool = self ._make_one (size = 4 )
953957 database = _Database ("name" )
@@ -974,8 +978,6 @@ def test_get_hit_w_ping(self, mock_region):
974978 )
975979 def test_get_hit_w_ping_expired (self , mock_region ):
976980 import datetime
977- from google .cloud ._testing import _Monkey
978- from google .cloud .spanner_v1 import pool as MUT
979981
980982 pool = self ._make_one (size = 4 )
981983 database = _Database ("name" )
@@ -1097,8 +1099,6 @@ def test_spans_put_full(self, mock_region):
10971099 )
10981100 def test_put_non_full (self , mock_region ):
10991101 import datetime
1100- from google .cloud ._testing import _Monkey
1101- from google .cloud .spanner_v1 import pool as MUT
11021102
11031103 pool = self ._make_one (size = 1 )
11041104 session_queue = pool ._sessions = _Queue ()
@@ -1172,8 +1172,6 @@ def test_ping_oldest_fresh(self, mock_region):
11721172 )
11731173 def test_ping_oldest_stale_but_exists (self , mock_region ):
11741174 import datetime
1175- from google .cloud ._testing import _Monkey
1176- from google .cloud .spanner_v1 import pool as MUT
11771175
11781176 pool = self ._make_one (size = 1 )
11791177 database = _Database ("name" )
@@ -1193,8 +1191,6 @@ def test_ping_oldest_stale_but_exists(self, mock_region):
11931191 )
11941192 def test_ping_oldest_stale_and_not_exists (self , mock_region ):
11951193 import datetime
1196- from google .cloud ._testing import _Monkey
1197- from google .cloud .spanner_v1 import pool as MUT
11981194
11991195 pool = self ._make_one (size = 1 )
12001196 database = _Database ("name" )
@@ -1257,7 +1253,6 @@ def test_spans_get_and_leave_empty_pool(self, mock_region):
12571253
12581254class TestSessionCheckout (unittest .TestCase ):
12591255 def _getTargetClass (self ):
1260- from google .cloud .spanner_v1 .pool import SessionCheckout
12611256
12621257 return SessionCheckout
12631258
@@ -1314,7 +1309,6 @@ def test_context_manager_w_kwargs(self):
13141309
13151310
13161311def _make_transaction (* args , ** kw ):
1317- from google .cloud .spanner_v1 .transaction import Transaction
13181312
13191313 txn = mock .create_autospec (Transaction )(* args , ** kw )
13201314 txn .committed = None
@@ -1352,14 +1346,12 @@ def exists(self):
13521346 return self ._exists
13531347
13541348 def ping (self ):
1355- from google .cloud .exceptions import NotFound
13561349
13571350 self ._pinged = True
13581351 if not self ._exists :
13591352 raise NotFound ("expired session" )
13601353
13611354 def delete (self ):
1362- from google .cloud .exceptions import NotFound
13631355
13641356 self ._deleted = True
13651357 if not self ._exists :
@@ -1391,9 +1383,6 @@ def mock_batch_create_sessions(
13911383 metadata = [],
13921384 labels = {},
13931385 ):
1394- from google .cloud .spanner_v1 import BatchCreateSessionsResponse
1395- from google .cloud .spanner_v1 import Session
1396-
13971386 database_role = request .session_template .creator_role if request else None
13981387 if request .session_count < 2 :
13991388 response = BatchCreateSessionsResponse (
@@ -1408,10 +1397,15 @@ def mock_batch_create_sessions(
14081397 )
14091398 return response
14101399
1411- from google .cloud .spanner_v1 import SpannerClient
1412-
14131400 self .spanner_api = mock .create_autospec (SpannerClient , instance = True )
14141401 self .spanner_api .batch_create_sessions .side_effect = mock_batch_create_sessions
1402+ self ._instance = mock .Mock ()
1403+ self ._instance ._client = mock .Mock ()
1404+ self ._instance ._client ._client_context = None
1405+ self ._instance ._client .spanner_api = self .spanner_api
1406+ self ._instance ._client ._query_options = ExecuteSqlRequest .QueryOptions (
1407+ optimizer_version = "1"
1408+ )
14151409
14161410 @property
14171411 def database_role (self ):
0 commit comments