TestMachineNode.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. ## Creates a machine node without anything underneath it. No sub-nodes.
  24. #
  25. # For testing stuff with machine nodes without testing _loadAll(). You'll need
  26. # to add subnodes manually in your test.
  27. @pytest.fixture
  28. def empty_machine_node():
  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. ## Test getting quality groups when there are quality profiles available for
  64. # the requested configurations on two extruders.
  65. def test_getQualityGroupsBothExtrudersAvailable(empty_machine_node):
  66. # Prepare a tree inside the machine node.
  67. extruder_0_node = MagicMock(quality_type = "quality_type_1")
  68. extruder_1_node = MagicMock(quality_type = "quality_type_1") # Same quality type, so this is the one that can be returned.
  69. empty_machine_node.variants = {
  70. "variant_1": MagicMock(
  71. materials = {
  72. "material_1": MagicMock(
  73. qualities = {
  74. "quality_1": extruder_0_node
  75. }
  76. )
  77. }
  78. ),
  79. "variant_2": MagicMock(
  80. materials = {
  81. "material_2": MagicMock(
  82. qualities = {
  83. "quality_2": extruder_1_node
  84. }
  85. )
  86. }
  87. )
  88. }
  89. global_node = MagicMock(
  90. container = MagicMock(id = "global_quality_container"), # Needs to exist, otherwise it won't add this quality type.
  91. getMetaDataEntry = lambda _, __: "Global Quality Profile Name"
  92. )
  93. empty_machine_node.global_qualities = {
  94. "quality_type_1": global_node
  95. }
  96. # Request the quality groups for the variants in the machine tree.
  97. result = empty_machine_node.getQualityGroups(["variant_1", "variant_2"], ["material_1", "material_2"], [True, True])
  98. assert "quality_type_1" in result, "This quality type was available for both extruders."
  99. assert result["quality_type_1"].node_for_global == global_node
  100. assert result["quality_type_1"].nodes_for_extruders[0] == extruder_0_node
  101. assert result["quality_type_1"].nodes_for_extruders[1] == extruder_1_node
  102. assert result["quality_type_1"].name == global_node.getMetaDataEntry("name", "Unnamed Profile")
  103. assert result["quality_type_1"].quality_type == "quality_type_1"
  104. ## Test the "is_available" flag on quality groups.
  105. #
  106. # If a profile is available for a quality type on an extruder but not on all
  107. # extruders, there should be a quality group for it but it should not be made
  108. # available.
  109. def test_getQualityGroupsAvailability(empty_machine_node):
  110. # Prepare a tree inside the machine node.
  111. extruder_0_both = MagicMock(quality_type = "quality_type_both") # This quality type is available for both extruders.
  112. extruder_1_both = MagicMock(quality_type = "quality_type_both")
  113. extruder_0_exclusive = MagicMock(quality_type = "quality_type_0") # These quality types are only available on one of the extruders.
  114. extruder_1_exclusive = MagicMock(quality_type = "quality_type_1")
  115. empty_machine_node.variants = {
  116. "variant_1": MagicMock(
  117. materials = {
  118. "material_1": MagicMock(
  119. qualities = {
  120. "quality_0_both": extruder_0_both,
  121. "quality_0_exclusive": extruder_0_exclusive
  122. }
  123. )
  124. }
  125. ),
  126. "variant_2": MagicMock(
  127. materials = {
  128. "material_2": MagicMock(
  129. qualities = {
  130. "quality_1_both": extruder_1_both,
  131. "quality_1_exclusive": extruder_1_exclusive
  132. }
  133. )
  134. }
  135. )
  136. }
  137. global_both = MagicMock(container = MagicMock(id = "global_quality_both"), getMetaDataEntry = lambda _, __: "Global Quality Both")
  138. global_0 = MagicMock(container = MagicMock(id = "global_quality_0"), getMetaDataEntry = lambda _, __: "Global Quality 0 Exclusive")
  139. global_1 = MagicMock(container = MagicMock(id = "global_quality_1"), getMetaDataEntry = lambda _, __: "Global Quality 1 Exclusive")
  140. empty_machine_node.global_qualities = {
  141. "quality_type_both": global_both,
  142. "quality_type_0": global_0,
  143. "quality_type_1": global_1
  144. }
  145. # Request the quality groups for the variants in the machine tree.
  146. result = empty_machine_node.getQualityGroups(["variant_1", "variant_2"], ["material_1", "material_2"], [True, True])
  147. assert "quality_type_both" in result, "This quality type was available on both extruders."
  148. assert result["quality_type_both"].is_available, "This quality type was available on both extruders and thus should be made available."
  149. 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)."
  150. 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."
  151. 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)."
  152. 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."