Commit 0bfe5813 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[test] Add AssetWidget tests

parent 00778118
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.editor module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import os
import pytest
import simplejson as json
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMessageBox
from ..widgets.assetwidget import AssetWidget
from ..widgets.editor import PlaceholderEditor
from ..widgets.algorithmeditor import AlgorithmEditor
from ..widgets.databaseeditor import DatabaseEditor
from ..widgets.dataformateditor import DataformatEditor
from ..widgets.experimenteditor import ExperimentEditor
from ..widgets.libraryeditor import LibraryEditor
from ..widgets.plottereditor import PlotterEditor
from ..widgets.plotterparameterseditor import PlotterParametersEditor
from ..widgets.toolchaineditor import ToolchainEditor
from ..backend.assetmodel import AssetType
@pytest.fixture()
def asset_type_prefix_entry_map():
return {
AssetType.ALGORITHM: ("user/integers_add_v2/1", AlgorithmEditor),
AssetType.DATABASE: ("integers_db/1", DatabaseEditor),
AssetType.DATAFORMAT: ("user/single_string/1", DataformatEditor),
AssetType.EXPERIMENT: (
"user/user/integers_echo/1/integers_echo",
ExperimentEditor,
),
AssetType.LIBRARY: ("user/sum/1", LibraryEditor),
AssetType.PLOTTER: ("user/scatter/1", PlotterEditor),
AssetType.PLOTTERPARAMETERS: ("", PlotterParametersEditor),
AssetType.TOOLCHAIN: ("user/double/1", ToolchainEditor),
}
class TestAssetWidget:
"""Test that the AssetWidget works correctly"""
def get_asset_path(self, prefix, asset_type, asset_name):
return os.path.join(prefix, AssetType.path(asset_type), asset_name + ".json")
def test_matching_editor(self, qtbot, test_prefix, asset_type_prefix_entry_map):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_prefix_root(test_prefix)
assert isinstance(asset_widget.current_editor, PlaceholderEditor)
for (
asset_type,
(asset_name, editor_type),
) in asset_type_prefix_entry_map.items():
if asset_name:
asset_widget.load_json(
self.get_asset_path(test_prefix, asset_type, asset_name)
)
assert isinstance(asset_widget.current_editor, editor_type)
def test_dirty(self, qtbot, monkeypatch, test_prefix, asset_type_prefix_entry_map):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
description = "dummy description"
# Question about saving editor content after modification before a new json is loaded
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.No)
asset_widget.set_prefix_root(test_prefix)
for (
asset_type,
(asset_name, editor_type),
) in asset_type_prefix_entry_map.items():
if asset_name:
asset_widget.load_json(
self.get_asset_path(test_prefix, asset_type, asset_name)
)
assert not asset_widget.current_editor.is_dirty()
with qtbot.waitSignal(asset_widget.current_editor.dataChanged):
qtbot.keyClicks(
asset_widget.current_editor.description_lineedit, description
)
assert asset_widget.save_button.isEnabled()
assert asset_widget.current_editor.is_dirty()
dumped_json = asset_widget.current_editor.dump_json()
assert dumped_json["description"] == description
def test_save(self, qtbot, monkeypatch, test_prefix, asset_type_prefix_entry_map):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
description = "dummy description"
asset_widget.set_prefix_root(test_prefix)
for (
asset_type,
(asset_name, editor_type),
) in asset_type_prefix_entry_map.items():
if asset_name:
asset_path = self.get_asset_path(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset_path)
with qtbot.waitSignal(asset_widget.current_editor.dataChanged):
qtbot.keyClicks(
asset_widget.current_editor.description_lineedit, description
)
assert asset_widget.save_button.isEnabled()
qtbot.mouseClick(asset_widget.save_button, QtCore.Qt.LeftButton)
assert not asset_widget.save_button.isEnabled()
with open(asset_path, "rt") as json_file:
json_data = json.loads(json_file.read())
assert json_data["description"] == description
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