Claude Code helped fix a ton of .close() warnings, refs #692

https://gistpreview.github.io/?730f0c5dc38528a1dd0615f330bd5481
This commit is contained in:
Simon Willison 2025-12-11 16:23:38 -08:00
commit 81b0599078
6 changed files with 119 additions and 30 deletions

View file

@ -16,7 +16,9 @@ def pytest_configure(config):
@pytest.fixture
def fresh_db():
return Database(memory=True)
db = Database(memory=True)
yield db
db.close()
@pytest.fixture
@ -30,7 +32,8 @@ def existing_db():
INSERT INTO foo (text) values ("three");
"""
)
return database
yield database
database.close()
@pytest.fixture
@ -38,4 +41,5 @@ def db_path(tmpdir):
path = str(tmpdir / "test.db")
db = sqlite3.connect(path)
db.executescript(CREATE_TABLES)
db.close()
return path

View file

@ -44,6 +44,7 @@ def big_db_to_analyze_path(tmpdir):
}
)
db["stuff"].insert_all(to_insert)
db.close()
return path
@ -137,6 +138,7 @@ def db_to_analyze_path(db_to_analyze, tmpdir):
db = sqlite3.connect(path)
sql = "\n".join(db_to_analyze.iterdump())
db.executescript(sql)
db.close()
return path
@ -311,6 +313,7 @@ def test_analyze_table_validate_columns(tmpdir, args, expected_error):
"age": 5,
}
)
db.close()
result = CliRunner().invoke(
cli.cli,
["analyze-tables", path] + args,

View file

@ -82,6 +82,7 @@ def test_tables_counts_and_columns(db_path):
db = Database(db_path)
with db.conn:
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
db.close()
result = CliRunner().invoke(cli.cli, ["tables", "--counts", "--columns", db_path])
assert (
'[{"table": "Gosh", "count": 0, "columns": ["c1", "c2", "c3"]},\n'
@ -117,6 +118,7 @@ def test_tables_counts_and_columns_csv(db_path, format, expected):
db = Database(db_path)
with db.conn:
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
db.close()
result = CliRunner().invoke(
cli.cli, ["tables", "--counts", "--columns", format, db_path]
)
@ -127,6 +129,7 @@ def test_tables_schema(db_path):
db = Database(db_path)
with db.conn:
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
db.close()
result = CliRunner().invoke(cli.cli, ["tables", "--schema", db_path])
assert (
'[{"table": "Gosh", "schema": "CREATE TABLE Gosh (c1 text, c2 text, c3 text)"},\n'
@ -188,6 +191,7 @@ def test_output_table(db_path, options, expected):
for i in range(4)
]
)
db.close()
result = CliRunner().invoke(cli.cli, ["rows", db_path, "rows"] + options)
assert result.exit_code == 0
assert expected == result.output.strip()
@ -243,6 +247,7 @@ def test_create_index(db_path):
CliRunner().invoke(cli.cli, create_index_unique_args + [option]).exit_code
== 0
)
db.close()
def test_create_index_analyze(db_path):
@ -622,6 +627,7 @@ def test_optimize(db_path, tables):
)
db["Gosh"].enable_fts(["c1", "c2", "c3"], fts_version="FTS4")
db["Gosh2"].enable_fts(["c1", "c2", "c3"], fts_version="FTS5")
db.close()
size_before_optimize = os.stat(db_path).st_size
result = CliRunner().invoke(cli.cli, ["optimize", db_path] + tables)
assert result.exit_code == 0
@ -1450,6 +1456,7 @@ def test_drop_table_error():
with runner.isolated_filesystem():
db = Database("test.db")
db["t"].create({"pk": int}, pk="pk")
db.close()
result = runner.invoke(
cli.cli,
[
@ -1474,6 +1481,7 @@ def test_drop_view():
db = Database("test.db")
db.create_view("hello", "select 1")
assert "hello" in db.view_names()
db.close()
result = runner.invoke(
cli.cli,
[
@ -1483,7 +1491,9 @@ def test_drop_view():
],
)
assert result.exit_code == 0
db = Database("test.db")
assert "hello" not in db.view_names()
db.close()
def test_drop_view_error():
@ -1491,6 +1501,7 @@ def test_drop_view_error():
with runner.isolated_filesystem():
db = Database("test.db")
db["t"].create({"pk": int}, pk="pk")
db.close()
result = runner.invoke(
cli.cli,
[
@ -1728,10 +1739,13 @@ def test_transform(db_path, args, expected_schema):
defaults={"age": 1},
pk="id",
)
db.close()
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs"] + args)
print(result.output)
assert result.exit_code == 0
db = Database(db_path)
schema = db["dogs"].schema
db.close()
assert schema == expected_schema

View file

@ -328,9 +328,11 @@ def test_memory_return_db(tmpdir):
from sqlite_utils.cli import cli
path = str(tmpdir / "dogs.csv")
open(path, "w").write("id,name\n1,Cleo")
with open(path, "w") as f:
f.write("id,name\n1,Cleo")
with click.Context(cli) as ctx:
db = ctx.invoke(cli.commands["memory"], paths=(path,), return_db=True)
assert db.table_names() == ["dogs"]
db.close()

View file

@ -6,39 +6,39 @@ import sqlite_utils
# SQLite integers are -(2^63) to 2^63 - 1
@given(st.integers(-9223372036854775808, 9223372036854775807))
def test_roundtrip_integers(integer):
db = sqlite_utils.Database(memory=True)
row = {
"integer": integer,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
with sqlite_utils.Database(memory=True) as db:
row = {
"integer": integer,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
@given(st.text())
def test_roundtrip_text(text):
db = sqlite_utils.Database(memory=True)
row = {
"text": text,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
with sqlite_utils.Database(memory=True) as db:
row = {
"text": text,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
@given(st.binary(max_size=1024 * 1024))
def test_roundtrip_binary(binary):
db = sqlite_utils.Database(memory=True)
row = {
"binary": binary,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
with sqlite_utils.Database(memory=True) as db:
row = {
"binary": binary,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
@given(st.floats(allow_nan=False))
def test_roundtrip_floats(floats):
db = sqlite_utils.Database(memory=True)
row = {
"floats": floats,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]
with sqlite_utils.Database(memory=True) as db:
row = {
"floats": floats,
}
db["test"].insert(row)
assert list(db["test"].rows) == [row]