test_protocoletemplateditor.py 5.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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/.           #
#                                                                             #
###############################################################################

26
import copy
Samuel GAIST's avatar
Samuel GAIST committed
27

28
29
30
31
32
import pytest
import simplejson as json

from PyQt5 import QtCore

Samuel GAIST's avatar
Samuel GAIST committed
33
from ..backend.asset import Asset
Samuel GAIST's avatar
Samuel GAIST committed
34
from ..backend.asset import AssetType
35
from ..backend.assetmodel import DataFormatModel
36
from ..widgets.dialogs import NameInputDialog
37
from ..widgets.protocoltemplateeditor import ProtocolTemplateEditor
Samuel GAIST's avatar
Samuel GAIST committed
38
from ..widgets.protocoltemplateeditor import SetWidget
39
40


41
42
@pytest.fixture()
def dataformat_model(test_prefix):
43
    asset_model = DataFormatModel()
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    asset_model.prefix_path = test_prefix
    return asset_model


@pytest.fixture()
def reference_set_json():
    return {"name": "test", "outputs": {"out1": "int8"}}


@pytest.fixture()
def reference_pt_json():
    return {"sets": [{"name": "original", "outputs": {"a": "int8"}}]}


class TestSetWidget:
    """Test that the editor dedicated to a set works correctly"""

    def test_load_and_dump(self, qtbot, dataformat_model, reference_set_json):
        set_widget = SetWidget(dataformat_model)
        set_widget.load(reference_set_json)

        qtbot.addWidget(set_widget)

        assert set_widget.dump() == reference_set_json

    def test_add_output(self, qtbot, dataformat_model, reference_set_json):
        edited_json = copy.deepcopy(reference_set_json)
        edited_json["outputs"]["Change_me"] = "int8"

        set_widget = SetWidget(dataformat_model)
        set_widget.load(reference_set_json)

        qtbot.addWidget(set_widget)

        qtbot.mouseClick(set_widget.add_button, QtCore.Qt.LeftButton)

        assert set_widget.dump() == edited_json

    def test_remove_output(self, qtbot, dataformat_model, reference_set_json):
        edited_json = copy.deepcopy(reference_set_json)
        edited_json["outputs"] = {}

        set_widget = SetWidget(dataformat_model)
        set_widget.load(reference_set_json)

        qtbot.addWidget(set_widget)

        qtbot.mouseClick(set_widget.remove_button, QtCore.Qt.LeftButton)

        assert set_widget.dump() == reference_set_json

        set_widget.outputs_tablewidget.setCurrentCell(0, 0)

        qtbot.mouseClick(set_widget.remove_button, QtCore.Qt.LeftButton)

        assert set_widget.dump() == edited_json


102
103
104
class TestProtocolTemplateEditor:
    """Test that the protocol template editor works correctly"""

105
106
    def test_load_and_dump(self, qtbot, test_prefix):
        asset_name = "double/1"
Samuel GAIST's avatar
Samuel GAIST committed
107
108
        asset = Asset(test_prefix, AssetType.PROTOCOLTEMPLATE, asset_name)
        with open(asset.declaration_path, "rt") as json_file:
109
110
111
112
113
114
115
116
            json_data = json.load(json_file)
            editor = ProtocolTemplateEditor()
            editor.load_json(json_data)

            qtbot.addWidget(editor)

            assert editor.dump_json() == json_data

117
    def test_add_set(self, qtbot, monkeypatch, beat_context, reference_pt_json):
118
119
120
121
122
123
124
        edited_json = copy.deepcopy(reference_pt_json)
        edited_json["schema_version"] = 1
        edited_json["sets"].append(
            {"name": "test_set", "outputs": {"Change_me": "int8"}}
        )

        monkeypatch.setattr(
125
            NameInputDialog, "getText", classmethod(lambda *args: ("test_set", True))
126
127
128
        )

        editor = ProtocolTemplateEditor()
129
        editor.set_context(beat_context)
130
131
132
133
134
135
136
        editor.load_json(reference_pt_json)

        qtbot.addWidget(editor)
        qtbot.mouseClick(editor.add_set_button, QtCore.Qt.LeftButton)

        assert editor.dump_json() == edited_json

137
    def test_remove_set(self, qtbot, beat_context, reference_pt_json):
138
139
140
141
        edited_json = copy.deepcopy(reference_pt_json)
        edited_json["schema_version"] = 1
        edited_json["sets"] = []

142
        editor = ProtocolTemplateEditor()
143
        editor.set_context(beat_context)
144
        editor.load_json(reference_pt_json)
145

146
147
        qtbot.addWidget(editor)
        qtbot.mouseClick(editor.set_widgets[0].delete_button, QtCore.Qt.LeftButton)
148

149
        assert editor.dump_json() == edited_json