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,44 @@ 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+ session_properties = {"query_max_run_time" : "1d" },
87+ http_headers = {"trino" : 1 },
88+ extra_credential = [("a" , "b" ), ("c" , "d" )],
89+ client_tags = ["1" , "sql" ],
90+ experimental_python_types = True ,
91+ )),
92+ 'trino://user@localhost:8080'
93+ '?client_tags=%5B%221%22%2C+%22sql%22%5D'
94+ '&experimental_python_types=true'
95+ '&extra_credential=%5B%28%27a%27%2C+%27b%27%29%2C+%28%27c%27%2C+%27d%27%29%5D'
96+ '&http_headers=%7B%22trino%22%3A+1%7D'
97+ '&session_properties=%7B%22query_max_run_time%22%3A+%221d%22%7D'
98+ '&source=trino-sqlalchemy' ,
5299 list (),
53100 dict (
54101 host = "localhost" ,
@@ -59,13 +106,66 @@ def setup(self):
59106 session_properties = {"query_max_run_time" : "1d" },
60107 http_headers = {"trino" : 1 },
61108 extra_credential = [("a" , "b" ), ("c" , "d" )],
62- client_tags = [1 , "sql" ],
109+ client_tags = ["1" , "sql" ],
63110 experimental_python_types = True ,
64111 ),
65112 ),
113+ # url encoding
114+ (
115+ make_url (trino_url (
116+ 117+ password = "pass /*&" ,
118+ host = "localhost" ,
119+ session_properties = {"query_max_run_time" : "1d" },
120+ http_headers = {"trino" : 1 },
121+ extra_credential = [
122+ 123+ 124+ experimental_python_types = True ,
125+ client_tags = ["1 @& /\" " , "sql" ],
126+ verify = False ,
127+ )),
128+ 'trino://user%40test.org%2Fmy_role:***@localhost:8080'
129+ '?client_tags=%5B%221+%40%26+%2F%5C%22%22%2C+%22sql%22%5D'
130+ '&experimental_python_types=true'
131+ '&extra_credential=%5B%28%27user1%40test.org%2Fmy_role%27%2C'
132+ '+%27user2%40test.org%2Fmy_role%27%29%2C'
133+ '+%28%27user3%40test.org%2Fmy_role%27%2C'
134+ '+%27user36%40test.org%2Fmy_role%27%29%5D'
135+ '&http_headers=%7B%22trino%22%3A+1%7D'
136+ '&session_properties=%7B%22query_max_run_time%22%3A+%221d%22%7D'
137+ '&source=trino-sqlalchemy'
138+ '&verify=false' ,
139+ list (),
140+ dict (
141+ host = "localhost" ,
142+ port = 8080 ,
143+ catalog = "system" ,
144+ 145+ auth = BasicAuthentication ("[email protected] /my_role" , "pass /*&" ), 146+ http_scheme = "https" ,
147+ source = "trino-sqlalchemy" ,
148+ session_properties = {"query_max_run_time" : "1d" },
149+ http_headers = {"trino" : 1 },
150+ extra_credential = [
151+ 152+ 153+ experimental_python_types = True ,
154+ client_tags = ["1 @& /\" " , "sql" ],
155+ verify = False ,
156+ ),
157+ ),
66158 ],
67159 )
68- def test_create_connect_args (self , url : URL , expected_args : List [Any ], expected_kwargs : Dict [str , Any ]):
160+ def test_create_connect_args (
161+ self ,
162+ url : URL ,
163+ generated_url : str ,
164+ expected_args : List [Any ],
165+ expected_kwargs : Dict [str , Any ]
166+ ):
167+ assert repr (url ) == generated_url
168+
69169 actual_args , actual_kwargs = self .dialect .create_connect_args (url )
70170
71171 assert actual_args == expected_args
0 commit comments