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 91 92 93 94 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 183 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
import unittest.mock
import pytest
import qtpy.QtCore
import qtpynodeeditor as nodeeditor
from qtpynodeeditor import PortType
class MyNodeData(nodeeditor.NodeData):
data_type = nodeeditor.NodeDataType('MyNodeData', 'My Node Data')
class MyOtherNodeData(nodeeditor.NodeData):
data_type = nodeeditor.NodeDataType('MyOtherNodeData', 'My Other Node Data')
class BasicDataModel(nodeeditor.NodeDataModel):
name = 'MyDataModel'
caption = 'Caption'
caption_visible = True
num_ports = {'input': 3,
'output': 3
}
data_type = MyNodeData.data_type
def model(self):
return 'MyDataModel'
def out_data(self, port_index):
return MyNodeData()
def set_in_data(self, node_data, port):
...
def embedded_widget(self):
return None
class BasicOtherDataModel(nodeeditor.NodeDataModel):
name = 'MyOtherDataModel'
caption = 'Caption'
caption_visible = True
num_ports = {'input': 1,
'output': 1
}
data_type = MyOtherNodeData.data_type
# @pytest.mark.parametrize("model_class", [...])
@pytest.fixture(scope='function')
def model():
return BasicDataModel
@pytest.fixture(scope='function')
def other_model():
return BasicOtherDataModel
@pytest.fixture(scope='function')
def registry(model, other_model):
registry = nodeeditor.DataModelRegistry()
registry.register_model(model, category='My Category')
registry.register_model(other_model, category='My Category')
return registry
@pytest.fixture(scope='function')
def scene(qapp, registry):
return nodeeditor.FlowScene(registry=registry)
@pytest.fixture(scope='function')
def view(qtbot, scene):
view = nodeeditor.FlowView(scene)
qtbot.addWidget(view)
view.setWindowTitle("nodeeditor test suite")
view.resize(800, 600)
view.show()
return view
def test_instantiation(view):
...
def test_create_node(scene, model):
node = scene.create_node(model)
assert node in scene.nodes.values()
assert node.id in scene.nodes
assert scene.allow_node_creation
assert scene.allow_node_deletion
def test_selected_nodes(scene, model):
node = scene.create_node(model)
node.graphics_object.setSelected(True)
assert scene.selected_nodes() == [node]
def test_create_connection(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
scene.create_connection(node2[PortType.output][2],
node1[PortType.input][1],
)
view.update()
assert len(scene.connections) == 1
all_c1 = node1.state.all_connections
assert len(all_c1) == 1
all_c2 = node1.state.all_connections
assert len(all_c2) == 1
assert all_c1 == all_c2
conn, = all_c1
# conn_state = conn.state
in_node = conn.get_node(PortType.input)
in_port = conn.get_port_index(PortType.input)
out_node = conn.get_node(PortType.output)
out_port = conn.get_port_index(PortType.output)
assert in_node == node1
assert in_port == 1
assert out_node == node2
assert out_port == 2
scene.delete_connection(conn)
assert len(scene.connections) == 0
all_c1 = node1.state.all_connections
assert len(all_c1) == 0
all_c2 = node1.state.all_connections
assert len(all_c2) == 0
def test_create_connection_with_converter(scene, view, model, other_model):
node1 = scene.create_node(model)
node2 = scene.create_node(other_model)
# Converter not registerd, must raise Exception
with pytest.raises(nodeeditor.ConnectionDataTypeFailure):
scene.create_connection(node1[PortType.output][0], node2[PortType.input][0])
# Wrong converter, must fail
converter = nodeeditor.type_converter.TypeConverter(MyOtherNodeData.data_type,
MyNodeData.data_type,
lambda x: None)
scene.registry.register_type_converter(MyNodeData.data_type, MyOtherNodeData.data_type, converter)
with pytest.raises(nodeeditor.ConnectionDataTypeFailure):
scene.create_connection(node1[PortType.output][0], node2[PortType.input][0])
# Correct converter registered, must pass
converter = nodeeditor.type_converter.TypeConverter(MyNodeData.data_type,
MyOtherNodeData.data_type,
lambda x: None)
scene.registry.register_type_converter(MyNodeData.data_type, MyOtherNodeData.data_type, converter)
scene.create_connection(node1[PortType.output][0], node2[PortType.input][0])
def test_clear_scene(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
scene.create_connection(node2[PortType.output][2],
node1[PortType.input][1],
)
scene.clear_scene()
assert len(scene.nodes) == 0
assert len(scene.connections) == 0
all_c1 = node1.state.all_connections
assert len(all_c1) == 0
all_c2 = node1.state.all_connections
assert len(all_c2) == 0
def test_get_and_set_state(scene, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
scene.create_connection(node2[PortType.output][2],
node1[PortType.input][1],
)
state = scene.__getstate__()
scene.__setstate__(state)
assert scene.__getstate__() == state
def test_save_load(tmp_path, scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
created_nodes = (node1, node2)
assert len(scene.nodes) == len(created_nodes)
for node in created_nodes:
assert node in scene.nodes.values()
assert node.id in scene.nodes
fname = tmp_path / 'temp.flow'
scene.save(fname)
scene.load(fname)
assert len(scene.nodes) == len(created_nodes)
for node in created_nodes:
assert node not in scene.nodes.values()
assert node.id in scene.nodes
@pytest.mark.parametrize('reset, port_type',
[(True, 'input'),
(False, 'output')])
def test_smoke_reacting(scene, view, model, reset, port_type):
node = scene.create_node(model)
dtype = node.model.data_type[port_type][0]
node.react_to_possible_connection(
reacting_port_type=port_type,
reacting_data_type=dtype,
scene_point=qtpy.QtCore.QPointF(0, 0),
)
view.update()
if reset:
node.reset_reaction_to_connection()
def test_smoke_node_size_updated(scene, view, model):
node = scene.create_node(model)
node.on_node_size_updated()
view.update()
def test_connection_cycles(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
node3 = scene.create_node(model)
scene.create_connection(node1[PortType.output][0],
node2[PortType.input][0])
scene.create_connection(node2[PortType.output][0],
node3[PortType.input][0])
# node1 -> node2 -> node3
# Test with a fully-specified connection: try to connect node3->node1
with pytest.raises(nodeeditor.ConnectionCycleFailure):
scene.create_connection(node3[PortType.output][0],
node1[PortType.input][0])
# Test with a half-specified connection: start with node3
conn = scene.create_connection(node3[PortType.output][0])
# and then pretend the user attempts to connect it to node1:
interaction = nodeeditor.NodeConnectionInteraction(
node=node1, connection=conn, scene=scene)
assert interaction.creates_cycle
def test_smoke_connection_interaction(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
conn = scene.create_connection(node1[PortType.output][0])
interaction = nodeeditor.NodeConnectionInteraction(
node=node2, connection=conn, scene=scene)
node_scene_transform = node2.graphics_object.sceneTransform()
pos = node2.geometry.port_scene_position(PortType.input, 0,
node_scene_transform)
conn.geometry.set_end_point(PortType.input, pos)
with pytest.raises(nodeeditor.ConnectionPointFailure):
interaction.can_connect()
conn.geometry.set_end_point(PortType.output, pos)
with pytest.raises(nodeeditor.ConnectionPointFailure):
interaction.can_connect()
assert interaction.node_port_is_empty(PortType.input, 0)
assert interaction.connection_required_port == PortType.input
# TODO node still not on it?
interaction.can_connect = lambda: (node1.state[PortType.input][0], None)
assert interaction.try_connect()
interaction.disconnect(PortType.output)
interaction.connection_end_scene_position(PortType.input)
interaction.node_port_scene_position(PortType.input, 0)
interaction.node_port_under_scene_point(PortType.input, qtpy.QtCore.QPointF(0, 0))
def test_connection_interaction_wrong_data_type(scene, view, model, other_model):
node1 = scene.create_node(model)
node2 = scene.create_node(other_model)
conn = scene.create_connection(node1[PortType.output][0])
interaction = nodeeditor.NodeConnectionInteraction(
node=node2, connection=conn, scene=scene)
node_scene_transform = node2.graphics_object.sceneTransform()
pos = node2.geometry.port_scene_position(PortType.input, 0, node_scene_transform)
conn.geometry.set_end_point(PortType.input, conn.graphics_object.mapFromScene(pos))
with pytest.raises(nodeeditor.ConnectionDataTypeFailure):
interaction.can_connect()
def test_locate_node(scene, view, model):
node = scene.create_node(model)
assert scene.locate_node_at(node.position, view.transform()) == node
def test_view_scale(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
scene.create_connection(node2[PortType.output][2],
node1[PortType.input][1],
)
view.scale_up()
view.scale_down()
def test_view_delete_selected(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
conn = scene.create_connection(node2[PortType.output][2],
node1[PortType.input][1])
node1.graphics_object.setSelected(True)
conn.graphics_object.setSelected(True)
node2.graphics_object.setSelected(True)
view.delete_selected()
assert node1 not in scene.nodes.values()
assert node2 not in scene.nodes.values()
assert conn not in scene.connections
def test_smoke_view_context_menu(qtbot, view):
view.generate_context_menu(qtpy.QtCore.QPoint(0, 0))
def test_smoke_repr(scene, view, model):
node1 = scene.create_node(model)
node2 = scene.create_node(model)
print()
print('node1', node1)
print('node2', node2)
ports = (node2[PortType.output][2], node1[PortType.input][1])
print()
print('ports', ports)
conn = scene.create_connection(*ports)
print()
print('connection', conn)
def test_smoke_scene_signal_connections(scene, view, model):
mock = unittest.mock.Mock()
scene.connection_created.connect(mock)
node1 = scene.create_node(model)
node2 = scene.create_node(model)
conn = scene.create_connection(node2[PortType.output][2],
node1[PortType.input][1])
assert mock.call_count == 1
mock = unittest.mock.Mock()
scene.connection_deleted.connect(mock)
node1 = scene.create_node(model)
node2 = scene.create_node(model)
scene.delete_connection(conn)
assert mock.call_count == 1
def test_smoke_scene_signal_nodes(scene, view, model):
mock = unittest.mock.Mock()
scene.node_created.connect(mock)
node1 = scene.create_node(model)
node2 = scene.create_node(model)
assert mock.call_count == 2
mock = unittest.mock.Mock()
scene.node_deleted.connect(mock)
scene.remove_node(node1)
scene.remove_node(node2)
assert mock.call_count == 2
|