Skip to content

Commit

Permalink
Add test for error with dicts and executemany
Browse files Browse the repository at this point in the history
  • Loading branch information
volcan01010 committed Aug 28, 2023
1 parent bea64cb commit 868ceda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion etlhelper/db_helpers/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ def executemany(self, cursor, query, chunk):
cursor.executemany(query, chunk)
except TypeError:
msg = ("pyodbc driver for MS SQL only supports positional placeholders. "
"Try again with namedtuple, tuple or list row_factory.")
"Use namedtuple, tuple or list (via row_factory setting for copy_rows).")
raise ETLHelperInsertError(msg)
20 changes: 20 additions & 0 deletions test/integration/db/test_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
copy_rows,
copy_table_rows,
execute,
executemany,
fetchall,
generate_insert_sql,
load,
Expand Down Expand Up @@ -199,6 +200,25 @@ def test_copy_rows_bad_param_style(test_tables, testdb_conn):
row_factory=namedtuple_row_factory)


def test_executemany_dicts_raises_error(testdb_conn, test_tables, test_table_data_dict):
# Arrange
# Placeholder doesn't really matter as pydodbc doesn't support
# named placeholders
insert_sql = dedent("""
INSERT INTO dest (id, value, simple_text, utf8_text, day, date_time)
VALUES (:id, :value, :simple_text, :utf8_text, :day, :date_time)
;""").strip()
expected_message = ("pyodbc driver for MS SQL only supports positional placeholders. "
"Use namedtuple, tuple or list (via row_factory setting for copy_rows).")

# Act and assert
# pyodbc doesn't support named parameters.
with pytest.raises(ETLHelperInsertError) as exc_info:
executemany(insert_sql, testdb_conn, test_table_data_dict)

assert str(exc_info.value) == expected_message


def test_load_namedtuples(testdb_conn, test_tables, test_table_data_namedtuple):
# Act
load('dest', testdb_conn, test_table_data_namedtuple)
Expand Down

0 comments on commit 868ceda

Please sign in to comment.