TestExtruderStack.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os.path #To find the test stack files.
  4. import pytest #This module contains automated tests.
  5. import unittest.mock #For the mocking and monkeypatching functionality.
  6. import UM.Settings.ContainerRegistry #To create empty instance containers.
  7. import UM.Settings.ContainerStack #To set the container registry the container stacks use.
  8. from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer.
  9. from UM.Settings.InstanceContainer import InstanceContainer #To check against the class of InstanceContainer.
  10. import cura.Settings.ExtruderStack #The module we're testing.
  11. from cura.Settings.Exceptions import InvalidContainerError, InvalidOperationError #To check whether the correct exceptions are raised.
  12. ## Fake container registry that always provides all containers you ask of.
  13. @pytest.fixture()
  14. def container_registry():
  15. registry = unittest.mock.MagicMock()
  16. def findContainers(id = None):
  17. if not id:
  18. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer("test_container")]
  19. else:
  20. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  21. registry.findContainers = findContainers
  22. return registry
  23. ## An empty extruder stack to test with.
  24. @pytest.fixture()
  25. def extruder_stack() -> cura.Settings.ExtruderStack.ExtruderStack:
  26. return cura.Settings.ExtruderStack.ExtruderStack("TestStack")
  27. ## Place-in function for findContainer that finds only containers that start
  28. # with "some_".
  29. def findSomeContainers(container_id = "*", container_type = None, type = None, category = "*"):
  30. if container_id.startswith("some_"):
  31. return UM.Settings.ContainerRegistry._EmptyInstanceContainer(container_id)
  32. if container_type == DefinitionContainer:
  33. definition_mock = unittest.mock.MagicMock()
  34. definition_mock.getId = unittest.mock.MagicMock(return_value = "some_definition") #getId returns some_definition.
  35. return definition_mock
  36. ## Helper function to read the contents of a container stack in the test
  37. # stack folder.
  38. #
  39. # \param filename The name of the file in the "stacks" folder to read from.
  40. # \return The contents of that file.
  41. def readStack(filename):
  42. with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)) as file_handle:
  43. serialized = file_handle.read()
  44. return serialized
  45. ## Gets an instance container with a specified container type.
  46. #
  47. # \param container_type The type metadata for the instance container.
  48. # \return An instance container instance.
  49. def getInstanceContainer(container_type) -> InstanceContainer:
  50. container = InstanceContainer(container_id = "InstanceContainer")
  51. container.addMetaDataEntry("type", container_type)
  52. return container
  53. class DefinitionContainerSubClass(DefinitionContainer):
  54. def __init__(self):
  55. super().__init__(container_id = "SubDefinitionContainer")
  56. class InstanceContainerSubClass(InstanceContainer):
  57. def __init__(self, container_type):
  58. super().__init__(container_id = "SubInstanceContainer")
  59. self.addMetaDataEntry("type", container_type)
  60. #############################START OF TEST CASES################################
  61. ## Tests whether adding a container is properly forbidden.
  62. def test_addContainer(extruder_stack):
  63. with pytest.raises(InvalidOperationError):
  64. extruder_stack.addContainer(unittest.mock.MagicMock())
  65. #Tests setting user changes profiles to invalid containers.
  66. @pytest.mark.parametrize("container", [
  67. getInstanceContainer(container_type = "wrong container type"),
  68. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  69. DefinitionContainer(container_id = "wrong class")
  70. ])
  71. def test_constrainUserChangesInvalid(container, extruder_stack):
  72. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  73. extruder_stack.userChanges = container
  74. #Tests setting user changes profiles.
  75. @pytest.mark.parametrize("container", [
  76. getInstanceContainer(container_type = "user"),
  77. InstanceContainerSubClass(container_type = "user")
  78. ])
  79. def test_constrainUserChangesValid(container, extruder_stack):
  80. extruder_stack.userChanges = container #Should not give an error.
  81. #Tests setting quality changes profiles to invalid containers.
  82. @pytest.mark.parametrize("container", [
  83. getInstanceContainer(container_type = "wrong container type"),
  84. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  85. DefinitionContainer(container_id = "wrong class")
  86. ])
  87. def test_constrainQualityChangesInvalid(container, extruder_stack):
  88. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  89. extruder_stack.qualityChanges = container
  90. #Test setting quality changes profiles.
  91. @pytest.mark.parametrize("container", [
  92. getInstanceContainer(container_type = "quality_changes"),
  93. InstanceContainerSubClass(container_type = "quality_changes")
  94. ])
  95. def test_constrainQualityChangesValid(container, extruder_stack):
  96. extruder_stack.qualityChanges = container #Should not give an error.
  97. #Tests setting quality profiles to invalid containers.
  98. @pytest.mark.parametrize("container", [
  99. getInstanceContainer(container_type = "wrong container type"),
  100. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  101. DefinitionContainer(container_id = "wrong class")
  102. ])
  103. def test_constrainQualityInvalid(container, extruder_stack):
  104. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  105. extruder_stack.quality = container
  106. #Test setting quality profiles.
  107. @pytest.mark.parametrize("container", [
  108. getInstanceContainer(container_type = "quality"),
  109. InstanceContainerSubClass(container_type = "quality")
  110. ])
  111. def test_constrainQualityValid(container, extruder_stack):
  112. extruder_stack.quality = container #Should not give an error.
  113. #Tests setting materials to invalid containers.
  114. @pytest.mark.parametrize("container", [
  115. getInstanceContainer(container_type = "wrong container type"),
  116. getInstanceContainer(container_type = "quality"), #Existing, but still wrong type.
  117. DefinitionContainer(container_id = "wrong class")
  118. ])
  119. def test_constrainMaterialInvalid(container, extruder_stack):
  120. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  121. extruder_stack.material = container
  122. #Test setting materials.
  123. @pytest.mark.parametrize("container", [
  124. getInstanceContainer(container_type = "material"),
  125. InstanceContainerSubClass(container_type = "material")
  126. ])
  127. def test_constrainMaterialValid(container, extruder_stack):
  128. extruder_stack.material = container #Should not give an error.
  129. #Tests setting variants to invalid containers.
  130. @pytest.mark.parametrize("container", [
  131. getInstanceContainer(container_type = "wrong container type"),
  132. getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
  133. DefinitionContainer(container_id = "wrong class")
  134. ])
  135. def test_constrainVariantInvalid(container, extruder_stack):
  136. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  137. extruder_stack.variant = container
  138. #Test setting variants.
  139. @pytest.mark.parametrize("container", [
  140. getInstanceContainer(container_type = "variant"),
  141. InstanceContainerSubClass(container_type = "variant")
  142. ])
  143. def test_constrainVariantValid(container, extruder_stack):
  144. extruder_stack.variant = container #Should not give an error.
  145. #Tests setting definitions to invalid containers.
  146. @pytest.mark.parametrize("container", [
  147. getInstanceContainer(container_type = "wrong class"),
  148. getInstanceContainer(container_type = "material"), #Existing, but still wrong class.
  149. ])
  150. def test_constrainVariantInvalid(container, extruder_stack):
  151. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
  152. extruder_stack.definition = container
  153. #Test setting definitions.
  154. @pytest.mark.parametrize("container", [
  155. DefinitionContainer(container_id = "DefinitionContainer"),
  156. DefinitionContainerSubClass()
  157. ])
  158. def test_constrainDefinitionValid(container, extruder_stack):
  159. extruder_stack.definition = container #Should not give an error.
  160. ## Tests whether deserialising completes the missing containers with empty
  161. # ones.
  162. @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.
  163. def test_deserializeCompletesEmptyContainers(extruder_stack: cura.Settings.ExtruderStack):
  164. extruder_stack._containers = [DefinitionContainer(container_id = "definition")] #Set the internal state of this stack manually.
  165. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  166. extruder_stack.deserialize("")
  167. assert len(extruder_stack.getContainers()) == len(cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap) #Needs a slot for every type.
  168. for container_type_index in cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap:
  169. if container_type_index == cura.Settings.CuraContainerStack._ContainerIndexes.Definition: #We're not checking the definition.
  170. continue
  171. assert extruder_stack.getContainer(container_type_index).getId() == "empty" #All others need to be empty.
  172. ## Tests whether an instance container with the wrong type gets removed when
  173. # deserialising.
  174. def test_deserializeRemovesWrongInstanceContainer(extruder_stack):
  175. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type")
  176. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  177. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  178. extruder_stack.deserialize("")
  179. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
  180. ## Tests whether a container with the wrong class gets removed when
  181. # deserialising.
  182. def test_deserializeRemovesWrongContainerClass(extruder_stack):
  183. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class")
  184. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  185. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  186. extruder_stack.deserialize("")
  187. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
  188. ## Tests whether an instance container in the definition spot results in an
  189. # error.
  190. def test_deserializeWrongDefinitionClass(extruder_stack):
  191. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class.
  192. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  193. with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container.
  194. extruder_stack.deserialize("")
  195. ## Tests whether an instance container with the wrong type is moved into the
  196. # correct slot by deserialising.
  197. def test_deserializeMoveInstanceContainer(extruder_stack):
  198. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot.
  199. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
  200. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  201. extruder_stack.deserialize("")
  202. assert extruder_stack.quality.getId() == "empty"
  203. assert extruder_stack.material.getId() != "empty"
  204. ## Tests whether a definition container in the wrong spot is moved into the
  205. # correct spot by deserialising.
  206. @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.
  207. def test_deserializeMoveDefinitionContainer(extruder_stack):
  208. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot.
  209. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
  210. extruder_stack.deserialize("")
  211. assert extruder_stack.material.getId() == "empty"
  212. assert extruder_stack.definition.getId() != "empty"
  213. UM.Settings.ContainerStack._containerRegistry = None
  214. ## Tests whether materials are being read properly from an extruder stack.
  215. @pytest.mark.parametrize("filename, material_id", [
  216. ("Left.extruder.cfg", "some_instance"),
  217. ("ExtruderLegacy.stack.cfg", "some_instance"),
  218. ("OnlyMaterial.extruder.cfg", "some_instance"),
  219. ("OnlyDefinition.extruder.cfg", "empty"),
  220. ("Complete.extruder.cfg", "some_material")
  221. ])
  222. def test_deserializeMaterial(filename, material_id, container_registry, extruder_stack):
  223. serialized = readStack(filename)
  224. #Mock the loading of the instance containers.
  225. extruder_stack.findContainer = findSomeContainers
  226. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  227. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
  228. extruder_stack.deserialize(serialized)
  229. assert extruder_stack.material.getId() == material_id
  230. #Restore.
  231. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  232. ## Tests that when an extruder is loaded with an unknown instance, it raises an
  233. # exception.
  234. def test_deserializeMissingContainer(extruder_stack):
  235. serialized = readStack("Left.extruder.cfg")
  236. with pytest.raises(Exception):
  237. extruder_stack.deserialize(serialized)
  238. try:
  239. extruder_stack.deserialize(serialized)
  240. except Exception as e:
  241. #Must be exactly Exception, not one of its subclasses, since that is what gets raised when a stack has an unknown container.
  242. #That's why we can't use pytest.raises.
  243. assert type(e) == Exception
  244. ## Tests whether qualities are being read properly from an extruder stack.
  245. @pytest.mark.parametrize("filename, quality_id", [
  246. ("Left.extruder.cfg", "empty"),
  247. ("ExtruderLegacy.stack.cfg", "empty"),
  248. ("OnlyQuality.extruder.cfg", "some_instance"),
  249. ("Complete.extruder.cfg", "some_quality")
  250. ])
  251. def test_deserializeQuality(filename, quality_id, container_registry, extruder_stack):
  252. serialized = readStack(filename)
  253. #Mock the loading of the instance containers.
  254. extruder_stack.findContainer = findSomeContainers
  255. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  256. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
  257. extruder_stack.deserialize(serialized)
  258. assert extruder_stack.quality.getId() == quality_id
  259. #Restore.
  260. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  261. ## Tests whether quality changes are being read properly from an extruder
  262. # stack.
  263. @pytest.mark.parametrize("filename, quality_changes_id", [
  264. ("Left.extruder.cfg", "empty"),
  265. ("ExtruderLegacy.stack.cfg", "empty"),
  266. ("OnlyQualityChanges.extruder.cfg", "some_instance"),
  267. ("Complete.extruder.cfg", "some_quality_changes")
  268. ])
  269. def test_deserializeQualityChanges(filename, quality_changes_id, container_registry, extruder_stack):
  270. serialized = readStack(filename)
  271. #Mock the loading of the instance containers.
  272. extruder_stack.findContainer = findSomeContainers
  273. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  274. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
  275. extruder_stack.deserialize(serialized)
  276. assert extruder_stack.qualityChanges.getId() == quality_changes_id
  277. #Restore.
  278. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  279. ## Tests whether user changes are being read properly from an extruder stack.
  280. @pytest.mark.parametrize("filename, user_changes_id", [
  281. ("Left.extruder.cfg", "empty"),
  282. ("ExtruderLegacy.stack.cfg", "empty"),
  283. ("OnlyUser.extruder.cfg", "some_instance"),
  284. ("Complete.extruder.cfg", "some_user_changes")
  285. ])
  286. def test_deserializeUserChanges(filename, user_changes_id, container_registry, extruder_stack):
  287. serialized = readStack(filename)
  288. #Mock the loading of the instance containers.
  289. extruder_stack.findContainer = findSomeContainers
  290. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  291. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
  292. extruder_stack.deserialize(serialized)
  293. assert extruder_stack.userChanges.getId() == user_changes_id
  294. #Restore.
  295. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  296. ## Tests whether variants are being read properly from an extruder stack.
  297. @pytest.mark.parametrize("filename, variant_id", [
  298. ("Left.extruder.cfg", "empty"),
  299. ("ExtruderLegacy.stack.cfg", "empty"),
  300. ("OnlyVariant.extruder.cfg", "some_instance"),
  301. ("Complete.extruder.cfg", "some_variant")
  302. ])
  303. def test_deserializeVariant(filename, variant_id, container_registry, extruder_stack):
  304. serialized = readStack(filename)
  305. #Mock the loading of the instance containers.
  306. extruder_stack.findContainer = findSomeContainers
  307. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  308. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
  309. extruder_stack.deserialize(serialized)
  310. assert extruder_stack.variant.getId() == variant_id
  311. #Restore.
  312. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  313. ## Tests whether getProperty properly applies the stack-like behaviour on its
  314. # containers.
  315. def test_getPropertyFallThrough(extruder_stack):
  316. #A few instance container mocks to put in the stack.
  317. layer_height_5 = unittest.mock.MagicMock() #Sets layer height to 5.
  318. layer_height_5.getProperty = lambda key, property: 5 if (key == "layer_height" and property == "value") else None
  319. layer_height_5.hasProperty = lambda key: key == "layer_height"
  320. layer_height_10 = unittest.mock.MagicMock() #Sets layer height to 10.
  321. layer_height_10.getProperty = lambda key, property: 10 if (key == "layer_height" and property == "value") else None
  322. layer_height_10.hasProperty = lambda key: key == "layer_height"
  323. no_layer_height = unittest.mock.MagicMock() #No settings at all.
  324. no_layer_height.getProperty = lambda key, property: None
  325. no_layer_height.hasProperty = lambda key: False
  326. extruder_stack.userChanges = no_layer_height
  327. extruder_stack.qualityChanges = no_layer_height
  328. extruder_stack.quality = no_layer_height
  329. extruder_stack.material = no_layer_height
  330. extruder_stack.variant = no_layer_height
  331. extruder_stack.definition = layer_height_5 #Here it is!
  332. assert extruder_stack.getProperty("layer_height", "value") == 5
  333. extruder_stack.variant = layer_height_10
  334. assert extruder_stack.getProperty("layer_height", "value") == 10
  335. extruder_stack.material = layer_height_5
  336. assert extruder_stack.getProperty("layer_height", "value") == 5
  337. extruder_stack.quality = layer_height_10
  338. assert extruder_stack.getProperty("layer_height", "value") == 10
  339. extruder_stack.qualityChanges = layer_height_5
  340. assert extruder_stack.getProperty("layer_height", "value") == 5
  341. extruder_stack.userChanges = layer_height_10
  342. assert extruder_stack.getProperty("layer_height", "value") == 10
  343. ## Tests whether inserting a container is properly forbidden.
  344. def test_insertContainer(extruder_stack):
  345. with pytest.raises(InvalidOperationError):
  346. extruder_stack.insertContainer(0, unittest.mock.MagicMock())
  347. ## Tests whether removing a container is properly forbidden.
  348. def test_removeContainer(extruder_stack):
  349. with pytest.raises(InvalidOperationError):
  350. extruder_stack.removeContainer(unittest.mock.MagicMock())
  351. ## Tests setting definitions by specifying an ID of a definition that exists.
  352. def test_setDefinitionByIdExists(extruder_stack, container_registry):
  353. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  354. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  355. extruder_stack.setDefinitionById("some_definition") #The container registry always has a container with the ID.
  356. #Restore.
  357. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  358. ## Tests setting definitions by specifying an ID of a definition that doesn't
  359. # exist.
  360. def test_setDefinitionByIdDoesntExist(extruder_stack):
  361. with pytest.raises(KeyError):
  362. extruder_stack.setDefinitionById("some_definition") #Container registry is empty now.
  363. ## Tests setting materials by specifying an ID of a material that exists.
  364. def test_setMaterialByIdExists(extruder_stack, container_registry):
  365. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  366. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  367. extruder_stack.setMaterialById("some_material") #The container registry always has a container with the ID.
  368. #Restore.
  369. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  370. ## Tests setting materials by specifying an ID of a material that doesn't
  371. # exist.
  372. def test_setMaterialByIdDoesntExist(extruder_stack):
  373. with pytest.raises(KeyError):
  374. extruder_stack.setMaterialById("some_material") #Container registry is empty now.
  375. ## Tests setting properties directly on the extruder stack.
  376. @pytest.mark.parametrize("key, property, value, output_value", [
  377. ("layer_height", "value", "0.1337", 0.1337),
  378. ("foo", "value", "100", 100),
  379. ("support_enabled", "value", "True", True),
  380. ("layer_height", "default_value", 0.1337, 0.1337),
  381. ("layer_height", "is_bright_pink", "of course", "of course")
  382. ])
  383. def test_setPropertyUser(key, property, value, output_value, extruder_stack):
  384. extruder_stack.setProperty(key, value, property)
  385. assert extruder_stack.userChanges.getProperty(key, property) == output_value
  386. ## Tests setting properties on specific containers on the extruder stack.
  387. @pytest.mark.parametrize("target_container", [
  388. "user",
  389. "quality_changes",
  390. "quality",
  391. "material",
  392. "variant",
  393. "definition"
  394. ])
  395. def test_setPropertyOtherContainers(target_container, extruder_stack):
  396. #Other parameters that don't need to be varied.
  397. key = "layer_height"
  398. property = "value",
  399. value = "0.1337",
  400. output_value = 0.1337
  401. extruder_stack.setProperty(key, value, property, target_container = target_container)
  402. containers = {
  403. "user": extruder_stack.userChanges,
  404. "quality_changes": extruder_stack.qualityChanges,
  405. "quality": extruder_stack.quality,
  406. "material": extruder_stack.material,
  407. "variant": extruder_stack.variant,
  408. "definition_changes": extruder_stack.definition_changes,
  409. "definition": extruder_stack.definition
  410. }
  411. assert containers[target_container].getProperty(key, property) == output_value
  412. ## Tests setting qualities by specifying an ID of a quality that exists.
  413. def test_setQualityByIdExists(extruder_stack, container_registry):
  414. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  415. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  416. extruder_stack.setQualityById("some_quality") #The container registry always has a container with the ID.
  417. #Restore.
  418. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  419. ## Tests setting qualities by specifying an ID of a quality that doesn't exist.
  420. def test_setQualityByIdDoesntExist(extruder_stack):
  421. with pytest.raises(KeyError):
  422. extruder_stack.setQualityById("some_quality") #Container registry is empty now.
  423. ## Tests setting quality changes by specifying an ID of a quality change that
  424. # exists.
  425. def test_setQualityChangesByIdExists(extruder_stack, container_registry):
  426. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  427. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  428. extruder_stack.setQualityChangesById("some_quality_changes") #The container registry always has a container with the ID.
  429. #Restore.
  430. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  431. ## Tests setting quality changes by specifying an ID of a quality change that
  432. # doesn't exist.
  433. def test_setQualityChangesByIdDoesntExist(extruder_stack):
  434. with pytest.raises(KeyError):
  435. extruder_stack.setQualityChangesById("some_quality_changes") #Container registry is empty now.
  436. ## Tests setting variants by specifying an ID of a variant that exists.
  437. def test_setVariantByIdExists(extruder_stack, container_registry):
  438. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  439. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  440. extruder_stack.setVariantById("some_variant") #The container registry always has a container with the ID.
  441. #Restore.
  442. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  443. ## Tests setting variants by specifying an ID of a variant that doesn't exist.
  444. def test_setVariantByIdDoesntExist(extruder_stack):
  445. with pytest.raises(KeyError):
  446. extruder_stack.setVariantById("some_variant") #Container registry is empty now.