From ec0e95969ed7988eb5443156b9db8d4e9f7a4028 Mon Sep 17 00:00:00 2001 From: Imran Sobir Date: Wed, 8 Feb 2017 13:28:04 +0500 Subject: [PATCH] Add unit tests for qute://history. --- qutebrowser/browser/qutescheme.py | 4 +- tests/unit/browser/test_qutehistory.py | 95 ++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 tests/unit/browser/test_qutehistory.py diff --git a/qutebrowser/browser/qutescheme.py b/qutebrowser/browser/qutescheme.py index 3960f24ed..1b51f405b 100644 --- a/qutebrowser/browser/qutescheme.py +++ b/qutebrowser/browser/qutescheme.py @@ -172,7 +172,7 @@ def qute_history(url): curr_date = datetime.datetime.strptime(query_date, "%Y-%m-%d") curr_date = curr_date.date() except ValueError: - log.misc.error("Invalid date passed to qute:history: " + query_date) + log.misc.debug("Invalid date passed to qute:history: " + query_date) one_day = datetime.timedelta(days=1) next_date = curr_date + one_day @@ -184,7 +184,7 @@ def qute_history(url): try: item_atime = datetime.datetime.fromtimestamp(item.atime) except (ValueError, OSError, OverflowError): - log.misc.error("Invalid timestamp {}.".format(item.atime)) + log.misc.debug("Invalid timestamp {}.".format(item.atime)) continue # Skip items not on curr_date diff --git a/tests/unit/browser/test_qutehistory.py b/tests/unit/browser/test_qutehistory.py new file mode 100644 index 000000000..43df79d26 --- /dev/null +++ b/tests/unit/browser/test_qutehistory.py @@ -0,0 +1,95 @@ +# 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 . + +from PyQt5.QtCore import QUrl +from qutebrowser.browser import history, qutescheme +from qutebrowser.utils import objreg +import datetime +import pytest +import tempfile + + +class TestHistoryHandler: + """Test the qute://history endpoint.""" + + @pytest.fixture(autouse=True) + def fake_objects(self, fake_save_manager): + """ Create fake web-history with history for three different days """ + temp_dir = tempfile.TemporaryDirectory() + fake_web_history = history.WebHistory(temp_dir.name, 'fake-history') + objreg.register('web-history', fake_web_history, update=True) + objreg.register('save-manager', fake_save_manager, update=True) + + # Add fake history items for different days + one_day = datetime.timedelta(days=1) + self.curr_date = datetime.datetime.today() + self.next_date = self.curr_date + one_day + self.prev_date = self.curr_date - one_day + + today = history.Entry(atime=str(self.curr_date.timestamp()), + url=QUrl('www.today.com'), title='today') + tomorrow = history.Entry(atime=str(self.next_date.timestamp()), + url=QUrl('www.tomorrow.com'), title='tomorrow') + yesterday = history.Entry(atime=str(self.prev_date.timestamp()), + url=QUrl('www.yesterday.com'), title='yesterday') + + web_history = objreg.get('web-history') + web_history._add_entry(today) + web_history._add_entry(tomorrow) + web_history._add_entry(yesterday) + web_history.save() + + def test_history_without_query(self): + """ Test qute://history shows today's history when it has no query """ + _mimetype, data = qutescheme.qute_history(QUrl("qute://history")) + key = "{}".format( + datetime.date.today().strftime("%a, %d %B %Y")) + assert key in data + + def test_history_with_bad_query(self): + """ Test qute://history shows today's history when given bad query """ + url = QUrl("qute://history?date=204-blaah") + _mimetype, data = qutescheme.qute_history(url) + key = "{}".format( + datetime.date.today().strftime("%a, %d %B %Y")) + assert key in data + + def test_history_today(self): + """ Test qute://history shows history for today """ + url = QUrl("qute://history") + _mimetype, data = qutescheme.qute_history(url) + assert "today" in data + assert "tomorrow" not in data + assert "yesterday" not in data + + def test_history_yesterday(self): + """ Test qute://history shows history for yesterday """ + url = QUrl("qute://history?date=" + self.prev_date.strftime("%Y-%m-%d")) + _mimetype, data = qutescheme.qute_history(url) + assert "today" not in data + assert "tomorrow" not in data + assert "yesterday" in data + + def test_history_tomorrow(self): + """ Test qute://history shows history for tomorrow """ + url = QUrl("qute://history?date=" + self.next_date.strftime("%Y-%m-%d")) + _mimetype, data = qutescheme.qute_history(url) + assert "today" not in data + assert "tomorrow" in data + assert "yesterday" not in data