Esempio n. 1
0
    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(database,
                                              stubs.FakeHistoryProgress())
        assert not web_history2.metainfo['force_rebuild']
Esempio n. 2
0
    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(database,
                                              stubs.FakeHistoryProgress())
        assert not web_history2.metainfo['force_rebuild']
Esempio n. 3
0
def test_user_version(hist, monkeypatch):
    """Ensure that completion is regenerated if user_version is incremented."""
    hist.add_url(QUrl('example.com/1'), redirect=False, atime=1)
    hist.add_url(QUrl('example.com/2'), redirect=False, atime=2)
    hist.completion.delete('url', 'example.com/2')

    hist2 = history.WebHistory()
    assert list(hist2.completion) == [('example.com/1', '', 1)]

    monkeypatch.setattr(history, '_USER_VERSION', history._USER_VERSION + 1)
    hist3 = history.WebHistory()
    assert list(hist3.completion) == [
        ('example.com/1', '', 1),
        ('example.com/2', '', 2),
    ]
Esempio n. 4
0
 def fake_web_history(self, fake_save_manager, tmpdir, init_sql,
                      config_stub):
     """Create a fake web-history and register it into objreg."""
     web_history = history.WebHistory()
     objreg.register('web-history', web_history)
     yield web_history
     objreg.delete('web-history')
Esempio n. 5
0
    def test_user_version(self, web_history, stubs, monkeypatch):
        """Ensure that completion is regenerated if user_version changes."""
        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(progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [('example.com/1', '', 1)]

        monkeypatch.setattr(history, '_USER_VERSION',
                            history._USER_VERSION + 1)
        hist3 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist3.completion) == [
            ('example.com/1', '', 1),
            ('example.com/2', '', 2),
        ]
Esempio n. 6
0
    def test_force_rebuild(self, web_history, stubs):
        """Ensure that completion is regenerated if we force a rebuild."""
        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(progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [('example.com/1', '', 1)]
        hist2.metainfo['force_rebuild'] = True

        hist3 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist3.completion) == [
            ('example.com/1', '', 1),
            ('example.com/2', '', 2),
        ]
        assert not hist3.metainfo['force_rebuild']
Esempio n. 7
0
def test_private_browsing(qtbot, tmpdir, fake_save_manager, config_stub):
    """Make sure no data is saved at all with private browsing."""
    config_stub.data = {'general': {'private-browsing': True}}
    private_hist = history.WebHistory(hist_dir=str(tmpdir),
                                      hist_name='history')

    # Before initial read
    with qtbot.assertNotEmitted(private_hist.add_completion_item), \
            qtbot.assertNotEmitted(private_hist.item_added):
        private_hist.add_url(QUrl('http://www.example.com/'))
    assert not private_hist._temp_history

    # read
    with qtbot.assertNotEmitted(private_hist.add_completion_item), \
            qtbot.assertNotEmitted(private_hist.item_added):
        with qtbot.waitSignals([private_hist.async_read_done], order='strict'):
            list(private_hist.async_read())

    # after read
    with qtbot.assertNotEmitted(private_hist.add_completion_item), \
            qtbot.assertNotEmitted(private_hist.item_added):
        private_hist.add_url(QUrl('http://www.example.com/'))

    assert not private_hist._temp_history
    assert not private_hist._new_history
    assert not private_hist.history_dict
Esempio n. 8
0
    def test_force_rebuild(self, hist):
        """Ensure that completion is regenerated if we force a rebuild."""
        hist.add_url(QUrl('example.com/1'), redirect=False, atime=1)
        hist.add_url(QUrl('example.com/2'), redirect=False, atime=2)
        hist.completion.delete('url', 'example.com/2')

        hist2 = history.WebHistory()
        assert list(hist2.completion) == [('example.com/1', '', 1)]
        hist2.metainfo['force_rebuild'] = True

        hist3 = history.WebHistory()
        assert list(hist3.completion) == [
            ('example.com/1', '', 1),
            ('example.com/2', '', 2),
        ]
        assert not hist3.metainfo['force_rebuild']
def web_history(init_sql, stubs, config_stub):
    """Fixture which provides a web-history object."""
    config_stub.val.completion.timestamp_format = '%Y-%m-%d'
    config_stub.val.completion.web_history_max_items = -1
    stub = history.WebHistory()
    objreg.register('web-history', stub)
    yield stub
    objreg.delete('web-history')
Esempio n. 10
0
def test_no_rebuild_completion(hist):
    """Ensure that completion is not regenerated unless completely empty."""
    hist.add_url(QUrl('example.com/1'), redirect=False, atime=1)
    hist.add_url(QUrl('example.com/2'), redirect=False, atime=2)
    hist.completion.delete('url', 'example.com/2')

    hist2 = history.WebHistory()
    assert list(hist2.completion) == [('example.com/1', '', 1)]
Esempio n. 11
0
def web_history(fake_save_manager, tmpdir, init_sql, 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
    web_history = history.WebHistory(stubs.FakeHistoryProgress())
    monkeypatch.setattr(history, 'web_history', web_history)
    return web_history
Esempio n. 12
0
    def test_no_rebuild(self, web_history, stubs):
        """Ensure that completion is not regenerated unless empty."""
        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(progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [('example.com/1', '', 1)]
Esempio n. 13
0
def web_history(fake_save_manager, tmpdir, init_sql, config_stub, stubs):
    """Create a web history and register it into objreg."""
    config_stub.val.completion.timestamp_format = '%Y-%m-%d'
    config_stub.val.completion.web_history.max_items = -1
    web_history = history.WebHistory(stubs.FakeHistoryProgress())
    objreg.register('web-history', web_history)
    yield web_history
    objreg.delete('web-history')
Esempio n. 14
0
def test_updated_entries(hist, tmpdir):
    (tmpdir / 'filled-history').write('12345 http://example.com/\n'
                                      '67890 http://example.com/\n')
    hist = history.WebHistory(hist_dir=str(tmpdir), hist_name='filled-history')
    list(hist.async_read())

    assert hist.history_dict['http://example.com/'].atime == 67890
    hist.add_url(QUrl('http://example.com/'), atime=99999)
    assert hist.history_dict['http://example.com/'].atime == 99999
Esempio n. 15
0
def web_history(init_sql, stubs, config_stub):
    """Fixture which provides a web-history object."""
    config_stub.data['completion'] = {
        'timestamp-format': '%Y-%m-%d',
        'web-history-max-items': -1
    }
    stub = history.WebHistory()
    objreg.register('web-history', stub)
    yield stub
    objreg.delete('web-history')
Esempio n. 16
0
    def test_interrupted(self, stubs, database, monkeypatch):
        """If we interrupt the rebuilding process, force_rebuild should still be set."""
        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)

        # Trigger a completion rebuild
        monkeypatch.setattr(web_history.database, 'user_version_changed',
                            lambda: True)

        with pytest.raises(Exception, match='tick-tock'):
            history.WebHistory(web_history.database, progress=progress)

        assert web_history.metainfo['force_rebuild']

        hist2 = history.WebHistory(web_history.database,
                                   progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [('example.com/1', '', 1)]
Esempio n. 17
0
def test_get_recent(hist, tmpdir):
    (tmpdir / 'filled-history').write('12345 http://example.com/')
    hist = history.WebHistory(hist_dir=str(tmpdir), hist_name='filled-history')
    list(hist.async_read())

    hist.add_url(QUrl('http://www.qutebrowser.org/'), atime=67890)
    lines = hist.get_recent()

    expected = ['12345 http://example.com/',
                '67890 http://www.qutebrowser.org/']
    assert lines == expected
Esempio n. 18
0
    def test_user_version(self, database, stubs, monkeypatch):
        """Ensure that completion is regenerated if user_version changes."""
        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(database,
                                   progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [('example.com/1', '', 1)]

        monkeypatch.setattr(web_history.database, 'user_version_changed',
                            lambda: True)

        hist3 = history.WebHistory(web_history.database,
                                   progress=stubs.FakeHistoryProgress())
        assert list(hist3.completion) == [
            ('example.com/1', '', 1),
            ('example.com/2', '', 2),
        ]
        assert not hist3.metainfo['force_rebuild']
    def test_interrupted(self, stubs, web_history, monkeypatch):
        """If we interrupt the rebuilding process, force_rebuild should still be set."""
        web_history.add_url(QUrl('example.com/1'), redirect=False, atime=1)
        progress = stubs.FakeHistoryProgress(raise_on_tick=True)

        # Trigger a completion rebuild
        monkeypatch.setattr(sql, 'user_version_changed', lambda: True)

        with pytest.raises(Exception, match='tick-tock'):
            history.WebHistory(progress=progress)

        assert web_history.metainfo['force_rebuild']
Esempio n. 20
0
def test_invalid_read(hist, tmpdir, caplog):
    (tmpdir / 'filled-history').write('foobar\n12345 http://example.com/')
    hist = history.WebHistory(hist_dir=str(tmpdir), hist_name='filled-history')
    with caplog.at_level(logging.WARNING):
        list(hist.async_read())

    entries = list(hist.history_dict.values())

    assert len(entries) == 1
    assert len(caplog.records) == 1
    msg = "Invalid history entry 'foobar': 2 or 3 fields expected!"
    assert caplog.records[0].msg == msg
Esempio n. 21
0
    def test_progress(self, monkeypatch, web_history, config_stub, stubs):
        web_history.add_url(QUrl('example.com/1'), redirect=False, atime=1)
        web_history.add_url(QUrl('example.com/2'), redirect=False, atime=2)

        # Trigger a completion rebuild
        monkeypatch.setattr(sql, 'user_version_changed', lambda: True)

        progress = stubs.FakeHistoryProgress()
        history.WebHistory(progress=progress)
        assert progress._value == 2
        assert progress._started
        assert progress._finished
Esempio n. 22
0
    def test_pattern_change_rebuild(self, config_stub, web_history, stubs):
        """Ensure that completion is rebuilt when exclude patterns change."""
        config_stub.val.completion.web_history.exclude = ['*.example.org']

        web_history.add_url(QUrl('http://example.com'),
                            redirect=False,
                            atime=1)
        web_history.add_url(QUrl('http://example.org'),
                            redirect=False,
                            atime=2)

        hist2 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [
            ('http://example.com', '', 1),
        ]

        config_stub.val.completion.web_history.exclude = []

        hist3 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist3.completion) == [('http://example.com', '', 1),
                                          ('http://example.org', '', 2)]
Esempio n. 23
0
def test_async_read_twice(monkeypatch, qtbot, tmpdir, caplog):
    (tmpdir / 'filled-history').write('\n'.join([
        '12345 http://example.com/ title',
        '67890 http://example.com/',
        '12345 http://qutebrowser.org/ blah',
    ]))
    hist = history.WebHistory(hist_dir=str(tmpdir), hist_name='filled-history')
    next(hist.async_read())
    with pytest.raises(StopIteration):
        next(hist.async_read())
    expected = "Ignoring async_read() because reading is started."
    assert len(caplog.records) == 1
    assert caplog.records[0].msg == expected
Esempio n. 24
0
    def test_exclude(self, config_stub, hist):
        """Ensure that patterns in completion.web_history.exclude are ignored.

        This setting should only be used for the completion.
        """
        config_stub.val.completion.web_history.exclude = ['*.example.org']
        assert hist.metainfo['force_rebuild']

        hist.add_url(QUrl('http://example.com'), redirect=False, atime=1)
        hist.add_url(QUrl('http://example.org'), redirect=False, atime=2)

        hist2 = history.WebHistory()
        assert list(hist2.completion) == [('http://example.com', '', 1)]
Esempio n. 25
0
    def test_progress(self, web_history, config_stub, monkeypatch, stubs,
                      patch_threshold):
        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.metainfo['force_rebuild'] = True

        if patch_threshold:
            monkeypatch.setattr(history.WebHistory, '_PROGRESS_THRESHOLD', 1)

        progress = stubs.FakeHistoryProgress()
        history.WebHistory(progress=progress)
        assert progress._value == 2
        assert progress._finished
        assert progress._started == patch_threshold
Esempio n. 26
0
    def test_progress(self, web_history, config_stub, monkeypatch, stubs,
                      patch_threshold):
        web_history.add_url(QUrl('example.com/1'), redirect=False, atime=1)
        web_history.add_url(QUrl('example.com/2'), redirect=False, atime=2)
        # Change cached patterns to trigger a completion rebuild
        web_history.metainfo['excluded_patterns'] = 'http://example.org'

        if patch_threshold:
            monkeypatch.setattr(history.WebHistory, '_PROGRESS_THRESHOLD', 1)

        progress = stubs.FakeHistoryProgress()
        history.WebHistory(progress=progress)
        assert progress._value == 2
        assert progress._finished
        assert progress._started == patch_threshold
Esempio n. 27
0
def test_add_item_redirect_update(qtbot, tmpdir):
    """A redirect update added should override a non-redirect one."""
    url = 'http://www.example.com/'

    hist_file = tmpdir / 'filled-history'
    hist_file.write('12345 {}\n'.format(url))
    hist = history.WebHistory(hist_dir=str(tmpdir), hist_name='filled-history')
    list(hist.async_read())

    with qtbot.assertNotEmitted(hist.add_completion_item):
        with qtbot.waitSignal(hist.item_added):
            hist.add_url(QUrl(url), redirect=True, atime=67890)

    entry = history.Entry(url=QUrl(url), redirect=True, atime=67890, title="")
    assert hist.history_dict[url] == entry
Esempio n. 28
0
    def test_exclude(self, config_stub, web_history, stubs):
        """Ensure that patterns in completion.web_history.exclude are ignored.

        This setting should only be used for the completion.
        """
        config_stub.val.completion.web_history.exclude = ['*.example.org']

        web_history.add_url(QUrl('http://example.com'),
                            redirect=False,
                            atime=1)
        web_history.add_url(QUrl('http://example.org'),
                            redirect=False,
                            atime=2)

        hist2 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [('http://example.com', '', 1)]
Esempio n. 29
0
    def test_user_version(self, web_history, stubs, monkeypatch):
        """Ensure that completion is regenerated if user_version changes."""
        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')

        # User version always changes, so this won't work
        # hist2 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        # assert list(hist2.completion) == [('example.com/1', '', 1)]

        monkeypatch.setattr(sql, 'user_version_changed', lambda: True)

        hist3 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist3.completion) == [
            ('example.com/1', '', 1),
            ('example.com/2', '', 2),
        ]
Esempio n. 30
0
    def test_delete(self, web_history, stubs):
        web_history.insert({'url': 'example.com/1', 'title': 'example1',
                            'redirect': False, 'atime': 1})
        web_history.insert({'url': 'example.com/1', 'title': 'example1',
                            'redirect': False, 'atime': 2})
        web_history.insert({'url': 'example.com/2%203', 'title': 'example2',
                            'redirect': False, 'atime': 3})
        web_history.insert({'url': 'example.com/3', 'title': 'example3',
                            'redirect': True, 'atime': 4})
        web_history.insert({'url': 'example.com/2 3', 'title': 'example2',
                            'redirect': False, 'atime': 5})
        web_history.completion.delete_all()

        hist2 = history.WebHistory(progress=stubs.FakeHistoryProgress())
        assert list(hist2.completion) == [
            ('example.com/1', 'example1', 2),
            ('example.com/2 3', 'example2', 5),
        ]