1616import unittest2
1717
1818
19+ class Test__get_cluster (unittest2 .TestCase ):
20+
21+ def _callFUT (self , timeout = None ):
22+ from gcloud .bigtable .happybase .connection import _get_cluster
23+ return _get_cluster (timeout = timeout )
24+
25+ def _helper (self , timeout = None , clusters = (), failed_zones = ()):
26+ from functools import partial
27+ from gcloud ._testing import _Monkey
28+ from gcloud .bigtable .happybase import connection as MUT
29+
30+ client_with_clusters = partial (_Client , clusters = clusters ,
31+ failed_zones = failed_zones )
32+ with _Monkey (MUT , Client = client_with_clusters ):
33+ result = self ._callFUT (timeout = timeout )
34+
35+ # If we've reached this point, then _callFUT didn't fail, so we know
36+ # there is exactly one cluster.
37+ cluster , = clusters
38+ self .assertEqual (result , cluster )
39+ client = cluster .client
40+ self .assertEqual (client .args , ())
41+ expected_kwargs = {'admin' : True }
42+ if timeout is not None :
43+ expected_kwargs ['timeout_seconds' ] = timeout / 1000.0
44+ self .assertEqual (client .kwargs , expected_kwargs )
45+ self .assertEqual (client .start_calls , 1 )
46+ self .assertEqual (client .stop_calls , 1 )
47+
48+ def test_default (self ):
49+ cluster = _Cluster ()
50+ self ._helper (clusters = [cluster ])
51+
52+ def test_with_timeout (self ):
53+ cluster = _Cluster ()
54+ self ._helper (timeout = 2103 , clusters = [cluster ])
55+
56+ def test_with_no_clusters (self ):
57+ with self .assertRaises (ValueError ):
58+ self ._helper ()
59+
60+ def test_with_too_many_clusters (self ):
61+ clusters = [_Cluster (), _Cluster ()]
62+ with self .assertRaises (ValueError ):
63+ self ._helper (clusters = clusters )
64+
65+ def test_with_failed_zones (self ):
66+ cluster = _Cluster ()
67+ failed_zone = 'us-central1-c'
68+ with self .assertRaises (ValueError ):
69+ self ._helper (clusters = [cluster ],
70+ failed_zones = [failed_zone ])
71+
72+
1973class TestConnection (unittest2 .TestCase ):
2074
2175 def _getTargetClass (self ):
@@ -26,48 +80,83 @@ def _makeOne(self, *args, **kwargs):
2680 return self ._getTargetClass ()(* args , ** kwargs )
2781
2882 def test_constructor_defaults (self ):
29- connection = self ._makeOne ()
30- self .assertEqual (connection .timeout , None )
83+ cluster = _Cluster () # Avoid implicit environ check.
84+ connection = self ._makeOne (cluster = cluster )
85+
3186 self .assertTrue (connection .autoconnect )
87+ self .assertEqual (connection ._cluster , cluster )
3288 self .assertEqual (connection .table_prefix , None )
3389 self .assertEqual (connection .table_prefix_separator , '_' )
3490
91+ def test_constructor_missing_cluster (self ):
92+ from gcloud ._testing import _Monkey
93+ from gcloud .bigtable .happybase import connection as MUT
94+
95+ cluster = _Cluster ()
96+ timeout = object ()
97+ get_cluster_called = []
98+
99+ def mock_get_cluster (timeout ):
100+ get_cluster_called .append (timeout )
101+ return cluster
102+
103+ with _Monkey (MUT , _get_cluster = mock_get_cluster ):
104+ connection = self ._makeOne (autoconnect = False , cluster = None ,
105+ timeout = timeout )
106+ self .assertEqual (connection .table_prefix , None )
107+ self .assertEqual (connection .table_prefix_separator , '_' )
108+ self .assertEqual (connection ._cluster , cluster )
109+
110+ self .assertEqual (get_cluster_called , [timeout ])
111+
35112 def test_constructor_explicit (self ):
36- timeout = 12345
37113 autoconnect = False
38114 table_prefix = 'table-prefix'
39115 table_prefix_separator = 'sep'
116+ cluster_copy = _Cluster ()
117+ cluster = _Cluster (copies = [cluster_copy ])
40118
41119 connection = self ._makeOne (
42- timeout = timeout ,
43120 autoconnect = autoconnect ,
44121 table_prefix = table_prefix ,
45- table_prefix_separator = table_prefix_separator )
46- self . assertEqual ( connection . timeout , timeout )
122+ table_prefix_separator = table_prefix_separator ,
123+ cluster = cluster )
47124 self .assertFalse (connection .autoconnect )
48125 self .assertEqual (connection .table_prefix , table_prefix )
49126 self .assertEqual (connection .table_prefix_separator ,
50127 table_prefix_separator )
51128
52- def test_constructor_with_host (self ):
53- with self .assertRaises (ValueError ):
54- self ._makeOne (host = object ())
129+ def test_constructor_with_unknown_argument (self ):
130+ cluster = _Cluster ()
131+ with self .assertRaises (TypeError ):
132+ self ._makeOne (cluster = cluster , unknown = 'foo' )
55133
56- def test_constructor_with_port (self ):
57- with self . assertRaises ( ValueError ):
58- self . _makeOne ( port = object ())
134+ def test_constructor_with_legacy_args (self ):
135+ from gcloud . _testing import _Monkey
136+ from gcloud . bigtable . happybase import connection as MUT
59137
60- def test_constructor_with_compat (self ):
61- with self .assertRaises (ValueError ):
62- self ._makeOne (compat = object ())
138+ warned = []
63139
64- def test_constructor_with_transport (self ):
65- with self .assertRaises (ValueError ):
66- self ._makeOne (transport = object ())
140+ def mock_warn (msg ):
141+ warned .append (msg )
142+
143+ cluster = _Cluster ()
144+ with _Monkey (MUT , _WARN = mock_warn ):
145+ self ._makeOne (cluster = cluster , host = object (),
146+ port = object (), compat = object (),
147+ transport = object (), protocol = object ())
67148
68- def test_constructor_with_protocol (self ):
149+ self .assertEqual (len (warned ), 1 )
150+ self .assertIn ('host' , warned [0 ])
151+ self .assertIn ('port' , warned [0 ])
152+ self .assertIn ('compat' , warned [0 ])
153+ self .assertIn ('transport' , warned [0 ])
154+ self .assertIn ('protocol' , warned [0 ])
155+
156+ def test_constructor_with_timeout_and_cluster (self ):
157+ cluster = _Cluster ()
69158 with self .assertRaises (ValueError ):
70- self ._makeOne (protocol = object ())
159+ self ._makeOne (cluster = cluster , timeout = object ())
71160
72161 def test_constructor_non_string_prefix (self ):
73162 table_prefix = object ()
@@ -82,3 +171,42 @@ def test_constructor_non_string_prefix_separator(self):
82171 with self .assertRaises (TypeError ):
83172 self ._makeOne (autoconnect = False ,
84173 table_prefix_separator = table_prefix_separator )
174+
175+
176+ class _Client (object ):
177+
178+ def __init__ (self , * args , ** kwargs ):
179+ self .clusters = kwargs .pop ('clusters' , [])
180+ for cluster in self .clusters :
181+ cluster .client = self
182+ self .failed_zones = kwargs .pop ('failed_zones' , [])
183+ self .args = args
184+ self .kwargs = kwargs
185+ self .start_calls = 0
186+ self .stop_calls = 0
187+
188+ def start (self ):
189+ self .start_calls += 1
190+
191+ def stop (self ):
192+ self .stop_calls += 1
193+
194+ def list_clusters (self ):
195+ return self .clusters , self .failed_zones
196+
197+
198+ class _Cluster (object ):
199+
200+ def __init__ (self , copies = (), list_tables_result = ()):
201+ self .copies = list (copies )
202+ # Included to support Connection.__del__
203+ self ._client = _Client ()
204+ self .list_tables_result = list_tables_result
205+
206+ def copy (self ):
207+ if self .copies :
208+ result = self .copies [0 ]
209+ self .copies [:] = self .copies [1 :]
210+ return result
211+ else :
212+ return self
0 commit comments