TestExtruderStack.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # Copyright (c) 2018 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 cura.Settings.CuraContainerStack #To get the list of container types.
  6. import UM.Settings.ContainerRegistry #To create empty instance containers.
  7. import UM.Settings.ContainerStack #To set the container registry the container stacks use.
  8. from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer.
  9. from UM.Settings.InstanceContainer import InstanceContainer #To check against the class of InstanceContainer.
  10. from cura.Settings import Exceptions
  11. from cura.Settings.Exceptions import InvalidContainerError, InvalidOperationError #To check whether the correct exceptions are raised.
  12. from cura.Settings.ExtruderManager import ExtruderManager
  13. from cura.Settings.cura_empty_instance_containers import empty_container
  14. ## Gets an instance container with a specified container type.
  15. #
  16. # \param container_type The type metadata for the instance container.
  17. # \return An instance container instance.
  18. def getInstanceContainer(container_type) -> InstanceContainer:
  19. container = InstanceContainer(container_id = "InstanceContainer")
  20. container.setMetaDataEntry("type", container_type)
  21. return container
  22. class DefinitionContainerSubClass(DefinitionContainer):
  23. def __init__(self):
  24. super().__init__(container_id = "SubDefinitionContainer")
  25. class InstanceContainerSubClass(InstanceContainer):
  26. def __init__(self, container_type):
  27. super().__init__(container_id = "SubInstanceContainer")
  28. self.setMetaDataEntry("type", container_type)
  29. #############################START OF TEST CASES################################
  30. ## Tests whether adding a container is properly forbidden.
  31. def test_addContainer(extruder_stack):
  32. with pytest.raises(InvalidOperationError):
  33. extruder_stack.addContainer(unittest.mock.MagicMock())
  34. #Tests setting user changes profiles to invalid containers.
  35. @pytest.mark.parametrize("container", [
  36. getInstanceContainer(container_type = "wrong container type"),
  37. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  38. DefinitionContainer(container_id = "wrong class")
  39. ])
  40. def test_constrainUserChangesInvalid(container, extruder_stack):
  41. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  42. extruder_stack.userChanges = container
  43. #Tests setting user changes profiles.
  44. @pytest.mark.parametrize("container", [
  45. getInstanceContainer(container_type = "user"),
  46. InstanceContainerSubClass(container_type = "user")
  47. ])
  48. def test_constrainUserChangesValid(container, extruder_stack):
  49. extruder_stack.userChanges = container #Should not give an error.
  50. #Tests setting quality changes profiles to invalid containers.
  51. @pytest.mark.parametrize("container", [
  52. getInstanceContainer(container_type = "wrong container type"),
  53. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  54. DefinitionContainer(container_id = "wrong class")
  55. ])
  56. def test_constrainQualityChangesInvalid(container, extruder_stack):
  57. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  58. extruder_stack.qualityChanges = container
  59. #Test setting quality changes profiles.
  60. @pytest.mark.parametrize("container", [
  61. getInstanceContainer(container_type = "quality_changes"),
  62. InstanceContainerSubClass(container_type = "quality_changes")
  63. ])
  64. def test_constrainQualityChangesValid(container, extruder_stack):
  65. extruder_stack.qualityChanges = container #Should not give an error.
  66. #Tests setting quality profiles to invalid containers.
  67. @pytest.mark.parametrize("container", [
  68. getInstanceContainer(container_type = "wrong container type"),
  69. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  70. DefinitionContainer(container_id = "wrong class")
  71. ])
  72. def test_constrainQualityInvalid(container, extruder_stack):
  73. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  74. extruder_stack.quality = container
  75. #Test setting quality profiles.
  76. @pytest.mark.parametrize("container", [
  77. getInstanceContainer(container_type = "quality"),
  78. InstanceContainerSubClass(container_type = "quality")
  79. ])
  80. def test_constrainQualityValid(container, extruder_stack):
  81. extruder_stack.quality = container #Should not give an error.
  82. #Tests setting materials to invalid containers.
  83. @pytest.mark.parametrize("container", [
  84. getInstanceContainer(container_type = "wrong container type"),
  85. getInstanceContainer(container_type = "quality"), #Existing, but still wrong type.
  86. DefinitionContainer(container_id = "wrong class")
  87. ])
  88. def test_constrainMaterialInvalid(container, extruder_stack):
  89. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  90. extruder_stack.material = container
  91. #Test setting materials.
  92. @pytest.mark.parametrize("container", [
  93. getInstanceContainer(container_type = "material"),
  94. InstanceContainerSubClass(container_type = "material")
  95. ])
  96. def test_constrainMaterialValid(container, extruder_stack):
  97. extruder_stack.material = container #Should not give an error.
  98. #Tests setting variants to invalid containers.
  99. @pytest.mark.parametrize("container", [
  100. getInstanceContainer(container_type = "wrong container type"),
  101. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  102. DefinitionContainer(container_id = "wrong class")
  103. ])
  104. def test_constrainVariantInvalid(container, extruder_stack):
  105. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  106. extruder_stack.variant = container
  107. #Test setting variants.
  108. @pytest.mark.parametrize("container", [
  109. getInstanceContainer(container_type = "variant"),
  110. InstanceContainerSubClass(container_type = "variant")
  111. ])
  112. def test_constrainVariantValid(container, extruder_stack):
  113. extruder_stack.variant = container #Should not give an error.
  114. #Tests setting definition changes profiles to invalid containers.
  115. @pytest.mark.parametrize("container", [
  116. getInstanceContainer(container_type = "wrong container type"),
  117. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  118. DefinitionContainer(container_id = "wrong class")
  119. ])
  120. def test_constrainDefinitionChangesInvalid(container, global_stack):
  121. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  122. global_stack.definitionChanges = container
  123. #Test setting definition changes profiles.
  124. @pytest.mark.parametrize("container", [
  125. getInstanceContainer(container_type = "definition_changes"),
  126. InstanceContainerSubClass(container_type = "definition_changes")
  127. ])
  128. def test_constrainDefinitionChangesValid(container, global_stack):
  129. global_stack.definitionChanges = container #Should not give an error.
  130. #Tests setting definitions to invalid containers.
  131. @pytest.mark.parametrize("container", [
  132. getInstanceContainer(container_type = "wrong class"),
  133. getInstanceContainer(container_type = "material"), #Existing, but still wrong class.
  134. ])
  135. def test_constrainDefinitionInvalid(container, extruder_stack):
  136. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  137. extruder_stack.definition = container
  138. #Test setting definitions.
  139. @pytest.mark.parametrize("container", [
  140. DefinitionContainer(container_id = "DefinitionContainer"),
  141. DefinitionContainerSubClass()
  142. ])
  143. def test_constrainDefinitionValid(container, extruder_stack):
  144. extruder_stack.definition = container #Should not give an error.
  145. ## Tests whether deserialising completes the missing containers with empty ones.
  146. def test_deserializeCompletesEmptyContainers(extruder_stack):
  147. extruder_stack._containers = [DefinitionContainer(container_id = "definition"), extruder_stack.definitionChanges] #Set the internal state of this stack manually.
  148. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  149. extruder_stack.deserialize("")
  150. assert len(extruder_stack.getContainers()) == len(cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap) #Needs a slot for every type.
  151. for container_type_index in cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap:
  152. if container_type_index in \
  153. (cura.Settings.CuraContainerStack._ContainerIndexes.Definition,
  154. cura.Settings.CuraContainerStack._ContainerIndexes.DefinitionChanges): # We're not checking the definition or definition_changes
  155. continue
  156. assert extruder_stack.getContainer(container_type_index) == empty_container #All others need to be empty.
  157. ## Tests whether an instance container with the wrong type gets removed when deserialising.
  158. def test_deserializeRemovesWrongInstanceContainer(extruder_stack):
  159. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type")
  160. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  161. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  162. extruder_stack.deserialize("")
  163. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
  164. ## Tests whether a container with the wrong class gets removed when deserialising.
  165. def test_deserializeRemovesWrongContainerClass(extruder_stack):
  166. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class")
  167. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  168. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  169. extruder_stack.deserialize("")
  170. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
  171. ## Tests whether an instance container in the definition spot results in an error.
  172. def test_deserializeWrongDefinitionClass(extruder_stack):
  173. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class.
  174. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  175. with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container.
  176. extruder_stack.deserialize("")
  177. ## Tests whether an instance container with the wrong type is moved into the correct slot by deserialising.
  178. def test_deserializeMoveInstanceContainer(extruder_stack):
  179. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot.
  180. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  181. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  182. extruder_stack.deserialize("")
  183. assert extruder_stack.quality == empty_container
  184. assert extruder_stack.material != empty_container
  185. ## Tests whether a definition container in the wrong spot is moved into the correct spot by deserialising.
  186. def test_deserializeMoveDefinitionContainer(extruder_stack):
  187. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot.
  188. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  189. extruder_stack.deserialize("")
  190. assert extruder_stack.material == empty_container
  191. assert extruder_stack.definition != empty_container
  192. ## Tests whether getProperty properly applies the stack-like behaviour on its containers.
  193. def test_getPropertyFallThrough(global_stack, extruder_stack):
  194. # ExtruderStack.setNextStack calls registerExtruder for backward compatibility, but we do not need a complete extruder manager
  195. ExtruderManager._ExtruderManager__instance = unittest.mock.MagicMock()
  196. #A few instance container mocks to put in the stack.
  197. mock_layer_heights = {} #For each container type, a mock container that defines layer height to something unique.
  198. mock_no_settings = {} #For each container type, a mock container that has no settings at all.
  199. container_indices = cura.Settings.CuraContainerStack._ContainerIndexes #Cache.
  200. for type_id, type_name in container_indices.IndexTypeMap.items():
  201. container = unittest.mock.MagicMock()
  202. # Return type_id when asking for value and -1 when asking for settable_per_extruder
  203. 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.
  204. container.hasProperty = lambda key, property: key == "layer_height"
  205. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  206. mock_layer_heights[type_id] = container
  207. container = unittest.mock.MagicMock()
  208. container.getProperty = unittest.mock.MagicMock(return_value = None) #Has no settings at all.
  209. container.hasProperty = unittest.mock.MagicMock(return_value = False)
  210. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  211. mock_no_settings[type_id] = container
  212. extruder_stack.userChanges = mock_no_settings[container_indices.UserChanges]
  213. extruder_stack.qualityChanges = mock_no_settings[container_indices.QualityChanges]
  214. extruder_stack.quality = mock_no_settings[container_indices.Quality]
  215. extruder_stack.material = mock_no_settings[container_indices.Material]
  216. extruder_stack.variant = mock_no_settings[container_indices.Variant]
  217. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  218. extruder_stack.definition = mock_layer_heights[container_indices.Definition] #There's a layer height in here!
  219. extruder_stack.setNextStack(global_stack)
  220. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Definition
  221. extruder_stack.variant = mock_layer_heights[container_indices.Variant]
  222. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Variant
  223. extruder_stack.material = mock_layer_heights[container_indices.Material]
  224. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Material
  225. extruder_stack.quality = mock_layer_heights[container_indices.Quality]
  226. assert extruder_stack.getProperty("layer_height", "value") == container_indices.Quality
  227. extruder_stack.qualityChanges = mock_layer_heights[container_indices.QualityChanges]
  228. assert extruder_stack.getProperty("layer_height", "value") == container_indices.QualityChanges
  229. extruder_stack.userChanges = mock_layer_heights[container_indices.UserChanges]
  230. assert extruder_stack.getProperty("layer_height", "value") == container_indices.UserChanges
  231. ## Tests whether inserting a container is properly forbidden.
  232. def test_insertContainer(extruder_stack):
  233. with pytest.raises(InvalidOperationError):
  234. extruder_stack.insertContainer(0, unittest.mock.MagicMock())
  235. ## Tests whether removing a container is properly forbidden.
  236. def test_removeContainer(extruder_stack):
  237. with pytest.raises(InvalidOperationError):
  238. extruder_stack.removeContainer(unittest.mock.MagicMock())
  239. ## Tests setting properties directly on the extruder stack.
  240. @pytest.mark.parametrize("key, property, value", [
  241. ("layer_height", "value", 0.1337),
  242. ("foo", "value", 100),
  243. ("support_enabled", "value", True),
  244. ("layer_height", "default_value", 0.1337),
  245. ("layer_height", "is_bright_pink", "of course")
  246. ])
  247. def test_setPropertyUser(key, property, value, extruder_stack):
  248. user_changes = unittest.mock.MagicMock()
  249. user_changes.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user")
  250. extruder_stack.userChanges = user_changes
  251. extruder_stack.setProperty(key, property, value) #The actual test.
  252. extruder_stack.userChanges.setProperty.assert_called_once_with(key, property, value, None, False) #Make sure that the user container gets a setProperty call.
  253. def test_setEnabled(extruder_stack):
  254. extruder_stack.setEnabled(True)
  255. assert extruder_stack.isEnabled
  256. extruder_stack.setEnabled(False)
  257. assert not extruder_stack.isEnabled
  258. def test_getPropertyWithoutGlobal(extruder_stack):
  259. assert extruder_stack.getNextStack() is None
  260. with pytest.raises(Exceptions.NoGlobalStackError):
  261. extruder_stack.getProperty("whatever", "value")
  262. def test_getMachineDefinitionWithoutGlobal(extruder_stack):
  263. assert extruder_stack.getNextStack() is None
  264. with pytest.raises(Exceptions.NoGlobalStackError):
  265. extruder_stack._getMachineDefinition()
  266. def test_getMachineDefinition(extruder_stack):
  267. mocked_next_stack = unittest.mock.MagicMock()
  268. mocked_next_stack._getMachineDefinition = unittest.mock.MagicMock(return_value = "ZOMG")
  269. extruder_stack.getNextStack = unittest.mock.MagicMock(return_value = mocked_next_stack)
  270. assert extruder_stack._getMachineDefinition() == "ZOMG"