TestGlobalStack.py 31 KB

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