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

fix for boolean type conversion #78

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/airflow_clickhouse_plugin/hooks/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ def execute(
return last_result


def strtobool(str_value: str) -> bool:
str_value = str_value.lower()
if str_value == 'true':
return True
elif str_value == 'false':
return False
raise ValueError(f'unsupported string value: {str_value!r}')


def conn_to_kwargs(conn: Connection, database: t.Optional[str]) -> t.Dict[str, t.Any]:
""" Translate Airflow Connection to clickhouse-driver Connection kwargs. """
connection_kwargs = conn.extra_dejson.copy()
Expand All @@ -105,6 +114,13 @@ def conn_to_kwargs(conn: Connection, database: t.Optional[str]) -> t.Dict[str, t
connection_kwargs.update(database=database)
elif conn.schema:
connection_kwargs.update(database=conn.schema)
# converting types for flags
for key in ('secure', 'verify'):
if key in connection_kwargs:
current_value = connection_kwargs[key]
if isinstance(current_value, (bool, int)):
continue
connection_kwargs[key] = strtobool(current_value)
return connection_kwargs


Expand Down
4 changes: 3 additions & 1 deletion tests/unit/hooks/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_arguments(self):
login='test-login',
password='test-pass',
schema='test-schema',
extra='{"test_extra": "test-extra-value"}',
extra='{"test_extra": "test-extra-value", "secure": "true", "verify": "false"}',
)

return_value = ClickHouseHook(
Expand Down Expand Up @@ -47,6 +47,8 @@ def test_arguments(self):
password='test-pass',
database='test-database',
test_extra='test-extra-value',
secure=True,
verify=False,
)

for query, mock_call \
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/hooks/test_strtobool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from airflow_clickhouse_plugin.hooks.clickhouse import strtobool


class StrToBoolTestCase(unittest.TestCase):
def test_correct_true(self):
for v in ['true', 'True']:
with self.subTest('Testing %s value' % v, v=v):
self.assertTrue(strtobool(v))

def test_correct_false(self):
for v in ['false', 'False']:
with self.subTest('Testing %s value' % v, v=v):
self.assertFalse(strtobool(v))

def test_unknown_throws(self):
with self.assertRaises(ValueError):
strtobool('unknown')


if __name__ == '__main__':
unittest.main()