Commit 554ab087 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[backend][assetmodel] Add support for creating new assets to AssetType

**WARNING** not all asset types can be created from scratch.
This will fail accordingly.
parent 54264f88
......@@ -33,6 +33,7 @@ from PyQt5.QtCore import pyqtProperty
from PyQt5.QtCore import QStringListModel
from beat.core.schema import validate
from beat.cmdline import common
from ..utils import dataformat_basetypes
......@@ -101,11 +102,28 @@ class AssetType(Enum):
else:
raise RuntimeError("Unknown asset path {}".format(path))
def can_create(self):
"""Returns whether a new asset can be created from scratch"""
return self not in [self.UNKNOWN, self.EXPERIMENT]
def validate(self, data):
if self == self.UNKNOWN:
raise RuntimeError("Trying to validate unknown type")
return validate(self.name.lower(), data)
def create_new(self, prefix, name):
"""Create a new asset from a prototype
:param prefix str: Path to the prefix
:param name str: name of the asset
"""
if self == self.UNKNOWN:
raise RuntimeError("Trying to create an asset of unknown type")
success = common.create(prefix, self.name.lower(), [name])
return success == 0
class AssetModel(QStringListModel):
"""The asset model present a list of available asset from a given type"""
......
......@@ -34,6 +34,16 @@ def asset_type(request):
return request.param
@pytest.fixture(params=[item for item in AssetType if item.can_create()])
def creatable_asset_type(request):
return request.param
@pytest.fixture(params=[item for item in AssetType if not item.can_create()])
def uncreatable_asset_type(request):
return request.param
def create_model(qtbot, test_prefix, asset_type):
model = AssetModel()
......@@ -92,7 +102,41 @@ class TestAssetModel:
class TestAssetType:
"""Test the asset type enum"""
asset_name_map = {
AssetType.ALGORITHM: "user/foo/1",
AssetType.DATABASE: "foo/1",
AssetType.DATAFORMAT: "user/foo/1",
AssetType.EXPERIMENT: "user/foo/bar/1/baz",
AssetType.LIBRARY: "user/foo/1",
AssetType.PLOTTER: "user/foo/1",
AssetType.PLOTTERPARAMETER: "user/foo/1",
AssetType.PROTOCOLTEMPLATE: "foo/1",
AssetType.TOOLCHAIN: "user/foo/1",
AssetType.UNKNOWN: None,
}
def test_unknown_validation(self):
unknown = AssetType.UNKNOWN
with pytest.raises(RuntimeError):
unknown.validate({})
AssetType.UNKNOWN.validate({})
def test_unknown_creation(self):
with pytest.raises(RuntimeError):
AssetType.UNKNOWN.create_new("foo", "bar")
def test_unknown_new_version(self):
with pytest.raises(RuntimeError):
AssetType.UNKNOWN.create_new_version("foo", "bar")
def test_unknown_fork(self):
with pytest.raises(RuntimeError):
AssetType.UNKNOWN.fork("foo", "bar", "baz")
def test_creation(self, test_prefix, creatable_asset_type):
asset_name = self.asset_name_map[creatable_asset_type]
result = creatable_asset_type.create_new(test_prefix, asset_name)
assert result
def test_creation_failure(self, test_prefix, uncreatable_asset_type):
asset_name = self.asset_name_map[uncreatable_asset_type]
with pytest.raises(RuntimeError):
uncreatable_asset_type.create_new(test_prefix, asset_name)
Markdown is supported
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