From 22a21f0898e24b4cf12d935ec781804f47386f02 Mon Sep 17 00:00:00 2001 From: Shivansh Singh <89853707+shivansh02@users.noreply.github.com> Date: Mon, 1 Jul 2024 19:45:45 +0530 Subject: [PATCH] Remove duplicate sources on profile import. By @shivansh02 (#2017) --- src/vorta/profile_export.py | 2 ++ tests/unit/test_profile.py | 40 +++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/vorta/profile_export.py b/src/vorta/profile_export.py index a370ce1d..18c295b5 100644 --- a/src/vorta/profile_export.py +++ b/src/vorta/profile_export.py @@ -130,6 +130,8 @@ def to_db(self, overwrite_profile=False, overwrite_settings=True): # Set the profile ids to be match new profile for source in self._profile_dict['SourceFileModel']: source['profile'] = self.id + # Delete existing Sources to avoid duplicates + SourceFileModel.delete().where(SourceFileModel.profile == self.id).execute() SourceFileModel.insert_many(self._profile_dict['SourceFileModel']).execute() # Delete added dictionaries to make it match BackupProfileModel diff --git a/tests/unit/test_profile.py b/tests/unit/test_profile.py index 03ad56e7..c757dc89 100644 --- a/tests/unit/test_profile.py +++ b/tests/unit/test_profile.py @@ -1,6 +1,9 @@ +import tempfile + from PyQt6 import QtCore -from PyQt6.QtWidgets import QDialogButtonBox, QMessageBox, QToolTip -from vorta.store.models import BackupProfileModel +from PyQt6.QtWidgets import QDialogButtonBox, QFileDialog, QMessageBox, QToolTip +from vorta.store.models import BackupProfileModel, SourceFileModel +from vorta.views.export_window import ExportWindow def test_profile_add_delete(qapp, qtbot, mocker): @@ -47,3 +50,36 @@ def test_profile_edit(qapp, qtbot): assert BackupProfileModel.get_or_none(name='Default') is None assert BackupProfileModel.get_or_none(name='Test Profile') is not None assert main.profileSelector.currentItem().text() == 'Test Profile' + + +def test_profile_import_no_duplicate_sources(qapp, qtbot, mocker): + """Tests importing an existing profile and choosing to overwrite does not add duplicate sources.""" + main = qapp.main_window + + # Create a new profile and add sources + profile = BackupProfileModel.create(name='Test Profile') + sources = ['/path/to/source1', '/path/to/source2'] + for source_path in sources: + SourceFileModel.create(dir=source_path, path=source_path, profile=profile) + + # Export the profile + with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as temp_file: + temp_file_path = temp_file.name + + export_window = ExportWindow(profile) + mocker.patch.object(QFileDialog, 'getSaveFileName', return_value=(temp_file_path, 'JSON (*.json)')) + export_window.run() + + # Mock the QMessageBox to return 'Yes' to overwrite the profile + mocker.patch.object(QMessageBox, 'question', return_value=QMessageBox.StandardButton.Yes) + + # Mock the QFileDialog to return the name of the temporary file + mocker.patch.object(QFileDialog, 'getOpenFileName', return_value=(temp_file_path, 'JSON (*.json)')) + + # Import the profile and choose to overwrite + main.profile_import_action() + + # Check that the sources in the profile are the same as before and that there are no duplicates + imported_sources = [source.dir for source in SourceFileModel.select().where(SourceFileModel.profile == profile)] + assert set(imported_sources) == set(sources) + assert len(imported_sources) == len(sources)