TestExtruderStack.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import pytest #This module contains automated tests.
  4. import unittest.mock #For the mocking and monkeypatching functionality.
  5. import copy
  6. import cura.CuraApplication
  7. import UM.Settings.ContainerRegistry #To create empty instance containers.
  8. import UM.Settings.ContainerStack #To set the container registry the container stacks use.
  9. from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer.
  10. from UM.Settings.InstanceContainer import InstanceContainer #To check against the class of InstanceContainer.
  11. import cura.Settings.ExtruderStack #The module we're testing.
  12. from cura.Settings.Exceptions import InvalidContainerError, InvalidOperationError #To check whether the correct exceptions are raised.
  13. from cura.Settings.ExtruderManager import ExtruderManager
  14. from UM.Settings.ContainerRegistry import ContainerRegistry
  15. from cura.Settings.GlobalStack import GlobalStack
  16. ## Fake container registry that always provides all containers you ask of.
  17. @pytest.yield_fixture()
  18. def container_registry():
  19. registry = unittest.mock.MagicMock()
  20. registry.return_value = unittest.mock.NonCallableMagicMock()
  21. registry.findInstanceContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
  22. registry.findDefinitionContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
  23. UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = registry
  24. UM.Settings.ContainerStack._containerRegistry = registry
  25. yield registry
  26. UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = None
  27. UM.Settings.ContainerStack._containerRegistry = None
  28. ## An empty extruder stack to test with.
  29. @pytest.fixture()
  30. def extruder_stack() -> cura.Settings.ExtruderStack.ExtruderStack:
  31. creteEmptyContainers()
  32. return cura.Settings.ExtruderStack.ExtruderStack("TestStack")
  33. ## Gets an instance container with a specified container type.
  34. #
  35. # \param container_type The type metadata for the instance container.
  36. # \return An instance container instance.
  37. def getInstanceContainer(container_type) -> InstanceContainer:
  38. container = InstanceContainer(container_id = "InstanceContainer")
  39. container.addMetaDataEntry("type", container_type)
  40. return container
  41. def creteEmptyContainers():
  42. empty_container = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  43. empty_variant_container = copy.deepcopy(empty_container)
  44. empty_variant_container.setMetaDataEntry("id", "empty_variant")
  45. empty_variant_container.addMetaDataEntry("type", "variant")
  46. ContainerRegistry.getInstance().addContainer(empty_variant_container)
  47. empty_material_container = copy.deepcopy(empty_container)
  48. empty_material_container.setMetaDataEntry("id", "empty_material")
  49. empty_material_container.addMetaDataEntry("type", "material")
  50. ContainerRegistry.getInstance().addContainer(empty_material_container)
  51. empty_quality_container = copy.deepcopy(empty_container)
  52. empty_quality_container.setMetaDataEntry("id", "empty_quality")
  53. empty_quality_container.setName("Not Supported")
  54. empty_quality_container.addMetaDataEntry("quality_type", "not_supported")
  55. empty_quality_container.addMetaDataEntry("type", "quality")
  56. empty_quality_container.addMetaDataEntry("supported", False)
  57. ContainerRegistry.getInstance().addContainer(empty_quality_container)
  58. empty_quality_changes_container = copy.deepcopy(empty_container)
  59. empty_quality_changes_container.setMetaDataEntry("id", "empty_quality_changes")
  60. empty_quality_changes_container.addMetaDataEntry("type", "quality_changes")
  61. ContainerRegistry.getInstance().addContainer(empty_quality_changes_container)
  62. class DefinitionContainerSubClass(DefinitionContainer):
  63. def __init__(self):
  64. super().__init__(container_id = "SubDefinitionContainer")
  65. class InstanceContainerSubClass(InstanceContainer):
  66. def __init__(self, container_type):
  67. super().__init__(container_id = "SubInstanceContainer")
  68. self.addMetaDataEntry("type", container_type)
  69. #############################START OF TEST CASES################################
  70. ## Tests whether adding a container is properly forbidden.
  71. def test_addContainer(extruder_stack):
  72. with pytest.raises(InvalidOperationError):
  73. extruder_stack.addContainer(unittest.mock.MagicMock())
  74. #Tests setting user changes profiles to invalid containers.
  75. @pytest.mark.parametrize("container", [
  76. getInstanceContainer(container_type = "wrong container type"),
  77. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  78. DefinitionContainer(container_id = "wrong class")
  79. ])
  80. def test_constrainUserChangesInvalid(container, extruder_stack):
  81. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  82. extruder_stack.userChanges = container
  83. #Tests setting user changes profiles.
  84. @pytest.mark.parametrize("container", [
  85. getInstanceContainer(container_type = "user"),
  86. InstanceContainerSubClass(container_type = "user")
  87. ])
  88. def test_constrainUserChangesValid(container, extruder_stack):
  89. extruder_stack.userChanges = container #Should not give an error.
  90. #Tests setting quality changes profiles to invalid containers.
  91. @pytest.mark.parametrize("container", [
  92. getInstanceContainer(container_type = "wrong container type"),
  93. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  94. DefinitionContainer(container_id = "wrong class")
  95. ])
  96. def test_constrainQualityChangesInvalid(container, extruder_stack):
  97. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  98. extruder_stack.qualityChanges = container
  99. #Test setting quality changes profiles.
  100. @pytest.mark.parametrize("container", [
  101. getInstanceContainer(container_type = "quality_changes"),
  102. InstanceContainerSubClass(container_type = "quality_changes")
  103. ])
  104. def test_constrainQualityChangesValid(container, extruder_stack):
  105. extruder_stack.qualityChanges = container #Should not give an error.
  106. #Tests setting quality profiles to invalid containers.
  107. @pytest.mark.parametrize("container", [
  108. getInstanceContainer(container_type = "wrong container type"),
  109. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  110. DefinitionContainer(container_id = "wrong class")
  111. ])
  112. def test_constrainQualityInvalid(container, extruder_stack):
  113. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  114. extruder_stack.quality = container
  115. #Test setting quality profiles.
  116. @pytest.mark.parametrize("container", [
  117. getInstanceContainer(container_type = "quality"),
  118. InstanceContainerSubClass(container_type = "quality")
  119. ])
  120. def test_constrainQualityValid(container, extruder_stack):
  121. extruder_stack.quality = container #Should not give an error.
  122. #Tests setting materials to invalid containers.
  123. @pytest.mark.parametrize("container", [
  124. getInstanceContainer(container_type = "wrong container type"),
  125. getInstanceContainer(container_type = "quality"), #Existing, but still wrong type.
  126. DefinitionContainer(container_id = "wrong class")
  127. ])
  128. def test_constrainMaterialInvalid(container, extruder_stack):
  129. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  130. extruder_stack.material = container
  131. #Test setting materials.
  132. @pytest.mark.parametrize("container", [
  133. getInstanceContainer(container_type = "material"),
  134. InstanceContainerSubClass(container_type = "material")
  135. ])
  136. def test_constrainMaterialValid(container, extruder_stack):
  137. extruder_stack.material = container #Should not give an error.
  138. #Tests setting variants to invalid containers.
  139. @pytest.mark.parametrize("container", [
  140. getInstanceContainer(container_type = "wrong container type"),
  141. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  142. DefinitionContainer(container_id = "wrong class")
  143. ])
  144. def test_constrainVariantInvalid(container, extruder_stack):
  145. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  146. extruder_stack.variant = container
  147. #Test setting variants.
  148. @pytest.mark.parametrize("container", [
  149. getInstanceContainer(container_type = "variant"),
  150. InstanceContainerSubClass(container_type = "variant")
  151. ])
  152. def test_constrainVariantValid(container, extruder_stack):
  153. extruder_stack.variant = container #Should not give an error.
  154. #Tests setting definitions to invalid containers.
  155. @pytest.mark.parametrize("container", [
  156. getInstanceContainer(container_type = "wrong class"),
  157. getInstanceContainer(container_type = "material"), #Existing, but still wrong class.
  158. ])
  159. def test_constrainVariantInvalid(container, extruder_stack):
  160. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  161. extruder_stack.definition = container
  162. #Test setting definitions.
  163. @pytest.mark.parametrize("container", [
  164. DefinitionContainer(container_id = "DefinitionContainer"),
  165. DefinitionContainerSubClass()
  166. ])
  167. def test_constrainDefinitionValid(container, extruder_stack):
  168. extruder_stack.definition = container #Should not give an error.
  169. ## Tests whether deserialising completes the missing containers with empty
  170. # ones.
  171. @pytest.mark.skip #The test currently fails because the definition container doesn't have a category, which is wrong but we don't have time to refactor that right now.
  172. def test_deserializeCompletesEmptyContainers(extruder_stack: cura.Settings.ExtruderStack):
  173. extruder_stack._containers = [DefinitionContainer(container_id = "definition")] #Set the internal state of this stack manually.
  174. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  175. extruder_stack.deserialize("")
  176. assert len(extruder_stack.getContainers()) == len(cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap) #Needs a slot for every type.
  177. for container_type_index in cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap:
  178. if container_type_index == cura.Settings.CuraContainerStack._ContainerIndexes.Definition: #We're not checking the definition.
  179. continue
  180. assert extruder_stack.getContainer(container_type_index).getId() == "empty" #All others need to be empty.
  181. ## Tests whether an instance container with the wrong type gets removed when
  182. # deserialising.
  183. def test_deserializeRemovesWrongInstanceContainer(extruder_stack):
  184. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type")
  185. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  186. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  187. extruder_stack.deserialize("")
  188. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
  189. ## Tests whether a container with the wrong class gets removed when
  190. # deserialising.
  191. def test_deserializeRemovesWrongContainerClass(extruder_stack):
  192. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class")
  193. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  194. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  195. extruder_stack.deserialize("")
  196. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
  197. ## Tests whether an instance container in the definition spot results in an
  198. # error.
  199. def test_deserializeWrongDefinitionClass(extruder_stack):
  200. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class.
  201. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  202. with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container.
  203. extruder_stack.deserialize("")
  204. ## Tests whether an instance container with the wrong type is moved into the
  205. # correct slot by deserialising.
  206. def test_deserializeMoveInstanceContainer(extruder_stack):
  207. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot.
  208. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  209. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  210. extruder_stack.deserialize("")
  211. assert extruder_stack.quality.getId() == "empty"
  212. assert extruder_stack.material.getId() != "empty"
  213. from UM.Settings.Validator import Validator
  214. ## Tests whether a definition container in the wrong spot is moved into the
  215. # correct spot by deserialising.
  216. @pytest.mark.skip #The test currently fails because the definition container doesn't have a category, which is wrong but we don't have time to refactor that right now.
  217. def test_deserializeMoveDefinitionContainer(extruder_stack):
  218. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot.
  219. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  220. extruder_stack.deserialize("")
  221. assert extruder_stack.material.getId() == "empty"
  222. assert extruder_stack.definition.getId() != "empty"
  223. UM.Settings.ContainerStack._containerRegistry = None
  224. ## Tests whether getProperty properly applies the stack-like behaviour on its
  225. # containers.
  226. def test_getPropertyFallThrough(extruder_stack):
  227. # ExtruderStack.setNextStack calls registerExtruder for backward compatibility, but we do not need a complete extruder manager
  228. ExtruderManager._ExtruderManager__instance = unittest.mock.MagicMock()
  229. #A few instance container mocks to put in the stack.
  230. mock_layer_heights = {} #For each container type, a mock container that defines layer height to something unique.
  231. mock_no_settings = {} #For each container type, a mock container that has no settings at all.
  232. container_indices = cura.Settings.CuraContainerStack._ContainerIndexes #Cache.
  233. for type_id, type_name in container_indices.IndexTypeMap.items():
  234. container = unittest.mock.MagicMock()
  235. # Return type_id when asking for value and -1 when asking for settable_per_extruder
  236. container.getProperty = lambda key, property, context = None, type_id = type_id: type_id if (key == "layer_height" and property == "value") else (None if property != "settable_per_extruder" else "-1") #Returns the container type ID as layer height, in order to identify it.
  237. container.hasProperty = lambda key, property: key == "layer_height"
  238. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  239. mock_layer_heights[type_id] = container
  240. container = unittest.mock.MagicMock()
  241. container.getProperty = unittest.mock.MagicMock(return_value = None) #Has no settings at all.
  242. container.hasProperty = unittest.mock.MagicMock(return_value = False)
  243. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  244. mock_no_settings[type_id] = container
  245. extruder_stack.userChanges = mock_no_settings[container_indices.UserChanges]
  246. extruder_stack.qualityChanges = mock_no_settings[container_indices.QualityChanges]
  247. extruder_stack.quality = mock_no_settings[container_indices.Quality]
  248. extruder_stack.material = mock_no_settings[container_indices.Material]
  249. extruder_stack.variant = mock_no_settings[container_indices.Variant]
  250. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  251. extruder_stack.definition = mock_layer_heights[container_indices.Definition] #There's a layer height in here!
  252. stack = GlobalStack("PyTest GlobalStack")
  253. extruder_stack.setNextStack(stack)
  254. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Definition
  255. extruder_stack.variant = mock_layer_heights[container_indices.Variant]
  256. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Variant
  257. extruder_stack.material = mock_layer_heights[container_indices.Material]
  258. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Material
  259. extruder_stack.quality = mock_layer_heights[container_indices.Quality]
  260. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Quality
  261. extruder_stack.qualityChanges = mock_layer_heights[container_indices.QualityChanges]
  262. assert extruder_stack.getProperty("layer_height", "value") == container_indices.QualityChanges
  263. extruder_stack.userChanges = mock_layer_heights[container_indices.UserChanges]
  264. assert extruder_stack.getProperty("layer_height", "value") == container_indices.UserChanges
  265. ## Tests whether inserting a container is properly forbidden.
  266. def test_insertContainer(extruder_stack):
  267. with pytest.raises(InvalidOperationError):
  268. extruder_stack.insertContainer(0, unittest.mock.MagicMock())
  269. ## Tests whether removing a container is properly forbidden.
  270. def test_removeContainer(extruder_stack):
  271. with pytest.raises(InvalidOperationError):
  272. extruder_stack.removeContainer(unittest.mock.MagicMock())
  273. ## Tests setting properties directly on the extruder stack.
  274. @pytest.mark.parametrize("key, property, value", [
  275. ("layer_height", "value", 0.1337),
  276. ("foo", "value", 100),
  277. ("support_enabled", "value", True),
  278. ("layer_height", "default_value", 0.1337),
  279. ("layer_height", "is_bright_pink", "of course")
  280. ])
  281. def test_setPropertyUser(key, property, value, extruder_stack):
  282. user_changes = unittest.mock.MagicMock()
  283. user_changes.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user")
  284. extruder_stack.userChanges = user_changes
  285. extruder_stack.setProperty(key, property, value) #The actual test.
  286. extruder_stack.userChanges.setProperty.assert_called_once_with(key, property, value) #Make sure that the user container gets a setProperty call.
  287. ## Tests setting properties on specific containers on the global stack.
  288. @pytest.mark.parametrize("target_container, stack_variable", [
  289. ("user", "userChanges"),
  290. ("quality_changes", "qualityChanges"),
  291. ("quality", "quality"),
  292. ("material", "material"),
  293. ("variant", "variant")
  294. ])
  295. def test_setPropertyOtherContainers(target_container, stack_variable, extruder_stack):
  296. #Other parameters that don't need to be varied.
  297. key = "layer_height"
  298. property = "value"
  299. value = 0.1337
  300. #A mock container in the right spot.
  301. container = unittest.mock.MagicMock()
  302. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = target_container)
  303. setattr(extruder_stack, stack_variable, container) #For instance, set global_stack.qualityChanges = container.
  304. extruder_stack.setProperty(key, property, value, target_container = target_container) #The actual test.
  305. getattr(extruder_stack, stack_variable).setProperty.assert_called_once_with(key, property, value) #Make sure that the proper container gets a setProperty call.