1
0
Fork 0
mirror of https://github.com/borgbase/vorta synced 2024-12-21 23:33:13 +00:00

Improve import/export feature test coverage. By @bigtedde (#1774)

This commit is contained in:
Ted Lawson 2023-08-17 10:05:42 -07:00 committed by GitHub
parent 3bfa78bf04
commit e5c9b2245a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@
import pytest
from PyQt6 import QtCore
from PyQt6.QtWidgets import QDialogButtonBox, QFileDialog, QMessageBox
from vorta.profile_export import VersionException
from vorta.store.models import BackupProfileModel, SourceFileModel
from vorta.views.import_window import ImportWindow
@ -32,6 +33,41 @@ def test_import_success(qapp, qtbot, rootdir, monkeypatch):
assert len(SourceFileModel.select().where(SourceFileModel.profile == restored_profile)) == 3
@pytest.mark.parametrize(
"exception, error_message",
[
(AttributeError, "Schema upgrade failure"),
(VersionException, "Newer profile_export export files cannot be used on older versions"),
(PermissionError, "Cannot read profile_export export file due to permission error"),
(FileNotFoundError, "Profile export file not found"),
],
)
def test_import_exceptions(qapp, qtbot, rootdir, monkeypatch, mocker, exception, error_message):
monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args: [VALID_IMPORT_FILE])
monkeypatch.setattr(QMessageBox, 'information', lambda *args: None)
main = qapp.main_window
main.profile_import_action()
import_dialog: ImportWindow = main.window
import_dialog.overwriteExistingSettings.setChecked(True)
def raise_exception(*args, **kwargs):
raise exception
# force an exception and mock the error QMessageBox
monkeypatch.setattr(import_dialog.profile_export, 'to_db', raise_exception)
mock_messagebox = mocker.patch.object(QMessageBox, "critical")
qtbot.mouseClick(
import_dialog.buttonBox.button(QDialogButtonBox.StandardButton.Ok), QtCore.Qt.MouseButton.LeftButton
)
# assert the correct error appears, and the profile does not get added
mock_messagebox.assert_called_once()
assert error_message in mock_messagebox.call_args[0][2]
assert BackupProfileModel.get_or_none(name="Test Profile Restoration") is None
def test_import_bootstrap_success(qapp, mocker):
mocked_unlink = mocker.MagicMock()
mocker.patch.object(Path, 'unlink', mocked_unlink)
@ -92,7 +128,7 @@ def getSaveFileName(*args, **kwargs):
assert os.path.isfile(FILE_PATH)
def test_export_fail_unwritable(qapp, qtbot, tmpdir, monkeypatch):
def test_export_fail_unwritable(qapp, qtbot, monkeypatch):
FILE_PATH = os.path.join(os.path.abspath(os.sep), "testresult.vortabackup")
def getSaveFileName(*args, **kwargs):