Converted test_tabhistory to pytest
This commit is contained in:
parent
6429d29a23
commit
ea16fb3684
@ -19,103 +19,103 @@
|
|||||||
|
|
||||||
"""Tests for webelement.tabhistory."""
|
"""Tests for webelement.tabhistory."""
|
||||||
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from PyQt5.QtCore import QUrl, QPoint
|
from PyQt5.QtCore import QUrl, QPoint
|
||||||
|
import pytest
|
||||||
|
|
||||||
from qutebrowser.browser import tabhistory
|
from qutebrowser.browser import tabhistory
|
||||||
from qutebrowser.browser.tabhistory import TabHistoryItem as Item
|
from qutebrowser.browser.tabhistory import TabHistoryItem as Item
|
||||||
from qutebrowser.utils import qtutils
|
from qutebrowser.utils import qtutils
|
||||||
from qutebrowser.test import helpers
|
|
||||||
|
|
||||||
|
|
||||||
class SerializeHistoryTests(unittest.TestCase):
|
class TestSerializeHistory(object):
|
||||||
|
|
||||||
"""Tests for serialize()."""
|
"""Tests for serialize()."""
|
||||||
|
|
||||||
def setUp(self):
|
ITEMS = [
|
||||||
self.page = helpers.get_webpage()
|
Item(QUrl('https://www.heise.de/'), QUrl('http://www.heise.de/'),
|
||||||
self.history = self.page.history()
|
'heise'),
|
||||||
self.assertEqual(self.history.count(), 0)
|
Item(QUrl('http://example.com/%E2%80%A6'),
|
||||||
|
QUrl('http://example.com/%E2%80%A6'), 'percent', active=True),
|
||||||
|
Item(QUrl('http://example.com/?foo=bar'),
|
||||||
|
QUrl('http://original.url.example.com/'), 'arg',
|
||||||
|
user_data={'foo': 23, 'bar': 42}),
|
||||||
|
# From https://github.com/OtterBrowser/otter-browser/issues/709#issuecomment-74749471
|
||||||
|
Item(QUrl(
|
||||||
|
'http://github.com/OtterBrowser/24/134/2344/otter-browser/issues/709/'),
|
||||||
|
QUrl(
|
||||||
|
'http://github.com/OtterBrowser/24/134/2344/otter-browser/issues/709/'),
|
||||||
|
'Page not found | github',
|
||||||
|
user_data={'zoom': 149, 'scroll-pos': QPoint(0, 0)}),
|
||||||
|
Item(QUrl(
|
||||||
|
'https://mail.google.com/mail/u/0/#label/some+label/234lkjsd0932lkjf884jqwerdf4'),
|
||||||
|
QUrl(
|
||||||
|
'https://mail.google.com/mail/u/0/#label/some+label/234lkjsd0932lkjf884jqwerdf4'),
|
||||||
|
'"some label" - email@gmail.com - Gmail"',
|
||||||
|
user_data={'zoom': 120, 'scroll-pos': QPoint(0, 0)}),
|
||||||
|
]
|
||||||
|
|
||||||
self.items = [
|
@pytest.fixture(autouse=True)
|
||||||
Item(QUrl('https://www.heise.de/'), QUrl('http://www.heise.de/'),
|
def setup(self, webpage):
|
||||||
'heise'),
|
self.page = webpage
|
||||||
Item(QUrl('http://example.com/%E2%80%A6'),
|
self.history = self.page.history()
|
||||||
QUrl('http://example.com/%E2%80%A6'), 'percent', active=True),
|
assert self.history.count() == 0
|
||||||
Item(QUrl('http://example.com/?foo=bar'),
|
|
||||||
QUrl('http://original.url.example.com/'), 'arg',
|
stream, _data, self.user_data = tabhistory.serialize(self.ITEMS)
|
||||||
user_data={'foo': 23, 'bar': 42}),
|
|
||||||
# From https://github.com/OtterBrowser/otter-browser/issues/709#issuecomment-74749471
|
|
||||||
Item(QUrl('http://github.com/OtterBrowser/24/134/2344/otter-browser/issues/709/'),
|
|
||||||
QUrl('http://github.com/OtterBrowser/24/134/2344/otter-browser/issues/709/'),
|
|
||||||
'Page not found | github',
|
|
||||||
user_data={'zoom': 149, 'scroll-pos': QPoint(0, 0)}),
|
|
||||||
Item(QUrl('https://mail.google.com/mail/u/0/#label/some+label/234lkjsd0932lkjf884jqwerdf4'),
|
|
||||||
QUrl('https://mail.google.com/mail/u/0/#label/some+label/234lkjsd0932lkjf884jqwerdf4'),
|
|
||||||
'"some label" - email@gmail.com - Gmail"',
|
|
||||||
user_data={'zoom': 120, 'scroll-pos': QPoint(0, 0)}),
|
|
||||||
]
|
|
||||||
stream, _data, self.user_data = tabhistory.serialize(self.items)
|
|
||||||
qtutils.deserialize_stream(stream, self.history)
|
qtutils.deserialize_stream(stream, self.history)
|
||||||
|
|
||||||
def test_count(self):
|
def test_count(self):
|
||||||
"""Check if the history's count was loaded correctly."""
|
"""Check if the history's count was loaded correctly."""
|
||||||
with helpers.disable_logger('qt'):
|
assert self.history.count() == len(self.ITEMS)
|
||||||
self.assertEqual(self.history.count(), len(self.items))
|
|
||||||
|
|
||||||
def test_valid(self):
|
@pytest.mark.parametrize('i', range(len(ITEMS)))
|
||||||
|
def test_valid(self, i):
|
||||||
"""Check if all items are valid."""
|
"""Check if all items are valid."""
|
||||||
for i, _item in enumerate(self.items):
|
assert self.history.itemAt(i).isValid()
|
||||||
self.assertTrue(self.history.itemAt(i).isValid())
|
|
||||||
|
|
||||||
def test_no_userdata(self):
|
@pytest.mark.parametrize('i', range(len(ITEMS)))
|
||||||
|
def test_no_userdata(self, i):
|
||||||
"""Check if all items have no user data."""
|
"""Check if all items have no user data."""
|
||||||
for i, _item in enumerate(self.items):
|
assert self.history.itemAt(i).userData() is None
|
||||||
self.assertIsNone(self.history.itemAt(i).userData())
|
|
||||||
|
|
||||||
def test_userdata(self):
|
def test_userdata(self):
|
||||||
"""Check if all user data has been restored to self.user_data."""
|
"""Check if all user data has been restored to self.user_data."""
|
||||||
for item, user_data in zip(self.items, self.user_data):
|
for item, user_data in zip(self.ITEMS, self.user_data):
|
||||||
self.assertEqual(user_data, item.user_data)
|
assert user_data == item.user_data
|
||||||
|
|
||||||
def test_currentitem(self):
|
def test_currentitem(self):
|
||||||
"""Check if the current item index was loaded correctly."""
|
"""Check if the current item index was loaded correctly."""
|
||||||
self.assertEqual(self.history.currentItemIndex(), 1)
|
assert self.history.currentItemIndex() == 1
|
||||||
|
|
||||||
def test_urls(self):
|
@pytest.mark.parametrize('i, item', enumerate(ITEMS))
|
||||||
|
def test_urls(self, i, item):
|
||||||
"""Check if the URLs were loaded correctly."""
|
"""Check if the URLs were loaded correctly."""
|
||||||
for i, item in enumerate(self.items):
|
assert self.history.itemAt(i).url() == item.url
|
||||||
with self.subTest(i=i, item=item):
|
|
||||||
self.assertEqual(self.history.itemAt(i).url(), item.url)
|
|
||||||
|
|
||||||
def test_original_urls(self):
|
@pytest.mark.parametrize('i, item', enumerate(ITEMS))
|
||||||
|
def test_original_urls(self, i, item):
|
||||||
"""Check if the original URLs were loaded correctly."""
|
"""Check if the original URLs were loaded correctly."""
|
||||||
for i, item in enumerate(self.items):
|
assert self.history.itemAt(i).originalUrl() == item.original_url
|
||||||
with self.subTest(i=i, item=item):
|
|
||||||
self.assertEqual(self.history.itemAt(i).originalUrl(),
|
|
||||||
item.original_url)
|
|
||||||
|
|
||||||
def test_titles(self):
|
@pytest.mark.parametrize('i, item', enumerate(ITEMS))
|
||||||
|
def test_titles(self, i, item):
|
||||||
"""Check if the titles were loaded correctly."""
|
"""Check if the titles were loaded correctly."""
|
||||||
for i, item in enumerate(self.items):
|
assert self.history.itemAt(i).title() == item.title
|
||||||
with self.subTest(i=i, item=item):
|
|
||||||
self.assertEqual(self.history.itemAt(i).title(), item.title)
|
|
||||||
|
|
||||||
|
|
||||||
class SerializeHistorySpecialTests(unittest.TestCase):
|
class TestSerializeHistorySpecial(object):
|
||||||
|
|
||||||
"""Tests for serialize() without items set up in setUp."""
|
"""Tests for serialize() without items set up in setUp."""
|
||||||
|
|
||||||
def setUp(self):
|
@pytest.fixture(autouse=True)
|
||||||
self.page = helpers.get_webpage()
|
def setUp(self, webpage):
|
||||||
|
self.page = webpage
|
||||||
self.history = self.page.history()
|
self.history = self.page.history()
|
||||||
self.assertEqual(self.history.count(), 0)
|
assert self.history.count() == 0
|
||||||
|
|
||||||
def test_no_active_item(self):
|
def test_no_active_item(self):
|
||||||
"""Check tabhistory.serialize with no active item."""
|
"""Check tabhistory.serialize with no active item."""
|
||||||
items = [Item(QUrl(), QUrl(), '')]
|
items = [Item(QUrl(), QUrl(), '')]
|
||||||
with self.assertRaises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
tabhistory.serialize(items)
|
tabhistory.serialize(items)
|
||||||
|
|
||||||
def test_two_active_items(self):
|
def test_two_active_items(self):
|
||||||
@ -123,7 +123,7 @@ class SerializeHistorySpecialTests(unittest.TestCase):
|
|||||||
items = [Item(QUrl(), QUrl(), '', active=True),
|
items = [Item(QUrl(), QUrl(), '', active=True),
|
||||||
Item(QUrl(), QUrl(), ''),
|
Item(QUrl(), QUrl(), ''),
|
||||||
Item(QUrl(), QUrl(), '', active=True)]
|
Item(QUrl(), QUrl(), '', active=True)]
|
||||||
with self.assertRaises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
tabhistory.serialize(items)
|
tabhistory.serialize(items)
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
@ -131,10 +131,8 @@ class SerializeHistorySpecialTests(unittest.TestCase):
|
|||||||
items = []
|
items = []
|
||||||
stream, _data, user_data = tabhistory.serialize(items)
|
stream, _data, user_data = tabhistory.serialize(items)
|
||||||
qtutils.deserialize_stream(stream, self.history)
|
qtutils.deserialize_stream(stream, self.history)
|
||||||
self.assertEqual(self.history.count(), 0)
|
assert self.history.count() == 0
|
||||||
self.assertEqual(self.history.currentItemIndex(), 0)
|
assert self.history.currentItemIndex() == 0
|
||||||
self.assertFalse(user_data)
|
assert not user_data
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
||||||
|
@ -50,3 +50,15 @@ def unicode_encode_err():
|
|||||||
0, # start
|
0, # start
|
||||||
2, # end
|
2, # end
|
||||||
'fake exception') # reason
|
'fake exception') # reason
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def webpage():
|
||||||
|
"""Get a new QWebPage object."""
|
||||||
|
from PyQt5.QtWebKitWidgets import QWebPage
|
||||||
|
from PyQt5.QtNetwork import QNetworkAccessManager
|
||||||
|
|
||||||
|
page = QWebPage()
|
||||||
|
nam = page.networkAccessManager()
|
||||||
|
nam.setNetworkAccessible(QNetworkAccessManager.NotAccessible)
|
||||||
|
return page
|
@ -24,8 +24,6 @@ import contextlib
|
|||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
from PyQt5.QtGui import QKeyEvent
|
from PyQt5.QtGui import QKeyEvent
|
||||||
from PyQt5.QtWebKitWidgets import QWebPage
|
|
||||||
from PyQt5.QtNetwork import QNetworkAccessManager
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
@ -47,14 +45,6 @@ def fake_keyevent(key, modifiers=0, text=''):
|
|||||||
return evtmock
|
return evtmock
|
||||||
|
|
||||||
|
|
||||||
def get_webpage():
|
|
||||||
"""Get a new QWebPage object."""
|
|
||||||
page = QWebPage()
|
|
||||||
nam = page.networkAccessManager()
|
|
||||||
nam.setNetworkAccessible(QNetworkAccessManager.NotAccessible)
|
|
||||||
return page
|
|
||||||
|
|
||||||
|
|
||||||
class MessageModule:
|
class MessageModule:
|
||||||
|
|
||||||
"""A drop-in replacement for qutebrowser.utils.message."""
|
"""A drop-in replacement for qutebrowser.utils.message."""
|
||||||
|
Loading…
Reference in New Issue
Block a user