diff options
author | Kostas Karmas <konskarm@gmail.com> | 2020-11-10 18:47:16 +0300 |
---|---|---|
committer | Kostas Karmas <konskarm@gmail.com> | 2020-11-10 18:47:16 +0300 |
commit | 644c3443b14ee88b1bc0393ca501cae7ab0956c5 (patch) | |
tree | c76dbef3ca9be26bd1fe7e855072a41f51401042 | |
parent | 9afbc64934fc7e4c2e14ab516938e6a935e48336 (diff) |
Add tests for correctPrintSequence
CURA-7833
-rw-r--r-- | tests/TestMachineManager.py | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/tests/TestMachineManager.py b/tests/TestMachineManager.py index 788b8eee41..f63944c153 100644 --- a/tests/TestMachineManager.py +++ b/tests/TestMachineManager.py @@ -1,3 +1,4 @@ +import functools from unittest.mock import MagicMock, patch, PropertyMock import pytest @@ -10,6 +11,23 @@ def createMockedStack(stack_id: str, name: str): return stack +def getPropertyMocked(setting_key, setting_property, settings_dict): + """ + Mocks the getProperty function of containers so that it returns the setting values needed for a test. + + Use this function as follows: + container.getProperty = functools.partial(getPropertyMocked, settings_dict = {"print_sequence": "one_at_a_time"}) + + :param setting_key: The key of the setting to be returned (e.g. "print_sequence", "infill_sparse_density" etc) + :param setting_property: The setting property (usually "value") + :param settings_dict: All the settings and their values expected to be returned by this mocked function + :return: The mocked setting value specified by the settings_dict + """ + if setting_property == "value": + return settings_dict.get(setting_key) + return None + + @pytest.fixture() def global_stack(): return createMockedStack("GlobalStack", "Global Stack") @@ -255,3 +273,115 @@ def test_isActiveQualityNotSupported(machine_manager): def test_isActiveQualityNotSupported_noQualityGroup(machine_manager): machine_manager.activeQualityGroup = MagicMock(return_value=None) assert not machine_manager.isActiveQualitySupported + + +def test_correctPrintSequence_globalStackHasAllAtOnce(machine_manager, application): + + # Global container stack already has all_at_once + mocked_stack = application.getGlobalContainerStack() + mocked_global_settings = {"print_sequence": "all_at_once"} + mocked_stack.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_global_settings) + + mocked_user_changes_container = MagicMock(name="UserChangesContainer") + mocked_stack.userChanges = mocked_user_changes_container + + machine_manager.correctPrintSequence() + + # After the function is called, the user changes container should not have tried to change any properties + assert not mocked_user_changes_container.setProperty.called, "The Print Sequence should not be attempted to be changed when it is already 'all-at-once'" + + +def test_correctPrintSequence_OneEnabledExtruder(machine_manager, application): + # Global container stack reports print sequence as one_at_a_time + mocked_stack = application.getGlobalContainerStack() + mocked_global_settings = {"print_sequence": "one_at_a_time"} + mocked_stack.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_global_settings) + + # The definition changes container reports 1 enabled extruders, so the correctPrintSequence should not attempt to + # change the print sequence + mocked_definition_changes_container = MagicMock(name = "DefinitionChangesContainer") + mocked_definition_changes_settings = {"extruders_enabled_count": 1} + mocked_definition_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_definition_changes_settings) + mocked_stack.definitionChanges = mocked_definition_changes_container + + mocked_user_changes_container = MagicMock(name = "UserChangesContainer") + mocked_stack.userChanges = mocked_user_changes_container + + machine_manager.correctPrintSequence() + + # After the function is called, the user changes container should not have tried to change any properties + assert not mocked_user_changes_container.setProperty.called, "The Print Sequence should not be attempted to be changed when there is only one enabled extruder." + + +def test_correctPrintSequence_TwoExtrudersEnabled_printSequenceIsOneAtATimeInUserSettings(machine_manager, application): + # Global container stack reports print sequence as one_at_a_time + mocked_stack = application.getGlobalContainerStack() + mocked_global_settings = {"print_sequence": "one_at_a_time"} + mocked_stack.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_global_settings) + + # The definition changes container reports 2 enabled extruders. Also the print sequence change is not saved in the + # quality changes container. + mocked_definition_changes_container = MagicMock(name = "DefinitionChangesContainer") + mocked_definition_changes_settings = {"extruders_enabled_count": 2, "print_sequence": None} + mocked_definition_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_definition_changes_settings) + mocked_stack.definitionChanges = mocked_definition_changes_container + + # The user changes container reports print sequence as "one-at-a-time" + mocked_user_changes_container = MagicMock(name = "UserChangesContainer") + mocked_user_changes_settings = {"print_sequence": "one_at_a_time"} + mocked_user_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_user_changes_settings) + mocked_stack.userChanges = mocked_user_changes_container + + machine_manager.correctPrintSequence() + + # After the function is called, the user changes container should have tried to remove the print sequence from the + # user changes container + mocked_user_changes_container.removeInstance.assert_called_once() + + +def test_correctPrintSequence_TwoExtrudersEnabled_printSequenceIsOneAtATimeInDefinitionChangesSettings(machine_manager, application): + # Global container stack reports print sequence as one_at_a_time + mocked_stack = application.getGlobalContainerStack() + mocked_global_settings = {"print_sequence": "one_at_a_time"} + mocked_stack.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_global_settings) + + # The definition changes container reports 2 enabled extruders and contains the print_sequence change to "one-at-a-time" + mocked_definition_changes_container = MagicMock(name = "DefinitionChangesContainer") + mocked_definition_changes_settings = {"extruders_enabled_count": 2, "print_sequence": "one_at_a_time"} + mocked_definition_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_definition_changes_settings) + mocked_stack.definitionChanges = mocked_definition_changes_container + + # The user changes container doesn't contain print_sequence + mocked_user_changes_container = MagicMock(name = "UserChangesContainer") + mocked_user_changes_settings = {"print_sequence": None} + mocked_user_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_user_changes_settings) + mocked_stack.userChanges = mocked_user_changes_container + + machine_manager.correctPrintSequence() + + # After the function is called, the print sequence should be set to "all-at-once" in the user changes container + mocked_user_changes_container.setProperty.assert_called_once_with("print_sequence", "value", "all_at_once") + + +def test_correctPrintSequence_TwoExtrudersEnabled_printSequenceInUserAndDefinitionChangesSettingsIsNone(machine_manager, application): + # Global container stack reports print sequence as one_at_a_time + mocked_stack = application.getGlobalContainerStack() + mocked_global_settings = {"print_sequence": "one_at_a_time"} + mocked_stack.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_global_settings) + + # The definition changes container reports 2 enabled extruders but doesn't contain the print_sequence changes + mocked_definition_changes_container = MagicMock(name = "DefinitionChangesContainer") + mocked_definition_changes_settings = {"extruders_enabled_count": 2, "print_sequence": None} + mocked_definition_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_definition_changes_settings) + mocked_stack.definitionChanges = mocked_definition_changes_container + + # The user changes container doesn't contain the print_sequence changes + mocked_user_changes_container = MagicMock(name = "UserChangesContainer") + mocked_user_changes_settings = {"print_sequence": None} + mocked_user_changes_container.getProperty = functools.partial(getPropertyMocked, settings_dict=mocked_user_changes_settings) + mocked_stack.userChanges = mocked_user_changes_container + + machine_manager.correctPrintSequence() + + # After the function is called, the print sequence should be set to "all-at-once" in the user changes container + mocked_user_changes_container.setProperty.assert_called_once_with("print_sequence", "value", "all_at_once") |