Skip to content

Commit a74c591

Browse files
committed
reworked tests to include sqlite cases
1 parent 4213cc2 commit a74c591

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

pandas/io/sql.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,7 @@ def delete_rows(self, name: str, schema: str | None = None) -> None:
24262426
table_name = f"{schema}.{name}" if schema else name
24272427
if self.has_table(name, schema):
24282428
with self.con.cursor() as cur:
2429-
cur.execute(f"DELETE FROM {table_name)")
2429+
cur.execute(f"DELETE FROM {table_name}")
24302430

24312431
def _create_sql_schema(
24322432
self,
@@ -2878,8 +2878,7 @@ def drop_table(self, name: str, schema: str | None = None) -> None:
28782878
def delete_rows(self, name: str, schema: str | None = None) -> None:
28792879
delete_sql = f"DELETE FROM {_get_valid_sqlite_name(name)}"
28802880
if self.has_table(name, schema):
2881-
with self.run_transaction() as cur:
2882-
cur.execute(delete_sql)
2881+
self.execute(delete_sql)
28832882

28842883
def _create_sql_schema(
28852884
self,

pandas/tests/io/test_sql.py

+24-20
Original file line numberDiff line numberDiff line change
@@ -2716,36 +2716,40 @@ def test_delete_rows_success(conn, test_frame1, request):
27162716
assert pandasSQL.has_table("temp_frame")
27172717

27182718

2719-
@pytest.mark.parametrize("conn", all_connectable)
2720-
def test_delete_rows_is_atomic(conn, request):
2719+
@pytest.mark.parametrize("conn_name", all_connectable)
2720+
def test_delete_rows_is_atomic(conn_name, request):
27212721
adbc_driver_manager = pytest.importorskip("adbc_driver_manager")
27222722
sqlalchemy = pytest.importorskip("sqlalchemy")
27232723

2724-
if "sqlite" in conn:
2725-
reason = "This test relies on strict column types, SQLite has a dynamic one"
2726-
request.applymarker(
2727-
pytest.mark.xfail(
2728-
reason=reason,
2729-
strict=True,
2730-
)
2731-
)
2732-
27332724
table_name = "temp_frame"
2734-
original_df = DataFrame({"a": [1, 2, 3]})
2735-
replacing_df = DataFrame({"a": ["a", "b", "c", "d"]})
2725+
table_stmt = f"CREATE TABLE {table_name} (a INTEGER, b INTEGER UNIQUE NOT NULL)"
27362726

2737-
conn = request.getfixturevalue(conn)
2727+
if conn_name != "sqlite_buildin" and "adbc" not in conn_name:
2728+
table_stmt = sqlalchemy.text(table_stmt)
2729+
2730+
# setting dtype is mandatory for adbc related tests
2731+
original_df = DataFrame({"a": [1, 2], "b": [3, 4]}, dtype="int32")
2732+
replacing_df = DataFrame({"a": [5, 6], "b": [7, 7]}, dtype="int32")
2733+
2734+
conn = request.getfixturevalue(conn_name)
27382735
pandasSQL = pandasSQL_builder(conn)
27392736

2740-
if isinstance(conn, adbc_driver_manager.dbapi.Connection):
2737+
with pandasSQL.run_transaction() as cur:
2738+
cur.execute(table_stmt)
2739+
2740+
if conn_name != "sqlite_buildin" and "adbc" not in conn_name:
2741+
expected_exception = sqlalchemy.exc.IntegrityError
2742+
elif "adbc" in conn_name and "sqlite" in conn_name:
2743+
expected_exception = adbc_driver_manager.InternalError
2744+
elif "adbc" in conn_name and "postgres" in conn_name:
27412745
expected_exception = adbc_driver_manager.ProgrammingError
2742-
else:
2743-
expected_exception = sqlalchemy.exc.DataError
2746+
elif conn_name == "sqlite_buildin":
2747+
expected_exception = sqlite3.IntegrityError
27442748

27452749
with pandasSQL.run_transaction():
2746-
pandasSQL.to_sql(original_df, table_name, if_exists="fail", index=False)
2750+
pandasSQL.to_sql(original_df, table_name, if_exists="append", index=False)
27472751

2748-
# trying to insert strings in an integer column
2752+
# inserting duplicated values in a UNIQUE constraint column
27492753
with pytest.raises(expected_exception):
27502754
with pandasSQL.run_transaction():
27512755
pandasSQL.to_sql(
@@ -2754,7 +2758,7 @@ def test_delete_rows_is_atomic(conn, request):
27542758

27552759
# failed "delete_rows" is rolled back preserving original data
27562760
with pandasSQL.run_transaction():
2757-
result_df = pandasSQL.read_query(f"SELECT * FROM {table_name}")
2761+
result_df = pandasSQL.read_query(f"SELECT * FROM {table_name}", dtype="int32")
27582762
tm.assert_frame_equal(result_df, original_df)
27592763

27602764

0 commit comments

Comments
 (0)