test_databaseeditor.py 11.7 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
import pytest
29

30
31
32
33
from PyQt5 import QtCore
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QInputDialog

Samuel GAIST's avatar
Samuel GAIST committed
34
from ..backend.asset import Asset
Samuel GAIST's avatar
Samuel GAIST committed
35
from ..backend.asset import AssetType
36
from ..widgets.databaseeditor import DatabaseEditor
37
38
39
from ..widgets.databaseeditor import DatabaseWidget
from ..widgets.databaseeditor import ParameterTypeDelegate
from ..widgets.databaseeditor import ProtocolDialog
Samuel GAIST's avatar
Samuel GAIST committed
40
41
42
from ..widgets.databaseeditor import ProtocolEditor
from ..widgets.databaseeditor import ViewEditor
from ..widgets.databaseeditor import ViewsEditor
43
44


45
@pytest.fixture(params=[i for i in range(1, 3)])
46
47
48
49
def test_database(request):
    yield "simple/{}".format(request.param)


50
@pytest.fixture(
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    params=[
        {},
        {"test_string": "test string", "test_number": 12.0, "test_boolean": True},
    ]
)
def reference_view_json(request):
    parameters = request.param

    reference_json = {"view": "testing_view"}

    if parameters:
        reference_json["parameters"] = parameters

    yield reference_json


@pytest.fixture()
def reference_database_json(test_prefix):
Samuel GAIST's avatar
Samuel GAIST committed
69
    asset = Asset(test_prefix, AssetType.DATABASE, "simple/2")
70
    return asset.declaration
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93


@pytest.fixture()
def reference_viewseditor_data(request, reference_database_json):
    protocol = reference_database_json["protocols"][0]
    return protocol["template"], protocol["views"]


@pytest.fixture()
def reference_protocols(request, reference_database_json):
    return reference_database_json["protocols"]


TYPE_VALUES_DICT = {"boolean": True, "number": 0, "string": "Change_me"}


# ------------------------------------------------------------------------------


class ParameterTypeTestDelegate(ParameterTypeDelegate):
    def createEditor(self, parent, options, index):
        """Force commit to model on index change, otherwise testing won't work"""

94
        combobox = super().createEditor(parent, options, index)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
        combobox.currentIndexChanged.connect(lambda: self.commitData.emit(combobox))
        return combobox


@pytest.fixture()
def view_editor(qtbot, reference_view_json):
    editor = ViewEditor()
    qtbot.addWidget(editor)
    editor.load(reference_view_json)
    return editor


class TestViewEditor:
    """Test that the view editor works correctly"""

    def test_load_and_dump(self, qtbot, view_editor, reference_view_json):
        assert view_editor.dump() == reference_view_json

    def test_add_parameter(self, qtbot, view_editor, reference_view_json):
        with qtbot.waitSignal(view_editor.dataChanged):
            qtbot.mouseClick(view_editor.add_button, QtCore.Qt.LeftButton)

        if "parameters" not in reference_view_json:
            reference_view_json["parameters"] = {}
        reference_view_json["parameters"]["Name_me"] = "Change_me"

        assert view_editor.dump() == reference_view_json

    def test_remove_parameter(self, qtbot, view_editor, reference_view_json):
        assert not view_editor.remove_button.isEnabled()

        if "parameters" in reference_view_json:

            view_editor.parameters_tablewidget.setCurrentCell(0, 0)

            assert view_editor.remove_button.isEnabled()

            with qtbot.waitSignal(view_editor.dataChanged):
                qtbot.mouseClick(view_editor.remove_button, QtCore.Qt.LeftButton)

    def test_modify_name(self, qtbot, view_editor, reference_view_json):
        new_name = "new_name"

        with qtbot.waitSignal(view_editor.dataChanged):
            qtbot.keyClicks(view_editor.view_name_lineedit, new_name)

        reference_view_json["view"] += new_name

        assert view_editor.dump() == reference_view_json

    @pytest.mark.parametrize("value", TYPE_VALUES_DICT.values())
    @pytest.mark.parametrize("new_type, new_value", TYPE_VALUES_DICT.items())
    def test_modify_parameter_type(
        self, qtbot, view_editor, value, new_type, new_value
    ):
        reference_json = {"view": "test", "parameters": {"test_param": value}}
        new_json = copy.deepcopy(reference_json)
        new_json["parameters"] = {"test_param": new_value}

        view_editor.load(reference_json)
        type_delegate = ParameterTypeTestDelegate(
            view_editor.parameter_type_model, view_editor
        )
        view_editor.parameters_tablewidget.setItemDelegateForColumn(1, type_delegate)
        type_item = view_editor.parameters_tablewidget.item(0, 1)

        def __do_edit():
            view_editor.parameters_tablewidget.openPersistentEditor(type_item)
            combobox = view_editor.findChildren(QComboBox)[0]
            combobox.setCurrentText(new_type)
            view_editor.parameters_tablewidget.closePersistentEditor(type_item)

        if type_item.text() != new_type:
            with qtbot.waitSignal(view_editor.parameters_tablewidget.itemChanged):
                __do_edit()
        else:
            __do_edit()

        assert view_editor.dump() == new_json


# ------------------------------------------------------------------------------


@pytest.fixture()
def views_editor(qtbot, test_prefix):
    editor = ViewsEditor()
    qtbot.addWidget(editor)
183
    editor.setPrefixPath(test_prefix)
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    return editor


class TestViewsEditor:
    """Test that the views editor works correctly"""

    @staticmethod
    def setup_editor(editor, protocol, views):
        editor.protocol = protocol
        editor.load(views)
        return editor

    def test_load_and_dump(self, views_editor, reference_viewseditor_data):
        protocol, views_json = reference_viewseditor_data
        self.setup_editor(views_editor, protocol, views_json)

        assert views_editor.dump() == views_json

    def test_add_view(
        self, qtbot, monkeypatch, views_editor, reference_viewseditor_data
    ):
        protocol, views_json = reference_viewseditor_data
        views_json.pop(next(iter(views_json)))
        self.setup_editor(views_editor, protocol, views_json)

        available_sets = views_editor.available_sets()
        monkeypatch.setattr(
            QInputDialog,
            "getItem",
            classmethod(lambda *args, **kwargs: (available_sets[0], True)),
        )

        with qtbot.waitSignal(views_editor.dataChanged):
            qtbot.mouseClick(views_editor.add_view_button, QtCore.Qt.LeftButton)

        assert len(views_editor.view_editors) == len(views_json) + 1

    def test_remove_view(self, qtbot, views_editor, reference_viewseditor_data):
        protocol, views_json = reference_viewseditor_data
        self.setup_editor(views_editor, protocol, views_json)

        view_editor = views_editor.view_editors[-1]

        with qtbot.waitSignal(views_editor.dataChanged):
            qtbot.mouseClick(view_editor.delete_button, QtCore.Qt.LeftButton)

        assert len(views_editor.view_editors) == len(views_json) - 1


# ------------------------------------------------------------------------------


class TestProtocolEditor:
    """Test that the protocol editor works correctly"""

    def test_load_and_dump(self, qtbot, test_prefix, reference_database_json):
        protocol_editor = ProtocolEditor()
        qtbot.addWidget(protocol_editor)
242
        protocol_editor.setPrefixPath(test_prefix)
243
244
245
246
247
248
249
250
251
252
253
254
255

        for protocol in reference_database_json["protocols"]:
            protocol_editor.load(protocol)
            assert protocol_editor.dump() == protocol


# ------------------------------------------------------------------------------


@pytest.fixture()
def database_widget(qtbot, test_prefix):
    editor = DatabaseWidget()
    qtbot.addWidget(editor)
256
    editor.setPrefixPath(test_prefix)
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    return editor


class TestDatabaseWidget:
    """Test that the database widget works correctly"""

    def test_load_and_dump(self, database_widget, reference_database_json):
        database_widget.load(reference_database_json)
        reference_database_json.pop("schema_version")
        assert database_widget.dump() == reference_database_json

    def test_add_protocol(
        self, qtbot, monkeypatch, database_widget, reference_database_json
    ):
        database_widget.load(reference_database_json)
        name = "test_protocol"
        protocol = database_widget.protocoltemplate_model.stringList()[0]

        monkeypatch.setattr(
            ProtocolDialog,
            "getProtocol",
            classmethod(lambda *args, **kwargs: (name, protocol, True)),
        )

        with qtbot.waitSignal(database_widget.dataChanged):
            qtbot.mouseClick(database_widget.add_protocol_button, QtCore.Qt.LeftButton)

        assert (
            len(database_widget.protocol_editors)
            == len(reference_database_json["protocols"]) + 1
        )

        reference_database_json.pop("schema_version")
        new_protocol = {"name": name, "template": protocol, "views": {}}
        reference_database_json["protocols"].append(new_protocol)
        assert database_widget.dump() == reference_database_json

    def test_remove_protocol(self, qtbot, database_widget, reference_database_json):
        database_widget.load(reference_database_json)

        protocol_editor = database_widget.protocol_editors[-1]

        with qtbot.waitSignal(database_widget.dataChanged):
            qtbot.mouseClick(protocol_editor.delete_button, QtCore.Qt.LeftButton)

        assert (
            len(database_widget.protocol_editors)
            == len(reference_database_json["protocols"]) - 1
        )

        reference_database_json.pop("schema_version")
        reference_database_json["protocols"].pop(-1)
        assert database_widget.dump() == reference_database_json


# ------------------------------------------------------------------------------
313
314
315


class TestDatabaseEditor:
316
    """Test that the database editor works correctly"""
317

318
    def test_load_and_dump(self, qtbot, beat_context, test_prefix, test_database):
Samuel GAIST's avatar
Samuel GAIST committed
319
        asset = Asset(test_prefix, AssetType.DATABASE, test_database)
320
321
322
323
        json_data = asset.declaration
        editor = DatabaseEditor()
        editor.set_context(beat_context)
        editor.load_json(json_data)
Samuel GAIST's avatar
Samuel GAIST committed
324

325
        assert editor.dump_json() == json_data