TestXmlMaterialProfile.py 2.9 KB

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