TestContainerManager.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from unittest import TestCase
  2. from unittest.mock import MagicMock
  3. from PyQt6.QtCore import QUrl
  4. from unittest.mock import patch
  5. from UM.MimeTypeDatabase import MimeTypeDatabase
  6. from cura.Settings.ContainerManager import ContainerManager
  7. import tempfile
  8. import os
  9. class TestContainerManager(TestCase):
  10. def setUp(self):
  11. self._application = MagicMock()
  12. self._container_registry = MagicMock()
  13. self._machine_manager = MagicMock()
  14. self._machine_manager.activeMachine.extruderList = [MagicMock(name="Left Extruder Mock"), MagicMock(name="Right Extruder Mock")]
  15. self._mocked_mime = MagicMock()
  16. self._mocked_mime.preferredSuffix = "omg"
  17. self._mocked_mime.suffixes = ["omg"]
  18. self._mocked_mime.comment = "UnitTest!"
  19. self._mocked_container = MagicMock()
  20. self._mocked_container_data = "SOME DATA :D"
  21. self._mocked_container.serialize = MagicMock(return_value = self._mocked_container_data)
  22. self._containers_meta_data = [{"id": "test", "test_data": "omg"}]
  23. self._container_registry.findContainersMetadata = MagicMock(return_value = self._containers_meta_data)
  24. self._container_registry.getMimeTypeForContainer = MagicMock(return_value = self._mocked_mime)
  25. self._container_registry.findContainers = MagicMock(return_value = [self._mocked_container])
  26. self._application.getContainerRegistry = MagicMock(return_value = self._container_registry)
  27. self._application.getMachineManager = MagicMock(return_value = self._machine_manager)
  28. # Destroy the previous instance of the container manager
  29. if ContainerManager.getInstance() is not None:
  30. ContainerManager._ContainerManager__instance = None
  31. self._container_manager = ContainerManager(self._application)
  32. MimeTypeDatabase.addMimeType(self._mocked_mime)
  33. def tearDown(self):
  34. MimeTypeDatabase.removeMimeType(self._mocked_mime)
  35. def test_getContainerMetaDataEntry(self):
  36. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=self._application)):
  37. assert self._container_manager.getContainerMetaDataEntry("test", "test_data") == "omg"
  38. assert self._container_manager.getContainerMetaDataEntry("test", "entry_that_is_not_defined") == ""
  39. def test_clearUserContainer(self):
  40. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=self._application)):
  41. self._container_manager.clearUserContainers()
  42. assert self._machine_manager.activeMachine.userChanges.clear.call_count == 1
  43. def test_getContainerNameFilters(self):
  44. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=self._application)):
  45. # If nothing is added, we still expect to get the all files filter
  46. assert self._container_manager.getContainerNameFilters("") == ['All Files (*)']
  47. # Pretend that a new type was added.
  48. self._container_registry.getContainerTypes = MagicMock(return_value=[("None", None)])
  49. assert self._container_manager.getContainerNameFilters("") == ['UnitTest! (*.omg)', 'All Files (*)']
  50. def test_exportContainerUnknownFileType(self):
  51. # The filetype is not known, so this should cause an error!
  52. assert self._container_manager.exportContainer("test", "zomg", "whatever")["status"] == "error"
  53. def test_exportContainerInvalidPath(self):
  54. assert self._container_manager.exportContainer("test", "zomg", "")["status"] == "error"
  55. assert self._container_manager.exportContainer("test", "zomg", QUrl())["status"] == "error"
  56. def test_exportContainerInvalidId(self):
  57. assert self._container_manager.exportContainer("", "whatever", "whatever")["status"] == "error"
  58. def test_exportContainer(self):
  59. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=self._application)):
  60. with tempfile.TemporaryDirectory() as tmpdirname:
  61. result = self._container_manager.exportContainer("test", "whatever", os.path.join(tmpdirname, "whatever.omg"))
  62. assert(os.path.exists(result["path"]))
  63. with open(result["path"], "r", encoding="utf-8") as f:
  64. assert f.read() == self._mocked_container_data