From 64e8a053c3757eb7973f819983580db75c31a424 Mon Sep 17 00:00:00 2001 From: Julien Pinchelimouroux Date: Fri, 21 Jun 2024 10:59:01 +0200 Subject: [PATCH] fix: backport oracle connector jinja fix (#1682) * fix: support jinja templates & fix string oracle fixtures (#1677) * doc: add changelog entry * style: format test file --- CHANGELOG.md | 4 ++ tests/oracle_sql/fixtures/world.sql | 2 +- tests/oracle_sql/test_oracle_sql.py | 39 +++++++++++++++++-- .../oracle_sql/oracle_sql_connector.py | 2 +- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef0ac3885..eca437928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fix + +- OracleSQL: Fix jinja templates and test string fixtures + ## [3.23.30] 2024-06-13 ### Changed diff --git a/tests/oracle_sql/fixtures/world.sql b/tests/oracle_sql/fixtures/world.sql index 7fa4ec951..d63d24fc4 100644 --- a/tests/oracle_sql/fixtures/world.sql +++ b/tests/oracle_sql/fixtures/world.sql @@ -1,6 +1,6 @@ CREATE TABLE SYSTEM.City ( ID int, - Name char(35), + Name varchar(35), CountryCode char(3), District char(20), Population int diff --git a/tests/oracle_sql/test_oracle_sql.py b/tests/oracle_sql/test_oracle_sql.py index 66a817252..60cdddefb 100644 --- a/tests/oracle_sql/test_oracle_sql.py +++ b/tests/oracle_sql/test_oracle_sql.py @@ -115,6 +115,27 @@ def test_oracle_get_df_with_variables(mocker): ) +def test_oracle_get_df_with_variables_jinja_syntax(mocker): + """It should connect to the database and retrieve the response to the query""" + snock = mocker.patch('cx_Oracle.connect') + reasq = mocker.patch('pandas.read_sql') + oracle_connector = OracleSQLConnector( + name='my_oracle_sql_con', user='system', password='oracle', dsn='localhost:22/xe' + ) + ds = OracleSQLDataSource( + query='SELECT * FROM City WHERE Name = {{ __front_var_0__ }}', + domain='Oracle test', + name='my_oracle_sql_con', + table='Cities', + parameters={'__front_var_0__': 'Kabul'}, + ) + oracle_connector.get_df(ds) + snock.assert_called_once_with(user='system', password='oracle', dsn='localhost:22/xe') + reasq.assert_called_once_with( + 'SELECT * FROM City WHERE Name = :1', con=snock(), params=['Kabul'] + ) + + def test_get_df_db(oracle_connector): """It should extract the table City and make some merge with some foreign key""" data_sources_spec = [ @@ -136,15 +157,27 @@ def test_get_df_db(oracle_connector): assert len(df[df['POPULATION'] > 500000]) == 5 -def test_get_df_db_with_variable(oracle_connector): +@pytest.mark.parametrize( + 'query,parameters', + [ + ('SELECT * FROM City WHERE population < %(population)s;', {'population': 2346}), + ('SELECT * FROM City WHERE population < {{ __front_var_0__ }}', {'__front_var_0__': 2346}), + ('SELECT * FROM City WHERE Name = %(name)s;', {'name': 'Willemstad'}), + ( + 'SELECT * FROM City WHERE Name = {{ __front_var_0__ }}', + {'__front_var_0__': 'Willemstad'}, + ), + ], +) +def test_get_df_db_with_variable(oracle_connector, query, parameters): """It should extract the table City and make some merge with some foreign key""" data_sources_spec = [ { 'domain': 'Oracle test', 'type': 'external_database', 'name': 'my_oracle_sql_con', - 'query': 'SELECT * FROM City WHERE population < %(population)s;', - 'parameters': {'population': 2346}, + 'query': query, + 'parameters': parameters, } ] diff --git a/toucan_connectors/oracle_sql/oracle_sql_connector.py b/toucan_connectors/oracle_sql/oracle_sql_connector.py index 6ddc297f9..cf960d6b2 100644 --- a/toucan_connectors/oracle_sql/oracle_sql_connector.py +++ b/toucan_connectors/oracle_sql/oracle_sql_connector.py @@ -85,7 +85,7 @@ def _retrieve_data(self, data_source: OracleSQLDataSource) -> pd.DataFrame: con=connection, params=query_params, convert_to_numeric=True, - convert_to_printf=False, + convert_to_printf=True, ) connection.close()