TestGlobalStack.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import pytest #This module contains unit tests.
  4. import unittest.mock #To monkeypatch some mocks in place of dependencies.
  5. import cura.Settings.CuraContainerStack #To get the list of container types.
  6. from cura.Settings.Exceptions import InvalidContainerError, InvalidOperationError #To test raising these errors.
  7. from UM.Settings.DefinitionContainer import DefinitionContainer #To test against the class DefinitionContainer.
  8. from UM.Settings.InstanceContainer import InstanceContainer #To test against the class InstanceContainer.
  9. from UM.Settings.SettingInstance import InstanceState
  10. import UM.Settings.ContainerRegistry
  11. import UM.Settings.ContainerStack
  12. import UM.Settings.SettingDefinition #To add settings to the definition.
  13. from cura.Settings.cura_empty_instance_containers import empty_container
  14. def getInstanceContainer(container_type) -> InstanceContainer:
  15. """Gets an instance container with a specified container type.
  16. :param container_type: The type metadata for the instance container.
  17. :return: An instance container instance.
  18. """
  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. def test_addContainer(global_stack):
  31. """Tests whether adding a container is properly forbidden."""
  32. with pytest.raises(InvalidOperationError):
  33. global_stack.addContainer(unittest.mock.MagicMock())
  34. def test_addExtruder(global_stack):
  35. """Tests adding extruders to the global stack."""
  36. mock_definition = unittest.mock.MagicMock()
  37. mock_definition.getProperty = lambda key, property, context = None: 2 if key == "machine_extruder_count" and property == "value" else None
  38. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  39. global_stack.definition = mock_definition
  40. assert len(global_stack.extruderList) == 0
  41. first_extruder = unittest.mock.MagicMock()
  42. first_extruder.getMetaDataEntry = lambda key: 0 if key == "position" else None
  43. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  44. global_stack.addExtruder(first_extruder)
  45. assert len(global_stack.extruderList) == 1
  46. assert global_stack.extruderList[0] == first_extruder
  47. second_extruder = unittest.mock.MagicMock()
  48. second_extruder.getMetaDataEntry = lambda key: 1 if key == "position" else None
  49. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  50. global_stack.addExtruder(second_extruder)
  51. assert len(global_stack.extruderList) == 2
  52. assert global_stack.extruderList[1] == second_extruder
  53. # Disabled for now for Custom FDM Printer
  54. # with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  55. # with pytest.raises(TooManyExtrudersError): #Should be limited to 2 extruders because of machine_extruder_count.
  56. # global_stack.addExtruder(unittest.mock.MagicMock())
  57. assert len(global_stack.extruderList) == 2 # Didn't add the faulty extruder.
  58. #Tests setting user changes profiles to invalid containers.
  59. @pytest.mark.parametrize("container", [
  60. getInstanceContainer(container_type = "wrong container type"),
  61. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  62. DefinitionContainer(container_id = "wrong class")
  63. ])
  64. def test_constrainUserChangesInvalid(container, global_stack):
  65. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  66. global_stack.userChanges = container
  67. #Tests setting user changes profiles.
  68. @pytest.mark.parametrize("container", [
  69. getInstanceContainer(container_type = "user"),
  70. InstanceContainerSubClass(container_type = "user")
  71. ])
  72. def test_constrainUserChangesValid(container, global_stack):
  73. global_stack.userChanges = container #Should not give an error.
  74. #Tests setting quality 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_constrainQualityChangesInvalid(container, global_stack):
  81. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  82. global_stack.qualityChanges = container
  83. #Test setting quality changes profiles.
  84. @pytest.mark.parametrize("container", [
  85. getInstanceContainer(container_type = "quality_changes"),
  86. InstanceContainerSubClass(container_type = "quality_changes")
  87. ])
  88. def test_constrainQualityChangesValid(container, global_stack):
  89. global_stack.qualityChanges = container #Should not give an error.
  90. #Tests setting quality 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_constrainQualityInvalid(container, global_stack):
  97. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  98. global_stack.quality = container
  99. #Test setting quality profiles.
  100. @pytest.mark.parametrize("container", [
  101. getInstanceContainer(container_type = "quality"),
  102. InstanceContainerSubClass(container_type = "quality")
  103. ])
  104. def test_constrainQualityValid(container, global_stack):
  105. global_stack.quality = container #Should not give an error.
  106. #Tests setting materials to invalid containers.
  107. @pytest.mark.parametrize("container", [
  108. getInstanceContainer(container_type = "wrong container type"),
  109. getInstanceContainer(container_type = "quality"), #Existing, but still wrong type.
  110. DefinitionContainer(container_id = "wrong class")
  111. ])
  112. def test_constrainMaterialInvalid(container, global_stack):
  113. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  114. global_stack.material = container
  115. #Test setting materials.
  116. @pytest.mark.parametrize("container", [
  117. getInstanceContainer(container_type = "material"),
  118. InstanceContainerSubClass(container_type = "material")
  119. ])
  120. def test_constrainMaterialValid(container, global_stack):
  121. global_stack.material = container #Should not give an error.
  122. #Tests setting variants to invalid containers.
  123. @pytest.mark.parametrize("container", [
  124. getInstanceContainer(container_type = "wrong container type"),
  125. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  126. DefinitionContainer(container_id = "wrong class")
  127. ])
  128. def test_constrainVariantInvalid(container, global_stack):
  129. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  130. global_stack.variant = container
  131. #Test setting variants.
  132. @pytest.mark.parametrize("container", [
  133. getInstanceContainer(container_type = "variant"),
  134. InstanceContainerSubClass(container_type = "variant")
  135. ])
  136. def test_constrainVariantValid(container, global_stack):
  137. global_stack.variant = container #Should not give an error.
  138. #Tests setting definition changes profiles 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_constrainDefinitionChangesInvalid(container, global_stack):
  145. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  146. global_stack.definitionChanges = container
  147. #Test setting definition changes profiles.
  148. @pytest.mark.parametrize("container", [
  149. getInstanceContainer(container_type = "definition_changes"),
  150. InstanceContainerSubClass(container_type = "definition_changes")
  151. ])
  152. def test_constrainDefinitionChangesValid(container, global_stack):
  153. global_stack.definitionChanges = 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_constrainDefinitionInvalid(container, global_stack):
  160. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  161. global_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, global_stack):
  168. global_stack.definition = container #Should not give an error.
  169. def test_deserializeCompletesEmptyContainers(global_stack):
  170. """Tests whether deserialising completes the missing containers with empty ones. The initial containers are just the
  171. definition and the definition_changes (that cannot be empty after CURA-5281)
  172. """
  173. global_stack._containers = [DefinitionContainer(container_id = "definition"), global_stack.definitionChanges] #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. global_stack.deserialize("")
  176. assert len(global_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 in \
  179. (cura.Settings.CuraContainerStack._ContainerIndexes.Definition, cura.Settings.CuraContainerStack._ContainerIndexes.DefinitionChanges): #We're not checking the definition or definition_changes
  180. continue
  181. assert global_stack.getContainer(container_type_index) == empty_container #All others need to be empty.
  182. def test_deserializeRemovesWrongInstanceContainer(global_stack):
  183. """Tests whether an instance container with the wrong type gets removed when deserialising."""
  184. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type")
  185. global_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. global_stack.deserialize("")
  188. assert global_stack.quality == global_stack._empty_instance_container #Replaced with empty.
  189. def test_deserializeRemovesWrongContainerClass(global_stack):
  190. """Tests whether a container with the wrong class gets removed when deserialising."""
  191. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class")
  192. global_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. global_stack.deserialize("")
  195. assert global_stack.quality == global_stack._empty_instance_container #Replaced with empty.
  196. def test_deserializeWrongDefinitionClass(global_stack):
  197. """Tests whether an instance container in the definition spot results in an error."""
  198. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class.
  199. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  200. with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container.
  201. global_stack.deserialize("")
  202. def test_deserializeMoveInstanceContainer(global_stack):
  203. """Tests whether an instance container with the wrong type is moved into the correct slot by deserialising."""
  204. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot.
  205. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  206. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  207. global_stack.deserialize("")
  208. assert global_stack.quality == empty_container
  209. assert global_stack.material != empty_container
  210. def test_deserializeMoveDefinitionContainer(global_stack):
  211. """Tests whether a definition container in the wrong spot is moved into the correct spot by deserialising."""
  212. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot.
  213. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  214. global_stack.deserialize("")
  215. assert global_stack.material == empty_container
  216. assert global_stack.definition != empty_container
  217. def test_getPropertyFallThrough(global_stack):
  218. """Tests whether getProperty properly applies the stack-like behaviour on its containers."""
  219. #A few instance container mocks to put in the stack.
  220. mock_layer_heights = {} #For each container type, a mock container that defines layer height to something unique.
  221. mock_no_settings = {} #For each container type, a mock container that has no settings at all.
  222. container_indexes = cura.Settings.CuraContainerStack._ContainerIndexes #Cache.
  223. for type_id, type_name in container_indexes.IndexTypeMap.items():
  224. container = unittest.mock.MagicMock()
  225. container.getProperty = lambda key, property, context = None, type_id = type_id: type_id if (key == "layer_height" and property == "value") else None #Returns the container type ID as layer height, in order to identify it.
  226. container.hasProperty = lambda key, property: key == "layer_height"
  227. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  228. mock_layer_heights[type_id] = container
  229. container = unittest.mock.MagicMock()
  230. container.getProperty = unittest.mock.MagicMock(return_value = None) #Has no settings at all.
  231. container.hasProperty = unittest.mock.MagicMock(return_value = False)
  232. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  233. mock_no_settings[type_id] = container
  234. global_stack.userChanges = mock_no_settings[container_indexes.UserChanges]
  235. global_stack.qualityChanges = mock_no_settings[container_indexes.QualityChanges]
  236. global_stack.quality = mock_no_settings[container_indexes.Quality]
  237. global_stack.material = mock_no_settings[container_indexes.Material]
  238. global_stack.variant = mock_no_settings[container_indexes.Variant]
  239. global_stack.definitionChanges = mock_no_settings[container_indexes.DefinitionChanges]
  240. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  241. global_stack.definition = mock_layer_heights[container_indexes.Definition] #There's a layer height in here!
  242. assert global_stack.getProperty("layer_height", "value") == container_indexes.Definition
  243. global_stack.definitionChanges = mock_layer_heights[container_indexes.DefinitionChanges]
  244. assert global_stack.getProperty("layer_height", "value") == container_indexes.DefinitionChanges
  245. global_stack.variant = mock_layer_heights[container_indexes.Variant]
  246. assert global_stack.getProperty("layer_height", "value") == container_indexes.Variant
  247. global_stack.material = mock_layer_heights[container_indexes.Material]
  248. assert global_stack.getProperty("layer_height", "value") == container_indexes.Material
  249. global_stack.quality = mock_layer_heights[container_indexes.Quality]
  250. assert global_stack.getProperty("layer_height", "value") == container_indexes.Quality
  251. global_stack.qualityChanges = mock_layer_heights[container_indexes.QualityChanges]
  252. assert global_stack.getProperty("layer_height", "value") == container_indexes.QualityChanges
  253. global_stack.userChanges = mock_layer_heights[container_indexes.UserChanges]
  254. assert global_stack.getProperty("layer_height", "value") == container_indexes.UserChanges
  255. def test_getPropertyNoResolveInDefinition(global_stack):
  256. """In definitions, test whether having no resolve allows us to find the value."""
  257. value = unittest.mock.MagicMock() #Just sets the value for bed temperature.
  258. value.getProperty = lambda key, property, context = None: 10 if (key == "material_bed_temperature" and property == "value") else None
  259. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  260. global_stack.definition = value
  261. assert global_stack.getProperty("material_bed_temperature", "value") == 10 #No resolve, so fall through to value.
  262. def test_getPropertyResolveInDefinition(global_stack):
  263. """In definitions, when the value is asked and there is a resolve function, it must get the resolve first."""
  264. resolve_and_value = unittest.mock.MagicMock() #Sets the resolve and value for bed temperature.
  265. resolve_and_value.getProperty = lambda key, property, context = None: (7.5 if property == "resolve" else 5) if (key == "material_bed_temperature" and property in ("resolve", "value")) else None #7.5 resolve, 5 value.
  266. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  267. global_stack.definition = resolve_and_value
  268. assert global_stack.getProperty("material_bed_temperature", "value") == 7.5 #Resolve wins in the definition.
  269. def test_getPropertyResolveInInstance(global_stack):
  270. """In instance containers, when the value is asked and there is a resolve function, it must get the value first."""
  271. container_indices = cura.Settings.CuraContainerStack._ContainerIndexes
  272. instance_containers = {}
  273. for container_type in container_indices.IndexTypeMap:
  274. instance_containers[container_type] = unittest.mock.MagicMock() #Sets the resolve and value for bed temperature.
  275. instance_containers[container_type].getProperty = lambda key, property, context = None: (7.5 if property == "resolve" else (InstanceState.User if property == "state" else (5 if property != "limit_to_extruder" else "-1"))) if (key == "material_bed_temperature") else None #7.5 resolve, 5 value.
  276. instance_containers[container_type].getMetaDataEntry = unittest.mock.MagicMock(return_value = container_indices.IndexTypeMap[container_type]) #Make queries for the type return the desired type.
  277. instance_containers[container_indices.Definition].getProperty = lambda key, property, context = None: 10 if (key == "material_bed_temperature" and property == "value") else None #Definition only has value.
  278. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  279. global_stack.definition = instance_containers[container_indices.Definition] #Stack must have a definition.
  280. #For all instance container slots, the value reigns over resolve.
  281. global_stack.definitionChanges = instance_containers[container_indices.DefinitionChanges]
  282. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  283. global_stack.variant = instance_containers[container_indices.Variant]
  284. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  285. global_stack.material = instance_containers[container_indices.Material]
  286. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  287. global_stack.quality = instance_containers[container_indices.Quality]
  288. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  289. global_stack.qualityChanges = instance_containers[container_indices.QualityChanges]
  290. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  291. global_stack.userChanges = instance_containers[container_indices.UserChanges]
  292. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  293. def test_getPropertyInstancesBeforeResolve(global_stack):
  294. """Tests whether the value in instances gets evaluated before the resolve in definitions."""
  295. def getValueProperty(key, property, context = None):
  296. if key != "material_bed_temperature":
  297. return None
  298. if property == "value":
  299. return 10
  300. if property == "limit_to_extruder":
  301. return -1
  302. return InstanceState.User
  303. def getResolveProperty(key, property, context = None):
  304. if key != "material_bed_temperature":
  305. return None
  306. if property == "resolve":
  307. return 7.5
  308. return None
  309. value = unittest.mock.MagicMock() #Sets just the value.
  310. value.getProperty = unittest.mock.MagicMock(side_effect = getValueProperty)
  311. value.getMetaDataEntry = unittest.mock.MagicMock(return_value = "quality_changes")
  312. resolve = unittest.mock.MagicMock() #Sets just the resolve.
  313. resolve.getProperty = unittest.mock.MagicMock(side_effect = getResolveProperty)
  314. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  315. global_stack.definition = resolve
  316. global_stack.qualityChanges = value
  317. assert global_stack.getProperty("material_bed_temperature", "value") == 10
  318. def test_hasUserValueUserChanges(global_stack):
  319. """Tests whether the hasUserValue returns true for settings that are changed in the user-changes container."""
  320. container = unittest.mock.MagicMock()
  321. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user")
  322. container.hasProperty = lambda key, property: key == "layer_height" #Only have the layer_height property set.
  323. global_stack.userChanges = container
  324. assert global_stack.hasUserValue("layer_height")
  325. assert not global_stack.hasUserValue("infill_sparse_density")
  326. assert not global_stack.hasUserValue("")
  327. def test_hasUserValueQualityChanges(global_stack):
  328. """Tests whether the hasUserValue returns true for settings that are changed in the quality-changes container."""
  329. container = unittest.mock.MagicMock()
  330. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = "quality_changes")
  331. container.hasProperty = lambda key, property: key == "layer_height" #Only have the layer_height property set.
  332. global_stack.qualityChanges = container
  333. assert global_stack.hasUserValue("layer_height")
  334. assert not global_stack.hasUserValue("infill_sparse_density")
  335. assert not global_stack.hasUserValue("")
  336. def test_hasNoUserValue(global_stack):
  337. """Tests whether a container in some other place on the stack is correctly not recognised as user value."""
  338. container = unittest.mock.MagicMock()
  339. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = "quality")
  340. container.hasProperty = lambda key, property: key == "layer_height" #Only have the layer_height property set.
  341. global_stack.quality = container
  342. assert not global_stack.hasUserValue("layer_height") #However this container is quality, so it's not a user value.
  343. def test_insertContainer(global_stack):
  344. """Tests whether inserting a container is properly forbidden."""
  345. with pytest.raises(InvalidOperationError):
  346. global_stack.insertContainer(0, unittest.mock.MagicMock())
  347. def test_removeContainer(global_stack):
  348. """Tests whether removing a container is properly forbidden."""
  349. with pytest.raises(InvalidOperationError):
  350. global_stack.removeContainer(unittest.mock.MagicMock())
  351. def test_setNextStack(global_stack):
  352. """Tests whether changing the next stack is properly forbidden."""
  353. with pytest.raises(InvalidOperationError):
  354. global_stack.setNextStack(unittest.mock.MagicMock())
  355. ## Tests setting properties directly on the global stack.
  356. @pytest.mark.parametrize("key, property, value", [
  357. ("layer_height", "value", 0.1337),
  358. ("foo", "value", 100),
  359. ("support_enabled", "value", True),
  360. ("layer_height", "default_value", 0.1337),
  361. ("layer_height", "is_bright_pink", "of course")
  362. ])
  363. def test_setPropertyUser(key, property, value, global_stack):
  364. user_changes = unittest.mock.MagicMock()
  365. user_changes.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user")
  366. global_stack.userChanges = user_changes
  367. global_stack.setProperty(key, property, value) # The actual test.
  368. # Make sure that the user container gets a setProperty call.
  369. global_stack.userChanges.setProperty.assert_called_once_with(key, property, value, None, False)