21
21
22
22
from unittest import TestCase
23
23
import sqlalchemy as sa
24
+ from sqlalchemy .exc import NoSuchModuleError
24
25
25
26
26
27
class SqlAlchemyConnectionTest (TestCase ):
27
28
28
- def setUp (self ):
29
- self .engine = sa . create_engine ( 'crate://' )
30
- self . connection = self . engine . connect ( )
29
+ def test_connection_server_uri_unknown_sa_plugin (self ):
30
+ with self .assertRaises ( NoSuchModuleError ):
31
+ sa . create_engine ( "foobar://otherhost:19201" )
31
32
32
33
def test_default_connection (self ):
33
34
engine = sa .create_engine ('crate://' )
34
35
conn = engine .raw_connection ()
35
36
self .assertEqual ("<Connection <Client ['http://127.0.0.1:4200']>>" ,
36
37
repr (conn .connection ))
37
38
38
- def test_connection_server (self ):
39
+ def test_connection_server_uri_http (self ):
39
40
engine = sa .create_engine (
40
41
"crate://otherhost:19201" )
41
42
conn = engine .raw_connection ()
42
43
self .assertEqual ("<Connection <Client ['http://otherhost:19201']>>" ,
43
44
repr (conn .connection ))
44
45
45
- def test_connection_multiple_server (self ):
46
+ def test_connection_server_uri_https (self ):
47
+ engine = sa .create_engine (
48
+ "crate://otherhost:19201/?ssl=true" )
49
+ conn = engine .raw_connection ()
50
+ self .assertEqual ("<Connection <Client ['https://otherhost:19201']>>" ,
51
+ repr (conn .connection ))
52
+
53
+ def test_connection_server_uri_invalid_port (self ):
54
+ with self .assertRaises (ValueError ) as context :
55
+ sa .create_engine ("crate://foo:bar" )
56
+ self .assertTrue ("invalid literal for int() with base 10: 'bar'" in str (context .exception ))
57
+
58
+ def test_connection_server_uri_https_with_trusted_user (self ):
59
+ engine = sa .create_engine (
60
+ "crate://foo@otherhost:19201/?ssl=true" )
61
+ conn = engine .raw_connection ()
62
+ self .assertEqual ("<Connection <Client ['https://otherhost:19201']>>" ,
63
+ repr (conn .connection ))
64
+ self .assertEqual (conn .connection .client .username , "foo" )
65
+ self .assertEqual (conn .connection .client .password , None )
66
+
67
+ def test_connection_server_uri_https_with_credentials (self ):
68
+ engine = sa .create_engine (
69
+ "crate://foo:bar@otherhost:19201/?ssl=true" )
70
+ conn = engine .raw_connection ()
71
+ self .assertEqual ("<Connection <Client ['https://otherhost:19201']>>" ,
72
+ repr (conn .connection ))
73
+ self .assertEqual (conn .connection .client .username , "foo" )
74
+ self .assertEqual (conn .connection .client .password , "bar" )
75
+
76
+ def test_connection_multiple_server_http (self ):
46
77
engine = sa .create_engine (
47
78
"crate://" , connect_args = {
48
79
'servers' : ['localhost:4201' , 'localhost:4202' ]
@@ -53,3 +84,16 @@ def test_connection_multiple_server(self):
53
84
"<Connection <Client ['http://localhost:4201', " +
54
85
"'http://localhost:4202']>>" ,
55
86
repr (conn .connection ))
87
+
88
+ def test_connection_multiple_server_https (self ):
89
+ engine = sa .create_engine (
90
+ "crate://" , connect_args = {
91
+ 'servers' : ['localhost:4201' , 'localhost:4202' ],
92
+ 'ssl' : True ,
93
+ }
94
+ )
95
+ conn = engine .raw_connection ()
96
+ self .assertEqual (
97
+ "<Connection <Client ['https://localhost:4201', " +
98
+ "'https://localhost:4202']>>" ,
99
+ repr (conn .connection ))
0 commit comments