Skip to content

Commit 7bd195f

Browse files
committed
test(Cursor, insert_data_bulk): table_name with and without schema
1 parent b199bfa commit 7bd195f

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

test/integration/test_cursor.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from csv import reader
12
from io import StringIO
23
from unittest.mock import mock_open, patch
34

@@ -42,12 +43,6 @@ def test_get_description_multiple_column_names(db_kwargs, col_names) -> None:
4243

4344
@patch("builtins.open", new_callable=mock_open)
4445
def test_insert_data_invalid_column_raises(mocked_csv, db_kwargs) -> None:
45-
indexes, names, exp_execute_args = (
46-
[0],
47-
["col1"],
48-
("INSERT INTO githubissue161 (col1) VALUES (%s), (%s), (%s);", ["1", "2", "-1"]),
49-
)
50-
5146
mocked_csv.side_effect = [StringIO("""\col1,col2,col3\n1,3,foo\n2,5,bar\n-1,7,baz""")]
5247

5348
with redshift_connector.connect(**db_kwargs) as conn:
@@ -61,13 +56,56 @@ def test_insert_data_invalid_column_raises(mocked_csv, db_kwargs) -> None:
6156
cursor.insert_data_bulk(
6257
filename="mocked_csv",
6358
table_name="githubissue161",
64-
parameter_indices=indexes,
59+
parameter_indices=[0],
6560
column_names=["IncorrectColumnName"],
6661
delimiter=",",
6762
batch_size=3,
6863
)
6964

7065

66+
@patch("builtins.open", new_callable=mock_open)
67+
def test_insert_data_invalid_schema_raises(mocked_csv, db_kwargs) -> None:
68+
mocked_csv.side_effect = [StringIO("""\col1,col2,col3\n1,3,foo\n2,5,bar\n-1,7,baz""")]
69+
70+
with redshift_connector.connect(**db_kwargs) as conn:
71+
with conn.cursor() as cursor:
72+
cursor.execute("create table public.githubissue161 (id int)")
73+
74+
with pytest.raises(
75+
InterfaceError,
76+
match="Invalid table name passed to insert_data_bulk: githubissue198.githubissue161",
77+
):
78+
cursor.insert_data_bulk(
79+
filename="mocked_csv",
80+
table_name="githubissue198.githubissue161",
81+
parameter_indices=[0],
82+
column_names=["id"],
83+
delimiter=",",
84+
batch_size=3,
85+
)
86+
87+
88+
def test_insert_data_valid_schema_does_not_raise(db_kwargs) -> None:
89+
with redshift_connector.connect(**db_kwargs) as conn:
90+
with conn.cursor() as cursor:
91+
cursor.execute("create table public.githubissue198 (id int)")
92+
with patch(
93+
"builtins.open", mock_open(read_data="\col1,col2,col3\n1,3,foo\n2,5,bar\n-1,7,baz")
94+
) as mock_file:
95+
cursor.insert_data_bulk(
96+
filename="mocked_csv",
97+
table_name="public.githubissue198",
98+
parameter_indices=[0],
99+
column_names=["id"],
100+
delimiter=",",
101+
batch_size=2,
102+
)
103+
cursor.execute("select * from public.githubissue198 order by id asc")
104+
res = cursor.fetchall()
105+
assert len(res) == 3
106+
assert res == ([-1], [1], [2])
107+
108+
71109
# max binding parameters for a prepared statement
72110
max_params = 32767
73111

0 commit comments

Comments
 (0)