Skip to content

Commit

Permalink
fix: backport oracle connector jinja fix (#1682)
Browse files Browse the repository at this point in the history
* fix: support jinja templates & fix string oracle fixtures (#1677)

* doc: add changelog entry

* style: format test file
  • Loading branch information
julien-pinchelimouroux authored Jun 21, 2024
1 parent 7716daa commit 64e8a05
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fix

- OracleSQL: Fix jinja templates and test string fixtures

## [3.23.30] 2024-06-13

### Changed
Expand Down
2 changes: 1 addition & 1 deletion tests/oracle_sql/fixtures/world.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE SYSTEM.City (
ID int,
Name char(35),
Name varchar(35),
CountryCode char(3),
District char(20),
Population int
Expand Down
39 changes: 36 additions & 3 deletions tests/oracle_sql/test_oracle_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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,
}
]

Expand Down
2 changes: 1 addition & 1 deletion toucan_connectors/oracle_sql/oracle_sql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 64e8a05

Please sign in to comment.