3
3
4
4
from urllib3 import Timeout
5
5
6
+ import crate .client .exceptions
6
7
from crate .client import connect
7
8
from crate .client .connection import Connection
8
9
from crate .client .http import Client
11
12
12
13
13
14
class ConnectionTest (TestCase ):
15
+
14
16
def test_connection_mock (self ):
15
17
"""
16
18
For testing purposes it is often useful to replace the client used for
@@ -21,21 +23,27 @@ def test_connection_mock(self):
21
23
"""
22
24
23
25
class MyConnectionClient :
24
- active_servers = ["localhost:4200" ]
26
+ active_servers = [crate_host ]
25
27
26
28
def __init__ (self ):
27
29
pass
28
30
29
31
def server_infos (self , server ):
30
- return ("localhost:4200" , "my server" , "0.42.0" )
32
+ return (crate_host , "my server" , "0.42.0" )
31
33
32
34
connection = connect ([crate_host ], client = MyConnectionClient ())
33
35
self .assertIsInstance (connection , Connection )
34
36
self .assertEqual (
35
37
connection .client .server_infos ("foo" ),
36
- ("localhost:4200" , "my server" , "0.42.0" ),
38
+ (crate_host , "my server" , "0.42.0" ),
37
39
)
38
40
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
+
39
47
def test_lowest_server_version (self ):
40
48
infos = [
41
49
(None , None , "0.42.3" ),
@@ -50,14 +58,14 @@ def test_lowest_server_version(self):
50
58
connection .close ()
51
59
52
60
def test_invalid_server_version (self ):
53
- client = Client (servers = "localhost:4200" )
61
+ client = Client (servers = crate_host )
54
62
client .server_infos = lambda server : (None , None , "No version" )
55
63
connection = connect (client = client )
56
64
self .assertEqual ((0 , 0 , 0 ), connection .lowest_server_version .version )
57
65
connection .close ()
58
66
59
67
def test_context_manager (self ):
60
- with connect ("localhost:4200" ) as conn :
68
+ with connect (crate_host ) as conn :
61
69
pass
62
70
self .assertEqual (conn ._closed , True )
63
71
@@ -70,7 +78,7 @@ def test_with_timezone(self):
70
78
"""
71
79
72
80
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 )
74
82
cursor = connection .cursor ()
75
83
self .assertEqual (cursor .time_zone .tzname (None ), "MST" )
76
84
self .assertEqual (
@@ -88,20 +96,20 @@ def test_timeout_float(self):
88
96
"""
89
97
Verify setting the timeout value as a scalar (float) works.
90
98
"""
91
- with connect ("localhost:4200" , timeout = 2.42 ) as conn :
99
+ with connect (crate_host , timeout = 2.42 ) as conn :
92
100
self .assertEqual (conn .client ._pool_kw ["timeout" ], 2.42 )
93
101
94
102
def test_timeout_string (self ):
95
103
"""
96
104
Verify setting the timeout value as a scalar (string) works.
97
105
"""
98
- with connect ("localhost:4200" , timeout = "2.42" ) as conn :
106
+ with connect (crate_host , timeout = "2.42" ) as conn :
99
107
self .assertEqual (conn .client ._pool_kw ["timeout" ], 2.42 )
100
108
101
109
def test_timeout_object (self ):
102
110
"""
103
111
Verify setting the timeout value as a Timeout object works.
104
112
"""
105
113
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 :
107
115
self .assertEqual (conn .client ._pool_kw ["timeout" ], timeout )
0 commit comments