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%5B%22a%22%2C+%22b%22%5D%2C+%5B%22c%22%2C+%22d%22%5D%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,23 +126,87 @@ 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
66
134
(
67
- make_url ('trino://user@localhost:8080?roles={"hive":"finance","system":"analyst"}' ),
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%5B%22user1%40test.org%2Fmy_role%22%2C+'
152
+ '%22user2%40test.org%2Fmy_role%22%5D%2C+'
153
+ '%5B%22user3%40test.org%2Fmy_role%22%2C+'
154
+ '%22user36%40test.org%2Fmy_role%22%5D%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' ,
68
159
list (),
69
- dict (host = "localhost" ,
70
- port = 8080 ,
71
- catalog = "system" ,
72
- user = "user" ,
73
- roles = {"hive" : "finance" , "system" : "analyst" },
74
- source = "trino-sqlalchemy" ),
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
+ ),
178
+ (
179
+ make_url (trino_url (
180
+ user = "user" ,
181
+ host = "localhost" ,
182
+ roles = {
183
+ "hive" : "finance" ,
184
+ "system" : "analyst" ,
185
+ }
186
+ )),
187
+ 'trino://user@localhost:8080'
188
+ '?roles=%7B%22hive%22%3A+%22finance%22%2C+%22system%22%3A+%22analyst%22%7D&source=trino-sqlalchemy' ,
189
+ list (),
190
+ dict (
191
+ host = "localhost" ,
192
+ port = 8080 ,
193
+ catalog = "system" ,
194
+ user = "user" ,
195
+ roles = {"hive" : "finance" , "system" : "analyst" },
196
+ source = "trino-sqlalchemy" ,
197
+ ),
75
198
),
76
199
],
77
200
)
78
- def test_create_connect_args (self , url : URL , expected_args : List [Any ], expected_kwargs : Dict [str , Any ]):
201
+ def test_create_connect_args (
202
+ self ,
203
+ url : URL ,
204
+ generated_url : str ,
205
+ expected_args : List [Any ],
206
+ expected_kwargs : Dict [str , Any ]
207
+ ):
208
+ assert repr (url ) == generated_url
209
+
79
210
actual_args , actual_kwargs = self .dialect .create_connect_args (url )
80
211
81
212
assert actual_args == expected_args
0 commit comments