qutebrowser/tests/unit/browser/test_qutescheme.py

140 lines
5.2 KiB
Python
Raw Normal View History

2017-02-08 09:28:04 +01:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2017 Imran Sobir
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2017-02-09 11:44:51 +01:00
import datetime
2017-02-09 18:27:32 +01:00
import collections
2017-02-09 11:44:51 +01:00
2017-02-08 09:28:04 +01:00
from PyQt5.QtCore import QUrl
2017-02-09 11:44:51 +01:00
import pytest
2017-02-08 09:28:04 +01:00
from qutebrowser.browser import history, qutescheme
from qutebrowser.utils import objreg
2017-02-09 18:27:32 +01:00
Dates = collections.namedtuple('Dates', ['yesterday', 'today', 'tomorrow'])
2017-02-08 09:28:04 +01:00
class TestHistoryHandler:
2017-02-09 11:44:51 +01:00
2017-02-08 09:28:04 +01:00
"""Test the qute://history endpoint."""
@pytest.fixture
2017-02-09 18:27:32 +01:00
def dates(self):
2017-02-08 09:28:04 +01:00
one_day = datetime.timedelta(days=1)
2017-02-09 18:27:32 +01:00
today = datetime.datetime.today()
tomorrow = today + one_day
yesterday = today - one_day
return Dates(yesterday, today, tomorrow)
2017-02-08 09:28:04 +01:00
2017-02-09 18:27:32 +01:00
@pytest.fixture
def entries(self, dates):
today = history.Entry(atime=str(dates.today.timestamp()),
2017-02-08 09:28:04 +01:00
url=QUrl('www.today.com'), title='today')
2017-02-09 18:27:32 +01:00
tomorrow = history.Entry(atime=str(dates.tomorrow.timestamp()),
2017-02-08 09:28:04 +01:00
url=QUrl('www.tomorrow.com'), title='tomorrow')
2017-02-09 18:27:32 +01:00
yesterday = history.Entry(atime=str(dates.yesterday.timestamp()),
2017-02-08 09:28:04 +01:00
url=QUrl('www.yesterday.com'), title='yesterday')
2017-02-09 18:27:32 +01:00
return Dates(yesterday, today, tomorrow)
2017-02-08 09:28:04 +01:00
2017-02-09 18:27:32 +01:00
@pytest.fixture
def fake_web_history(self, fake_save_manager, tmpdir):
"""Create a fake web-history and register it into objreg."""
web_history = history.WebHistory(tmpdir.dirname, 'fake-history')
objreg.register('web-history', web_history)
yield web_history
objreg.delete('web-history')
@pytest.fixture(autouse=True)
def fake_history(self, fake_web_history, entries):
"""Create fake history for three different days."""
fake_web_history._add_entry(entries.yesterday)
fake_web_history._add_entry(entries.today)
fake_web_history._add_entry(entries.tomorrow)
fake_web_history.save()
2017-02-08 09:28:04 +01:00
def test_history_without_query(self):
2017-02-09 14:28:10 +01:00
"""Ensure qute://history shows today's history without any query."""
2017-02-08 09:28:04 +01:00
_mimetype, data = qutescheme.qute_history(QUrl("qute://history"))
key = "<span class=\"date\">{}</span>".format(
datetime.date.today().strftime("%a, %d %B %Y"))
assert key in data
def test_history_with_bad_query(self):
2017-02-09 14:28:10 +01:00
"""Ensure qute://history shows today's history with bad query."""
2017-02-08 09:28:04 +01:00
url = QUrl("qute://history?date=204-blaah")
_mimetype, data = qutescheme.qute_history(url)
key = "<span class=\"date\">{}</span>".format(
datetime.date.today().strftime("%a, %d %B %Y"))
assert key in data
def test_history_today(self):
2017-02-09 11:44:51 +01:00
"""Ensure qute://history shows history for today."""
2017-02-08 09:28:04 +01:00
url = QUrl("qute://history")
_mimetype, data = qutescheme.qute_history(url)
assert "today" in data
assert "tomorrow" not in data
assert "yesterday" not in data
2017-02-09 18:27:32 +01:00
def test_history_yesterday(self, dates):
2017-02-09 11:44:51 +01:00
"""Ensure qute://history shows history for yesterday."""
2017-02-09 14:28:10 +01:00
url = QUrl("qute://history?date=" +
2017-02-09 18:27:32 +01:00
dates.yesterday.strftime("%Y-%m-%d"))
2017-02-08 09:28:04 +01:00
_mimetype, data = qutescheme.qute_history(url)
assert "today" not in data
assert "tomorrow" not in data
assert "yesterday" in data
2017-02-09 18:27:32 +01:00
def test_history_tomorrow(self, dates):
2017-02-09 11:44:51 +01:00
"""Ensure qute://history shows history for tomorrow."""
2017-02-09 14:28:10 +01:00
url = QUrl("qute://history?date=" +
2017-02-09 18:27:32 +01:00
dates.tomorrow.strftime("%Y-%m-%d"))
2017-02-08 09:28:04 +01:00
_mimetype, data = qutescheme.qute_history(url)
assert "today" not in data
assert "tomorrow" in data
assert "yesterday" not in data
2017-02-09 13:18:57 +01:00
2017-02-09 18:27:32 +01:00
def test_no_next_link_to_future(self, dates):
2017-02-09 13:18:57 +01:00
"""Ensure there's no next link pointing to the future."""
url = QUrl("qute://history")
_mimetype, data = qutescheme.qute_history(url)
assert "Next" not in data
2017-02-09 14:28:10 +01:00
url = QUrl("qute://history?date=" +
2017-02-09 18:27:32 +01:00
dates.tomorrow.strftime("%Y-%m-%d"))
2017-02-09 13:18:57 +01:00
_mimetype, data = qutescheme.qute_history(url)
assert "Next" not in data
2017-02-09 21:56:06 +01:00
def test_qute_history_benchmark(self, dates, entries, fake_web_history,
benchmark):
for i in range(100000):
entry = history.Entry(
atime=str(dates.yesterday.timestamp()),
url=QUrl('www.yesterday.com/{}'.format(i)),
title='yesterday')
fake_web_history._add_entry(entry)
fake_web_history._add_entry(entries.today)
fake_web_history._add_entry(entries.tomorrow)
url = QUrl("qute://history")
_mimetype, data = benchmark(qutescheme.qute_history, url)
assert "today" in data
assert "tomorrow" not in data
assert "yesterday" not in data