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

[backend][assetmodel] Fix latest only feature

Implement the missing filtering. Currently experiments are not
concerned.
parent 8274a692
......@@ -156,6 +156,12 @@ class AssetType(Enum):
return success == 0
def declaration_path(self, prefix, name):
"""Returns the full path to the declaration file
:param prefix str: Path to the prefix
:param name str: name of the asset to delete
"""
asset_folder = os.path.join(prefix, self.path)
return os.path.join(asset_folder, "{}.json".format(name))
......@@ -201,7 +207,7 @@ class AssetModel(QStringListModel):
)
return json_files
latest_assets_list = []
assets_list = []
if self.asset_type in [AssetType.DATABASE, AssetType.PROTOCOLTEMPLATE]:
# These assets have no user associated with them
......@@ -209,19 +215,14 @@ class AssetModel(QStringListModel):
json_files = _find_json_files(asset_folder)
if json_files:
if self.__latest_only:
latest_assets_list.append(
json_files = json_files[-1:]
for json_file in json_files:
assets_list.append(
"{name}/{version}".format(
name=asset_folder.name,
version=json_files[-1].split(".")[0],
name=asset_folder.name, version=json_file.split(".")[0]
)
)
else:
for item in json_files:
latest_assets_list.append(
"{name}/{version}".format(
name=asset_folder.name, version=item.split(".")[0]
)
)
else:
# Assets belonging to a user
asset_users = os.scandir(self.asset_folder)
......@@ -238,7 +239,7 @@ class AssetModel(QStringListModel):
for json_file in [
file for file in files if file.endswith("json")
]:
latest_assets_list.append(
assets_list.append(
"{experiment_path}/{name}".format(
experiment_path=experiment_path,
name=json_file.split(".")[0],
......@@ -247,19 +248,23 @@ class AssetModel(QStringListModel):
else:
json_files = _find_json_files(asset_folder)
if json_files:
latest_assets_list.append(
"{user}/{name}/{version}".format(
user=asset_user.name,
name=asset_folder.name,
version=json_files[-1].split(".")[0],
if self.__latest_only:
json_files = json_files[-1:]
for json_file in json_files:
assets_list.append(
"{user}/{name}/{version}".format(
user=asset_user.name,
name=asset_folder.name,
version=json_file.split(".")[0],
)
)
)
latest_assets_list = sorted(latest_assets_list)
assets_list = sorted(assets_list)
if self.asset_type == AssetType.DATAFORMAT:
latest_assets_list = dataformat_basetypes() + latest_assets_list
assets_list = dataformat_basetypes() + assets_list
self.setStringList(latest_assets_list)
self.setStringList(assets_list)
def assetType(self):
"""Returns the asset type of this model
......
......@@ -106,6 +106,19 @@ class TestAssetModel:
with pytest.raises(RuntimeError):
model.json_path("invalid")
def test_latest_only(self, qtbot, test_prefix, asset_type):
model = create_model(qtbot, test_prefix, asset_type)
asset_list = model.stringList()
assert len(asset_list) > 0
model.setLatestOnlyEnabled(False)
new_asset_list = model.stringList()
if asset_type == AssetType.EXPERIMENT:
assert len(new_asset_list) == len(asset_list)
assert new_asset_list == asset_list
else:
assert len(new_asset_list) > len(asset_list)
assert new_asset_list != asset_list
class TestAssetType:
"""Test the asset type enum"""
......@@ -197,3 +210,8 @@ class TestAssetType:
asset_name = self.existing_asset_name_map[asset_type]
result = asset_type.delete(tmp_prefix, asset_name)
assert result
def test_declaration_path(self, test_prefix, asset_type):
asset_name = self.existing_asset_name_map[asset_type]
declaration_path = asset_type.declaration_path(test_prefix, asset_name)
assert os.path.exists(declaration_path)
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