Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vdk-core: make db_default_type case insensitive #935

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions projects/vdk-core/src/vdk/api/plugin/plugin_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ def vdk_configure(config_builder: ConfigurationBuilder) -> None:
@hookimpl
def initialize_job(context: JobContext) -> None:
context.connections.add_open_connection_factory_method(
"BIG_QUERY", lambda: dbapi.connect(host=cfg.get_value('host'), post=cfg.get_value('port')
"BIG-QUERY", lambda: dbapi.connect(host=cfg.get_value('host'), post=cfg.get_value('port')
)

:param dbtype: the name of the database connection - e.g: redshift, impala, presto, big-query, postgres, etc.
:param dbtype: the name of the database connection (case-insensitive) - e.g: redshift, impala, presto, big-query, postgres, etc.
:param open_connection_func:
The function must implement the code necessary to open a new connection that includes getting all configuration necessary like url, user, etc.
The function can return either original PEP 249 (DBAPI) Connection from the python library for the given database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def add_open_connection_factory_method(
"""
Add new connection factory method. See parent doc for more.
"""
self._connection_builders[dbtype] = open_connection_func
self._connection_builders[dbtype.lower()] = open_connection_func
antoniivanov marked this conversation as resolved.
Show resolved Hide resolved

def open_default_connection(self) -> ManagedConnectionBase:
"""
Expand All @@ -74,6 +74,7 @@ def open_connection(self, dbtype: str) -> ManagedConnectionBase:
or it will thrown an error
:return: the new connection if succesfull or throws an expception
"""
dbtype = dbtype.lower() if dbtype else None
if dbtype in self._connections:
conn = self._connections[dbtype]
if conn._is_connected():
Expand All @@ -94,7 +95,7 @@ def open_connection(self, dbtype: str) -> ManagedConnectionBase:
f"Currently possible values are {list(self._connection_builders.keys())}",
)

def __create_connection(self, dbtype):
def __create_connection(self, dbtype: str):
conn = self._connection_builders[dbtype]()
if isinstance(conn, ManagedConnectionBase):
self._connections[dbtype] = conn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def test_router_open_connection():
assert conn is conn.connect()


def test_router_open_connection_case_insensitive_type():
router, mock_conn, _ = managed_connection_router()

conn = router.open_connection("test_db")
assert conn is conn.connect()


def test_router_raw_connection():
conf = MagicMock(spec=Configuration)
router = ManagedConnectionRouter(conf, MagicMock(spec=ConnectionHookSpec))
Expand Down