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
|
from unittest.mock import MagicMock, patch
import pytest
from cura.Settings.MachineManager import MachineManager
@pytest.fixture()
def global_stack():
stack = MagicMock(name = "Global Stack")
stack.getId = MagicMock(return_value = "GlobalStack")
return stack
@pytest.fixture()
def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
application.getExtruderManager = MagicMock(return_value = extruder_manager)
application.getGlobalContainerStack = MagicMock(return_value = global_stack)
with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
manager = MachineManager(application)
manager._onGlobalContainerChanged()
return manager
@pytest.mark.skip(reason = "Outdated test")
def test_setActiveMachine(machine_manager):
registry = MagicMock()
mocked_global_stack = MagicMock()
mocked_global_stack.getId = MagicMock(return_value = "test_machine")
registry.findContainerStacks = MagicMock(return_value = [mocked_global_stack])
with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=registry)):
machine_manager.setActiveMachine("test_machine")
# Although we mocked the application away, we still want to know if it was notified about the attempted change.
machine_manager._application.setGlobalContainerStack.assert_called_with(mocked_global_stack)
def test_getMachine():
registry = MagicMock()
mocked_global_stack = MagicMock()
mocked_global_stack.getId = MagicMock(return_value="test_machine")
mocked_global_stack.definition.getId = MagicMock(return_value = "test")
registry.findContainerStacks = MagicMock(return_value=[mocked_global_stack])
with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
assert MachineManager.getMachine("test") == mocked_global_stack
def test_addMachine(machine_manager):
registry = MagicMock()
mocked_stack = MagicMock()
mocked_stack.getId = MagicMock(return_value="newlyCreatedStack")
mocked_create_machine = MagicMock(name="createMachine", return_value = mocked_stack)
machine_manager.setActiveMachine = MagicMock()
with patch("cura.Settings.CuraStackBuilder.CuraStackBuilder.createMachine", mocked_create_machine):
with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
machine_manager.addMachine("derp")
machine_manager.setActiveMachine.assert_called_with("newlyCreatedStack")
def test_hasUserSettings(machine_manager, application):
mocked_stack = application.getGlobalContainerStack()
mocked_instance_container = MagicMock(name="UserSettingContainer")
mocked_instance_container.getNumInstances = MagicMock(return_value = 12)
mocked_stack.getTop = MagicMock(return_value = mocked_instance_container)
assert machine_manager.numUserSettings == 12
assert machine_manager.hasUserSettings
def test_totalNumberOfSettings(machine_manager):
registry = MagicMock()
mocked_definition = MagicMock()
mocked_definition.getAllKeys = MagicMock(return_value = ["omg", "zomg", "foo"])
registry.findDefinitionContainers = MagicMock(return_value = [mocked_definition])
with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
assert machine_manager.totalNumberOfSettings == 3
def createMockedExtruder(extruder_id):
extruder = MagicMock()
extruder.getId = MagicMock(return_value = extruder_id)
return extruder
def createMockedInstanceContainer(instance_id, name = ""):
instance = MagicMock()
instance.getName = MagicMock(return_value = name)
instance.getId = MagicMock(return_value=instance_id)
return instance
def test_globalVariantName(machine_manager, application):
global_stack = application.getGlobalContainerStack()
global_stack.variant = createMockedInstanceContainer("beep", "zomg")
assert machine_manager.globalVariantName == "zomg"
def test_resetSettingForAllExtruders(machine_manager):
global_stack = machine_manager.activeMachine
extruder_1 = createMockedExtruder("extruder_1")
extruder_2 = createMockedExtruder("extruder_2")
extruder_1.userChanges = createMockedInstanceContainer("settings_1")
extruder_2.userChanges = createMockedInstanceContainer("settings_2")
global_stack.extruderList = [extruder_1, extruder_2]
machine_manager.resetSettingForAllExtruders("whatever")
extruder_1.userChanges.removeInstance.assert_called_once_with("whatever")
extruder_2.userChanges.removeInstance.assert_called_once_with("whatever")
|