TestContainerManager.py 4.1 KB

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