TestCuraContainerRegistry.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 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. ## Gives an arbitrary definition container.
  24. @pytest.fixture()
  25. def definition_container():
  26. return DefinitionContainer(container_id = "Test Definition")
  27. def teardown():
  28. #If the temporary file for the legacy file rename test still exists, remove it.
  29. temporary_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
  30. if os.path.isfile(temporary_file):
  31. os.remove(temporary_file)
  32. ## Tests whether addContainer properly converts to ExtruderStack.
  33. def test_addContainerExtruderStack(container_registry, definition_container):
  34. container_registry.addContainer(definition_container)
  35. container_stack = UM.Settings.ContainerStack.ContainerStack(stack_id = "Test Container Stack") #A container we're going to convert.
  36. container_stack.addMetaDataEntry("type", "extruder_train") #This is now an extruder train.
  37. container_stack.insertContainer(0, definition_container) #Add a definition to it so it doesn't complain.
  38. mock_super_add_container = unittest.mock.MagicMock() #Takes the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  39. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  40. container_registry.addContainer(container_stack)
  41. assert len(mock_super_add_container.call_args_list) == 1 #Called only once.
  42. assert len(mock_super_add_container.call_args_list[0][0]) == 1 #Called with one parameter.
  43. assert type(mock_super_add_container.call_args_list[0][0][0]) == ExtruderStack
  44. ## Tests whether addContainer properly converts to GlobalStack.
  45. def test_addContainerGlobalStack(container_registry, definition_container):
  46. container_registry.addContainer(definition_container)
  47. container_stack = UM.Settings.ContainerStack.ContainerStack(stack_id = "Test Container Stack") #A container we're going to convert.
  48. container_stack.addMetaDataEntry("type", "machine") #This is now a global stack.
  49. container_stack.insertContainer(0, definition_container) #Must have a definition.
  50. mock_super_add_container = unittest.mock.MagicMock() #Takes the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  51. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  52. container_registry.addContainer(container_stack)
  53. assert len(mock_super_add_container.call_args_list) == 1 #Called only once.
  54. assert len(mock_super_add_container.call_args_list[0][0]) == 1 #Called with one parameter.
  55. assert type(mock_super_add_container.call_args_list[0][0][0]) == GlobalStack
  56. def test_addContainerGoodSettingVersion(container_registry, definition_container):
  57. from cura.CuraApplication import CuraApplication
  58. definition_container.getMetaData()["setting_version"] = CuraApplication.SettingVersion
  59. container_registry.addContainer(definition_container)
  60. instance = UM.Settings.InstanceContainer.InstanceContainer(container_id = "Test Instance")
  61. instance.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  62. instance.setDefinition(definition_container)
  63. mock_super_add_container = unittest.mock.MagicMock() #Take the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  64. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  65. container_registry.addContainer(instance)
  66. mock_super_add_container.assert_called_once_with(instance) #The instance must have been registered now.
  67. def test_addContainerNoSettingVersion(container_registry, definition_container):
  68. from cura.CuraApplication import CuraApplication
  69. definition_container.getMetaData()["setting_version"] = CuraApplication.SettingVersion
  70. container_registry.addContainer(definition_container)
  71. instance = UM.Settings.InstanceContainer.InstanceContainer(container_id = "Test Instance")
  72. #Don't add setting_version metadata.
  73. instance.setDefinition(definition_container)
  74. mock_super_add_container = unittest.mock.MagicMock() #Take the role of the Uranium-ContainerRegistry where the resulting container should not get registered.
  75. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  76. container_registry.addContainer(instance)
  77. mock_super_add_container.assert_not_called() #Should not get passed on to UM.Settings.ContainerRegistry.addContainer, because the setting_version is interpreted as 0!
  78. def test_addContainerBadSettingVersion(container_registry, definition_container):
  79. from cura.CuraApplication import CuraApplication
  80. definition_container.getMetaData()["setting_version"] = CuraApplication.SettingVersion
  81. container_registry.addContainer(definition_container)
  82. instance = UM.Settings.InstanceContainer.InstanceContainer(container_id = "Test Instance")
  83. instance.addMetaDataEntry("setting_version", 9001) #Wrong version!
  84. instance.setDefinition(definition_container)
  85. mock_super_add_container = unittest.mock.MagicMock() #Take the role of the Uranium-ContainerRegistry where the resulting container should not get registered.
  86. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  87. container_registry.addContainer(instance)
  88. mock_super_add_container.assert_not_called() #Should not get passed on to UM.Settings.ContainerRegistry.addContainer, because the setting_version doesn't match its definition!
  89. ## Tests whether loading gives objects of the correct type.
  90. @pytest.mark.parametrize("filename, output_class", [
  91. ("ExtruderLegacy.stack.cfg", ExtruderStack),
  92. ("MachineLegacy.stack.cfg", GlobalStack),
  93. ("Left.extruder.cfg", ExtruderStack),
  94. ("Global.global.cfg", GlobalStack),
  95. ("Global.stack.cfg", GlobalStack)
  96. ])
  97. def test_loadTypes(filename, output_class, container_registry):
  98. #Mock some dependencies.
  99. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
  100. def findContainers(container_type = 0, id = None):
  101. if id == "some_instance":
  102. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  103. elif id == "some_definition":
  104. return [DefinitionContainer(container_id = id)]
  105. else:
  106. return []
  107. container_registry.findContainers = findContainers
  108. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  109. with unittest.mock.patch("os.remove"):
  110. container_registry.load()
  111. #Check whether the resulting type was correct.
  112. stack_id = filename.split(".")[0]
  113. for container in container_registry._containers: #Stupid ContainerRegistry class doesn't expose any way of getting at this except by prodding the privates.
  114. if container.getId() == stack_id: #This is the one we're testing.
  115. assert type(container) == output_class
  116. break
  117. else:
  118. assert False #Container stack with specified ID was not loaded.
  119. ## Tests whether loading a legacy file moves the upgraded file properly.
  120. def test_loadLegacyFileRenamed(container_registry):
  121. #Create a temporary file for the registry to load.
  122. stacks_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks")
  123. temp_file = os.path.join(stacks_folder, "temporary.stack.cfg")
  124. temp_file_source = os.path.join(stacks_folder, "MachineLegacy.stack.cfg")
  125. shutil.copyfile(temp_file_source, temp_file)
  126. #Mock some dependencies.
  127. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  128. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [temp_file]) #Return a temporary file that we'll make for this test.
  129. def findContainers(container_type = 0, id = None):
  130. if id == "MachineLegacy":
  131. return None
  132. container = UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)
  133. container.getNextStack = unittest.mock.MagicMock()
  134. return [container]
  135. old_find_containers = container_registry.findContainers
  136. container_registry.findContainers = findContainers
  137. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  138. container_registry.load()
  139. container_registry.findContainers = old_find_containers
  140. container_registry.saveAll()
  141. print("all containers in registry", container_registry._containers)
  142. assert not os.path.isfile(temp_file)
  143. mime_type = container_registry.getMimeTypeForContainer(GlobalStack)
  144. file_name = urllib.parse.quote_plus("MachineLegacy") + "." + mime_type.preferredSuffix
  145. path = Resources.getStoragePath(Resources.ContainerStacks, file_name)
  146. assert os.path.isfile(path)