TestSettingInheritanceManager.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from unittest.mock import patch, MagicMock
  2. import pytest
  3. from UM.Settings.SettingFunction import SettingFunction
  4. from UM.Settings.SettingInstance import InstanceState
  5. from cura.Settings.SettingInheritanceManager import SettingInheritanceManager
  6. setting_function = SettingFunction("")
  7. setting_function.getUsedSettingKeys = MagicMock(return_value = ["omg", "zomg"])
  8. setting_property_dict = {"setting_1": {},
  9. "setting_2": {"state": InstanceState.User, "enabled": False},
  10. "setting_3": {"state": InstanceState.User, "enabled": True},
  11. "setting_4": {"state": InstanceState.User, "enabled": True, "value": 12},
  12. "setting_5": {"state": InstanceState.User, "enabled": True, "value": setting_function}}
  13. def getPropertySideEffect(*args, **kwargs):
  14. properties = setting_property_dict.get(args[0])
  15. if properties:
  16. return properties.get(args[1])
  17. @pytest.fixture
  18. def setting_inheritance_manager():
  19. with patch("UM.Application.Application.getInstance"):
  20. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  21. return SettingInheritanceManager()
  22. @pytest.fixture
  23. def mocked_stack():
  24. mocked_stack = MagicMock()
  25. mocked_stack.getProperty = MagicMock(side_effect=getPropertySideEffect)
  26. mocked_stack.getNextStack = MagicMock(return_value = None)
  27. mocked_stack.getAllKeys = MagicMock(return_value = ["omg", "zomg", "blarg"])
  28. return mocked_stack
  29. def test_getChildrenKeysWithOverrideNoGlobalStack(setting_inheritance_manager):
  30. setting_inheritance_manager._global_container_stack = None
  31. assert setting_inheritance_manager.getChildrenKeysWithOverride("derp") == []
  32. def test_getChildrenKeysWithOverrideEmptyDefinitions(setting_inheritance_manager):
  33. mocked_global_container = MagicMock()
  34. mocked_global_container.definition.findDefinitions = MagicMock(return_value = [])
  35. setting_inheritance_manager._global_container_stack = mocked_global_container
  36. assert setting_inheritance_manager.getChildrenKeysWithOverride("derp") == []
  37. def test_getChildrenKeysWithOverride(setting_inheritance_manager):
  38. mocked_global_container = MagicMock()
  39. mocked_definition = MagicMock()
  40. mocked_definition.getAllKeys = MagicMock(return_value = ["omg", "zomg", "blarg"])
  41. mocked_global_container.definition.findDefinitions = MagicMock(return_value=[mocked_definition])
  42. setting_inheritance_manager._global_container_stack = mocked_global_container
  43. setting_inheritance_manager._settings_with_inheritance_warning = ["omg", "zomg"]
  44. assert setting_inheritance_manager.getChildrenKeysWithOverride("derp") == ["omg", "zomg"]
  45. def test_manualRemoveOverrideWrongSetting(setting_inheritance_manager):
  46. setting_inheritance_manager._settings_with_inheritance_warning = ["omg", "zomg"]
  47. assert setting_inheritance_manager.settingsWithInheritanceWarning == ["omg", "zomg"]
  48. # Shouldn't do anything
  49. setting_inheritance_manager.manualRemoveOverride("BLARG")
  50. assert setting_inheritance_manager.settingsWithInheritanceWarning == ["omg", "zomg"]
  51. def test_manualRemoveOverrideExistingSetting(setting_inheritance_manager):
  52. setting_inheritance_manager._settings_with_inheritance_warning = ["omg", "zomg"]
  53. assert setting_inheritance_manager.settingsWithInheritanceWarning == ["omg", "zomg"]
  54. # Shouldn't do anything
  55. setting_inheritance_manager.manualRemoveOverride("omg")
  56. assert setting_inheritance_manager.settingsWithInheritanceWarning == ["zomg"]
  57. def test_getOverridesForExtruderNoGlobalStack(setting_inheritance_manager):
  58. setting_inheritance_manager._global_container_stack = None
  59. assert setting_inheritance_manager.getOverridesForExtruder("derp", 0) == []
  60. def test_settingIsOverwritingInheritanceNoUserState(setting_inheritance_manager, mocked_stack):
  61. # Setting 1 doesn't have a user state, so it cant have an override
  62. assert not setting_inheritance_manager._settingIsOverwritingInheritance("setting_1", mocked_stack)
  63. def test_settingIsOverwritingInheritanceNotEnabled(setting_inheritance_manager, mocked_stack):
  64. # Setting 2 doesn't have a enabled, so it cant have an override
  65. assert not setting_inheritance_manager._settingIsOverwritingInheritance("setting_2", mocked_stack)
  66. def test_settingIsOverwritingInheritanceNoContainers(setting_inheritance_manager, mocked_stack):
  67. mocked_stack.getContainers = MagicMock(return_value = [])
  68. # All the properties are correct, but there are no containers :(
  69. assert not setting_inheritance_manager._settingIsOverwritingInheritance("setting_3", mocked_stack)
  70. def test_settingIsOverwritingInheritanceNoneValue(setting_inheritance_manager, mocked_stack):
  71. mocked_container = MagicMock()
  72. mocked_container.getProperty = MagicMock(side_effect=getPropertySideEffect)
  73. mocked_stack.getContainers = MagicMock(return_value = [mocked_container])
  74. # Setting 3 doesn't have a value, so even though the container is there, it's value is None
  75. assert not setting_inheritance_manager._settingIsOverwritingInheritance("setting_3", mocked_stack)
  76. def test_settingIsOverwritingInheritanceNoSettingFunction(setting_inheritance_manager, mocked_stack):
  77. mocked_container = MagicMock()
  78. mocked_container.getProperty = MagicMock(side_effect=getPropertySideEffect)
  79. mocked_stack.getContainers = MagicMock(return_value=[mocked_container])
  80. # Setting 4 does have a value, but it's not a settingFunction
  81. assert not setting_inheritance_manager._settingIsOverwritingInheritance("setting_4", mocked_stack)
  82. def test_settingIsOverwritingInheritanceSingleSettingFunction(setting_inheritance_manager, mocked_stack):
  83. mocked_container = MagicMock()
  84. mocked_container.getProperty = MagicMock(side_effect=getPropertySideEffect)
  85. mocked_stack.getContainers = MagicMock(return_value=[mocked_container])
  86. setting_inheritance_manager._active_container_stack = mocked_stack
  87. # Setting 5 does have a value, but we only have one container filled
  88. assert not setting_inheritance_manager._settingIsOverwritingInheritance("setting_5", mocked_stack)
  89. def test_settingIsOverwritingInheritance(setting_inheritance_manager, mocked_stack):
  90. mocked_container = MagicMock()
  91. mocked_second_container = MagicMock()
  92. mocked_second_container.getProperty = MagicMock(return_value = 12)
  93. mocked_container.getProperty = MagicMock(side_effect=getPropertySideEffect)
  94. mocked_stack.getContainers = MagicMock(return_value=[mocked_second_container, mocked_container])
  95. setting_inheritance_manager._active_container_stack = mocked_stack
  96. assert setting_inheritance_manager._settingIsOverwritingInheritance("setting_5", mocked_stack)