TestVariantNode.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import collections # For OrderedDict to ensure that the first iteration of preferred material is dependable.
  4. from unittest.mock import patch, MagicMock
  5. import pytest
  6. from cura.Machines.VariantNode import VariantNode
  7. import copy
  8. instance_container_metadata_dict = {"fdmprinter": {"no_variant": [{"base_file": "material_1", "id": "material_1"}, {"base_file": "material_2", "id": "material_2"}]},
  9. "machine_1": {"no_variant": [{"base_file": "material_1", "id": "material_1"}, {"base_file": "material_2", "id": "material_2"}],
  10. "Variant One": [{"base_file": "material_1", "id": "material_1"}, {"base_file": "material_2", "id": "material_2"}]}}
  11. material_node_added_test_data = [({"type": "Not a material"}, ["material_1", "material_2"]), # Wrong type
  12. ({"type": "material", "base_file": "material_3"}, ["material_1", "material_2"]), # material_3 is on the "NOPE" list.
  13. ({"type": "material", "base_file": "material_4", "definition": "machine_3"}, ["material_1", "material_2"]), # Wrong machine
  14. ({"type": "material", "base_file": "material_4", "definition": "machine_1"}, ["material_1", "material_2", "material_4"]), # No variant
  15. ({"type": "material", "base_file": "material_4", "definition": "machine_1", "variant_name": "Variant Three"}, ["material_1", "material_2"]), # Wrong variant
  16. ({"type": "material", "base_file": "material_4", "definition": "machine_1", "variant_name": "Variant One"}, ["material_1", "material_2", "material_4"])
  17. ]
  18. material_node_update_test_data = [({"type": "material", "base_file": "material_1", "definition": "machine_1", "variant_name": "Variant One"}, ["material_1"], ["material_2"]),
  19. ({"type": "material", "base_file": "material_1", "definition": "fdmprinter", "variant_name": "Variant One"}, [], ["material_2", "material_1"]), # Too generic
  20. ({"type": "material", "base_file": "material_1", "definition": "machine_2", "variant_name": "Variant One"}, [], ["material_2", "material_1"]) # Wrong definition
  21. ]
  22. metadata_dict = {}
  23. def getMetadataEntrySideEffect(*args, **kwargs):
  24. return metadata_dict.get(args[0])
  25. def getInstanceContainerSideEffect(*args, **kwargs):
  26. variant = kwargs.get("variant")
  27. definition = kwargs.get("definition")
  28. if variant is not None:
  29. return instance_container_metadata_dict.get(definition).get(variant)
  30. return instance_container_metadata_dict.get(definition).get("no_variant")
  31. @pytest.fixture
  32. def machine_node():
  33. mocked_machine_node = MagicMock()
  34. mocked_machine_node.container_id = "machine_1"
  35. mocked_machine_node.isExcludedMaterialBaseFile = MagicMock(return_value=False)
  36. mocked_machine_node.preferred_material = "preferred_material"
  37. return mocked_machine_node
  38. @pytest.fixture
  39. def empty_variant_node(machine_node):
  40. """Constructs a variant node without any subnodes.
  41. This is useful for performing tests on VariantNode without being dependent
  42. on how _loadAll works.
  43. """
  44. container_registry = MagicMock(
  45. findContainersMetadata = MagicMock(return_value = [{"name": "test variant name"}])
  46. )
  47. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  48. with patch("cura.Machines.VariantNode.VariantNode._loadAll", MagicMock()):
  49. result = VariantNode("test_variant", machine = machine_node)
  50. return result
  51. @pytest.fixture
  52. def container_registry():
  53. result = MagicMock()
  54. result.findInstanceContainersMetadata = MagicMock(side_effect = getInstanceContainerSideEffect)
  55. result.findContainersMetadata = MagicMock(return_value = [{"name": "Variant One"}])
  56. return result
  57. def createMockedInstanceContainer():
  58. result = MagicMock()
  59. result.getMetaDataEntry = MagicMock(side_effect=getMetadataEntrySideEffect)
  60. return result
  61. def createVariantNode(container_id, machine_node, container_registry):
  62. with patch("cura.Machines.VariantNode.MaterialNode"): # We're not testing the material node here, so patch it out.
  63. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  64. return VariantNode(container_id, machine_node)
  65. def test_variantNodeInit(container_registry, machine_node):
  66. node = createVariantNode("variant_1", machine_node, container_registry)
  67. assert "material_1" in node.materials
  68. assert "material_2" in node.materials
  69. assert len(node.materials) == 2
  70. def test_variantNodeInit_excludedMaterial(container_registry, machine_node):
  71. machine_node.exclude_materials = ["material_1"]
  72. machine_node.isExcludedMaterialBaseFile = MagicMock(side_effect=lambda material: material == "material_1")
  73. node = createVariantNode("variant_1", machine_node, container_registry)
  74. assert "material_1" not in node.materials
  75. assert "material_2" in node.materials
  76. assert len(node.materials) == 1
  77. @pytest.mark.parametrize("metadata,material_result_list", material_node_added_test_data)
  78. def test_materialAdded(container_registry, machine_node, metadata, material_result_list):
  79. variant_node = createVariantNode("machine_1", machine_node, container_registry)
  80. machine_node.exclude_materials = ["material_3"]
  81. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  82. with patch("cura.Machines.VariantNode.MaterialNode"): # We're not testing the material node here, so patch it out.
  83. with patch.dict(metadata_dict, metadata):
  84. mocked_container = createMockedInstanceContainer()
  85. variant_node._materialAdded(mocked_container)
  86. assert len(material_result_list) == len(variant_node.materials)
  87. for name in material_result_list:
  88. assert name in variant_node.materials
  89. @pytest.mark.parametrize("metadata,changed_material_list,unchanged_material_list", material_node_update_test_data)
  90. def test_materialAdded_update(container_registry, machine_node, metadata, changed_material_list, unchanged_material_list):
  91. variant_node = createVariantNode("machine_1", machine_node, container_registry)
  92. original_material_nodes = copy.copy(variant_node.materials)
  93. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  94. with patch("cura.Machines.VariantNode.MaterialNode"): # We're not testing the material node here, so patch it out.
  95. with patch.dict(metadata_dict, metadata):
  96. mocked_container = createMockedInstanceContainer()
  97. variant_node._materialAdded(mocked_container)
  98. for key in unchanged_material_list:
  99. assert original_material_nodes[key] == variant_node.materials[key]
  100. for key in changed_material_list:
  101. assert original_material_nodes[key] != variant_node.materials[key]
  102. def test_preferredMaterialExactMatch(empty_variant_node):
  103. """Tests the preferred material when the exact base file is available in the
  104. materials list for this node.
  105. """
  106. empty_variant_node.materials = {
  107. "some_different_material": MagicMock(getMetaDataEntry = lambda x: 3),
  108. "preferred_material": MagicMock(getMetaDataEntry = lambda x: 3) # Exact match.
  109. }
  110. empty_variant_node.machine.preferred_material = "preferred_material"
  111. assert empty_variant_node.preferredMaterial(approximate_diameter = 3) == empty_variant_node.materials["preferred_material"], "It should match exactly on this one since it's the preferred material."
  112. def test_preferredMaterialSubmaterial(empty_variant_node):
  113. """Tests the preferred material when a submaterial is available in the
  114. materials list for this node.
  115. """
  116. empty_variant_node.materials = {
  117. "some_different_material": MagicMock(getMetaDataEntry = lambda x: 3),
  118. "preferred_material_base_file_aa04": MagicMock(getMetaDataEntry = lambda x: 3) # This is a submaterial of the preferred material.
  119. }
  120. empty_variant_node.machine.preferred_material = "preferred_material_base_file_aa04"
  121. assert empty_variant_node.preferredMaterial(approximate_diameter = 3) == empty_variant_node.materials["preferred_material_base_file_aa04"], "It should match on the submaterial just as well."
  122. def test_preferredMaterialDiameter(empty_variant_node):
  123. """Tests the preferred material matching on the approximate diameter of the filament.
  124. """
  125. empty_variant_node.materials = {
  126. "some_different_material": MagicMock(getMetaDataEntry = lambda x: 3),
  127. "preferred_material_wrong_diameter": MagicMock(getMetaDataEntry = lambda x: 2), # Approximate diameter is 2 instead of 3.
  128. "preferred_material_correct_diameter": MagicMock(getMetaDataEntry = lambda x: 3) # Correct approximate diameter.
  129. }
  130. empty_variant_node.machine.preferred_material = "preferred_material_correct_diameter"
  131. assert empty_variant_node.preferredMaterial(approximate_diameter = 3) == empty_variant_node.materials["preferred_material_correct_diameter"], "It should match only on the material with correct diameter."
  132. def test_preferredMaterialDiameterNoMatch(empty_variant_node):
  133. """Tests the preferred material matching on a different material if the diameter is wrong."""
  134. empty_variant_node.materials = collections.OrderedDict()
  135. empty_variant_node.materials["some_different_material"] = MagicMock(getMetaDataEntry = lambda x: 3) # This one first so that it gets iterated over first.
  136. empty_variant_node.materials["preferred_material"] = MagicMock(getMetaDataEntry = lambda x: 2) # Wrong diameter.
  137. assert empty_variant_node.preferredMaterial(approximate_diameter = 3) == empty_variant_node.materials["some_different_material"], "It should match on another material with the correct diameter if the preferred one is unavailable."
  138. def test_preferredMaterialDiameterPreference(empty_variant_node):
  139. """Tests that the material diameter is considered more important to match than
  140. the preferred diameter.
  141. """
  142. empty_variant_node.materials = collections.OrderedDict()
  143. empty_variant_node.materials["some_different_material"] = MagicMock(getMetaDataEntry = lambda x: 2) # This one first so that it gets iterated over first.
  144. empty_variant_node.materials["preferred_material"] = MagicMock(getMetaDataEntry = lambda x: 2) # Matches on ID but not diameter.
  145. empty_variant_node.materials["not_preferred_but_correct_diameter"] = MagicMock(getMetaDataEntry = lambda x: 3) # Matches diameter but not ID.
  146. assert empty_variant_node.preferredMaterial(approximate_diameter = 3) == empty_variant_node.materials["not_preferred_but_correct_diameter"], "It should match on the correct diameter even if it's not the preferred one."