-
Notifications
You must be signed in to change notification settings - Fork 140
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
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
de36481
parameterized + increased test coverage
bigtedde eb489f3
added 'test_sources_update'
bigtedde c64a926
'test_sources_update' -> updated to click button instead of calling m…
bigtedde 7782238
'test_paste_text' skipif linux due to spurious test fails.
bigtedde ab379e6
skipif fix, updateThreads check done in test_source_add_remove
bigtedde 78fd72f
wait for 'updateThreads' to return to 0 at end of each test
bigtedde 5beb1eb
updateThread check moved to fixture teardown
bigtedde c5e80c5
click buttons instead of method calls
bigtedde 1151cf5
patch QMessagebox
bigtedde 008b15f
Merge remote-tracking branch 'upstream/master' into source_tests
bigtedde 9c695b3
Merge branch 'master' into source_tests
bigtedde 8c3e544
updated fixture, included docstrings
bigtedde 7d2781f
Merge remote-tracking branch 'upstream/master' into source_tests
bigtedde 20c4c8c
Merge branch 'master' into source_tests
m3nu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.