TestXmlMaterialProfile.py 3.2 KB

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