Commit 9afc8722 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

Merge branch '242_add_missing_delete_confirmation' into 'v2'

Add missing delete confirmation

See merge request !101
parents 07eb4877 8487d65c
Pipeline #32319 passed with stage
in 12 minutes and 30 seconds
......@@ -25,6 +25,7 @@
import os
import pytest
import random
from PyQt5 import QtCore
......@@ -301,7 +302,10 @@ class TestAssetWidget:
assert target_name in asset_model.stringList()
assert os.path.exists(asset_model.json_path(target_name))
def test_delete(
@pytest.mark.parametrize(
"messagebox_answer", [QMessageBox.Yes, QMessageBox.No], ids=["Yes", "No"]
)
def test_delete_asset_in_edition(
self,
qtbot,
monkeypatch,
......@@ -309,21 +313,145 @@ class TestAssetWidget:
beat_context,
asset_type,
asset_type_prefix_entry_map,
messagebox_answer,
):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
asset_name = asset_type_prefix_entry_map[asset_type][0]
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
monkeypatch.setattr(QMessageBox, "question", lambda *args: messagebox_answer)
asset = Asset(test_prefix, asset_type, asset_name)
with qtbot.waitSignal(asset_widget.currentAssetChanged):
asset_widget.loadAsset(asset)
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
asset_widget.deleteAsset(asset.declaration_path)
assert asset_widget.current_editor.asset_type == AssetType.UNKNOWN
assert not os.path.exists(asset.declaration_path)
if messagebox_answer == QMessageBox.Yes:
assert asset_widget.current_editor.asset_type == AssetType.UNKNOWN
assert not os.path.exists(asset.declaration_path)
else:
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
assert asset_widget.current_asset == asset
assert os.path.exists(asset.declaration_path)
def __check_deletion(
self, qtbot, beat_context, asset_to_edit, asset_to_delete, messagebox_answer
):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
with qtbot.waitSignal(asset_widget.currentAssetChanged):
asset_widget.loadAsset(asset_to_edit)
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
asset_widget.deleteAsset(asset_to_delete.declaration_path)
if messagebox_answer == QMessageBox.Yes:
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
assert os.path.exists(asset_to_edit.declaration_path)
assert not os.path.exists(asset_to_delete.declaration_path)
else:
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
assert asset_widget.current_asset == asset_to_edit
assert os.path.exists(asset_to_edit.declaration_path)
assert os.path.exists(asset_to_delete.declaration_path)
def __get_asset_name_list(self, test_prefix, asset_type):
asset_model = AssetModel()
asset_model.setLatestOnlyEnabled(False)
asset_model.asset_type = asset_type
asset_model.prefix_path = test_prefix
asset_list = [
asset_name
for asset_name in asset_model.stringList()
if "error" not in asset_name
]
assert len(asset_list) >= 2
return asset_list
@pytest.mark.parametrize(
"messagebox_answer", [QMessageBox.Yes, QMessageBox.No], ids=["Yes", "No"]
)
def test_delete_same_asset_type(
self,
qtbot,
monkeypatch,
test_prefix,
beat_context,
asset_type,
asset_type_prefix_entry_map,
messagebox_answer,
):
monkeypatch.setattr(QMessageBox, "question", lambda *args: messagebox_answer)
asset_name_list = self.__get_asset_name_list(test_prefix, asset_type)
if asset_type == AssetType.DATAFORMAT:
# Using lasts assets in the list because of dataformats basetypes
asset_name_to_edit = asset_name_list[-1]
asset_name_to_delete = asset_name_list[-2]
else:
asset_name_to_edit = asset_name_list[0]
asset_name_to_delete = asset_name_list[1]
asset_to_edit = Asset(test_prefix, asset_type, asset_name_to_edit)
asset_to_delete = Asset(test_prefix, asset_type, asset_name_to_delete)
self.__check_deletion(
qtbot, beat_context, asset_to_edit, asset_to_delete, messagebox_answer
)
@pytest.mark.parametrize(
"messagebox_answer", [QMessageBox.Yes, QMessageBox.No], ids=["Yes", "No"]
)
def test_delete_other_asset_type(
self,
qtbot,
monkeypatch,
test_prefix,
beat_context,
asset_type,
asset_type_prefix_entry_map,
messagebox_answer,
):
monkeypatch.setattr(QMessageBox, "question", lambda *args: messagebox_answer)
asset_name_list = self.__get_asset_name_list(test_prefix, asset_type)
index = 0
if asset_type == AssetType.DATAFORMAT:
# Using lasts assets in the list because of dataformats basetypes
index = -1
asset_name_to_edit = asset_name_list[index]
asset_to_delete_type = random.choice(
[
asset
for asset in AssetType
if asset not in [asset_type, AssetType.UNKNOWN]
]
)
asset_to_delete_name_list = self.__get_asset_name_list(
test_prefix, asset_to_delete_type
)
index = 0
if asset_to_delete_type == AssetType.DATAFORMAT:
# Using lasts assets in the list because of dataformats basetypes
index = -1
asset_name_to_delete = asset_to_delete_name_list[index]
asset_to_edit = Asset(test_prefix, asset_type, asset_name_to_edit)
asset_to_delete = Asset(test_prefix, asset_to_delete_type, asset_name_to_delete)
self.__check_deletion(
qtbot, beat_context, asset_to_edit, asset_to_delete, messagebox_answer
)
def test_load_empty_json(
self,
......
......@@ -328,7 +328,7 @@ class AssetWidget(QWidget):
"""
if self.current_asset and self.current_asset.declaration_path == file_path:
"""Check before deletion"""
# Check before deletion
answer = QMessageBox.question(
self,
self.tr("Deletion requested"),
......@@ -345,6 +345,16 @@ class AssetWidget(QWidget):
self.current_asset.delete()
self.current_asset = None
else:
# Check before deletion
answer = QMessageBox.question(
self,
self.tr("Deletion requested"),
self.tr(
"You are about to delete an asset\n\nAre you sure you want to do that ?"
),
)
if answer == QMessageBox.No:
return
asset = Asset.from_path(self.prefix_root_path, file_path)
asset.delete()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment