diff options
author | lufte <javier@lufte.net> | 2021-07-09 01:10:36 -0300 |
---|---|---|
committer | lufte <javier@lufte.net> | 2021-07-09 01:10:36 -0300 |
commit | 0c7b746a4ebfda0c8446558a56514b349aabdf41 (patch) | |
tree | 3f4fdd1259496f7657e0722b5487b9598b6ca769 /tests | |
parent | 9aa6a76471d2669cfd61a164ba0111b1bb31fca6 (diff) | |
download | qutebrowser-0c7b746a4ebfda0c8446558a56514b349aabdf41.tar.gz qutebrowser-0c7b746a4ebfda0c8446558a56514b349aabdf41.zip |
Restore the sql fixture to close DBs after testing
Diffstat (limited to 'tests')
-rw-r--r-- | tests/helpers/fixtures.py | 14 | ||||
-rw-r--r-- | tests/unit/browser/test_history.py | 36 | ||||
-rw-r--r-- | tests/unit/misc/test_sql.py | 6 |
3 files changed, 26 insertions, 30 deletions
diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 3426e18b2..c09fbdc2c 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -673,13 +673,21 @@ def download_stub(win_registry, tmpdir, stubs): @pytest.fixture -def web_history(fake_save_manager, tmpdir, data_tmpdir, config_stub, stubs, +def database(data_tmpdir, request): + """Create a Database object.""" + name = request.node.get_closest_marker("db_name") or "history.db" + db = sql.Database(str(data_tmpdir / name)) + yield db + db.close() + + +@pytest.fixture +def web_history(fake_save_manager, tmpdir, database, config_stub, stubs, monkeypatch): """Create a WebHistory object.""" config_stub.val.completion.timestamp_format = '%Y-%m-%d' config_stub.val.completion.web_history.max_items = -1 - db = sql.Database(str(data_tmpdir / 'history.db')) - web_history = history.WebHistory(db, stubs.FakeHistoryProgress()) + web_history = history.WebHistory(database, stubs.FakeHistoryProgress()) monkeypatch.setattr(history, 'web_history', web_history) return web_history diff --git a/tests/unit/browser/test_history.py b/tests/unit/browser/test_history.py index 4f1032254..7906d385c 100644 --- a/tests/unit/browser/test_history.py +++ b/tests/unit/browser/test_history.py @@ -368,15 +368,14 @@ class TestDump: class TestRebuild: - def test_user_version(self, data_tmpdir, stubs, monkeypatch): + def test_user_version(self, database, stubs, monkeypatch): """Ensure that completion is regenerated if user_version changes.""" - db = sql.Database(str(data_tmpdir / 'test_user_version.db')) - web_history = history.WebHistory(db, stubs.FakeHistoryProgress()) + web_history = history.WebHistory(database, stubs.FakeHistoryProgress()) web_history.add_url(QUrl('example.com/1'), redirect=False, atime=1) web_history.add_url(QUrl('example.com/2'), redirect=False, atime=2) web_history.completion.delete('url', 'example.com/2') - hist2 = history.WebHistory(db, progress=stubs.FakeHistoryProgress()) + hist2 = history.WebHistory(database, progress=stubs.FakeHistoryProgress()) assert list(hist2.completion) == [('example.com/1', '', 1)] monkeypatch.setattr(web_history.database, 'user_version_changed', lambda: True) @@ -389,10 +388,9 @@ class TestRebuild: ] assert not hist3.metainfo['force_rebuild'] - def test_force_rebuild(self, data_tmpdir, stubs): + def test_force_rebuild(self, database, stubs): """Ensure that completion is regenerated if we force a rebuild.""" - db = sql.Database(str(data_tmpdir / 'test_user_version.db')) - web_history = history.WebHistory(db, stubs.FakeHistoryProgress()) + web_history = history.WebHistory(database, stubs.FakeHistoryProgress()) web_history.add_url(QUrl('example.com/1'), redirect=False, atime=1) web_history.add_url(QUrl('example.com/2'), redirect=False, atime=2) web_history.completion.delete('url', 'example.com/2') @@ -463,10 +461,9 @@ class TestRebuild: assert progress._started assert progress._finished - def test_interrupted(self, stubs, data_tmpdir, monkeypatch): + def test_interrupted(self, stubs, database, monkeypatch): """If we interrupt the rebuilding process, force_rebuild should still be set.""" - db = sql.Database(str(data_tmpdir / 'test_user_version.db')) - web_history = history.WebHistory(db, stubs.FakeHistoryProgress()) + web_history = history.WebHistory(database, stubs.FakeHistoryProgress()) web_history.add_url(QUrl('example.com/1'), redirect=False, atime=1) web_history.completion.delete('url', 'example.com/1') progress = stubs.FakeHistoryProgress(raise_on_tick=True) @@ -487,9 +484,8 @@ class TestRebuild: class TestCompletionMetaInfo: @pytest.fixture - def metainfo(self, data_tmpdir): - db = sql.Database(str(data_tmpdir / 'TestCompletionMetaInfo.db')) - return history.CompletionMetaInfo(db) + def metainfo(self, database): + return history.CompletionMetaInfo(database) def test_contains_keyerror(self, metainfo): with pytest.raises(KeyError): @@ -512,28 +508,26 @@ class TestCompletionMetaInfo: metainfo['excluded_patterns'] = value assert metainfo['excluded_patterns'] == value - def test_recovery_no_key(self, caplog, data_tmpdir, stubs): - db = sql.Database(str(data_tmpdir / 'test_recovery_no_key.db')) - web_history = history.WebHistory(db, stubs.FakeHistoryProgress()) + def test_recovery_no_key(self, caplog, database, stubs): + web_history = history.WebHistory(database, stubs.FakeHistoryProgress()) web_history.metainfo.delete('key', 'force_rebuild') with pytest.raises(sql.BugError, match='No result for single-result query'): web_history.metainfo['force_rebuild'] with caplog.at_level(logging.WARNING): - web_history2 = history.WebHistory(db, stubs.FakeHistoryProgress()) + web_history2 = history.WebHistory(database, stubs.FakeHistoryProgress()) assert not web_history2.metainfo['force_rebuild'] - def test_recovery_no_table(self, caplog, data_tmpdir, stubs): - db = sql.Database(str(data_tmpdir / 'test_recovery_no_table.db')) - web_history = history.WebHistory(db, stubs.FakeHistoryProgress()) + def test_recovery_no_table(self, caplog, database, stubs): + web_history = history.WebHistory(database, stubs.FakeHistoryProgress()) web_history.metainfo.database.query("DROP TABLE CompletionMetaInfo").run() with pytest.raises(sql.BugError, match='no such table: CompletionMetaInfo'): web_history.metainfo['force_rebuild'] with caplog.at_level(logging.WARNING): - web_history2 = history.WebHistory(db, stubs.FakeHistoryProgress()) + web_history2 = history.WebHistory(database, stubs.FakeHistoryProgress()) assert not web_history2.metainfo['force_rebuild'] diff --git a/tests/unit/misc/test_sql.py b/tests/unit/misc/test_sql.py index 1262f0c3f..80ab7513c 100644 --- a/tests/unit/misc/test_sql.py +++ b/tests/unit/misc/test_sql.py @@ -31,12 +31,6 @@ from qutebrowser.misc import sql pytestmark = pytest.mark.usefixtures('data_tmpdir') -@pytest.fixture -def database(data_tmpdir): - db = sql.Database(str(data_tmpdir / 'test_sql.db')) - return db - - class TestUserVersion: @pytest.mark.parametrize('val, major, minor', [ |