TestCuraContainerRegistry.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. import copy
  9. import cura.CuraApplication
  10. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry #The class we're testing.
  11. from cura.Settings.ExtruderStack import ExtruderStack #Testing for returning the correct types of stacks.
  12. from cura.Settings.GlobalStack import GlobalStack #Testing for returning the correct types of stacks.
  13. from UM.Resources import Resources #Mocking some functions of this.
  14. import UM.Settings.InstanceContainer #Creating instance containers to register.
  15. import UM.Settings.ContainerRegistry #Making empty container stacks.
  16. import UM.Settings.ContainerStack #Setting the container registry here properly.
  17. from UM.Settings.DefinitionContainer import DefinitionContainer
  18. from UM.Settings.ContainerRegistry import ContainerRegistry
  19. def creteEmptyContainers():
  20. empty_container = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  21. empty_variant_container = copy.deepcopy(empty_container)
  22. empty_variant_container.setMetaDataEntry("id", "empty_variant")
  23. empty_variant_container.addMetaDataEntry("type", "variant")
  24. ContainerRegistry.getInstance().addContainer(empty_variant_container)
  25. empty_material_container = copy.deepcopy(empty_container)
  26. empty_material_container.setMetaDataEntry("id", "empty_material")
  27. empty_material_container.addMetaDataEntry("type", "material")
  28. ContainerRegistry.getInstance().addContainer(empty_material_container)
  29. empty_quality_container = copy.deepcopy(empty_container)
  30. empty_quality_container.setMetaDataEntry("id", "empty_quality")
  31. empty_quality_container.setName("Not Supported")
  32. empty_quality_container.addMetaDataEntry("quality_type", "not_supported")
  33. empty_quality_container.addMetaDataEntry("type", "quality")
  34. empty_quality_container.addMetaDataEntry("supported", False)
  35. ContainerRegistry.getInstance().addContainer(empty_quality_container)
  36. empty_quality_changes_container = copy.deepcopy(empty_container)
  37. empty_quality_changes_container.setMetaDataEntry("id", "empty_quality_changes")
  38. empty_quality_changes_container.addMetaDataEntry("type", "quality_changes")
  39. ContainerRegistry.getInstance().addContainer(empty_quality_changes_container)
  40. ## Gives a fresh CuraContainerRegistry instance.
  41. @pytest.fixture()
  42. def container_registry():
  43. registry = CuraContainerRegistry()
  44. UM.Settings.InstanceContainer.setContainerRegistry(registry)
  45. UM.Settings.ContainerStack.setContainerRegistry(registry)
  46. return registry
  47. ## Gives an arbitrary definition container.
  48. @pytest.fixture()
  49. def definition_container():
  50. return DefinitionContainer(container_id = "Test Definition")
  51. def teardown():
  52. #If the temporary file for the legacy file rename test still exists, remove it.
  53. temporary_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
  54. if os.path.isfile(temporary_file):
  55. os.remove(temporary_file)
  56. ## Tests whether addContainer properly converts to ExtruderStack.
  57. def test_addContainerExtruderStack(container_registry, definition_container):
  58. creteEmptyContainers()
  59. container_registry.addContainer(definition_container)
  60. container_stack = UM.Settings.ContainerStack.ContainerStack(stack_id = "Test Container Stack") #A container we're going to convert.
  61. container_stack.addMetaDataEntry("type", "extruder_train") #This is now an extruder train.
  62. container_stack.insertContainer(0, definition_container) #Add a definition to it so it doesn't complain.
  63. mock_super_add_container = unittest.mock.MagicMock() #Takes 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(container_stack)
  66. assert len(mock_super_add_container.call_args_list) == 2 #Called only once.
  67. assert len(mock_super_add_container.call_args_list[1][0]) == 1 #Called with one parameter.
  68. assert type(mock_super_add_container.call_args_list[1][0][0]) == ExtruderStack
  69. ## Tests whether addContainer properly converts to GlobalStack.
  70. def test_addContainerGlobalStack(container_registry, definition_container):
  71. container_registry.addContainer(definition_container)
  72. container_stack = UM.Settings.ContainerStack.ContainerStack(stack_id = "Test Container Stack") #A container we're going to convert.
  73. container_stack.addMetaDataEntry("type", "machine") #This is now a global stack.
  74. container_stack.insertContainer(0, definition_container) #Must have a definition.
  75. mock_super_add_container = unittest.mock.MagicMock() #Takes the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  76. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  77. container_registry.addContainer(container_stack)
  78. assert len(mock_super_add_container.call_args_list) == 2 #Called only once.
  79. assert len(mock_super_add_container.call_args_list[1][0]) == 1 #Called with one parameter.
  80. assert type(mock_super_add_container.call_args_list[1][0][0]) == GlobalStack
  81. def test_addContainerGoodSettingVersion(container_registry, definition_container):
  82. from cura.CuraApplication import CuraApplication
  83. definition_container.getMetaData()["setting_version"] = CuraApplication.SettingVersion
  84. container_registry.addContainer(definition_container)
  85. instance = UM.Settings.InstanceContainer.InstanceContainer(container_id = "Test Instance")
  86. instance.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  87. instance.setDefinition(definition_container.getId())
  88. mock_super_add_container = unittest.mock.MagicMock() #Take the role of the Uranium-ContainerRegistry where the resulting containers get registered.
  89. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  90. container_registry.addContainer(instance)
  91. mock_super_add_container.assert_called_once_with(instance) #The instance must have been registered now.
  92. def test_addContainerNoSettingVersion(container_registry, definition_container):
  93. from cura.CuraApplication import CuraApplication
  94. definition_container.getMetaData()["setting_version"] = CuraApplication.SettingVersion
  95. container_registry.addContainer(definition_container)
  96. instance = UM.Settings.InstanceContainer.InstanceContainer(container_id = "Test Instance")
  97. #Don't add setting_version metadata.
  98. instance.setDefinition(definition_container.getId())
  99. mock_super_add_container = unittest.mock.MagicMock() #Take the role of the Uranium-ContainerRegistry where the resulting container should not get registered.
  100. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  101. container_registry.addContainer(instance)
  102. 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!
  103. def test_addContainerBadSettingVersion(container_registry, definition_container):
  104. from cura.CuraApplication import CuraApplication
  105. definition_container.getMetaData()["setting_version"] = CuraApplication.SettingVersion
  106. container_registry.addContainer(definition_container)
  107. instance = UM.Settings.InstanceContainer.InstanceContainer(container_id = "Test Instance")
  108. instance.addMetaDataEntry("setting_version", 9001) #Wrong version!
  109. instance.setDefinition(definition_container.getId())
  110. mock_super_add_container = unittest.mock.MagicMock() #Take the role of the Uranium-ContainerRegistry where the resulting container should not get registered.
  111. with unittest.mock.patch("UM.Settings.ContainerRegistry.ContainerRegistry.addContainer", mock_super_add_container):
  112. container_registry.addContainer(instance)
  113. 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!
  114. ## Tests whether loading gives objects of the correct type.
  115. # @pytest.mark.parametrize("filename, output_class", [
  116. # ("ExtruderLegacy.stack.cfg", ExtruderStack),
  117. # ("MachineLegacy.stack.cfg", GlobalStack),
  118. # ("Left.extruder.cfg", ExtruderStack),
  119. # ("Global.global.cfg", GlobalStack),
  120. # ("Global.stack.cfg", GlobalStack)
  121. # ])
  122. # def test_loadTypes(filename, output_class, container_registry):
  123. # #Mock some dependencies.
  124. # Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
  125. #
  126. # def findContainers(container_type = 0, id = None):
  127. # if id == "some_instance":
  128. # return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  129. # elif id == "some_definition":
  130. # return [DefinitionContainer(container_id = id)]
  131. # else:
  132. # return []
  133. #
  134. # container_registry.findContainers = findContainers
  135. #
  136. # with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  137. # with unittest.mock.patch("os.remove"):
  138. # container_registry.load()
  139. #
  140. # #Check whether the resulting type was correct.
  141. # stack_id = filename.split(".")[0]
  142. # for container_id, container in container_registry._containers.items(): #Stupid ContainerRegistry class doesn't expose any way of getting at this except by prodding the privates.
  143. # if container_id == stack_id: #This is the one we're testing.
  144. # assert type(container) == output_class
  145. # break
  146. # else:
  147. # assert False #Container stack with specified ID was not loaded.