33
44from urllib3 import Timeout
55
6+ import crate .client .exceptions
67from crate .client import connect
78from crate .client .connection import Connection
89from crate .client .http import Client
1112
1213
1314class ConnectionTest (TestCase ):
15+
1416 def test_connection_mock (self ):
1517 """
1618 For testing purposes it is often useful to replace the client used for
@@ -21,21 +23,27 @@ def test_connection_mock(self):
2123 """
2224
2325 class MyConnectionClient :
24- active_servers = ["localhost:4200" ]
26+ active_servers = [crate_host ]
2527
2628 def __init__ (self ):
2729 pass
2830
2931 def server_infos (self , server ):
30- return ("localhost:4200" , "my server" , "0.42.0" )
32+ return (crate_host , "my server" , "0.42.0" )
3133
3234 connection = connect ([crate_host ], client = MyConnectionClient ())
3335 self .assertIsInstance (connection , Connection )
3436 self .assertEqual (
3537 connection .client .server_infos ("foo" ),
36- ("localhost:4200" , "my server" , "0.42.0" ),
38+ (crate_host , "my server" , "0.42.0" ),
3739 )
3840
41+ def test_invalid_server_address (self ):
42+ client = Client (servers = "localhost:4202" )
43+ with self .assertRaises (crate .client .exceptions .ConnectionError ) as ex :
44+ connect (client = client )
45+ self .assertIn ("Server not available" , ex .exception .message )
46+
3947 def test_lowest_server_version (self ):
4048 infos = [
4149 (None , None , "0.42.3" ),
@@ -50,14 +58,14 @@ def test_lowest_server_version(self):
5058 connection .close ()
5159
5260 def test_invalid_server_version (self ):
53- client = Client (servers = "localhost:4200" )
61+ client = Client (servers = crate_host )
5462 client .server_infos = lambda server : (None , None , "No version" )
5563 connection = connect (client = client )
5664 self .assertEqual ((0 , 0 , 0 ), connection .lowest_server_version .version )
5765 connection .close ()
5866
5967 def test_context_manager (self ):
60- with connect ("localhost:4200" ) as conn :
68+ with connect (crate_host ) as conn :
6169 pass
6270 self .assertEqual (conn ._closed , True )
6371
@@ -70,7 +78,7 @@ def test_with_timezone(self):
7078 """
7179
7280 tz_mst = datetime .timezone (datetime .timedelta (hours = 7 ), name = "MST" )
73- connection = connect ("localhost:4200" , time_zone = tz_mst )
81+ connection = connect (crate_host , time_zone = tz_mst )
7482 cursor = connection .cursor ()
7583 self .assertEqual (cursor .time_zone .tzname (None ), "MST" )
7684 self .assertEqual (
@@ -88,20 +96,20 @@ def test_timeout_float(self):
8896 """
8997 Verify setting the timeout value as a scalar (float) works.
9098 """
91- with connect ("localhost:4200" , timeout = 2.42 ) as conn :
99+ with connect (crate_host , timeout = 2.42 ) as conn :
92100 self .assertEqual (conn .client ._pool_kw ["timeout" ], 2.42 )
93101
94102 def test_timeout_string (self ):
95103 """
96104 Verify setting the timeout value as a scalar (string) works.
97105 """
98- with connect ("localhost:4200" , timeout = "2.42" ) as conn :
106+ with connect (crate_host , timeout = "2.42" ) as conn :
99107 self .assertEqual (conn .client ._pool_kw ["timeout" ], 2.42 )
100108
101109 def test_timeout_object (self ):
102110 """
103111 Verify setting the timeout value as a Timeout object works.
104112 """
105113 timeout = Timeout (connect = 2.42 , read = 0.01 )
106- with connect ("localhost:4200" , timeout = timeout ) as conn :
114+ with connect (crate_host , timeout = timeout ) as conn :
107115 self .assertEqual (conn .client ._pool_kw ["timeout" ], timeout )
0 commit comments