diff --git a/tests/conftest.py b/tests/conftest.py index a9a47bc..4a43dd5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,11 +14,29 @@ def pytest_configure(config): sys._called_from_test = True +@pytest.fixture(autouse=True) +def close_all_databases(): + """Automatically close all Database objects created during a test.""" + databases = [] + original_init = Database.__init__ + + def tracking_init(self, *args, **kwargs): + original_init(self, *args, **kwargs) + databases.append(self) + + Database.__init__ = tracking_init + yield + Database.__init__ = original_init + for db in databases: + try: + db.close() + except Exception: + pass + + @pytest.fixture def fresh_db(): - db = Database(memory=True) - yield db - db.close() + return Database(memory=True) @pytest.fixture @@ -32,8 +50,7 @@ def existing_db(): INSERT INTO foo (text) values ("three"); """ ) - yield database - database.close() + return database @pytest.fixture diff --git a/tests/test_analyze_tables.py b/tests/test_analyze_tables.py index 2867bb5..4618eff 100644 --- a/tests/test_analyze_tables.py +++ b/tests/test_analyze_tables.py @@ -44,7 +44,6 @@ def big_db_to_analyze_path(tmpdir): } ) db["stuff"].insert_all(to_insert) - db.close() return path @@ -313,7 +312,6 @@ def test_analyze_table_validate_columns(tmpdir, args, expected_error): "age": 5, } ) - db.close() result = CliRunner().invoke( cli.cli, ["analyze-tables", path] + args, diff --git a/tests/test_cli.py b/tests/test_cli.py index 822748a..4198727 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -82,7 +82,6 @@ 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' @@ -118,7 +117,6 @@ 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] ) @@ -129,7 +127,6 @@ 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' @@ -191,7 +188,6 @@ 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() @@ -247,7 +243,6 @@ 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): @@ -627,7 +622,6 @@ 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 @@ -1456,7 +1450,6 @@ 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, [ @@ -1481,7 +1474,6 @@ 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, [ @@ -1491,9 +1483,7 @@ 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(): @@ -1501,7 +1491,6 @@ 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, [ @@ -1739,13 +1728,10 @@ 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 diff --git a/tests/test_cli_memory.py b/tests/test_cli_memory.py index 3d0e812..c8be35f 100644 --- a/tests/test_cli_memory.py +++ b/tests/test_cli_memory.py @@ -335,4 +335,3 @@ def test_memory_return_db(tmpdir): db = ctx.invoke(cli.commands["memory"], paths=(path,), return_db=True) assert db.table_names() == ["dogs"] - db.close() diff --git a/tests/test_hypothesis.py b/tests/test_hypothesis.py index 54bdc9d..f12f865 100644 --- a/tests/test_hypothesis.py +++ b/tests/test_hypothesis.py @@ -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): - with sqlite_utils.Database(memory=True) as db: - row = { - "integer": integer, - } - db["test"].insert(row) - assert list(db["test"].rows) == [row] + db = sqlite_utils.Database(memory=True) + row = { + "integer": integer, + } + db["test"].insert(row) + assert list(db["test"].rows) == [row] @given(st.text()) def test_roundtrip_text(text): - with sqlite_utils.Database(memory=True) as db: - row = { - "text": text, - } - db["test"].insert(row) - assert list(db["test"].rows) == [row] + db = sqlite_utils.Database(memory=True) + 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): - with sqlite_utils.Database(memory=True) as db: - row = { - "binary": binary, - } - db["test"].insert(row) - assert list(db["test"].rows) == [row] + db = sqlite_utils.Database(memory=True) + row = { + "binary": binary, + } + db["test"].insert(row) + assert list(db["test"].rows) == [row] @given(st.floats(allow_nan=False)) def test_roundtrip_floats(floats): - with sqlite_utils.Database(memory=True) as db: - row = { - "floats": floats, - } - db["test"].insert(row) - assert list(db["test"].rows) == [row] + db = sqlite_utils.Database(memory=True) + row = { + "floats": floats, + } + db["test"].insert(row) + assert list(db["test"].rows) == [row]