TestXmlMaterialProfile.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from unittest.mock import patch, MagicMock
  2. # Prevents error: "PyCapsule_GetPointer called with incorrect name" with conflicting SIP configurations between Arcus and PyQt: Import Arcus and Savitar first!
  3. import Savitar # Dont remove this line
  4. import Arcus # No really. Don't. It needs to be there!
  5. from UM.Qt.QtApplication import QtApplication # QtApplication import is required, even though it isn't used.
  6. import pytest
  7. import XmlMaterialProfile
  8. def createXmlMaterialProfile(material_id):
  9. try:
  10. return XmlMaterialProfile.XmlMaterialProfile.XmlMaterialProfile(material_id)
  11. except AttributeError:
  12. return XmlMaterialProfile.XmlMaterialProfile(material_id)
  13. def test_setName():
  14. material_1 = createXmlMaterialProfile("herpderp")
  15. material_2 = createXmlMaterialProfile("OMGZOMG")
  16. material_1.getMetaData()["base_file"] = "herpderp"
  17. material_2.getMetaData()["base_file"] = "herpderp"
  18. container_registry = MagicMock()
  19. container_registry.isReadOnly = MagicMock(return_value = False)
  20. container_registry.findInstanceContainers = MagicMock(return_value = [material_1, material_2])
  21. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  22. material_1.setName("beep!")
  23. assert material_1.getName() == "beep!"
  24. assert material_2.getName() == "beep!"
  25. def test_setDirty():
  26. material_1 = createXmlMaterialProfile("herpderp")
  27. material_2 = createXmlMaterialProfile("OMGZOMG")
  28. material_1.getMetaData()["base_file"] = "herpderp"
  29. material_2.getMetaData()["base_file"] = "herpderp"
  30. container_registry = MagicMock()
  31. container_registry.isReadOnly = MagicMock(return_value=False)
  32. container_registry.findContainers = MagicMock(return_value=[material_1, material_2])
  33. # Sanity check. Since we did a hacky thing to set the metadata, the container should not be dirty.
  34. # But this test assumes that it works like that, so we need to validate that.
  35. assert not material_1.isDirty()
  36. assert not material_2.isDirty()
  37. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  38. material_2.setDirty(True)
  39. assert material_1.isDirty()
  40. assert material_2.isDirty()
  41. # Setting the base material dirty does not set it's child as dirty.
  42. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  43. material_1.setDirty(False)
  44. assert not material_1.isDirty()
  45. assert material_2.isDirty()
  46. def test_serializeNonBaseMaterial():
  47. material_1 = createXmlMaterialProfile("herpderp")
  48. material_1.getMetaData()["base_file"] = "omgzomg"
  49. container_registry = MagicMock()
  50. container_registry.isReadOnly = MagicMock(return_value=False)
  51. container_registry.findContainers = MagicMock(return_value=[material_1])
  52. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  53. with pytest.raises(NotImplementedError):
  54. # This material is not a base material, so it can't be serialized!
  55. material_1.serialize()