9
9
from trino .dbapi import Connection
10
10
from trino .sqlalchemy .dialect import CertificateAuthentication , JWTAuthentication , TrinoDialect
11
11
from trino .transaction import IsolationLevel
12
+ from trino .sqlalchemy import URL as trino_url
12
13
13
14
14
15
class TestTrinoDialect :
15
16
def setup (self ):
16
17
self .dialect = TrinoDialect ()
17
18
18
19
@pytest .mark .parametrize (
19
- "url, expected_args, expected_kwargs" ,
20
+ "url, generated_url, expected_args, expected_kwargs" ,
20
21
[
21
22
(
22
- make_url ("trino://user@localhost" ),
23
+ make_url (trino_url (
24
+ user = "user" ,
25
+ host = "localhost" ,
26
+ )),
27
+ 'trino://user@localhost:8080?source=trino-sqlalchemy' ,
23
28
list (),
24
- dict (host = "localhost" , catalog = "system" , user = "user" , source = "trino-sqlalchemy" ),
29
+ dict (host = "localhost" , catalog = "system" , user = "user" , port = 8080 , source = "trino-sqlalchemy" ),
25
30
),
26
31
(
27
- make_url ("trino://user@localhost:8080" ),
32
+ make_url (trino_url (
33
+ user = "user" ,
34
+ host = "localhost" ,
35
+ port = 443 ,
36
+ )),
37
+ 'trino://user@localhost:443?source=trino-sqlalchemy' ,
28
38
list (),
29
- dict (host = "localhost" , port = 8080 , catalog = "system" , user = "user" , source = "trino-sqlalchemy" ),
39
+ dict (host = "localhost" , port = 443 , catalog = "system" , user = "user" , source = "trino-sqlalchemy" ),
30
40
),
31
41
(
32
- make_url ("trino://user:pass@localhost:8080?source=trino-rulez" ),
42
+ make_url (trino_url (
43
+ user = "user" ,
44
+ password = "pass" ,
45
+ host = "localhost" ,
46
+ source = "trino-rulez" ,
47
+ )),
48
+ 'trino://user:***@localhost:8080?source=trino-rulez' ,
33
49
list (),
34
50
dict (
35
51
host = "localhost" ,
@@ -42,13 +58,64 @@ def setup(self):
42
58
),
43
59
),
44
60
(
45
- make_url (
46
- 'trino://user@localhost:8080?'
47
- 'session_properties={"query_max_run_time": "1d"}'
48
- '&http_headers={"trino": 1}'
49
- '&extra_credential=[("a", "b"), ("c", "d")]'
50
- '&client_tags=[1, "sql"]'
51
- '&experimental_python_types=true' ),
61
+ make_url (trino_url (
62
+ user = "user" ,
63
+ host = "localhost" ,
64
+ cert = "/my/path/to/cert" ,
65
+ key = "afdlsdfk%4#'" ,
66
+ )),
67
+ 'trino://user@localhost:8080'
68
+ '?cert=%2Fmy%2Fpath%2Fto%2Fcert'
69
+ '&key=afdlsdfk%254%23%27'
70
+ '&source=trino-sqlalchemy' ,
71
+ list (),
72
+ dict (
73
+ host = "localhost" ,
74
+ port = 8080 ,
75
+ catalog = "system" ,
76
+ user = "user" ,
77
+ auth = CertificateAuthentication ("/my/path/to/cert" , "afdlsdfk%4#'" ),
78
+ http_scheme = "https" ,
79
+ source = "trino-sqlalchemy"
80
+ ),
81
+ ),
82
+ (
83
+ make_url (trino_url (
84
+ user = "user" ,
85
+ host = "localhost" ,
86
+ access_token = "afdlsdfk%4#'" ,
87
+ )),
88
+ 'trino://user@localhost:8080'
89
+ '?access_token=afdlsdfk%254%23%27'
90
+ '&source=trino-sqlalchemy' ,
91
+ list (),
92
+ dict (
93
+ host = "localhost" ,
94
+ port = 8080 ,
95
+ catalog = "system" ,
96
+ user = "user" ,
97
+ auth = JWTAuthentication ("afdlsdfk%4#'" ),
98
+ http_scheme = "https" ,
99
+ source = "trino-sqlalchemy"
100
+ ),
101
+ ),
102
+ (
103
+ make_url (trino_url (
104
+ user = "user" ,
105
+ host = "localhost" ,
106
+ session_properties = {"query_max_run_time" : "1d" },
107
+ http_headers = {"trino" : 1 },
108
+ extra_credential = [("a" , "b" ), ("c" , "d" )],
109
+ client_tags = ["1" , "sql" ],
110
+ experimental_python_types = True ,
111
+ )),
112
+ 'trino://user@localhost:8080'
113
+ '?client_tags=%5B%221%22%2C+%22sql%22%5D'
114
+ '&experimental_python_types=true'
115
+ '&extra_credential=%5B%28%27a%27%2C+%27b%27%29%2C+%28%27c%27%2C+%27d%27%29%5D'
116
+ '&http_headers=%7B%22trino%22%3A+1%7D'
117
+ '&session_properties=%7B%22query_max_run_time%22%3A+%221d%22%7D'
118
+ '&source=trino-sqlalchemy' ,
52
119
list (),
53
120
dict (
54
121
host = "localhost" ,
@@ -59,13 +126,66 @@ def setup(self):
59
126
session_properties = {"query_max_run_time" : "1d" },
60
127
http_headers = {"trino" : 1 },
61
128
extra_credential = [("a" , "b" ), ("c" , "d" )],
62
- client_tags = [1 , "sql" ],
129
+ client_tags = ["1" , "sql" ],
63
130
experimental_python_types = True ,
64
131
),
65
132
),
133
+ # url encoding
134
+ (
135
+ make_url (trino_url (
136
+
137
+ password = "pass /*&" ,
138
+ host = "localhost" ,
139
+ session_properties = {"query_max_run_time" : "1d" },
140
+ http_headers = {"trino" : 1 },
141
+ extra_credential = [
142
+
143
+
144
+ experimental_python_types = True ,
145
+ client_tags = ["1 @& /\" " , "sql" ],
146
+ verify = False ,
147
+ )),
148
+ 'trino://user%40test.org%2Fmy_role:***@localhost:8080'
149
+ '?client_tags=%5B%221+%40%26+%2F%5C%22%22%2C+%22sql%22%5D'
150
+ '&experimental_python_types=true'
151
+ '&extra_credential=%5B%28%27user1%40test.org%2Fmy_role%27%2C'
152
+ '+%27user2%40test.org%2Fmy_role%27%29%2C'
153
+ '+%28%27user3%40test.org%2Fmy_role%27%2C'
154
+ '+%27user36%40test.org%2Fmy_role%27%29%5D'
155
+ '&http_headers=%7B%22trino%22%3A+1%7D'
156
+ '&session_properties=%7B%22query_max_run_time%22%3A+%221d%22%7D'
157
+ '&source=trino-sqlalchemy'
158
+ '&verify=false' ,
159
+ list (),
160
+ dict (
161
+ host = "localhost" ,
162
+ port = 8080 ,
163
+ catalog = "system" ,
164
+
165
+ auth = BasicAuthentication ("[email protected] /my_role" , "pass /*&" ),
166
+ http_scheme = "https" ,
167
+ source = "trino-sqlalchemy" ,
168
+ session_properties = {"query_max_run_time" : "1d" },
169
+ http_headers = {"trino" : 1 },
170
+ extra_credential = [
171
+
172
+
173
+ experimental_python_types = True ,
174
+ client_tags = ["1 @& /\" " , "sql" ],
175
+ verify = False ,
176
+ ),
177
+ ),
66
178
],
67
179
)
68
- def test_create_connect_args (self , url : URL , expected_args : List [Any ], expected_kwargs : Dict [str , Any ]):
180
+ def test_create_connect_args (
181
+ self ,
182
+ url : URL ,
183
+ generated_url : str ,
184
+ expected_args : List [Any ],
185
+ expected_kwargs : Dict [str , Any ]
186
+ ):
187
+ assert repr (url ) == generated_url
188
+
69
189
actual_args , actual_kwargs = self .dialect .create_connect_args (url )
70
190
71
191
assert actual_args == expected_args
0 commit comments