TestContainerTree.py 5.3 KB

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