-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSlicerModelTest.py
More file actions
90 lines (69 loc) · 2.76 KB
/
Copy pathSlicerModelTest.py
File metadata and controls
90 lines (69 loc) · 2.76 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
import pytest
from PySide6 import QtGui
# Local
from sas.qtgui.Plotting.SlicerModel import SlicerModel
class SlicerModelTest:
'''Test the SlicerModel'''
@pytest.fixture(autouse=True)
def model(self, qapp):
'''Create/Destroy the SlicerModel'''
class SModel(SlicerModel):
params = {"a":1, "b":2}
def __init__(self):
SlicerModel.__init__(self)
def getParams(self):
return self.params
def setParams(self, par):
self.params = par
m = SModel()
yield m
def testBaseClass(self, qapp):
'''Assure that SlicerModel contains pure virtuals'''
model = SlicerModel()
with pytest.raises(NotImplementedError):
model.setParams({})
with pytest.raises(NotImplementedError):
model.setModelFromParams()
def testDefaults(self, model):
'''Test the GUI in its default state'''
assert isinstance(model.model(), QtGui.QStandardItemModel)
def testSetModelFromParams(self, model):
'''Test the model update'''
# Add a row to params
new_dict = model.getParams()
new_dict["c"] = 3
model.setParams(new_dict)
# Call the update
model.setModelFromParams()
# Check the new model.
assert model.model().rowCount() == 3
assert model.model().columnCount() == 2
def testSetParamsFromModel(self, model):
''' Test the parameters update'''
# First - the default model
model.setModelFromParams()
assert model.model().rowCount() == 2
assert model.model().columnCount() == 2
# Add a row
item1 = QtGui.QStandardItem("c")
item2 = QtGui.QStandardItem(3)
model.model().appendRow([item1, item2])
# Check the new model. The update should be automatic
assert model.model().rowCount() == 3
assert model.model().columnCount() == 2
def testSetModelFromParams_blocks_itemChanged(self, model):
"""Programmatic model writes should not emit itemChanged."""
emissions = []
model.model().itemChanged.connect(lambda *args: emissions.append(args))
model.setModelFromParams()
assert emissions == []
def testSetModelFromParams_restores_signal_state_on_exception(self, model, monkeypatch):
"""Signal blocking should be restored even if model population fails."""
def boom(value):
if value == 2:
raise RuntimeError("boom")
return str(value)
monkeypatch.setattr("sas.qtgui.Plotting.SlicerModel.GuiUtils.formatNumber", boom)
with pytest.raises(RuntimeError):
model.setModelFromParams()
assert model.model().signalsBlocked() is False