mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-15 10:47:49 -06:00
Add tests for ExtruderManager
This commit is contained in:
parent
1c93dffc22
commit
9ad8d91ff9
3 changed files with 51 additions and 18 deletions
31
tests/TestExtruderManager.py
Normal file
31
tests/TestExtruderManager.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
|
||||||
|
def createMockedExtruder(extruder_id):
|
||||||
|
extruder = MagicMock()
|
||||||
|
extruder.getId = MagicMock(return_value = extruder_id)
|
||||||
|
return extruder
|
||||||
|
|
||||||
|
|
||||||
|
def test_getAllExtruderSettings(extruder_manager):
|
||||||
|
extruder_1 = createMockedExtruder("extruder_1")
|
||||||
|
extruder_1.getProperty = MagicMock(return_value ="beep")
|
||||||
|
extruder_2 = createMockedExtruder("extruder_2")
|
||||||
|
extruder_2.getProperty = MagicMock(return_value="zomg")
|
||||||
|
extruder_manager.getActiveExtruderStacks = MagicMock(return_value = [extruder_1, extruder_2])
|
||||||
|
assert extruder_manager.getAllExtruderSettings("whatever", "value") == ["beep", "zomg"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_registerExtruder(extruder_manager):
|
||||||
|
extruder = createMockedExtruder("beep")
|
||||||
|
extruder.getMetaDataEntry = MagicMock(return_value = "0") # because the extruder position gets called
|
||||||
|
|
||||||
|
extruder_manager.extrudersChanged = MagicMock()
|
||||||
|
extruder_manager.registerExtruder(extruder, "zomg")
|
||||||
|
|
||||||
|
assert extruder_manager.extrudersChanged.emit.call_count == 1
|
||||||
|
|
||||||
|
# Doing it again should not trigger anything
|
||||||
|
extruder_manager.registerExtruder(extruder, "zomg")
|
||||||
|
assert extruder_manager.extrudersChanged.emit.call_count == 1
|
|
@ -2,8 +2,6 @@ from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from UM.Settings.ContainerRegistry import ContainerRegistry
|
|
||||||
from cura.Settings.ExtruderManager import ExtruderManager
|
|
||||||
from cura.Settings.MachineManager import MachineManager
|
from cura.Settings.MachineManager import MachineManager
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,22 +11,6 @@ def global_stack():
|
||||||
stack.getId = MagicMock(return_value ="GlobalStack")
|
stack.getId = MagicMock(return_value ="GlobalStack")
|
||||||
return stack
|
return stack
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def container_registry() -> ContainerRegistry:
|
|
||||||
return MagicMock(name = "ContainerRegistry")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
|
||||||
def extruder_manager(application, container_registry) -> ExtruderManager:
|
|
||||||
if ExtruderManager.getInstance() is not None:
|
|
||||||
# Reset the data
|
|
||||||
ExtruderManager._ExtruderManager__instance = None
|
|
||||||
|
|
||||||
with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
|
|
||||||
with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
|
|
||||||
manager = ExtruderManager()
|
|
||||||
return manager
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
|
def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
|
||||||
|
|
|
@ -11,9 +11,12 @@ import Savitar # Dont remove this line
|
||||||
import Arcus # No really. Don't. It needs to be there!
|
import Arcus # No really. Don't. It needs to be there!
|
||||||
from UM.Qt.QtApplication import QtApplication # QtApplication import is required, even though it isn't used.
|
from UM.Qt.QtApplication import QtApplication # QtApplication import is required, even though it isn't used.
|
||||||
# Even though your IDE says these files are not used, don't believe it. It's lying. They need to be there.
|
# Even though your IDE says these files are not used, don't believe it. It's lying. They need to be there.
|
||||||
|
from UM.Settings.ContainerRegistry import ContainerRegistry
|
||||||
|
|
||||||
from cura.CuraApplication import CuraApplication
|
from cura.CuraApplication import CuraApplication
|
||||||
|
from cura.Settings.ExtruderManager import ExtruderManager
|
||||||
from cura.UI.MachineActionManager import MachineActionManager
|
from cura.UI.MachineActionManager import MachineActionManager
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
# Create a CuraApplication object that will be shared among all tests. It needs to be initialized.
|
# Create a CuraApplication object that will be shared among all tests. It needs to be initialized.
|
||||||
# Since we need to use it more that once, we create the application the first time and use its instance afterwards.
|
# Since we need to use it more that once, we create the application the first time and use its instance afterwards.
|
||||||
|
@ -22,7 +25,24 @@ def application() -> CuraApplication:
|
||||||
app = unittest.mock.MagicMock()
|
app = unittest.mock.MagicMock()
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def container_registry() -> ContainerRegistry:
|
||||||
|
return MagicMock(name = "ContainerRegistry")
|
||||||
|
|
||||||
|
|
||||||
# Returns a MachineActionManager instance.
|
# Returns a MachineActionManager instance.
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def machine_action_manager(application) -> MachineActionManager:
|
def machine_action_manager(application) -> MachineActionManager:
|
||||||
return MachineActionManager(application)
|
return MachineActionManager(application)
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def extruder_manager(application, container_registry) -> ExtruderManager:
|
||||||
|
if ExtruderManager.getInstance() is not None:
|
||||||
|
# Reset the data
|
||||||
|
ExtruderManager._ExtruderManager__instance = None
|
||||||
|
|
||||||
|
with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
|
||||||
|
with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
|
||||||
|
manager = ExtruderManager()
|
||||||
|
return manager
|
Loading…
Add table
Add a link
Reference in a new issue