From 3a2c6b67f39be859eccb11521022a4dc8c417f50 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Tue, 17 Dec 2024 08:08:59 -0500 Subject: [PATCH] test(sqlite): use context managers for all sqlite connections in testing to avoid `ResourceWarning`s starting in python 3.13 --- ibis/backends/duckdb/tests/test_io.py | 17 ++++++++--------- ibis/backends/sqlite/tests/test_types.py | 15 ++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/ibis/backends/duckdb/tests/test_io.py b/ibis/backends/duckdb/tests/test_io.py index 7410807daa38..d9a5f38f6523 100644 --- a/ibis/backends/duckdb/tests/test_io.py +++ b/ibis/backends/duckdb/tests/test_io.py @@ -211,22 +211,21 @@ def test_read_mysql(con, mysqlurl): # pragma: no cover def test_read_sqlite(con, tmp_path): path = tmp_path / "test.db" - sqlite_con = sqlite3.connect(str(path)) - sqlite_con.execute("CREATE TABLE t AS SELECT 1 a UNION SELECT 2 UNION SELECT 3") + with sqlite3.connect(str(path)) as sqlite_con: + sqlite_con.execute("CREATE TABLE t AS SELECT 1 a UNION SELECT 2 UNION SELECT 3") - ft = con.read_sqlite(path, table_name="t") - assert ft.count().execute() + ft = con.read_sqlite(path, table_name="t") + assert ft.count().execute() def test_read_sqlite_no_table_name(con, tmp_path): path = tmp_path / "test.db" - sqlite3.connect(str(path)) + with sqlite3.connect(str(path)) as _: + assert path.exists() - assert path.exists() - - with pytest.raises(ValueError): - con.read_sqlite(path) + with pytest.raises(ValueError): + con.read_sqlite(path) # Because we create a new connection and the test requires loading/installing a diff --git a/ibis/backends/sqlite/tests/test_types.py b/ibis/backends/sqlite/tests/test_types.py index 14f8eeebd9f9..ddc96577ac6e 100644 --- a/ibis/backends/sqlite/tests/test_types.py +++ b/ibis/backends/sqlite/tests/test_types.py @@ -36,16 +36,14 @@ @pytest.fixture(scope="session") def db(tmp_path_factory): path = str(tmp_path_factory.mktemp("databases") / "formats.db") - con = sqlite3.connect(path) - con.execute("CREATE TABLE timestamps (ts TIMESTAMP)") - con.execute("CREATE TABLE timestamps_tz (ts TIMESTAMPTZ)") - con.execute("CREATE TABLE weird (str_col STRING, date_col ITSADATE)") - con.execute("CREATE TABLE basic (a INTEGER, b REAL, c BOOLEAN, d BLOB)") - with con: + with sqlite3.connect(path) as con: + con.execute("CREATE TABLE timestamps (ts TIMESTAMP)") + con.execute("CREATE TABLE timestamps_tz (ts TIMESTAMPTZ)") + con.execute("CREATE TABLE weird (str_col STRING, date_col ITSADATE)") + con.execute("CREATE TABLE basic (a INTEGER, b REAL, c BOOLEAN, d BLOB)") con.executemany("INSERT INTO timestamps VALUES (?)", [(t,) for t in TIMESTAMPS]) con.executemany( - "INSERT INTO timestamps_tz VALUES (?)", - [(t,) for t in TIMESTAMPS_TZ], + "INSERT INTO timestamps_tz VALUES (?)", [(t,) for t in TIMESTAMPS_TZ] ) con.executemany( "INSERT INTO weird VALUES (?, ?)", @@ -56,7 +54,6 @@ def db(tmp_path_factory): ("d", "2022-01-04"), ], ) - con.close() return path