Commit f65a3f46 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][assetwidget] Implement migration of API V1 based algorithm

The editor will only allow editing of API V2 or more recent
version of the algorithms.
parent dd5dd12d
......@@ -377,3 +377,35 @@ class TestAssetWidget:
asset.declaration = declaration
assert not asset_widget.save_button.isEnabled()
def test_loading_v1_algorithm_refuse_update(
self, qtbot, monkeypatch, test_prefix, beat_context
):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
asset_name = "v1/sum/1"
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.No)
asset = Asset(test_prefix, AssetType.ALGORITHM, asset_name)
with qtbot.assertNotEmitted(asset_widget.currentAssetChanged):
asset_widget.loadAsset(asset)
def test_loading_v1_algorithm_accept_update(
self, qtbot, monkeypatch, test_prefix, beat_context
):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
asset_name = "v1/sum/1"
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
asset = Asset(test_prefix, AssetType.ALGORITHM, asset_name)
with qtbot.waitSignal(asset_widget.currentAssetChanged):
asset_widget.loadAsset(asset)
assert asset_widget.current_asset.name == "v1/sum/2"
......@@ -49,6 +49,8 @@ from ..decorators import frozen
from .editor import PlaceholderEditor
from .algorithmeditor import AlgorithmEditor
from .algorithmeditor import migrate_to_api_v2
from .algorithmeditor import update_code
from .databaseeditor import DatabaseEditor
from .dataformateditor import DataformatEditor
from .experimenteditor import ExperimentEditor
......@@ -367,8 +369,32 @@ class AssetWidget(QWidget):
self.tr("Invalid asset"),
self.tr(f"The asset you are trying to load is invalid\n{errors}"),
)
else:
if asset.type == AssetType.ALGORITHM:
declaration = asset.declaration
if declaration.get("api_version", 0) < 2:
answer = QMessageBox.question(
self,
self.tr("Outdated content"),
self.tr(
"This algorithm implements an obsolete API\nWould you like to create a new updated version of it ?"
),
)
if answer == QMessageBox.No:
return
status, asset = migrate_to_api_v2(asset)
if status:
update_code(asset)
else:
QMessageBox.information(
self,
self.tr("Error occured"),
self.tr("Failed to create new version"),
)
return
self.watcher.addPath(asset.declaration_path)
self.current_asset = asset
self.__update_content()
......@@ -388,5 +414,8 @@ class AssetWidget(QWidget):
with FileBlocker(self.watcher, declaration_path):
self.current_asset.declaration = original
if self.current_asset.type == AssetType.ALGORITHM:
update_code(self.current_asset)
self.current_editor.clearDirty()
self.save_button.setEnabled(False)
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