TestExtruderStack.py 23 KB

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