deluge/deluge/tests/test_files_tab.py
Chase Sterling ece31cf3cf
[Tests] Transition tests to pure pytest
Convert all the twisted.trial tests to pytest_twisted. Also move off of unittest.TestCase as well. Seems there were several tests which weren't actually testing what they should, and also some code that wasn't doing what the broken test said it should.

Goals:

    Remove twisted.trial tests
    Move to pytest fixtures, rather than many classess and subclasses with setup and teardown functions
    Move away from self.assertX to assert style tests
    FIx broken tests

Going forward I think these should be the goals when adding/modifying tests:

* Don't use BaseTest or set_up tear_down methods any more. Fixtures should be used either in the test module/class, or make/improve the ones available in conftest.py
* For sure don't use unittest or twisted.trial, they mess up the pytest stuff.
* Prefer pytest_twisted.ensureDeferred with an async function over inlineCallbacks.
  - I think the async function syntax is nicer, and it helps catch silly mistakes, e.g. await None is invalid, but yield None isn't, so if some function returns an unexpected thing we try to await on, it will be caught earlier. (I struggled debugging a test for quite a while, then caught it immediately when switching to the new syntax)
  - Once the maybe_coroutine PR goes in, using the async syntax can also improve tracebacks when debugging tests.

Things that should probably be cleaned up going forward:

* Remove BaseTestCase
* Remove the subclasses like DaemonBase in favor of new fixtures.
  * I think there are some other utility subclasses that could be removed too
* Perhaps use parameterization in the ui_entry tests, rather that the weird combination of subclasses and the set_var fixture I mixed in.
* Convert some of the callback stuff to pytest_twisted.ensureDeferred tests, just for nicer readability

Details relating to pytest fixtures conftest.py in root dir:
 * https://github.com/pytest-dev/pytest/issues/5822#issuecomment-697331920
 * https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files

Closes: https://github.com/deluge-torrent/deluge/pull/354
2022-02-03 22:29:32 +00:00

164 lines
5.6 KiB
Python

#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
import pytest
import deluge.component as component
from deluge.configmanager import ConfigManager
from deluge.conftest import BaseTestCase
from deluge.i18n import setup_translation
libs_available = True
# Allow running other tests without GTKUI dependencies available
try:
from deluge.ui.gtk3.files_tab import FilesTab
from deluge.ui.gtk3.gtkui import DEFAULT_PREFS
from deluge.ui.gtk3.mainwindow import MainWindow
except (ImportError, ValueError):
# gi.require_version gives ValueError if library not available
libs_available = False
setup_translation()
@pytest.mark.gtkui
class TestFilesTab(BaseTestCase):
def set_up(self):
if libs_available is False:
pytest.skip('GTKUI dependencies not available')
ConfigManager('gtk3ui.conf', defaults=DEFAULT_PREFS)
self.mainwindow = MainWindow()
self.filestab = FilesTab()
self.t_id = '1'
self.filestab.torrent_id = self.t_id
self.index = 1
def tear_down(self):
return component.shutdown()
def print_treestore(self, title, treestore):
root = treestore.get_iter_first()
level = 1
def p_level(s, l):
print('{}{}'.format(' ' * l, s))
def _print_treestore_children(i, lvl):
while i:
p_level(treestore[i][0], lvl)
if treestore.iter_children(i):
_print_treestore_children(treestore.iter_children(i), lvl + 2)
i = treestore.iter_next(i)
print('\n%s' % title)
_print_treestore_children(root, level)
print('')
def verify_treestore(self, treestore, tree):
def _verify_treestore(itr, tree_values):
i = 0
while itr:
values = tree_values[i]
if treestore[itr][0] != values[0]:
return False
if treestore.iter_children(itr):
if not _verify_treestore(treestore.iter_children(itr), values[1]):
return False
itr = treestore.iter_next(itr)
i += 1
return True
return _verify_treestore(treestore.get_iter_first(), tree)
def test_files_tab(self):
self.filestab.files_list[self.t_id] = (
{'index': 0, 'path': '1/test_10.txt', 'offset': 0, 'size': 13},
{'index': 1, 'path': 'test_100.txt', 'offset': 13, 'size': 14},
)
self.filestab.update_files()
self.filestab._on_torrentfilerenamed_event(
self.t_id, self.index, '2/test_100.txt'
)
ret = self.verify_treestore(
self.filestab.treestore,
[['1/', [['test_10.txt']]], ['2/', [['test_100.txt']]]],
)
if not ret:
self.print_treestore('Treestore not expected:', self.filestab.treestore)
assert ret
def test_files_tab2(self):
self.filestab.files_list[self.t_id] = (
{'index': 0, 'path': '1/1/test_100.txt', 'offset': 0, 'size': 13},
{'index': 1, 'path': 'test_101.txt', 'offset': 13, 'size': 14},
)
self.filestab.update_files()
self.filestab._on_torrentfilerenamed_event(
self.t_id, self.index, '1/1/test_101.txt'
)
ret = self.verify_treestore(
self.filestab.treestore,
[['1/', [['1/', [['test_100.txt'], ['test_101.txt']]]]]],
)
if not ret:
self.print_treestore('Treestore not expected:', self.filestab.treestore)
assert ret
def test_files_tab3(self):
self.filestab.files_list[self.t_id] = (
{'index': 0, 'path': '1/test_100.txt', 'offset': 0, 'size': 13},
{'index': 1, 'path': 'test_101.txt', 'offset': 13, 'size': 14},
)
self.filestab.update_files()
self.filestab._on_torrentfilerenamed_event(
self.t_id, self.index, '1/test_101.txt'
)
ret = self.verify_treestore(
self.filestab.treestore, [['1/', [['test_100.txt'], ['test_101.txt']]]]
)
if not ret:
self.print_treestore('Treestore not expected:', self.filestab.treestore)
assert ret
def test_files_tab4(self):
self.filestab.files_list[self.t_id] = (
{'index': 0, 'path': '1/test_100.txt', 'offset': 0, 'size': 13},
{'index': 1, 'path': '1/test_101.txt', 'offset': 13, 'size': 14},
)
self.filestab.update_files()
self.filestab._on_torrentfilerenamed_event(
self.t_id, self.index, '1/2/test_101.txt'
)
ret = self.verify_treestore(
self.filestab.treestore,
[['1/', [['2/', [['test_101.txt']]], ['test_100.txt']]]],
)
if not ret:
self.print_treestore('Treestore not expected:', self.filestab.treestore)
assert ret
def test_files_tab5(self):
self.filestab.files_list[self.t_id] = (
{'index': 0, 'path': '1/test_100.txt', 'offset': 0, 'size': 13},
{'index': 1, 'path': '2/test_101.txt', 'offset': 13, 'size': 14},
)
self.filestab.update_files()
self.filestab._on_torrentfilerenamed_event(
self.t_id, self.index, '1/test_101.txt'
)
ret = self.verify_treestore(
self.filestab.treestore, [['1/', [['test_100.txt'], ['test_101.txt']]]]
)
if not ret:
self.print_treestore('Treestore not expected:', self.filestab.treestore)
assert ret