Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: tests/unit/test_source: cleanup, parametrize, and increase test coverage #1772

Merged
merged 14 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/vorta/views/source_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,5 @@ def paste_text(self):
if len(invalidSources) != 0: # Check if any invalid paths
msg = QMessageBox()
msg.setText(self.tr("Some of your sources are invalid:") + invalidSources)
self._msg = msg # for testing
msg.exec()
92 changes: 82 additions & 10 deletions tests/unit/test_source.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,94 @@
import sys

import pytest
import vorta.views
from PyQt6 import QtCore
from PyQt6.QtWidgets import QMessageBox


@pytest.fixture()
def sources_setup(qapp, qtbot, monkeypatch, choose_file_dialog):
def setup():
monkeypatch.setattr(vorta.views.source_tab, "choose_file_dialog", choose_file_dialog)
main = qapp.main_window
main.tabWidget.setCurrentIndex(1)
tab = main.sourceTab
qtbot.waitUntil(lambda: tab.sourceFilesWidget.rowCount() == 1, **pytest._wait_defaults)
return main, tab

return setup


def test_add_folder(qapp, qtbot, mocker, monkeypatch, choose_file_dialog):
monkeypatch.setattr(vorta.views.source_tab, "choose_file_dialog", choose_file_dialog)
main = qapp.main_window
main.tabWidget.setCurrentIndex(1)
tab = main.sourceTab
def test_source_add_remove(qapp, qtbot, monkeypatch, mocker, sources_setup):
main, tab = sources_setup()

# test adding a folder with os access
mocker.patch('os.access', return_value=True)
tab.source_add(want_folder=True)
qtbot.waitUntil(lambda: tab.sourceFilesWidget.rowCount() == 2, **pytest._wait_defaults)
assert tab.sourceFilesWidget.rowCount() == 2

# test adding a folder without os access
mocker.patch('os.access', return_value=False)
monkeypatch.setattr(QMessageBox, "exec", lambda *args: True) # prevent QMessageBox from stopping test
tab.source_add(want_folder=True)
assert tab.sourceFilesWidget.rowCount() == 2

# test removing a folder
tab.sourceFilesWidget.selectRow(1)
tab.source_remove()
qtbot.waitUntil(lambda: tab.sourceFilesWidget.rowCount() == 1, **pytest._wait_defaults)
assert tab.sourceFilesWidget.rowCount() == 1


# Test paste button with mocked clipboard
@pytest.mark.skipif(platform=sys.platform.startswith("linux"), reason="spurious test fails due to 'updateThreads'")
@pytest.mark.parametrize(
"path, valid",
[
(__file__, True), # valid path
("test", False), # invalid path
(f"file://{__file__}", True), # valid - normal path with prefix that will be stripped
(f"file://{__file__}\n{__file__}", True), # valid - two files separated by new line
(f"file://{__file__}{__file__}", False), # invalid - no new line separating file names
],
)
def test_paste_text(qapp, qtbot, mocker, monkeypatch, sources_setup, path, valid):
main, tab = sources_setup()
mock_clipboard = mocker.Mock()
mock_clipboard.text.return_value = __file__
mock_clipboard.text.return_value = path

mocker.patch.object(vorta.views.source_tab.QApplication, 'clipboard', return_value=mock_clipboard)
monkeypatch.setattr(QMessageBox, "exec", lambda *args: True) # prevent QMessageBox from stopping test
tab.paste_text()
qtbot.waitUntil(lambda: tab.sourceFilesWidget.rowCount() == 3, **pytest._wait_defaults)

# Wait for directory sizing to finish
qtbot.waitUntil(lambda: len(qapp.main_window.sourceTab.updateThreads) == 0, **pytest._wait_defaults)
if valid:
# valid paths will be added as a source
assert not hasattr(tab, '_msg')
qtbot.waitUntil(lambda: tab.sourceFilesWidget.rowCount() == 2, **pytest._wait_defaults)
assert tab.sourceFilesWidget.rowCount() == 2
# Wait for directory sizing to finish
qtbot.waitUntil(lambda: len(qapp.main_window.sourceTab.updateThreads) == 0, **pytest._wait_defaults)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is causing problems with the linux tests most of the time, but not always. Never seems to have trouble on darwin. Might need to do more digging here. @m3nu @real-yfprojects have either of y'all encountered errors caused by updateThreads before?

Specifically, updateThreads is not not getting set back to 0 and the wait times out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved - this needs to be called after each test. Moved to fixture teardown.

else:
# invalid paths will trigger an alert and not be added as a source
qtbot.waitUntil(lambda: hasattr(tab, "_msg"), **pytest._wait_defaults)
assert tab._msg.text().startswith("Some of your sources are invalid")
assert tab.sourceFilesWidget.rowCount() == 1


def test_sources_update(qapp, qtbot, mocker, sources_setup):
main, tab = sources_setup()

# test that `update_path_info()` has been called for each source path
update_path_info_spy = mocker.spy(tab, "update_path_info")
qtbot.mouseClick(tab.updateButton, QtCore.Qt.MouseButton.LeftButton)
assert tab.sourceFilesWidget.rowCount() == 1
assert update_path_info_spy.call_count == 1

# add a new source and test again
tab.source_add(want_folder=True)
qtbot.mouseClick(tab.updateButton, QtCore.Qt.MouseButton.LeftButton)
qtbot.waitUntil(lambda: tab.sourceFilesWidget.rowCount() == 2, **pytest._wait_defaults)
update_path_info_spy.reset_mock()
tab.sources_update()
assert tab.sourceFilesWidget.rowCount() == 2
assert update_path_info_spy.call_count == 2