TestExtruderStack.py 21 KB

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