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_diff: increase test coverage #1770

Merged
merged 8 commits into from
Aug 17, 2023
56 changes: 56 additions & 0 deletions tests/unit/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,59 @@ def test_archive_diff_json_parser(line, expected):

assert item.path == PurePath(expected[0]).parts
assert item.data == DiffData(*expected[1:])


def test_diff_item_copy(qapp, qtbot, mocker, borg_json_output):
main = qapp.main_window
tab = main.archiveTab
main.tabWidget.setCurrentIndex(3)

tab.populate_from_profile()
qtbot.waitUntil(lambda: tab.archiveTable.rowCount() == 2)

stdout, stderr = borg_json_output("diff_archives")
popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0)
mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result)

compat = vorta.utils.borg_compat

def check(feature_name):
if feature_name == 'DIFF_JSON_LINES':
return False
return vorta.utils.BorgCompatibility.check(compat, feature_name)

mocker.patch.object(vorta.utils.borg_compat, 'check', check)

selection_model: QItemSelectionModel = tab.archiveTable.selectionModel()
model = tab.archiveTable.model()

flags = QItemSelectionModel.SelectionFlag.Rows
flags |= QItemSelectionModel.SelectionFlag.Select

selection_model.select(model.index(0, 0), flags)
selection_model.select(model.index(1, 0), flags)

tab.diff_action()

qtbot.waitUntil(lambda: hasattr(tab, '_resultwindow'), **pytest._wait_defaults)

# mock the clipboard to ensure no changes are made to it during testing
mocker.patch.object(qapp.clipboard(), "setMimeData")
clipboard_spy = mocker.spy(qapp.clipboard(), "setMimeData")

# test 'diff_item_copy()' by passing it an item to copy
index = tab._resultwindow.treeView.model().index(0, 0)
assert index is not None
tab._resultwindow.diff_item_copy(index)
clipboard_data = clipboard_spy.call_args[0][0]
assert clipboard_data.hasText()
assert clipboard_data.text() == "/test"

clipboard_spy.reset_mock()

# test 'diff_item_copy()' by selecting a row to copy
tab._resultwindow.treeView.selectionModel().select(tab._resultwindow.treeView.model().index(0, 0), flags)
tab._resultwindow.diff_item_copy()
clipboard_data = clipboard_spy.call_args[0][0]
assert clipboard_data.hasText()
assert clipboard_data.text() == "/test"