Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
[AIRFLOW-3189] Remove schema from get_uri response if None (apache#6833)
Browse files Browse the repository at this point in the history
"None" was appended to uri if schema=None. Check was added if
schema is None.
  • Loading branch information
zuku1985 authored and galuszkak committed Mar 5, 2020
1 parent 260e4ed commit c928183
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion airflow/hooks/dbapi_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def get_uri(self):
host = conn.host
if conn.port is not None:
host += ':{port}'.format(port=conn.port)
return '{conn.conn_type}://{login}{host}/{conn.schema}'.format(
uri = '{conn.conn_type}://{login}{host}/'.format(
conn=conn, login=login, host=host)
if conn.schema:
uri += conn.schema
return uri

def get_sqlalchemy_engine(self, engine_kwargs=None):
if engine_kwargs is None:
Expand Down
23 changes: 23 additions & 0 deletions tests/hooks/test_dbapi_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from unittest import mock

from airflow.hooks.dbapi_hook import DbApiHook
from airflow.models import Connection


class TestDbApiHook(unittest.TestCase):
Expand Down Expand Up @@ -149,3 +150,25 @@ def test_insert_rows_commit_every(self):
sql = "INSERT INTO {} VALUES (%s)".format(table)
for row in rows:
self.cur.execute.assert_any_call(sql, row)

def test_get_uri_schema_not_none(self):
self.db_hook.get_connection = mock.MagicMock(return_value=Connection(
conn_type="conn_type",
host="host",
login="login",
password="password",
schema="schema",
port=1
))
self.assertEqual("conn_type://login:password@host:1/schema", self.db_hook.get_uri())

def test_get_uri_schema_none(self):
self.db_hook.get_connection = mock.MagicMock(return_value=Connection(
conn_type="conn_type",
host="host",
login="login",
password="password",
schema=None,
port=1
))
self.assertEqual("conn_type://login:password@host:1/", self.db_hook.get_uri())

0 comments on commit c928183

Please sign in to comment.