TestMaterialNode.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.MaterialNode import MaterialNode
  6. instance_container_metadata_dict = {"fdmprinter": {"no_variant": [{"id": "quality_1", "material": "material_1"}]},
  7. "machine_1": {"variant_1": {"material_1": [{"id": "quality_2", "material": "material_1"}, {"id": "quality_3","material": "material_1"}]}}}
  8. metadata_dict = {}
  9. def getMetadataEntrySideEffect(*args, **kwargs):
  10. return metadata_dict.get(args[0])
  11. def createMockedInstanceContainer(container_id):
  12. result = MagicMock()
  13. result.getId = MagicMock(return_value=container_id)
  14. result.getMetaDataEntry = MagicMock(side_effect=getMetadataEntrySideEffect)
  15. return result
  16. def getInstanceContainerSideEffect(*args, **kwargs):
  17. variant = kwargs.get("variant")
  18. definition = kwargs.get("definition")
  19. type = kwargs.get("type")
  20. material = kwargs.get("material")
  21. if material is not None and variant is not None:
  22. definition_dict = instance_container_metadata_dict.get(definition)
  23. variant_dict = definition_dict.get(variant)
  24. material_dict = variant_dict.get("material_1")
  25. return material_dict
  26. if type == "quality":
  27. if variant is None:
  28. return instance_container_metadata_dict.get(definition).get("no_variant")
  29. else:
  30. return instance_container_metadata_dict.get(definition).get(variant).get("material_1")
  31. if definition is None:
  32. return [{"id": "material_1", "material": "material_1"}]
  33. return instance_container_metadata_dict.get(definition).get("no_variant")
  34. @pytest.fixture
  35. def container_registry():
  36. result = MagicMock()
  37. result.findInstanceContainersMetadata = MagicMock(side_effect=getInstanceContainerSideEffect)
  38. result.findContainersMetadata = MagicMock(return_value = [{"base_file": "material_1", "material": "test_material_type", "GUID": "omg zomg"}])
  39. return result
  40. def test_materialNodeInit_noMachineQuality(container_registry):
  41. variant_node = MagicMock()
  42. variant_node.variant_name = "variant_1"
  43. variant_node.machine.has_machine_quality = False
  44. with patch("cura.Machines.MaterialNode.QualityNode"):
  45. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  46. node = MaterialNode("material_1", variant_node)
  47. assert len(node.qualities) == 1
  48. assert "quality_1" in node.qualities
  49. def test_materialNodeInit_MachineQuality(container_registry):
  50. variant_node = MagicMock()
  51. variant_node.variant_name = "variant_1"
  52. variant_node.machine.has_machine_quality = True
  53. variant_node.machine.quality_definition = "machine_1"
  54. with patch("cura.Machines.MaterialNode.QualityNode"):
  55. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  56. node = MaterialNode("material_1", variant_node)
  57. assert len(node.qualities) == 2
  58. assert "quality_2" in node.qualities
  59. assert "quality_3" in node.qualities
  60. def test_onRemoved_wrongContainer(container_registry):
  61. variant_node = MagicMock()
  62. variant_node.variant_name = "variant_1"
  63. variant_node.machine.has_machine_quality = True
  64. variant_node.machine.quality_definition = "machine_1"
  65. variant_node.materials = {"material_1": MagicMock()}
  66. with patch("cura.Machines.MaterialNode.QualityNode"):
  67. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance",MagicMock(return_value=container_registry)):
  68. node = MaterialNode("material_1", variant_node)
  69. container = createMockedInstanceContainer("material_2")
  70. node._onRemoved(container)
  71. assert "material_1" in variant_node.materials
  72. def test_onRemoved_rightContainer(container_registry):
  73. variant_node = MagicMock()
  74. variant_node.variant_name = "variant_1"
  75. variant_node.machine.has_machine_quality = True
  76. variant_node.machine.quality_definition = "machine_1"
  77. with patch("cura.Machines.MaterialNode.QualityNode"):
  78. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  79. node = MaterialNode("material_1", variant_node)
  80. container = createMockedInstanceContainer("material_1")
  81. variant_node.materials = {"material_1": MagicMock()}
  82. node._onRemoved(container)
  83. assert "material_1" not in variant_node.materials
  84. def test_onMetadataChanged(container_registry):
  85. variant_node = MagicMock()
  86. variant_node.variant_name = "variant_1"
  87. variant_node.machine.has_machine_quality = True
  88. variant_node.machine.quality_definition = "machine_1"
  89. with patch("cura.Machines.MaterialNode.QualityNode"):
  90. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  91. node = MaterialNode("material_1", variant_node)
  92. # We only do this now since we do want it to be constructed but not actually re-evaluated.
  93. node._loadAll = MagicMock()
  94. container = createMockedInstanceContainer("material_1")
  95. container.getMetaData = MagicMock(return_value = {"base_file": "new_base_file", "material": "new_material_type", "GUID": "new_guid"})
  96. node._onMetadataChanged(container)
  97. assert node.material_type == "new_material_type"
  98. assert node.guid == "new_guid"
  99. assert node.base_file == "new_base_file"
  100. def test_onMetadataChanged_wrongContainer(container_registry):
  101. variant_node = MagicMock()
  102. variant_node.variant_name = "variant_1"
  103. variant_node.machine.has_machine_quality = True
  104. variant_node.machine.quality_definition = "machine_1"
  105. with patch("cura.Machines.MaterialNode.QualityNode"):
  106. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance",
  107. MagicMock(return_value=container_registry)):
  108. node = MaterialNode("material_1", variant_node)
  109. # We only do this now since we do want it to be constructed but not actually re-evaluated.
  110. node._loadAll = MagicMock()
  111. container = createMockedInstanceContainer("material_2")
  112. container.getMetaData = MagicMock(
  113. return_value={"base_file": "new_base_file", "material": "new_material_type", "GUID": "new_guid"})
  114. node._onMetadataChanged(container)
  115. assert node.material_type == "test_material_type"
  116. assert node.guid == "omg zomg"
  117. assert node.base_file == "material_1"