TestMaterialManager.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from unittest.mock import MagicMock, patch
  2. from cura.Machines.MaterialManager import MaterialManager
  3. mocked_registry = MagicMock()
  4. material_1 = {"id": "test", "GUID":"TEST!", "base_file": "base_material", "definition": "fdmmachine", "approximate_diameter": 3, "brand": "generic"}
  5. material_2 = {"id": "base_material", "GUID": "TEST2!", "base_file": "test", "definition": "fdmmachine", "approximate_diameter": 3}
  6. mocked_registry.findContainersMetadata = MagicMock(return_value = [material_1, material_2])
  7. mocked_definition = MagicMock()
  8. mocked_definition.getId = MagicMock(return_value = "fdmmachine")
  9. mocked_definition.getMetaDataEntry = MagicMock(return_value = [])
  10. def test_initialize(application):
  11. # Just test if the simple loading works
  12. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  13. manager = MaterialManager(mocked_registry)
  14. manager.initialize()
  15. # Double check that we actually got some material nodes
  16. assert manager.getMaterialGroup("base_material").name == "base_material"
  17. assert manager.getMaterialGroup("test").name == "test"
  18. def test_getAvailableMaterials(application):
  19. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  20. manager = MaterialManager(mocked_registry)
  21. manager.initialize()
  22. available_materials = manager.getAvailableMaterials(mocked_definition, None, None, 3)
  23. assert "base_material" in available_materials
  24. assert "test" in available_materials
  25. def test_getMaterialNode(application):
  26. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  27. manager = MaterialManager(mocked_registry)
  28. manager.initialize()
  29. assert manager.getMaterialNode("fdmmachine", None, None, 3, "base_material").getMetaDataEntry("id") == "test"
  30. def test_getAvailableMaterialsForMachineExtruder(application):
  31. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  32. manager = MaterialManager(mocked_registry)
  33. manager.initialize()
  34. mocked_machine = MagicMock()
  35. mocked_machine.getBuildplateName = MagicMock(return_value = "build_plate")
  36. mocked_machine.definition = mocked_definition
  37. mocked_extruder_stack = MagicMock()
  38. mocked_extruder_stack.variant.getId = MagicMock(return_value = "test")
  39. mocked_extruder_stack.getApproximateMaterialDiameter = MagicMock(return_value = 2.85)
  40. available_materials = manager.getAvailableMaterialsForMachineExtruder(mocked_machine, mocked_extruder_stack)
  41. assert "base_material" in available_materials
  42. assert "test" in available_materials
  43. class TestFavorites:
  44. def test_addFavorite(self, application):
  45. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  46. manager = MaterialManager(mocked_registry)
  47. manager.materialsUpdated = MagicMock()
  48. manager.addFavorite("blarg")
  49. assert manager.getFavorites() == {"blarg"}
  50. application.getPreferences().setValue.assert_called_once_with("cura/favorite_materials", "blarg")
  51. manager.materialsUpdated.emit.assert_called_once_with()
  52. def test_removeNotExistingFavorite(self, application):
  53. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  54. manager = MaterialManager(mocked_registry)
  55. manager.materialsUpdated = MagicMock()
  56. manager.removeFavorite("blarg")
  57. manager.materialsUpdated.emit.assert_not_called()
  58. def test_removeExistingFavorite(self, application):
  59. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  60. manager = MaterialManager(mocked_registry)
  61. manager.materialsUpdated = MagicMock()
  62. manager.addFavorite("blarg")
  63. manager.removeFavorite("blarg")
  64. assert manager.materialsUpdated.emit.call_count == 2
  65. application.getPreferences().setValue.assert_called_with("cura/favorite_materials", "")
  66. assert manager.getFavorites() == set()