TestMachineNode.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.Interfaces import ContainerInterface
  6. from cura.Machines.MachineNode import MachineNode
  7. metadata_dict = {
  8. "has_materials": "false",
  9. "has_variants": "true",
  10. "has_machine_quality": "true",
  11. "quality_definition": "test_quality_definition",
  12. "exclude_materials": ["excluded_material_1", "excluded_material_2"],
  13. "preferred_variant_name": "beautiful_nozzle",
  14. "preferred_material": "beautiful_material",
  15. "preferred_quality_type": "beautiful_quality_type"
  16. }
  17. @pytest.fixture
  18. def container_registry():
  19. result = MagicMock()
  20. result.findInstanceContainersMetadata = MagicMock(return_value = [{"id": "variant_1", "name": "Variant One", "quality_type": "normal"}, {"id": "variant_2", "name": "Variant Two", "quality_type": "great"}])
  21. result.findContainersMetadata = MagicMock(return_value = [metadata_dict])
  22. return result
  23. @pytest.fixture
  24. def empty_machine_node():
  25. """Creates a machine node without anything underneath it. No sub-nodes.
  26. For testing stuff with machine nodes without testing _loadAll(). You'll need
  27. to add subnodes manually in your test.
  28. """
  29. empty_container_registry = MagicMock()
  30. empty_container_registry.findContainersMetadata = MagicMock(return_value = [metadata_dict]) # Still contain the MachineNode's own metadata for the constructor.
  31. empty_container_registry.findInstanceContainersMetadata = MagicMock(return_value = [])
  32. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = empty_container_registry)):
  33. with patch("cura.Machines.MachineNode.MachineNode._loadAll", MagicMock()):
  34. return MachineNode("machine_1")
  35. def getMetadataEntrySideEffect(*args, **kwargs):
  36. return metadata_dict.get(args[0])
  37. def createMockedInstanceContainer():
  38. result = MagicMock(spec = ContainerInterface)
  39. result.getMetaDataEntry = MagicMock(side_effect = getMetadataEntrySideEffect)
  40. return result
  41. def createMachineNode(container_id, container_registry):
  42. with patch("cura.Machines.MachineNode.VariantNode"): # We're not testing the variant node here, so patch it out.
  43. with patch("cura.Machines.MachineNode.QualityNode"):
  44. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  45. return MachineNode(container_id)
  46. def test_machineNodeInit(container_registry):
  47. machine_node = createMachineNode("machine_1", container_registry)
  48. # As variants get stored by name, we want to check if those get added.
  49. assert "Variant One" in machine_node.variants
  50. assert "Variant Two" in machine_node.variants
  51. assert len(machine_node.variants) == 2 # And ensure that *only* those two got added.
  52. def test_metadataProperties(container_registry):
  53. node = createMachineNode("machine_1", container_registry)
  54. # Check if each of the metadata entries got stored properly.
  55. assert not node.has_materials
  56. assert node.has_variants
  57. assert node.has_machine_quality
  58. assert node.quality_definition == metadata_dict["quality_definition"]
  59. assert node.exclude_materials == metadata_dict["exclude_materials"]
  60. assert node.preferred_variant_name == metadata_dict["preferred_variant_name"]
  61. assert node.preferred_material == metadata_dict["preferred_material"]
  62. assert node.preferred_quality_type == metadata_dict["preferred_quality_type"]
  63. def test_getQualityGroupsBothExtrudersAvailable(empty_machine_node):
  64. """Test getting quality groups when there are quality profiles available for
  65. the requested configurations on two extruders.
  66. """
  67. # Prepare a tree inside the machine node.
  68. extruder_0_node = MagicMock(quality_type = "quality_type_1")
  69. extruder_1_node = MagicMock(quality_type = "quality_type_1") # Same quality type, so this is the one that can be returned.
  70. empty_machine_node.variants = {
  71. "variant_1": MagicMock(
  72. materials = {
  73. "material_1": MagicMock(
  74. qualities = {
  75. "quality_1": extruder_0_node
  76. }
  77. )
  78. }
  79. ),
  80. "variant_2": MagicMock(
  81. materials = {
  82. "material_2": MagicMock(
  83. qualities = {
  84. "quality_2": extruder_1_node
  85. }
  86. )
  87. }
  88. )
  89. }
  90. global_node = MagicMock(
  91. container = MagicMock(id = "global_quality_container"), # Needs to exist, otherwise it won't add this quality type.
  92. getMetaDataEntry = lambda _, __: "Global Quality Profile Name"
  93. )
  94. empty_machine_node.global_qualities = {
  95. "quality_type_1": global_node
  96. }
  97. # Request the quality groups for the variants in the machine tree.
  98. result = empty_machine_node.getQualityGroups(["variant_1", "variant_2"], ["material_1", "material_2"], [True, True])
  99. assert "quality_type_1" in result, "This quality type was available for both extruders."
  100. assert result["quality_type_1"].node_for_global == global_node
  101. assert result["quality_type_1"].nodes_for_extruders[0] == extruder_0_node
  102. assert result["quality_type_1"].nodes_for_extruders[1] == extruder_1_node
  103. assert result["quality_type_1"].name == global_node.getMetaDataEntry("name", "Unnamed Profile")
  104. assert result["quality_type_1"].quality_type == "quality_type_1"
  105. def test_getQualityGroupsAvailability(empty_machine_node):
  106. """Test the "is_available" flag on quality groups.
  107. If a profile is available for a quality type on an extruder but not on all
  108. extruders, there should be a quality group for it but it should not be made
  109. available.
  110. """
  111. # Prepare a tree inside the machine node.
  112. extruder_0_both = MagicMock(quality_type = "quality_type_both") # This quality type is available for both extruders.
  113. extruder_1_both = MagicMock(quality_type = "quality_type_both")
  114. extruder_0_exclusive = MagicMock(quality_type = "quality_type_0") # These quality types are only available on one of the extruders.
  115. extruder_1_exclusive = MagicMock(quality_type = "quality_type_1")
  116. empty_machine_node.variants = {
  117. "variant_1": MagicMock(
  118. materials = {
  119. "material_1": MagicMock(
  120. qualities = {
  121. "quality_0_both": extruder_0_both,
  122. "quality_0_exclusive": extruder_0_exclusive
  123. }
  124. )
  125. }
  126. ),
  127. "variant_2": MagicMock(
  128. materials = {
  129. "material_2": MagicMock(
  130. qualities = {
  131. "quality_1_both": extruder_1_both,
  132. "quality_1_exclusive": extruder_1_exclusive
  133. }
  134. )
  135. }
  136. )
  137. }
  138. global_both = MagicMock(container = MagicMock(id = "global_quality_both"), getMetaDataEntry = lambda _, __: "Global Quality Both")
  139. global_0 = MagicMock(container = MagicMock(id = "global_quality_0"), getMetaDataEntry = lambda _, __: "Global Quality 0 Exclusive")
  140. global_1 = MagicMock(container = MagicMock(id = "global_quality_1"), getMetaDataEntry = lambda _, __: "Global Quality 1 Exclusive")
  141. empty_machine_node.global_qualities = {
  142. "quality_type_both": global_both,
  143. "quality_type_0": global_0,
  144. "quality_type_1": global_1
  145. }
  146. # Request the quality groups for the variants in the machine tree.
  147. result = empty_machine_node.getQualityGroups(["variant_1", "variant_2"], ["material_1", "material_2"], [True, True])
  148. assert "quality_type_both" in result, "This quality type was available on both extruders."
  149. assert result["quality_type_both"].is_available, "This quality type was available on both extruders and thus should be made available."
  150. assert "quality_type_0" in result, "This quality type was available for one of the extruders, and so there must be a group for it (even though it's unavailable)."
  151. assert not result["quality_type_0"].is_available, "This quality type was only available for one of the extruders and thus can't be activated."
  152. assert "quality_type_1" in result, "This quality type was available for one of the extruders, and so there must be a group for it (even though it's unavailable)."
  153. assert not result["quality_type_1"].is_available, "This quality type was only available for one of the extruders and thus can't be activated."