|
4 | 4 | import pytest # type: ignore
|
5 | 5 |
|
6 | 6 | import redshift_connector
|
7 |
| -from redshift_connector import InterfaceError |
| 7 | +from redshift_connector import InterfaceError, DataError |
8 | 8 |
|
9 | 9 |
|
10 | 10 | @pytest.mark.parametrize("col_name", (("apples", "apples"), ("author ", "author\u200e")))
|
@@ -66,3 +66,130 @@ def test_insert_data_invalid_column_raises(mocked_csv, db_kwargs):
|
66 | 66 | delimiter=",",
|
67 | 67 | batch_size=3,
|
68 | 68 | )
|
| 69 | + |
| 70 | + |
| 71 | +# max binding parameters for a prepared statement |
| 72 | +max_params = 32767 |
| 73 | + |
| 74 | +def test_insert_data_raises_too_many_params(db_kwargs): |
| 75 | + prepared_stmt = ( |
| 76 | + "INSERT INTO githubissue165 (col1) VALUES " + "(%s), " * max_params + "(%s);" |
| 77 | + ) |
| 78 | + params = [1 for _ in range(max_params + 1)] |
| 79 | + |
| 80 | + with redshift_connector.connect(**db_kwargs) as conn: |
| 81 | + with conn.cursor() as cursor: |
| 82 | + cursor.execute("create temporary table githubissue165 (col1 int)") |
| 83 | + |
| 84 | + with pytest.raises( |
| 85 | + DataError, |
| 86 | + match=f"Prepared statement exceeds bind parameter limit 32767. {32768} bind parameters were " |
| 87 | + f"provided.", |
| 88 | + ): |
| 89 | + cursor.execute(prepared_stmt, params) |
| 90 | + |
| 91 | + |
| 92 | +def test_insert_data_raises_no_exception(db_kwargs): |
| 93 | + prepared_stmt_32767 = ( |
| 94 | + "INSERT INTO githubissue165 (col1) VALUES " |
| 95 | + + "(%s), " * (max_params - 1) |
| 96 | + + "(%s);" |
| 97 | + ) |
| 98 | + params_32767 = [1 for _ in range(max_params)] |
| 99 | + |
| 100 | + prepared_stmt_32766 = ( |
| 101 | + "INSERT INTO githubissue165 (col1) VALUES " |
| 102 | + + "(%s), " * (max_params - 2) |
| 103 | + + "(%s);" |
| 104 | + ) |
| 105 | + params_32766 = [1 for _ in range(max_params - 1)] |
| 106 | + |
| 107 | + with redshift_connector.connect(**db_kwargs) as conn: |
| 108 | + with conn.cursor() as cursor: |
| 109 | + cursor.execute("create temporary table githubissue165 (col1 int)") |
| 110 | + try: |
| 111 | + cursor.execute(prepared_stmt_32767, params_32767) |
| 112 | + except Exception as e: |
| 113 | + assert ( |
| 114 | + False |
| 115 | + ), f"'execute' with {max_params} bind parameters raised an exception {e}" |
| 116 | + try: |
| 117 | + cursor.execute(prepared_stmt_32766, params_32766) |
| 118 | + except Exception as e: |
| 119 | + assert ( |
| 120 | + False |
| 121 | + ), f"'execute' with {max_params - 1} bind parameters raised an exception {e}" |
| 122 | + |
| 123 | + |
| 124 | +indices, names = ( |
| 125 | + [0], |
| 126 | + ["col1"], |
| 127 | +) |
| 128 | + |
| 129 | + |
| 130 | +@patch("builtins.open", new_callable=mock_open) |
| 131 | +def test_insert_data_bulk_raises_too_many_params(mocked_csv, db_kwargs): |
| 132 | + csv_str = "\col1\n" + "1\n" * max_params + "1" # 32768 rows |
| 133 | + mocked_csv.side_effect = [StringIO(csv_str)] |
| 134 | + |
| 135 | + with redshift_connector.connect(**db_kwargs) as conn: |
| 136 | + with conn.cursor() as cursor: |
| 137 | + cursor.execute("create temporary table githubissue165 (col1 int)") |
| 138 | + with pytest.raises( |
| 139 | + DataError, |
| 140 | + match="Prepared statement exceeds bind parameter limit 32767.", |
| 141 | + ): |
| 142 | + cursor.insert_data_bulk( |
| 143 | + filename="mocked_csv", |
| 144 | + table_name="githubissue165", |
| 145 | + parameter_indices=indices, |
| 146 | + column_names=["col1"], |
| 147 | + delimiter=",", |
| 148 | + batch_size=max_params + 1, |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +@patch("builtins.open", new_callable=mock_open) |
| 153 | +def test_insert_data_bulk_raises_no_exception_32766(mocked_csv_32766, db_kwargs): |
| 154 | + csv_str_32766 = "\col1\n" + "1\n" * (max_params - 2) + "1" |
| 155 | + mocked_csv_32766.side_effect = [StringIO(csv_str_32766)] |
| 156 | + |
| 157 | + with redshift_connector.connect(**db_kwargs) as conn: |
| 158 | + with conn.cursor() as cursor: |
| 159 | + cursor.execute("create temporary table githubissue165 (col1 int)") |
| 160 | + try: |
| 161 | + cursor.insert_data_bulk( |
| 162 | + filename="mocked_csv_32766", |
| 163 | + table_name="githubissue165", |
| 164 | + parameter_indices=indices, |
| 165 | + column_names=["col1"], |
| 166 | + delimiter=",", |
| 167 | + batch_size=max_params - 1, |
| 168 | + ) |
| 169 | + except Exception as e: |
| 170 | + assert ( |
| 171 | + False |
| 172 | + ), f"'insert_data_bulk' with {max_params - 1} bind parameters raised an exception {e}" |
| 173 | + |
| 174 | + |
| 175 | +@patch("builtins.open", new_callable=mock_open) |
| 176 | +def test_insert_data_bulk_raises_no_exception_32767(mocked_csv_32767, db_kwargs): |
| 177 | + csv_str_32767 = "\col1\n" + "1\n" * (max_params - 1) + "1" |
| 178 | + mocked_csv_32767.side_effect = [StringIO(csv_str_32767)] |
| 179 | + |
| 180 | + with redshift_connector.connect(**db_kwargs) as conn: |
| 181 | + with conn.cursor() as cursor: |
| 182 | + cursor.execute("create temporary table githubissue165 (col1 int)") |
| 183 | + try: |
| 184 | + cursor.insert_data_bulk( |
| 185 | + filename="mocked_csv_32767", |
| 186 | + table_name="githubissue165", |
| 187 | + parameter_indices=indices, |
| 188 | + column_names=["col1"], |
| 189 | + delimiter=",", |
| 190 | + batch_size=max_params, |
| 191 | + ) |
| 192 | + except Exception as e: |
| 193 | + assert ( |
| 194 | + False |
| 195 | + ), f"'insert_data_bulk' with {max_params} bind parameters raised an exception {e}" |
0 commit comments