Skip to content

Commit 77359be

Browse files
committed
New autouse fixture to help with test warnings
Refs #692 (comment)
1 parent 81b0599 commit 77359be

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

tests/conftest.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,29 @@ def pytest_configure(config):
1414
sys._called_from_test = True
1515

1616

17+
@pytest.fixture(autouse=True)
18+
def close_all_databases():
19+
"""Automatically close all Database objects created during a test."""
20+
databases = []
21+
original_init = Database.__init__
22+
23+
def tracking_init(self, *args, **kwargs):
24+
original_init(self, *args, **kwargs)
25+
databases.append(self)
26+
27+
Database.__init__ = tracking_init
28+
yield
29+
Database.__init__ = original_init
30+
for db in databases:
31+
try:
32+
db.close()
33+
except Exception:
34+
pass
35+
36+
1737
@pytest.fixture
1838
def fresh_db():
19-
db = Database(memory=True)
20-
yield db
21-
db.close()
39+
return Database(memory=True)
2240

2341

2442
@pytest.fixture
@@ -32,8 +50,7 @@ def existing_db():
3250
INSERT INTO foo (text) values ("three");
3351
"""
3452
)
35-
yield database
36-
database.close()
53+
return database
3754

3855

3956
@pytest.fixture

tests/test_analyze_tables.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def big_db_to_analyze_path(tmpdir):
4444
}
4545
)
4646
db["stuff"].insert_all(to_insert)
47-
db.close()
4847
return path
4948

5049

@@ -313,7 +312,6 @@ def test_analyze_table_validate_columns(tmpdir, args, expected_error):
313312
"age": 5,
314313
}
315314
)
316-
db.close()
317315
result = CliRunner().invoke(
318316
cli.cli,
319317
["analyze-tables", path] + args,

tests/test_cli.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def test_tables_counts_and_columns(db_path):
8282
db = Database(db_path)
8383
with db.conn:
8484
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
85-
db.close()
8685
result = CliRunner().invoke(cli.cli, ["tables", "--counts", "--columns", db_path])
8786
assert (
8887
'[{"table": "Gosh", "count": 0, "columns": ["c1", "c2", "c3"]},\n'
@@ -118,7 +117,6 @@ def test_tables_counts_and_columns_csv(db_path, format, expected):
118117
db = Database(db_path)
119118
with db.conn:
120119
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
121-
db.close()
122120
result = CliRunner().invoke(
123121
cli.cli, ["tables", "--counts", "--columns", format, db_path]
124122
)
@@ -129,7 +127,6 @@ def test_tables_schema(db_path):
129127
db = Database(db_path)
130128
with db.conn:
131129
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
132-
db.close()
133130
result = CliRunner().invoke(cli.cli, ["tables", "--schema", db_path])
134131
assert (
135132
'[{"table": "Gosh", "schema": "CREATE TABLE Gosh (c1 text, c2 text, c3 text)"},\n'
@@ -191,7 +188,6 @@ def test_output_table(db_path, options, expected):
191188
for i in range(4)
192189
]
193190
)
194-
db.close()
195191
result = CliRunner().invoke(cli.cli, ["rows", db_path, "rows"] + options)
196192
assert result.exit_code == 0
197193
assert expected == result.output.strip()
@@ -247,7 +243,6 @@ def test_create_index(db_path):
247243
CliRunner().invoke(cli.cli, create_index_unique_args + [option]).exit_code
248244
== 0
249245
)
250-
db.close()
251246

252247

253248
def test_create_index_analyze(db_path):
@@ -627,7 +622,6 @@ def test_optimize(db_path, tables):
627622
)
628623
db["Gosh"].enable_fts(["c1", "c2", "c3"], fts_version="FTS4")
629624
db["Gosh2"].enable_fts(["c1", "c2", "c3"], fts_version="FTS5")
630-
db.close()
631625
size_before_optimize = os.stat(db_path).st_size
632626
result = CliRunner().invoke(cli.cli, ["optimize", db_path] + tables)
633627
assert result.exit_code == 0
@@ -1456,7 +1450,6 @@ def test_drop_table_error():
14561450
with runner.isolated_filesystem():
14571451
db = Database("test.db")
14581452
db["t"].create({"pk": int}, pk="pk")
1459-
db.close()
14601453
result = runner.invoke(
14611454
cli.cli,
14621455
[
@@ -1481,7 +1474,6 @@ def test_drop_view():
14811474
db = Database("test.db")
14821475
db.create_view("hello", "select 1")
14831476
assert "hello" in db.view_names()
1484-
db.close()
14851477
result = runner.invoke(
14861478
cli.cli,
14871479
[
@@ -1491,17 +1483,14 @@ def test_drop_view():
14911483
],
14921484
)
14931485
assert result.exit_code == 0
1494-
db = Database("test.db")
14951486
assert "hello" not in db.view_names()
1496-
db.close()
14971487

14981488

14991489
def test_drop_view_error():
15001490
runner = CliRunner()
15011491
with runner.isolated_filesystem():
15021492
db = Database("test.db")
15031493
db["t"].create({"pk": int}, pk="pk")
1504-
db.close()
15051494
result = runner.invoke(
15061495
cli.cli,
15071496
[
@@ -1739,13 +1728,10 @@ def test_transform(db_path, args, expected_schema):
17391728
defaults={"age": 1},
17401729
pk="id",
17411730
)
1742-
db.close()
17431731
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs"] + args)
17441732
print(result.output)
17451733
assert result.exit_code == 0
1746-
db = Database(db_path)
17471734
schema = db["dogs"].schema
1748-
db.close()
17491735
assert schema == expected_schema
17501736

17511737

tests/test_cli_memory.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,3 @@ def test_memory_return_db(tmpdir):
335335
db = ctx.invoke(cli.commands["memory"], paths=(path,), return_db=True)
336336

337337
assert db.table_names() == ["dogs"]
338-
db.close()

tests/test_hypothesis.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@
66
# SQLite integers are -(2^63) to 2^63 - 1
77
@given(st.integers(-9223372036854775808, 9223372036854775807))
88
def test_roundtrip_integers(integer):
9-
with sqlite_utils.Database(memory=True) as db:
10-
row = {
11-
"integer": integer,
12-
}
13-
db["test"].insert(row)
14-
assert list(db["test"].rows) == [row]
9+
db = sqlite_utils.Database(memory=True)
10+
row = {
11+
"integer": integer,
12+
}
13+
db["test"].insert(row)
14+
assert list(db["test"].rows) == [row]
1515

1616

1717
@given(st.text())
1818
def test_roundtrip_text(text):
19-
with sqlite_utils.Database(memory=True) as db:
20-
row = {
21-
"text": text,
22-
}
23-
db["test"].insert(row)
24-
assert list(db["test"].rows) == [row]
19+
db = sqlite_utils.Database(memory=True)
20+
row = {
21+
"text": text,
22+
}
23+
db["test"].insert(row)
24+
assert list(db["test"].rows) == [row]
2525

2626

2727
@given(st.binary(max_size=1024 * 1024))
2828
def test_roundtrip_binary(binary):
29-
with sqlite_utils.Database(memory=True) as db:
30-
row = {
31-
"binary": binary,
32-
}
33-
db["test"].insert(row)
34-
assert list(db["test"].rows) == [row]
29+
db = sqlite_utils.Database(memory=True)
30+
row = {
31+
"binary": binary,
32+
}
33+
db["test"].insert(row)
34+
assert list(db["test"].rows) == [row]
3535

3636

3737
@given(st.floats(allow_nan=False))
3838
def test_roundtrip_floats(floats):
39-
with sqlite_utils.Database(memory=True) as db:
40-
row = {
41-
"floats": floats,
42-
}
43-
db["test"].insert(row)
44-
assert list(db["test"].rows) == [row]
39+
db = sqlite_utils.Database(memory=True)
40+
row = {
41+
"floats": floats,
42+
}
43+
db["test"].insert(row)
44+
assert list(db["test"].rows) == [row]

0 commit comments

Comments
 (0)