Skip to content

Commit 6e09a05

Browse files
committed
don't provide default username
Signed-off-by: Đặng Minh Dũng <dungdm93@live.com>
1 parent a359387 commit 6e09a05

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

tests/sqlalchemy/test_dialect.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def setup(self):
1919
@pytest.mark.parametrize(
2020
'url, expected_args, expected_kwargs',
2121
[
22-
(make_url('trino://localhost'),
23-
list(), dict(host='localhost', catalog='system', user='anonymous')),
24-
(make_url('trino://1.2.3.4:4321/mysql/sakila'),
25-
list(), dict(host='1.2.3.4', port=4321, catalog='mysql', schema='sakila', user='anonymous')),
22+
(make_url('trino://user@localhost'),
23+
list(), dict(host='localhost', catalog='system', user='user')),
24+
2625
(make_url('trino://user@localhost:8080'),
2726
list(), dict(host='localhost', port=8080, catalog='system', user='user')),
27+
2828
(make_url('trino://user:pass@localhost:8080'),
2929
list(), dict(host='localhost', port=8080, catalog='system', user='user',
3030
auth=BasicAuthentication('user', 'pass'), http_scheme='https')),
@@ -36,6 +36,18 @@ def test_create_connect_args(self, url: URL, expected_args: List[Any], expected_
3636
assert_that(actual_args).is_equal_to(expected_args)
3737
assert_that(actual_kwargs).is_equal_to(expected_kwargs)
3838

39+
def test_create_connect_args_missing_user_when_specify_password(self):
40+
url = make_url('trino://:pass@localhost')
41+
assert_that(self.dialect.create_connect_args).raises(ValueError) \
42+
.when_called_with(url) \
43+
.is_equal_to('Username is required when specify password in connection URL')
44+
45+
def test_create_connect_args_wrong_db_format(self):
46+
url = make_url('trino://abc@localhost/catalog/schema/foobar')
47+
assert_that(self.dialect.create_connect_args).raises(ValueError) \
48+
.when_called_with(url) \
49+
.is_equal_to('Unexpected database format catalog/schema/foobar')
50+
3951
def test_get_default_isolation_level(self):
4052
isolation_level = self.dialect.get_default_isolation_level(mock.Mock())
4153
assert_that(isolation_level).is_equal_to('AUTOCOMMIT')

trino/sqlalchemy/dialect.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
from textwrap import dedent
13-
from typing import Any, Dict, List, Optional, Tuple
13+
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple
1414

1515
from sqlalchemy import exc, sql
1616
from sqlalchemy.engine.base import Connection
@@ -58,10 +58,14 @@ def dbapi(cls):
5858
"""
5959
return trino_dbapi
6060

61-
def create_connect_args(self, url: URL) -> Tuple[List[Any], Dict[str, Any]]:
62-
args, kwargs = super(TrinoDialect, self).create_connect_args(url) # type: List[Any], Dict[str, Any]
61+
def create_connect_args(self, url: URL) -> Tuple[Sequence[Any], Mapping[str, Any]]:
62+
args = list()
63+
kwargs = dict(host=url.host)
6364

64-
db_parts = kwargs.pop('database', 'system').split('/')
65+
if url.port:
66+
kwargs['port'] = url.port
67+
68+
db_parts = (url.database or 'system').split('/')
6569
if len(db_parts) == 1:
6670
kwargs['catalog'] = db_parts[0]
6771
elif len(db_parts) == 2:
@@ -70,13 +74,14 @@ def create_connect_args(self, url: URL) -> Tuple[List[Any], Dict[str, Any]]:
7074
else:
7175
raise ValueError(f'Unexpected database format {url.database}')
7276

73-
username = kwargs.pop('username', 'anonymous')
74-
kwargs['user'] = username
77+
if url.username:
78+
kwargs['user'] = url.username
7579

76-
password = kwargs.pop('password', None)
77-
if password:
80+
if url.password:
81+
if not url.username:
82+
raise ValueError(f'Username is required when specify password in connection URL')
7883
kwargs['http_scheme'] = 'https'
79-
kwargs['auth'] = BasicAuthentication(username, password)
84+
kwargs['auth'] = BasicAuthentication(url.username, url.password)
8085

8186
return args, kwargs
8287

0 commit comments

Comments
 (0)