TestCuraContainerRegistry.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.InstanceContainer #Creating instance containers to register.
  13. import UM.Settings.ContainerRegistry #Making empty container stacks.
  14. import UM.Settings.ContainerStack #Setting the container registry here properly.
  15. from UM.Settings.DefinitionContainer import DefinitionContainer
  16. ## Gives a fresh CuraContainerRegistry instance.
  17. @pytest.fixture()
  18. def container_registry():
  19. registry = CuraContainerRegistry()
  20. UM.Settings.InstanceContainer.setContainerRegistry(registry)
  21. UM.Settings.ContainerStack.setContainerRegistry(registry)
  22. return registry
  23. def teardown():
  24. #If the temporary file for the legacy file rename test still exists, remove it.
  25. temporary_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
  26. if os.path.isfile(temporary_file):
  27. os.remove(temporary_file)
  28. ## Tests whether addContainer properly converts to ExtruderStack.
  29. def test_addContainerExtruderStack(container_registry):
  30. definition = DefinitionContainer(container_id = "Test Definition") #Need some definition first to be able to register stacks.
  31. container_registry.addContainer(definition)
  32. container_stack = UM.Settings.ContainerStack.ContainerStack(stack_id = "Test Container Stack") #A container we're going to convert.
  33. container_stack.addMetaDataEntry("type", "extruder_train") #This is now an extruder train.
  34. container_stack.insertContainer(0, definition) #Add a definition to it so it doesn't complain.
  35. mock_super_add_container = unittest.mock.MagicMock() #Takes the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  36. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  37. container_registry.addContainer(container_stack)
  38. assert len(mock_super_add_container.call_args_list) == 1 #Called only once.
  39. assert len(mock_super_add_container.call_args_list[0][0]) == 1 #Called with one parameter.
  40. assert type(mock_super_add_container.call_args_list[0][0][0]) == ExtruderStack
  41. ## Tests whether addContainer properly converts to GlobalStack.
  42. def test_addContainerGlobalStack(container_registry):
  43. definition = DefinitionContainer(container_id = "Test Definition") #Need some definition first to be able to register stacks.
  44. container_registry.addContainer(definition)
  45. container_stack = UM.Settings.ContainerStack.ContainerStack(stack_id = "Test Container Stack") #A container we're going to convert.
  46. container_stack.addMetaDataEntry("type", "machine") #This is now a global stack.
  47. container_stack.insertContainer(0, definition) #Must have a definition.
  48. mock_super_add_container = unittest.mock.MagicMock() #Takes the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  49. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  50. container_registry.addContainer(container_stack)
  51. assert len(mock_super_add_container.call_args_list) == 1 #Called only once.
  52. assert len(mock_super_add_container.call_args_list[0][0]) == 1 #Called with one parameter.
  53. assert type(mock_super_add_container.call_args_list[0][0][0]) == GlobalStack
  54. ## Tests whether loading gives objects of the correct type.
  55. @pytest.mark.parametrize("filename, output_class", [
  56. ("ExtruderLegacy.stack.cfg", ExtruderStack),
  57. ("MachineLegacy.stack.cfg", GlobalStack),
  58. ("Left.extruder.cfg", ExtruderStack),
  59. ("Global.global.cfg", GlobalStack),
  60. ("Global.stack.cfg", GlobalStack)
  61. ])
  62. def test_loadTypes(filename, output_class, container_registry):
  63. #Mock some dependencies.
  64. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  65. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
  66. def findContainers(container_type = 0, id = None):
  67. if id == "some_instance":
  68. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  69. elif id == "some_definition":
  70. return [DefinitionContainer(container_id = id)]
  71. else:
  72. return []
  73. container_registry.findContainers = findContainers
  74. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  75. with unittest.mock.patch("os.remove"):
  76. container_registry.load()
  77. #Check whether the resulting type was correct.
  78. stack_id = filename.split(".")[0]
  79. for container in container_registry._containers: #Stupid ContainerRegistry class doesn't expose any way of getting at this except by prodding the privates.
  80. if container.getId() == stack_id: #This is the one we're testing.
  81. assert type(container) == output_class
  82. break
  83. else:
  84. assert False #Container stack with specified ID was not loaded.
  85. ## Tests whether loading a legacy file moves the upgraded file properly.
  86. def test_loadLegacyFileRenamed(container_registry):
  87. #Create a temporary file for the registry to load.
  88. stacks_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks")
  89. temp_file = os.path.join(stacks_folder, "temporary.stack.cfg")
  90. temp_file_source = os.path.join(stacks_folder, "MachineLegacy.stack.cfg")
  91. shutil.copyfile(temp_file_source, temp_file)
  92. #Mock some dependencies.
  93. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  94. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [temp_file]) #Return a temporary file that we'll make for this test.
  95. def findContainers(container_type = 0, id = None):
  96. if id == "MachineLegacy":
  97. return None
  98. container = UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)
  99. container.getNextStack = unittest.mock.MagicMock()
  100. return [container]
  101. old_find_containers = container_registry.findContainers
  102. container_registry.findContainers = findContainers
  103. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  104. container_registry.load()
  105. container_registry.findContainers = old_find_containers
  106. container_registry.saveAll()
  107. print("all containers in registry", container_registry._containers)
  108. assert not os.path.isfile(temp_file)
  109. mime_type = container_registry.getMimeTypeForContainer(GlobalStack)
  110. file_name = urllib.parse.quote_plus("MachineLegacy") + "." + mime_type.preferredSuffix
  111. path = Resources.getStoragePath(Resources.ContainerStacks, file_name)
  112. assert os.path.isfile(path)