TestVariantNode.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.preferred_material = "preferred_material"
  36. return mocked_machine_node
  37. @pytest.fixture
  38. def empty_variant_node(machine_node):
  39. """Constructs a variant node without any subnodes.
  40. This is useful for performing tests on VariantNode without being dependent
  41. on how _loadAll works.
  42. """
  43. container_registry = MagicMock(
  44. findContainersMetadata = MagicMock(return_value = [{"name": "test variant name"}])
  45. )
  46. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  47. with patch("cura.Machines.VariantNode.VariantNode._loadAll", MagicMock()):
  48. result = VariantNode("test_variant", machine = machine_node)
  49. return result
  50. @pytest.fixture
  51. def container_registry():
  52. result = MagicMock()
  53. result.findInstanceContainersMetadata = MagicMock(side_effect = getInstanceContainerSideEffect)
  54. result.findContainersMetadata = MagicMock(return_value = [{"name": "Variant One"}])
  55. return result
  56. def createMockedInstanceContainer():
  57. result = MagicMock()
  58. result.getMetaDataEntry = MagicMock(side_effect=getMetadataEntrySideEffect)
  59. return result
  60. def createVariantNode(container_id, machine_node, container_registry):
  61. with patch("cura.Machines.VariantNode.MaterialNode"): # We're not testing the material node here, so patch it out.
  62. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  63. return VariantNode(container_id, machine_node)
  64. def test_variantNodeInit(container_registry, machine_node):
  65. node = createVariantNode("variant_1", machine_node, container_registry)
  66. assert "material_1" in node.materials
  67. assert "material_2" in node.materials
  68. assert len(node.materials) == 2
  69. def test_variantNodeInit_excludedMaterial(container_registry, machine_node):
  70. machine_node.exclude_materials = ["material_1"]
  71. node = createVariantNode("variant_1", machine_node, container_registry)
  72. assert "material_1" not in node.materials
  73. assert "material_2" in node.materials
  74. assert len(node.materials) == 1
  75. @pytest.mark.parametrize("metadata,material_result_list", material_node_added_test_data)
  76. def test_materialAdded(container_registry, machine_node, metadata, material_result_list):
  77. variant_node = createVariantNode("machine_1", machine_node, container_registry)
  78. machine_node.exclude_materials = ["material_3"]
  79. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  80. with patch("cura.Machines.VariantNode.MaterialNode"): # We're not testing the material node here, so patch it out.
  81. with patch.dict(metadata_dict, metadata):
  82. mocked_container = createMockedInstanceContainer()
  83. variant_node._materialAdded(mocked_container)
  84. assert len(material_result_list) == len(variant_node.materials)
  85. for name in material_result_list:
  86. assert name in variant_node.materials
  87. @pytest.mark.parametrize("metadata,changed_material_list,unchanged_material_list", material_node_update_test_data)
  88. def test_materialAdded_update(container_registry, machine_node, metadata, changed_material_list, unchanged_material_list):
  89. variant_node = createVariantNode("machine_1", machine_node, container_registry)
  90. original_material_nodes = copy.copy(variant_node.materials)
  91. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  92. with patch("cura.Machines.VariantNode.MaterialNode"): # We're not testing the material node here, so patch it out.
  93. with patch.dict(metadata_dict, metadata):
  94. mocked_container = createMockedInstanceContainer()
  95. variant_node._materialAdded(mocked_container)
  96. for key in unchanged_material_list:
  97. assert original_material_nodes[key] == variant_node.materials[key]
  98. for key in changed_material_list:
  99. assert original_material_nodes[key] != variant_node.materials[key]
  100. def test_preferredMaterialExactMatch(empty_variant_node):
  101. """Tests the preferred material when the exact base file is available in the
  102. materials list for this node.
  103. """
  104. empty_variant_node.materials = {
  105. "some_different_material": MagicMock(getMetaDataEntry = lambda x: 3),
  106. "preferred_material": MagicMock(getMetaDataEntry = lambda x: 3) # Exact match.
  107. }
  108. empty_variant_node.machine.preferred_material = "preferred_material"
  109. 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."
  110. def test_preferredMaterialSubmaterial(empty_variant_node):
  111. """Tests the preferred material when a submaterial is available in the
  112. materials list for this node.
  113. """
  114. empty_variant_node.materials = {
  115. "some_different_material": MagicMock(getMetaDataEntry = lambda x: 3),
  116. "preferred_material_base_file_aa04": MagicMock(getMetaDataEntry = lambda x: 3) # This is a submaterial of the preferred material.
  117. }
  118. empty_variant_node.machine.preferred_material = "preferred_material_base_file_aa04"
  119. 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."
  120. def test_preferredMaterialDiameter(empty_variant_node):
  121. """Tests the preferred material matching on the approximate diameter of the filament.
  122. """
  123. empty_variant_node.materials = {
  124. "some_different_material": MagicMock(getMetaDataEntry = lambda x: 3),
  125. "preferred_material_wrong_diameter": MagicMock(getMetaDataEntry = lambda x: 2), # Approximate diameter is 2 instead of 3.
  126. "preferred_material_correct_diameter": MagicMock(getMetaDataEntry = lambda x: 3) # Correct approximate diameter.
  127. }
  128. empty_variant_node.machine.preferred_material = "preferred_material_correct_diameter"
  129. 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."
  130. def test_preferredMaterialDiameterNoMatch(empty_variant_node):
  131. """Tests the preferred material matching on a different material if the diameter is wrong."""
  132. empty_variant_node.materials = collections.OrderedDict()
  133. empty_variant_node.materials["some_different_material"] = MagicMock(getMetaDataEntry = lambda x: 3) # This one first so that it gets iterated over first.
  134. empty_variant_node.materials["preferred_material"] = MagicMock(getMetaDataEntry = lambda x: 2) # Wrong diameter.
  135. 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."
  136. def test_preferredMaterialDiameterPreference(empty_variant_node):
  137. """Tests that the material diameter is considered more important to match than
  138. the preferred diameter.
  139. """
  140. empty_variant_node.materials = collections.OrderedDict()
  141. empty_variant_node.materials["some_different_material"] = MagicMock(getMetaDataEntry = lambda x: 2) # This one first so that it gets iterated over first.
  142. empty_variant_node.materials["preferred_material"] = MagicMock(getMetaDataEntry = lambda x: 2) # Matches on ID but not diameter.
  143. empty_variant_node.materials["not_preferred_but_correct_diameter"] = MagicMock(getMetaDataEntry = lambda x: 3) # Matches diameter but not ID.
  144. 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."