TestContainerTree.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest.mock import patch, MagicMock
  4. import pytest
  5. from UM.Settings.DefinitionContainer import DefinitionContainer
  6. from cura.Machines.ContainerTree import ContainerTree
  7. from cura.Settings.GlobalStack import GlobalStack
  8. def createMockedStack(definition_id: str):
  9. result = MagicMock(spec = GlobalStack)
  10. result.definition.getId = MagicMock(return_value = definition_id)
  11. extruder_left_mock = MagicMock()
  12. extruder_left_mock.variant.getName = MagicMock(return_value = definition_id + "_left_variant_name")
  13. extruder_left_mock.material.getMetaDataEntry = MagicMock(return_value = definition_id + "_left_material_base_file")
  14. extruder_left_mock.isEnabled = True
  15. extruder_right_mock = MagicMock()
  16. extruder_right_mock.variant.getName = MagicMock(return_value = definition_id + "_right_variant_name")
  17. extruder_right_mock.material.getMetaDataEntry = MagicMock(return_value = definition_id + "_right_material_base_file")
  18. extruder_right_mock.isEnabled = True
  19. extruder_list = [extruder_left_mock, extruder_right_mock]
  20. result.extruderList = extruder_list
  21. return result
  22. @pytest.fixture
  23. def container_registry():
  24. result = MagicMock()
  25. result.findContainerStacks = MagicMock(return_value = [createMockedStack("machine_1"), createMockedStack("machine_2")])
  26. result.findContainersMetadata = lambda id: [{"id": id}] if id in {"machine_1", "machine_2"} else []
  27. return result
  28. @pytest.fixture
  29. def application():
  30. return MagicMock(getGlobalContainerStack = MagicMock(return_value = createMockedStack("current_global_stack")))
  31. def test_containerTreeInit(container_registry):
  32. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  33. with patch("UM.Application.Application.getInstance"):
  34. container_tree = ContainerTree()
  35. assert "machine_1" in container_tree.machines
  36. assert "machine_2" in container_tree.machines
  37. def test_getCurrentQualityGroupsNoGlobalStack(container_registry):
  38. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  39. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = MagicMock(getGlobalContainerStack = MagicMock(return_value = None)))):
  40. container_tree = ContainerTree()
  41. result = container_tree.getCurrentQualityGroups()
  42. assert len(result) == 0
  43. def test_getCurrentQualityGroups(container_registry, application):
  44. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  45. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
  46. container_tree = ContainerTree()
  47. container_tree.machines._machines["current_global_stack"] = MagicMock() # Mock so that we can track whether the getQualityGroups function gets called with correct parameters.
  48. result = container_tree.getCurrentQualityGroups()
  49. # As defined in the fixture for application.
  50. expected_variant_names = ["current_global_stack_left_variant_name", "current_global_stack_right_variant_name"]
  51. expected_material_base_files = ["current_global_stack_left_material_base_file", "current_global_stack_right_material_base_file"]
  52. expected_is_enabled = [True, True]
  53. container_tree.machines["current_global_stack"].getQualityGroups.assert_called_with(expected_variant_names, expected_material_base_files, expected_is_enabled)
  54. assert result == container_tree.machines["current_global_stack"].getQualityGroups.return_value
  55. def test_getCurrentQualityChangesGroupsNoGlobalStack(container_registry):
  56. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  57. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = MagicMock(getGlobalContainerStack = MagicMock(return_value = None)))):
  58. container_tree = ContainerTree()
  59. result = container_tree.getCurrentQualityChangesGroups()
  60. assert len(result) == 0
  61. def test_getCurrentQualityChangesGroups(container_registry, application):
  62. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  63. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
  64. container_tree = ContainerTree()
  65. container_tree.machines._machines["current_global_stack"] = MagicMock() # Mock so that we can track whether the getQualityGroups function gets called with correct parameters.
  66. result = container_tree.getCurrentQualityChangesGroups()
  67. # As defined in the fixture for application.
  68. expected_variant_names = ["current_global_stack_left_variant_name", "current_global_stack_right_variant_name"]
  69. expected_material_base_files = ["current_global_stack_left_material_base_file", "current_global_stack_right_material_base_file"]
  70. expected_is_enabled = [True, True]
  71. container_tree.machines["current_global_stack"].getQualityChangesGroups.assert_called_with(expected_variant_names, expected_material_base_files, expected_is_enabled)
  72. assert result == container_tree.machines["current_global_stack"].getQualityChangesGroups.return_value