TestCuraContainerRegistry.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os #To find the directory with test files and find the test files.
  4. import pytest #This module contains unit tests.
  5. import shutil #To copy files to make a temporary file.
  6. import unittest.mock #To mock and monkeypatch stuff.
  7. import urllib.parse
  8. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry #The class we're testing.
  9. from cura.Settings.ExtruderStack import ExtruderStack #Testing for returning the correct types of stacks.
  10. from cura.Settings.GlobalStack import GlobalStack #Testing for returning the correct types of stacks.
  11. from UM.Resources import Resources #Mocking some functions of this.
  12. import UM.Settings.ContainerRegistry #Making empty container stacks.
  13. import UM.Settings.ContainerStack #Setting the container registry here properly.
  14. from UM.Settings.DefinitionContainer import DefinitionContainer
  15. ## Gives a fresh CuraContainerRegistry instance.
  16. @pytest.fixture()
  17. def container_registry():
  18. return CuraContainerRegistry()
  19. def teardown():
  20. #If the temporary file for the legacy file rename test still exists, remove it.
  21. temporary_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
  22. if os.path.isfile(temporary_file):
  23. os.remove(temporary_file)
  24. ## Tests whether loading gives objects of the correct type.
  25. @pytest.mark.parametrize("filename, output_class", [
  26. ("ExtruderLegacy.stack.cfg", ExtruderStack),
  27. ("MachineLegacy.stack.cfg", GlobalStack),
  28. ("Left.extruder.cfg", ExtruderStack),
  29. ("Global.global.cfg", GlobalStack),
  30. ("Global.stack.cfg", GlobalStack)
  31. ])
  32. def test_loadTypes(filename, output_class, container_registry):
  33. #Mock some dependencies.
  34. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  35. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
  36. def findContainers(container_type = 0, id = None):
  37. if id == "some_instance":
  38. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  39. elif id == "some_definition":
  40. return [DefinitionContainer(container_id = id)]
  41. else:
  42. return []
  43. container_registry.findContainers = findContainers
  44. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  45. with unittest.mock.patch("os.remove"):
  46. container_registry.load()
  47. #Check whether the resulting type was correct.
  48. stack_id = filename.split(".")[0]
  49. for container in container_registry._containers: #Stupid ContainerRegistry class doesn't expose any way of getting at this except by prodding the privates.
  50. if container.getId() == stack_id: #This is the one we're testing.
  51. assert type(container) == output_class
  52. break
  53. else:
  54. assert False #Container stack with specified ID was not loaded.
  55. ## Tests whether loading a legacy file moves the upgraded file properly.
  56. def test_loadLegacyFileRenamed(container_registry):
  57. #Create a temporary file for the registry to load.
  58. stacks_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks")
  59. temp_file = os.path.join(stacks_folder, "temporary.stack.cfg")
  60. temp_file_source = os.path.join(stacks_folder, "MachineLegacy.stack.cfg")
  61. shutil.copyfile(temp_file_source, temp_file)
  62. #Mock some dependencies.
  63. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  64. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [temp_file]) #Return a temporary file that we'll make for this test.
  65. def findContainers(container_type = 0, id = None):
  66. if id == "MachineLegacy":
  67. return None
  68. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  69. old_find_containers = container_registry.findContainers
  70. container_registry.findContainers = findContainers
  71. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  72. container_registry.load()
  73. container_registry.findContainers = old_find_containers
  74. container_registry.saveAll()
  75. print("all containers in registry", container_registry._containers)
  76. assert not os.path.isfile(temp_file)
  77. mime_type = container_registry.getMimeTypeForContainer(GlobalStack)
  78. file_name = urllib.parse.quote_plus("MachineLegacy") + "." + mime_type.preferredSuffix
  79. path = Resources.getStoragePath(Resources.ContainerStacks, file_name)
  80. assert os.path.isfile(path)