TestGlobalStack.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 unit tests.
  4. import unittest.mock #To monkeypatch some mocks in place of dependencies.
  5. import copy
  6. import cura.CuraApplication
  7. import cura.Settings.GlobalStack #The module we're testing.
  8. import cura.Settings.CuraContainerStack #To get the list of container types.
  9. from cura.Settings.Exceptions import TooManyExtrudersError, InvalidContainerError, InvalidOperationError #To test raising these errors.
  10. from UM.Settings.DefinitionContainer import DefinitionContainer #To test against the class DefinitionContainer.
  11. from UM.Settings.InstanceContainer import InstanceContainer #To test against the class InstanceContainer.
  12. from UM.Settings.SettingInstance import InstanceState
  13. import UM.Settings.ContainerRegistry
  14. import UM.Settings.ContainerStack
  15. import UM.Settings.SettingDefinition #To add settings to the definition.
  16. from UM.Settings.ContainerRegistry import ContainerRegistry
  17. ## Fake container registry that always provides all containers you ask of.
  18. @pytest.yield_fixture()
  19. def container_registry():
  20. registry = unittest.mock.MagicMock()
  21. registry.return_value = unittest.mock.NonCallableMagicMock()
  22. registry.findInstanceContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
  23. registry.findDefinitionContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
  24. UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = registry
  25. UM.Settings.ContainerStack._containerRegistry = registry
  26. yield registry
  27. UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = None
  28. UM.Settings.ContainerStack._containerRegistry = None
  29. #An empty global stack to test with.
  30. @pytest.fixture()
  31. def global_stack() -> cura.Settings.GlobalStack.GlobalStack:
  32. creteEmptyContainers()
  33. return cura.Settings.GlobalStack.GlobalStack("TestStack")
  34. ## Gets an instance container with a specified container type.
  35. #
  36. # \param container_type The type metadata for the instance container.
  37. # \return An instance container instance.
  38. def getInstanceContainer(container_type) -> InstanceContainer:
  39. container = InstanceContainer(container_id = "InstanceContainer")
  40. container.addMetaDataEntry("type", container_type)
  41. return container
  42. def creteEmptyContainers():
  43. empty_container = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  44. empty_variant_container = copy.deepcopy(empty_container)
  45. empty_variant_container.setMetaDataEntry("id", "empty_variant")
  46. empty_variant_container.addMetaDataEntry("type", "variant")
  47. ContainerRegistry.getInstance().addContainer(empty_variant_container)
  48. empty_material_container = copy.deepcopy(empty_container)
  49. empty_material_container.setMetaDataEntry("id", "empty_material")
  50. empty_material_container.addMetaDataEntry("type", "material")
  51. ContainerRegistry.getInstance().addContainer(empty_material_container)
  52. empty_quality_container = copy.deepcopy(empty_container)
  53. empty_quality_container.setMetaDataEntry("id", "empty_quality")
  54. empty_quality_container.setName("Not Supported")
  55. empty_quality_container.addMetaDataEntry("quality_type", "not_supported")
  56. empty_quality_container.addMetaDataEntry("type", "quality")
  57. empty_quality_container.addMetaDataEntry("supported", False)
  58. ContainerRegistry.getInstance().addContainer(empty_quality_container)
  59. empty_quality_changes_container = copy.deepcopy(empty_container)
  60. empty_quality_changes_container.setMetaDataEntry("id", "empty_quality_changes")
  61. empty_quality_changes_container.addMetaDataEntry("type", "quality_changes")
  62. ContainerRegistry.getInstance().addContainer(empty_quality_changes_container)
  63. class DefinitionContainerSubClass(DefinitionContainer):
  64. def __init__(self):
  65. super().__init__(container_id = "SubDefinitionContainer")
  66. class InstanceContainerSubClass(InstanceContainer):
  67. def __init__(self, container_type):
  68. super().__init__(container_id = "SubInstanceContainer")
  69. self.addMetaDataEntry("type", container_type)
  70. #############################START OF TEST CASES################################
  71. ## Tests whether adding a container is properly forbidden.
  72. def test_addContainer(global_stack):
  73. with pytest.raises(InvalidOperationError):
  74. global_stack.addContainer(unittest.mock.MagicMock())
  75. ## Tests adding extruders to the global stack.
  76. def test_addExtruder(global_stack):
  77. mock_definition = unittest.mock.MagicMock()
  78. mock_definition.getProperty = lambda key, property, context = None: 2 if key == "machine_extruder_count" and property == "value" else None
  79. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  80. global_stack.definition = mock_definition
  81. assert len(global_stack.extruders) == 0
  82. first_extruder = unittest.mock.MagicMock()
  83. first_extruder.getMetaDataEntry = lambda key: 0 if key == "position" else None
  84. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  85. global_stack.addExtruder(first_extruder)
  86. assert len(global_stack.extruders) == 1
  87. assert global_stack.extruders[0] == first_extruder
  88. second_extruder = unittest.mock.MagicMock()
  89. second_extruder.getMetaDataEntry = lambda key: 1 if key == "position" else None
  90. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  91. global_stack.addExtruder(second_extruder)
  92. assert len(global_stack.extruders) == 2
  93. assert global_stack.extruders[1] == second_extruder
  94. # Disabled for now for Custom FDM Printer
  95. # with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  96. # with pytest.raises(TooManyExtrudersError): #Should be limited to 2 extruders because of machine_extruder_count.
  97. # global_stack.addExtruder(unittest.mock.MagicMock())
  98. assert len(global_stack.extruders) == 2 #Didn't add the faulty extruder.
  99. #Tests setting user changes profiles to invalid containers.
  100. @pytest.mark.parametrize("container", [
  101. getInstanceContainer(container_type = "wrong container type"),
  102. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  103. DefinitionContainer(container_id = "wrong class")
  104. ])
  105. def test_constrainUserChangesInvalid(container, global_stack):
  106. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  107. global_stack.userChanges = container
  108. #Tests setting user changes profiles.
  109. @pytest.mark.parametrize("container", [
  110. getInstanceContainer(container_type = "user"),
  111. InstanceContainerSubClass(container_type = "user")
  112. ])
  113. def test_constrainUserChangesValid(container, global_stack):
  114. global_stack.userChanges = container #Should not give an error.
  115. #Tests setting quality changes profiles to invalid containers.
  116. @pytest.mark.parametrize("container", [
  117. getInstanceContainer(container_type = "wrong container type"),
  118. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  119. DefinitionContainer(container_id = "wrong class")
  120. ])
  121. def test_constrainQualityChangesInvalid(container, global_stack):
  122. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  123. global_stack.qualityChanges = container
  124. #Test setting quality changes profiles.
  125. @pytest.mark.parametrize("container", [
  126. getInstanceContainer(container_type = "quality_changes"),
  127. InstanceContainerSubClass(container_type = "quality_changes")
  128. ])
  129. def test_constrainQualityChangesValid(container, global_stack):
  130. global_stack.qualityChanges = container #Should not give an error.
  131. #Tests setting quality profiles to invalid containers.
  132. @pytest.mark.parametrize("container", [
  133. getInstanceContainer(container_type = "wrong container type"),
  134. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  135. DefinitionContainer(container_id = "wrong class")
  136. ])
  137. def test_constrainQualityInvalid(container, global_stack):
  138. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  139. global_stack.quality = container
  140. #Test setting quality profiles.
  141. @pytest.mark.parametrize("container", [
  142. getInstanceContainer(container_type = "quality"),
  143. InstanceContainerSubClass(container_type = "quality")
  144. ])
  145. def test_constrainQualityValid(container, global_stack):
  146. global_stack.quality = container #Should not give an error.
  147. #Tests setting materials to invalid containers.
  148. @pytest.mark.parametrize("container", [
  149. getInstanceContainer(container_type = "wrong container type"),
  150. getInstanceContainer(container_type = "quality"), #Existing, but still wrong type.
  151. DefinitionContainer(container_id = "wrong class")
  152. ])
  153. def test_constrainMaterialInvalid(container, global_stack):
  154. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  155. global_stack.material = container
  156. #Test setting materials.
  157. @pytest.mark.parametrize("container", [
  158. getInstanceContainer(container_type = "material"),
  159. InstanceContainerSubClass(container_type = "material")
  160. ])
  161. def test_constrainMaterialValid(container, global_stack):
  162. global_stack.material = container #Should not give an error.
  163. #Tests setting variants to invalid containers.
  164. @pytest.mark.parametrize("container", [
  165. getInstanceContainer(container_type = "wrong container type"),
  166. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  167. DefinitionContainer(container_id = "wrong class")
  168. ])
  169. def test_constrainVariantInvalid(container, global_stack):
  170. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  171. global_stack.variant = container
  172. #Test setting variants.
  173. @pytest.mark.parametrize("container", [
  174. getInstanceContainer(container_type = "variant"),
  175. InstanceContainerSubClass(container_type = "variant")
  176. ])
  177. def test_constrainVariantValid(container, global_stack):
  178. global_stack.variant = container #Should not give an error.
  179. #Tests setting definition changes profiles to invalid containers.
  180. @pytest.mark.parametrize("container", [
  181. getInstanceContainer(container_type = "wrong container type"),
  182. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  183. DefinitionContainer(container_id = "wrong class")
  184. ])
  185. def test_constrainDefinitionChangesInvalid(container, global_stack):
  186. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  187. global_stack.definitionChanges = container
  188. #Test setting definition changes profiles.
  189. @pytest.mark.parametrize("container", [
  190. getInstanceContainer(container_type = "definition_changes"),
  191. InstanceContainerSubClass(container_type = "definition_changes")
  192. ])
  193. def test_constrainDefinitionChangesValid(container, global_stack):
  194. global_stack.definitionChanges = container #Should not give an error.
  195. #Tests setting definitions to invalid containers.
  196. @pytest.mark.parametrize("container", [
  197. getInstanceContainer(container_type = "wrong class"),
  198. getInstanceContainer(container_type = "material"), #Existing, but still wrong class.
  199. ])
  200. def test_constrainVariantInvalid(container, global_stack):
  201. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  202. global_stack.definition = container
  203. #Test setting definitions.
  204. @pytest.mark.parametrize("container", [
  205. DefinitionContainer(container_id = "DefinitionContainer"),
  206. DefinitionContainerSubClass()
  207. ])
  208. def test_constrainDefinitionValid(container, global_stack):
  209. global_stack.definition = container #Should not give an error.
  210. ## Tests whether deserialising completes the missing containers with empty
  211. # ones.
  212. @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.
  213. def test_deserializeCompletesEmptyContainers(global_stack: cura.Settings.GlobalStack):
  214. global_stack._containers = [DefinitionContainer(container_id = "definition")] #Set the internal state of this stack manually.
  215. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  216. global_stack.deserialize("")
  217. assert len(global_stack.getContainers()) == len(cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap) #Needs a slot for every type.
  218. for container_type_index in cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap:
  219. if container_type_index == cura.Settings.CuraContainerStack._ContainerIndexes.Definition: #We're not checking the definition.
  220. continue
  221. assert global_stack.getContainer(container_type_index).getId() == "empty" #All others need to be empty.
  222. ## Tests whether an instance container with the wrong type gets removed when
  223. # deserialising.
  224. def test_deserializeRemovesWrongInstanceContainer(global_stack):
  225. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type")
  226. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  227. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  228. global_stack.deserialize("")
  229. assert global_stack.quality == global_stack._empty_instance_container #Replaced with empty.
  230. ## Tests whether a container with the wrong class gets removed when
  231. # deserialising.
  232. def test_deserializeRemovesWrongContainerClass(global_stack):
  233. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class")
  234. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  235. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  236. global_stack.deserialize("")
  237. assert global_stack.quality == global_stack._empty_instance_container #Replaced with empty.
  238. ## Tests whether an instance container in the definition spot results in an
  239. # error.
  240. def test_deserializeWrongDefinitionClass(global_stack):
  241. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class.
  242. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  243. with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container.
  244. global_stack.deserialize("")
  245. ## Tests whether an instance container with the wrong type is moved into the
  246. # correct slot by deserialising.
  247. def test_deserializeMoveInstanceContainer(global_stack):
  248. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot.
  249. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  250. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  251. global_stack.deserialize("")
  252. assert global_stack.quality.getId() == "empty"
  253. assert global_stack.material.getId() != "empty"
  254. ## Tests whether a definition container in the wrong spot is moved into the
  255. # correct spot by deserialising.
  256. @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.
  257. def test_deserializeMoveDefinitionContainer(global_stack):
  258. global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot.
  259. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  260. global_stack.deserialize("")
  261. assert global_stack.material.getId() == "empty"
  262. assert global_stack.definition.getId() != "empty"
  263. UM.Settings.ContainerStack._containerRegistry = None
  264. ## Tests whether getProperty properly applies the stack-like behaviour on its
  265. # containers.
  266. def test_getPropertyFallThrough(global_stack):
  267. #A few instance container mocks to put in the stack.
  268. mock_layer_heights = {} #For each container type, a mock container that defines layer height to something unique.
  269. mock_no_settings = {} #For each container type, a mock container that has no settings at all.
  270. container_indexes = cura.Settings.CuraContainerStack._ContainerIndexes #Cache.
  271. for type_id, type_name in container_indexes.IndexTypeMap.items():
  272. container = unittest.mock.MagicMock()
  273. 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.
  274. container.hasProperty = lambda key, property: key == "layer_height"
  275. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  276. mock_layer_heights[type_id] = container
  277. container = unittest.mock.MagicMock()
  278. container.getProperty = unittest.mock.MagicMock(return_value = None) #Has no settings at all.
  279. container.hasProperty = unittest.mock.MagicMock(return_value = False)
  280. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = type_name)
  281. mock_no_settings[type_id] = container
  282. global_stack.userChanges = mock_no_settings[container_indexes.UserChanges]
  283. global_stack.qualityChanges = mock_no_settings[container_indexes.QualityChanges]
  284. global_stack.quality = mock_no_settings[container_indexes.Quality]
  285. global_stack.material = mock_no_settings[container_indexes.Material]
  286. global_stack.variant = mock_no_settings[container_indexes.Variant]
  287. global_stack.definitionChanges = mock_no_settings[container_indexes.DefinitionChanges]
  288. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  289. global_stack.definition = mock_layer_heights[container_indexes.Definition] #There's a layer height in here!
  290. assert global_stack.getProperty("layer_height", "value") == container_indexes.Definition
  291. global_stack.definitionChanges = mock_layer_heights[container_indexes.DefinitionChanges]
  292. assert global_stack.getProperty("layer_height", "value") == container_indexes.DefinitionChanges
  293. global_stack.variant = mock_layer_heights[container_indexes.Variant]
  294. assert global_stack.getProperty("layer_height", "value") == container_indexes.Variant
  295. global_stack.material = mock_layer_heights[container_indexes.Material]
  296. assert global_stack.getProperty("layer_height", "value") == container_indexes.Material
  297. global_stack.quality = mock_layer_heights[container_indexes.Quality]
  298. assert global_stack.getProperty("layer_height", "value") == container_indexes.Quality
  299. global_stack.qualityChanges = mock_layer_heights[container_indexes.QualityChanges]
  300. assert global_stack.getProperty("layer_height", "value") == container_indexes.QualityChanges
  301. global_stack.userChanges = mock_layer_heights[container_indexes.UserChanges]
  302. assert global_stack.getProperty("layer_height", "value") == container_indexes.UserChanges
  303. ## In definitions, test whether having no resolve allows us to find the value.
  304. def test_getPropertyNoResolveInDefinition(global_stack):
  305. value = unittest.mock.MagicMock() #Just sets the value for bed temperature.
  306. value.getProperty = lambda key, property, context = None: 10 if (key == "material_bed_temperature" and property == "value") else None
  307. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  308. global_stack.definition = value
  309. assert global_stack.getProperty("material_bed_temperature", "value") == 10 #No resolve, so fall through to value.
  310. ## In definitions, when the value is asked and there is a resolve function, it
  311. # must get the resolve first.
  312. def test_getPropertyResolveInDefinition(global_stack):
  313. resolve_and_value = unittest.mock.MagicMock() #Sets the resolve and value for bed temperature.
  314. 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.
  315. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  316. global_stack.definition = resolve_and_value
  317. assert global_stack.getProperty("material_bed_temperature", "value") == 7.5 #Resolve wins in the definition.
  318. ## In instance containers, when the value is asked and there is a resolve
  319. # function, it must get the value first.
  320. def test_getPropertyResolveInInstance(global_stack):
  321. container_indices = cura.Settings.CuraContainerStack._ContainerIndexes
  322. instance_containers = {}
  323. for container_type in container_indices.IndexTypeMap:
  324. instance_containers[container_type] = unittest.mock.MagicMock() #Sets the resolve and value for bed temperature.
  325. 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.
  326. instance_containers[container_type].getMetaDataEntry = unittest.mock.MagicMock(return_value = container_indices.IndexTypeMap[container_type]) #Make queries for the type return the desired type.
  327. 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.
  328. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  329. global_stack.definition = instance_containers[container_indices.Definition] #Stack must have a definition.
  330. #For all instance container slots, the value reigns over resolve.
  331. global_stack.definitionChanges = instance_containers[container_indices.DefinitionChanges]
  332. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  333. global_stack.variant = instance_containers[container_indices.Variant]
  334. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  335. global_stack.material = instance_containers[container_indices.Material]
  336. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  337. global_stack.quality = instance_containers[container_indices.Quality]
  338. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  339. global_stack.qualityChanges = instance_containers[container_indices.QualityChanges]
  340. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  341. global_stack.userChanges = instance_containers[container_indices.UserChanges]
  342. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  343. ## Tests whether the value in instances gets evaluated before the resolve in
  344. # definitions.
  345. def test_getPropertyInstancesBeforeResolve(global_stack):
  346. value = unittest.mock.MagicMock() #Sets just the value.
  347. value.getProperty = lambda key, property, context = None: (10 if property == "value" else (InstanceState.User if property != "limit_to_extruder" else "-1")) if key == "material_bed_temperature" else None
  348. value.getMetaDataEntry = unittest.mock.MagicMock(return_value = "quality")
  349. resolve = unittest.mock.MagicMock() #Sets just the resolve.
  350. resolve.getProperty = lambda key, property, context = None: 7.5 if (key == "material_bed_temperature" and property == "resolve") else None
  351. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
  352. global_stack.definition = resolve
  353. global_stack.quality = value
  354. assert global_stack.getProperty("material_bed_temperature", "value") == 10
  355. ## Tests whether the hasUserValue returns true for settings that are changed in
  356. # the user-changes container.
  357. def test_hasUserValueUserChanges(global_stack):
  358. container = unittest.mock.MagicMock()
  359. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user")
  360. container.hasProperty = lambda key, property: key == "layer_height" #Only have the layer_height property set.
  361. global_stack.userChanges = container
  362. assert global_stack.hasUserValue("layer_height")
  363. assert not global_stack.hasUserValue("infill_sparse_density")
  364. assert not global_stack.hasUserValue("")
  365. ## Tests whether the hasUserValue returns true for settings that are changed in
  366. # the quality-changes container.
  367. def test_hasUserValueQualityChanges(global_stack):
  368. container = unittest.mock.MagicMock()
  369. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = "quality_changes")
  370. container.hasProperty = lambda key, property: key == "layer_height" #Only have the layer_height property set.
  371. global_stack.qualityChanges = container
  372. assert global_stack.hasUserValue("layer_height")
  373. assert not global_stack.hasUserValue("infill_sparse_density")
  374. assert not global_stack.hasUserValue("")
  375. ## Tests whether a container in some other place on the stack is correctly not
  376. # recognised as user value.
  377. def test_hasNoUserValue(global_stack):
  378. container = unittest.mock.MagicMock()
  379. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = "quality")
  380. container.hasProperty = lambda key, property: key == "layer_height" #Only have the layer_height property set.
  381. global_stack.quality = container
  382. assert not global_stack.hasUserValue("layer_height") #However this container is quality, so it's not a user value.
  383. ## Tests whether inserting a container is properly forbidden.
  384. def test_insertContainer(global_stack):
  385. with pytest.raises(InvalidOperationError):
  386. global_stack.insertContainer(0, unittest.mock.MagicMock())
  387. ## Tests whether removing a container is properly forbidden.
  388. def test_removeContainer(global_stack):
  389. with pytest.raises(InvalidOperationError):
  390. global_stack.removeContainer(unittest.mock.MagicMock())
  391. ## Tests whether changing the next stack is properly forbidden.
  392. def test_setNextStack(global_stack):
  393. with pytest.raises(InvalidOperationError):
  394. global_stack.setNextStack(unittest.mock.MagicMock())
  395. ## Tests setting properties directly on the global stack.
  396. @pytest.mark.parametrize("key, property, value", [
  397. ("layer_height", "value", 0.1337),
  398. ("foo", "value", 100),
  399. ("support_enabled", "value", True),
  400. ("layer_height", "default_value", 0.1337),
  401. ("layer_height", "is_bright_pink", "of course")
  402. ])
  403. def test_setPropertyUser(key, property, value, global_stack):
  404. user_changes = unittest.mock.MagicMock()
  405. user_changes.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user")
  406. global_stack.userChanges = user_changes
  407. global_stack.setProperty(key, property, value) #The actual test.
  408. global_stack.userChanges.setProperty.assert_called_once_with(key, property, value) #Make sure that the user container gets a setProperty call.
  409. ## Tests setting properties on specific containers on the global stack.
  410. @pytest.mark.parametrize("target_container, stack_variable", [
  411. ("user", "userChanges"),
  412. ("quality_changes", "qualityChanges"),
  413. ("quality", "quality"),
  414. ("material", "material"),
  415. ("variant", "variant"),
  416. ("definition_changes", "definitionChanges")
  417. ])
  418. def test_setPropertyOtherContainers(target_container, stack_variable, global_stack):
  419. #Other parameters that don't need to be varied.
  420. key = "layer_height"
  421. property = "value"
  422. value = 0.1337
  423. #A mock container in the right spot.
  424. container = unittest.mock.MagicMock()
  425. container.getMetaDataEntry = unittest.mock.MagicMock(return_value = target_container)
  426. setattr(global_stack, stack_variable, container) #For instance, set global_stack.qualityChanges = container.
  427. global_stack.setProperty(key, property, value, target_container = target_container) #The actual test.
  428. getattr(global_stack, stack_variable).setProperty.assert_called_once_with(key, property, value) #Make sure that the proper container gets a setProperty call.