TestCuraStackBuilder.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from unittest.mock import patch, MagicMock
  2. import pytest
  3. from UM.Settings.InstanceContainer import InstanceContainer
  4. from cura.Machines.QualityGroup import QualityGroup
  5. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  6. @pytest.fixture
  7. def global_variant():
  8. container = InstanceContainer(container_id="global_variant")
  9. container.setMetaDataEntry("type", "variant")
  10. return container
  11. @pytest.fixture
  12. def material_instance_container():
  13. container = InstanceContainer(container_id="material container")
  14. container.setMetaDataEntry("type", "material")
  15. return container
  16. @pytest.fixture
  17. def quality_container():
  18. container = InstanceContainer(container_id="quality container")
  19. container.setMetaDataEntry("type", "quality")
  20. return container
  21. @pytest.fixture
  22. def intent_container():
  23. container = InstanceContainer(container_id="intent container")
  24. container.setMetaDataEntry("type", "intent")
  25. return container
  26. @pytest.fixture
  27. def quality_changes_container():
  28. container = InstanceContainer(container_id="quality changes container")
  29. container.setMetaDataEntry("type", "quality_changes")
  30. return container
  31. def test_createMachineWithUnknownDefinition(application, container_registry):
  32. application.getContainerRegistry = MagicMock(return_value=container_registry)
  33. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
  34. with patch("UM.ConfigurationErrorMessage.ConfigurationErrorMessage.getInstance") as mocked_config_error:
  35. assert CuraStackBuilder.createMachine("Whatever", "NOPE") is None
  36. assert mocked_config_error.addFaultyContainers.called_with("NOPE")
  37. def test_createMachine(application, container_registry, definition_container, global_variant, material_instance_container,
  38. quality_container, intent_container, quality_changes_container):
  39. variant_manager = MagicMock(name = "Variant Manager")
  40. quality_manager = MagicMock(name = "Quality Manager")
  41. global_variant_node = MagicMock(name = "global variant node")
  42. global_variant_node.container = global_variant
  43. variant_manager.getDefaultVariantNode = MagicMock(return_value = global_variant_node)
  44. quality_group = QualityGroup(name = "zomg", quality_type = "normal")
  45. quality_group.node_for_global = MagicMock(name = "Node for global")
  46. quality_group.node_for_global.container = quality_container
  47. quality_manager.getQualityGroups = MagicMock(return_value = {"normal": quality_group})
  48. application.getContainerRegistry = MagicMock(return_value=container_registry)
  49. application.empty_material_container = material_instance_container
  50. application.empty_quality_container = quality_container
  51. application.empty_intent_container = intent_container
  52. application.empty_quality_changes_container = quality_changes_container
  53. application.empty_variant_container = global_variant
  54. metadata = definition_container.getMetaData()
  55. metadata["machine_extruder_trains"] = {}
  56. metadata["preferred_quality_type"] = "normal"
  57. container_registry.addContainer(definition_container)
  58. quality_node = MagicMock()
  59. machine_node = MagicMock()
  60. machine_node.preferredGlobalQuality = MagicMock(return_value = quality_node)
  61. quality_node.container = quality_container
  62. # Patch out the creation of MachineNodes since that isn't under test (and would require quite a bit of extra setup)
  63. with patch("cura.Machines.ContainerTree.MachineNode", MagicMock(return_value = machine_node)):
  64. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
  65. machine = CuraStackBuilder.createMachine("Whatever", "Test Definition")
  66. assert machine.quality == quality_container
  67. assert machine.definition == definition_container
  68. assert machine.variant == global_variant
  69. def test_createExtruderStack(application, definition_container, global_variant, material_instance_container,
  70. quality_container, intent_container, quality_changes_container):
  71. application.empty_material_container = material_instance_container
  72. application.empty_quality_container = quality_container
  73. application.empty_intent_container = intent_container
  74. application.empty_quality_changes_container = quality_changes_container
  75. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
  76. extruder_stack = CuraStackBuilder.createExtruderStack("Whatever", definition_container, "meh", 0, global_variant, material_instance_container, quality_container)
  77. assert extruder_stack.variant == global_variant
  78. assert extruder_stack.material == material_instance_container
  79. assert extruder_stack.quality == quality_container