99from trino .dbapi import Connection
1010from trino .sqlalchemy .dialect import CertificateAuthentication , JWTAuthentication , TrinoDialect
1111from trino .transaction import IsolationLevel
12+ from trino .sqlalchemy import URL as trino_url
1213
1314
1415class TestTrinoDialect :
1516 def setup (self ):
1617 self .dialect = TrinoDialect ()
1718
1819 @pytest .mark .parametrize (
19- "url, expected_args, expected_kwargs" ,
20+ "url, generated_url, expected_args, expected_kwargs" ,
2021 [
2122 (
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' ,
2328 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" ),
2530 ),
2631 (
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' ,
2838 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" ),
3040 ),
3141 (
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' ,
3349 list (),
3450 dict (
3551 host = "localhost" ,
@@ -42,13 +58,64 @@ def setup(self):
4258 ),
4359 ),
4460 (
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' ,
52119 list (),
53120 dict (
54121 host = "localhost" ,
@@ -59,13 +126,66 @@ def setup(self):
59126 session_properties = {"query_max_run_time" : "1d" },
60127 http_headers = {"trino" : 1 },
61128 extra_credential = [("a" , "b" ), ("c" , "d" )],
62- client_tags = [1 , "sql" ],
129+ client_tags = ["1" , "sql" ],
63130 experimental_python_types = True ,
64131 ),
65132 ),
133+ # url encoding
134+ (
135+ make_url (trino_url (
136+ user = "user@test.org/my_role" ,
137+ password = "pass /*&" ,
138+ host = "localhost" ,
139+ session_properties = {"query_max_run_time" : "1d" },
140+ http_headers = {"trino" : 1 },
141+ extra_credential = [
142+ ("user1@test.org/my_role" , "user2@test.org/my_role" ),
143+ ("user3@test.org/my_role" , "user36@test.org/my_role" )],
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+ user = "user@test.org/my_role" ,
165+ auth = BasicAuthentication ("user@test.org/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+ ("user1@test.org/my_role" , "user2@test.org/my_role" ),
172+ ("user3@test.org/my_role" , "user36@test.org/my_role" )],
173+ experimental_python_types = True ,
174+ client_tags = ["1 @& /\" " , "sql" ],
175+ verify = False ,
176+ ),
177+ ),
66178 ],
67179 )
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+
69189 actual_args , actual_kwargs = self .dialect .create_connect_args (url )
70190
71191 assert actual_args == expected_args
0 commit comments