TestGlobalStack.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 files.
  4. import pytest #This module contains unit tests.
  5. import unittest.mock #To monkeypatch some mocks in place of dependencies.
  6. import functools
  7. import cura.Settings.GlobalStack #The module we're testing.
  8. from cura.Settings.Exceptions import TooManyExtrudersError, InvalidContainerError, InvalidOperationError #To test raising these errors.
  9. from UM.Settings.DefinitionContainer import DefinitionContainer #To test against the class DefinitionContainer.
  10. from UM.Settings.InstanceContainer import InstanceContainer #To test against the class InstanceContainer.
  11. import UM.Settings.ContainerRegistry
  12. import UM.Settings.ContainerStack
  13. class MockContainer:
  14. def __init__(self, container_id, type = "mock"):
  15. self._id = container_id
  16. self._type = type
  17. self._property_map = {}
  18. def getId(self):
  19. return self._id
  20. def getMetaDataEntry(self, entry, default = None):
  21. if entry == "type":
  22. return self._type
  23. return default
  24. def getProperty(self, key, property_name):
  25. if key not in self._property_map:
  26. return None
  27. if property_name not in self._property_map[key]:
  28. return None
  29. return self._property_map[key][property_name]
  30. def setProperty(self, key, property_name, value):
  31. if key not in self._property_map:
  32. self._property_map[key] = {}
  33. self._property_map[key][property_name] = value
  34. propertyChanged = unittest.mock.MagicMock()
  35. ## Fake container registry that always provides all containers you ask of.
  36. @pytest.yield_fixture()
  37. def container_registry():
  38. registry = unittest.mock.MagicMock()
  39. registry.typeMetaData = "registry_mock"
  40. def findInstanceContainers(registry, **kwargs):
  41. container_id = kwargs.get("id", default = "test_container")
  42. return [MockContainer(container_id, registry.typeMetaData)]
  43. registry.findInstanceContainers = functools.partial(findInstanceContainers, registry)
  44. def findContainers(registry, container_type = None, id = None):
  45. if not id:
  46. id = "test_container"
  47. return [MockContainer(id, registry.typeMetaData)]
  48. registry.findContainers = functools.partial(findContainers, registry)
  49. def getEmptyInstanceContainer():
  50. return MockContainer(container_id = "empty")
  51. registry.getEmptyInstanceContainer = getEmptyInstanceContainer
  52. UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = registry
  53. UM.Settings.ContainerStack._containerRegistry = registry
  54. yield registry
  55. UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = None
  56. UM.Settings.ContainerStack._containerRegistry = None
  57. #An empty global stack to test with.
  58. @pytest.fixture()
  59. def global_stack() -> cura.Settings.GlobalStack.GlobalStack:
  60. return cura.Settings.GlobalStack.GlobalStack("TestStack")
  61. @pytest.fixture()
  62. def writable_global_stack(global_stack):
  63. global_stack.userChanges = MockContainer("test_user_changes", "user")
  64. global_stack.qualityChanges = MockContainer("test_quality_changes", "quality_changes")
  65. global_stack.quality = MockContainer("test_quality", "quality")
  66. global_stack.material = MockContainer("test_material", "material")
  67. global_stack.variant = MockContainer("test_variant", "variant")
  68. global_stack.definitionChanges = MockContainer("test_definition_changes", "definition_changes")
  69. global_stack.definition = DefinitionContainerSubClass()
  70. return global_stack
  71. ## Place-in function for findContainer that finds only containers that start
  72. # with "some_".
  73. def findSomeContainers(container_id = "*", container_type = None, type = None, category = "*"):
  74. if container_id.startswith("some_"):
  75. return UM.Settings.ContainerRegistry._EmptyInstanceContainer(container_id)
  76. if container_type == DefinitionContainer:
  77. definition_mock = unittest.mock.MagicMock()
  78. definition_mock.getId = unittest.mock.MagicMock(return_value = "some_definition") #getId returns some_definition.
  79. return definition_mock
  80. ## Helper function to read the contents of a container stack in the test
  81. # stack folder.
  82. #
  83. # \param filename The name of the file in the "stacks" folder to read from.
  84. # \return The contents of that file.
  85. def readStack(filename):
  86. with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)) as file_handle:
  87. serialized = file_handle.read()
  88. return serialized
  89. class DefinitionContainerSubClass(DefinitionContainer):
  90. def __init__(self):
  91. super().__init__(container_id = "SubDefinitionContainer")
  92. class InstanceContainerSubClass(InstanceContainer):
  93. def __init__(self):
  94. super().__init__(container_id = "SubInstanceContainer")
  95. #############################START OF TEST CASES################################
  96. ## Tests whether adding a container is properly forbidden.
  97. def test_addContainer(global_stack):
  98. with pytest.raises(InvalidOperationError):
  99. global_stack.addContainer(unittest.mock.MagicMock())
  100. ## Tests adding extruders to the global stack.
  101. def test_addExtruder(global_stack):
  102. mock_definition = unittest.mock.MagicMock()
  103. mock_definition.getProperty = lambda key, property: 2 if key == "machine_extruder_count" and property == "value" else None
  104. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  105. global_stack.definition = mock_definition
  106. assert len(global_stack.extruders) == 0
  107. first_extruder = unittest.mock.MagicMock()
  108. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  109. global_stack.addExtruder(first_extruder)
  110. assert len(global_stack.extruders) == 1
  111. assert global_stack.extruders[0] == first_extruder
  112. second_extruder = unittest.mock.MagicMock()
  113. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  114. global_stack.addExtruder(second_extruder)
  115. assert len(global_stack.extruders) == 2
  116. assert global_stack.extruders[1] == second_extruder
  117. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock):
  118. with pytest.raises(TooManyExtrudersError): #Should be limited to 2 extruders because of machine_extruder_count.
  119. global_stack.addExtruder(unittest.mock.MagicMock())
  120. assert len(global_stack.extruders) == 2 #Didn't add the faulty extruder.
  121. ## Tests whether the container types are properly enforced on the stack.
  122. #
  123. # When setting a field to have a different type of stack than intended, we
  124. # should get an exception.
  125. @pytest.mark.parametrize("definition_container, instance_container", [
  126. (DefinitionContainer(container_id = "TestDefinitionContainer"), InstanceContainer(container_id = "TestInstanceContainer")),
  127. (DefinitionContainerSubClass(), InstanceContainerSubClass())
  128. ])
  129. def test_constrainContainerTypes(definition_container, instance_container, global_stack):
  130. instance_container.addMetaDataEntry("type", "")
  131. with pytest.raises(InvalidContainerError): # Putting a definition container in the user changes is not allowed.
  132. global_stack.userChanges = definition_container
  133. with pytest.raises(InvalidContainerError):
  134. global_stack.userChanges = instance_container # Putting a random instance container in the user changes is not allowed.
  135. instance_container.setMetaDataEntry("type", "user") # After setting the metadata type entry correctly, we are allowed to set the container
  136. global_stack.userChanges = instance_container
  137. with pytest.raises(InvalidContainerError):
  138. global_stack.qualityChanges = definition_container
  139. with pytest.raises(InvalidContainerError):
  140. global_stack.qualityChanges = instance_container
  141. instance_container.setMetaDataEntry("type", "quality_changes")
  142. global_stack.qualityChanges = instance_container
  143. with pytest.raises(InvalidContainerError):
  144. global_stack.quality = definition_container
  145. with pytest.raises(InvalidContainerError):
  146. global_stack.quality = instance_container
  147. instance_container.setMetaDataEntry("type", "quality")
  148. global_stack.quality = instance_container
  149. with pytest.raises(InvalidContainerError):
  150. global_stack.material = definition_container
  151. with pytest.raises(InvalidContainerError):
  152. global_stack.material = instance_container
  153. instance_container.setMetaDataEntry("type", "material")
  154. global_stack.material = instance_container
  155. with pytest.raises(InvalidContainerError):
  156. global_stack.variant = definition_container
  157. with pytest.raises(InvalidContainerError):
  158. global_stack.variant = instance_container
  159. instance_container.setMetaDataEntry("type", "variant")
  160. global_stack.variant = instance_container
  161. with pytest.raises(InvalidContainerError):
  162. global_stack.definitionChanges = definition_container
  163. with pytest.raises(InvalidContainerError):
  164. global_stack.definitionChanges = instance_container
  165. instance_container.setMetaDataEntry("type", "definition_changes")
  166. global_stack.definitionChanges = instance_container
  167. with pytest.raises(InvalidContainerError): #Putting an instance container in the definition is not allowed.
  168. global_stack.definition = instance_container
  169. global_stack.definition = definition_container #Putting a definition container in the definition is allowed.
  170. ## Tests whether the user changes are being read properly from a global stack.
  171. @pytest.mark.parametrize("filename, user_changes_id", [
  172. ("Global.global.cfg", "empty"),
  173. ("Global.stack.cfg", "empty"),
  174. ("MachineLegacy.stack.cfg", "empty"),
  175. ("OnlyUser.global.cfg", "some_instance"), #This one does have a user profile.
  176. ("Complete.global.cfg", "some_user_changes")
  177. ])
  178. def test_deserializeUserChanges(filename, user_changes_id, container_registry, global_stack):
  179. serialized = readStack(filename)
  180. #Mock the loading of the instance containers.
  181. global_stack.findContainer = findSomeContainers
  182. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  183. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
  184. global_stack.deserialize(serialized)
  185. assert global_stack.userChanges.getId() == user_changes_id
  186. #Restore.
  187. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  188. ## Tests whether the quality changes are being read properly from a global
  189. # stack.
  190. @pytest.mark.parametrize("filename, quality_changes_id", [
  191. ("Global.global.cfg", "empty"),
  192. ("Global.stack.cfg", "empty"),
  193. ("MachineLegacy.stack.cfg", "empty"),
  194. ("OnlyQualityChanges.global.cfg", "some_instance"),
  195. ("Complete.global.cfg", "some_quality_changes")
  196. ])
  197. def test_deserializeQualityChanges(filename, quality_changes_id, container_registry, global_stack):
  198. serialized = readStack(filename)
  199. #Mock the loading of the instance containers.
  200. global_stack.findContainer = findSomeContainers
  201. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  202. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  203. global_stack.deserialize(serialized)
  204. assert global_stack.qualityChanges.getId() == quality_changes_id
  205. #Restore.
  206. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  207. ## Tests whether the quality profile is being read properly from a global
  208. # stack.
  209. @pytest.mark.parametrize("filename, quality_id", [
  210. ("Global.global.cfg", "empty"),
  211. ("Global.stack.cfg", "empty"),
  212. ("MachineLegacy.stack.cfg", "empty"),
  213. ("OnlyQuality.global.cfg", "some_instance"),
  214. ("Complete.global.cfg", "some_quality")
  215. ])
  216. def test_deserializeQuality(filename, quality_id, container_registry, global_stack):
  217. serialized = readStack(filename)
  218. #Mock the loading of the instance containers.
  219. global_stack.findContainer = findSomeContainers
  220. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  221. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  222. global_stack.deserialize(serialized)
  223. assert global_stack.quality.getId() == quality_id
  224. #Restore.
  225. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  226. ## Tests whether the material profile is being read properly from a global
  227. # stack.
  228. @pytest.mark.parametrize("filename, material_id", [
  229. ("Global.global.cfg", "some_instance"),
  230. ("Global.stack.cfg", "some_instance"),
  231. ("MachineLegacy.stack.cfg", "some_instance"),
  232. ("OnlyDefinition.global.cfg", "empty"),
  233. ("OnlyMaterial.global.cfg", "some_instance"),
  234. ("Complete.global.cfg", "some_material")
  235. ])
  236. def test_deserializeMaterial(filename, material_id, container_registry, global_stack):
  237. serialized = readStack(filename)
  238. #Mock the loading of the instance containers.
  239. global_stack.findContainer = findSomeContainers
  240. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  241. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  242. global_stack.deserialize(serialized)
  243. assert global_stack.material.getId() == material_id
  244. #Restore.
  245. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  246. ## Tests whether the variant profile is being read properly from a global
  247. # stack.
  248. @pytest.mark.parametrize("filename, variant_id", [
  249. ("Global.global.cfg", "empty"),
  250. ("Global.stack.cfg", "empty"),
  251. ("MachineLegacy.stack.cfg", "empty"),
  252. ("OnlyVariant.global.cfg", "some_instance"),
  253. ("Complete.global.cfg", "some_variant")
  254. ])
  255. def test_deserializeVariant(filename, variant_id, container_registry, global_stack):
  256. serialized = readStack(filename)
  257. #Mock the loading of the instance containers.
  258. global_stack.findContainer = findSomeContainers
  259. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  260. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  261. global_stack.deserialize(serialized)
  262. assert global_stack.variant.getId() == variant_id
  263. #Restore.
  264. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  265. ## Tests whether the definition changes profile is being read properly from a
  266. # global stack.
  267. @pytest.mark.parametrize("filename, definition_changes_id", [
  268. ("Global.global.cfg", "empty"),
  269. ("Global.stack.cfg", "empty"),
  270. ("MachineLegacy.stack.cfg", "empty"),
  271. ("OnlyDefinitionChanges.global.cfg", "some_instance"),
  272. ("Complete.global.cfg", "some_material")
  273. ])
  274. def test_deserializeDefinitionChanges(filename, definition_changes_id, container_registry, global_stack):
  275. serialized = readStack(filename)
  276. global_stack = cura.Settings.GlobalStack.GlobalStack("TestStack")
  277. #Mock the loading of the instance containers.
  278. global_stack.findContainer = findSomeContainers
  279. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  280. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  281. global_stack.deserialize(serialized)
  282. assert global_stack.definitionChanges.getId() == definition_changes_id
  283. #Restore.
  284. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  285. ## Tests whether the definition is being read properly from a global stack.
  286. @pytest.mark.parametrize("filename, definition_id", [
  287. ("Global.global.cfg", "some_definition"),
  288. ("Global.stack.cfg", "some_definition"),
  289. ("MachineLegacy.stack.cfg", "some_definition"),
  290. ("OnlyDefinition.global.cfg", "some_definition"),
  291. ("Complete.global.cfg", "some_definition")
  292. ])
  293. def test_deserializeDefinition(filename, definition_id, container_registry, global_stack):
  294. serialized = readStack(filename)
  295. #Mock the loading of the instance containers.
  296. global_stack.findContainer = findSomeContainers
  297. original_container_registry = UM.Settings.ContainerStack._containerRegistry
  298. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
  299. global_stack.deserialize(serialized)
  300. assert global_stack.definition.getId() == definition_id
  301. #Restore.
  302. UM.Settings.ContainerStack._containerRegistry = original_container_registry
  303. ## Tests that when a global stack is loaded with an unknown instance, it raises
  304. # an exception.
  305. def test_deserializeMissingContainer(global_stack):
  306. serialized = readStack("Global.global.cfg")
  307. with pytest.raises(Exception):
  308. global_stack.deserialize(serialized)
  309. try:
  310. global_stack.deserialize(serialized)
  311. except Exception as e:
  312. #Must be exactly Exception, not one of its subclasses, since that is what gets raised when a stack has an unknown container.
  313. #That's why we can't use pytest.raises.
  314. assert type(e) == Exception
  315. ## Tests whether getProperty properly applies the stack-like behaviour on its
  316. # containers.
  317. def test_getPropertyFallThrough(global_stack):
  318. #A few instance container mocks to put in the stack.
  319. layer_height_5 = unittest.mock.MagicMock() #Sets layer height to 5.
  320. layer_height_5.getProperty = lambda key, property: 5 if (key == "layer_height" and property == "value") else None
  321. layer_height_5.hasProperty = lambda key: key == "layer_height"
  322. layer_height_10 = unittest.mock.MagicMock() #Sets layer height to 10.
  323. layer_height_10.getProperty = lambda key, property: 10 if (key == "layer_height" and property == "value") else None
  324. layer_height_10.hasProperty = lambda key: key == "layer_height"
  325. no_layer_height = unittest.mock.MagicMock() #No settings at all.
  326. no_layer_height.getProperty = lambda key, property: None
  327. no_layer_height.hasProperty = lambda key: False
  328. global_stack.userChanges = no_layer_height
  329. global_stack.qualityChanges = no_layer_height
  330. global_stack.quality = no_layer_height
  331. global_stack.material = no_layer_height
  332. global_stack.variant = no_layer_height
  333. global_stack.definitionChanges = no_layer_height
  334. global_stack.definition = layer_height_5 #Here it is!
  335. assert global_stack.getProperty("layer_height", "value") == 5
  336. global_stack.definitionChanges = layer_height_10
  337. assert global_stack.getProperty("layer_height", "value") == 10
  338. global_stack.variant = layer_height_5
  339. assert global_stack.getProperty("layer_height", "value") == 5
  340. global_stack.material = layer_height_10
  341. assert global_stack.getProperty("layer_height", "value") == 10
  342. global_stack.quality = layer_height_5
  343. assert global_stack.getProperty("layer_height", "value") == 5
  344. global_stack.qualityChanges = layer_height_10
  345. assert global_stack.getProperty("layer_height", "value") == 10
  346. global_stack.userChanges = layer_height_5
  347. assert global_stack.getProperty("layer_height", "value") == 5
  348. def test_getPropertyWithResolve(global_stack):
  349. #Define some containers for the stack.
  350. resolve = unittest.mock.MagicMock() #Sets just the resolve for bed temperature.
  351. resolve.getProperty = lambda key, property: 15 if (key == "material_bed_temperature" and property == "resolve") else None
  352. resolve_and_value = unittest.mock.MagicMock() #Sets the resolve and value for bed temperature.
  353. resolve_and_value.getProperty = lambda key, property: (7.5 if property == "resolve" else 5) if (key == "material_bed_temperature") else None #7.5 resolve, 5 value.
  354. value = unittest.mock.MagicMock() #Sets just the value for bed temperature.
  355. value.getProperty = lambda key, property: 10 if (key == "material_bed_temperature" and property == "value") else None
  356. empty = unittest.mock.MagicMock() #Sets no value or resolve.
  357. empty.getProperty = unittest.mock.MagicMock(return_value = None)
  358. global_stack.definition = resolve_and_value
  359. assert global_stack.getProperty("material_bed_temperature", "value") == 7.5 #Resolve wins in the definition.
  360. global_stack.userChanges = resolve_and_value
  361. assert global_stack.getProperty("material_bed_temperature", "value") == 5 #Value wins in other places.
  362. global_stack.userChanges = value
  363. assert global_stack.getProperty("material_bed_temperature", "value") == 10 #Resolve in the definition doesn't influence the value in the user changes.
  364. global_stack.userChanges = resolve
  365. assert global_stack.getProperty("material_bed_temperature", "value") == 15 #Falls through to definition for lack of values, but then asks the start of the stack for the resolve.
  366. global_stack.userChanges = empty
  367. global_stack.qualityChanges = resolve_and_value
  368. assert global_stack.getProperty("material_bed_temperature", "value") == 5 #Value still wins in lower places, except definition.
  369. global_stack.qualityChanges = empty
  370. global_stack.quality = resolve_and_value
  371. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  372. global_stack.quality = empty
  373. global_stack.material = resolve_and_value
  374. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  375. global_stack.material = empty
  376. global_stack.variant = resolve_and_value
  377. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  378. global_stack.variant = empty
  379. global_stack.definitionChanges = resolve_and_value
  380. assert global_stack.getProperty("material_bed_temperature", "value") == 5
  381. ## Tests whether the hasUserValue returns true for settings that are changed in
  382. # the user-changes container.
  383. def test_hasUserValueUserChanges(global_stack):
  384. user_changes = MockContainer("test_user_changes", "user")
  385. def hasProperty(key, property):
  386. return key == "layer_height" and property == "value" # Only have the layer_height property set.
  387. user_changes.hasProperty = hasProperty
  388. global_stack.userChanges = user_changes
  389. assert not global_stack.hasUserValue("infill_sparse_density")
  390. assert global_stack.hasUserValue("layer_height")
  391. assert not global_stack.hasUserValue("")
  392. ## Tests whether the hasUserValue returns true for settings that are changed in
  393. # the quality-changes container.
  394. def test_hasUserValueQualityChanges(global_stack):
  395. quality_changes = MockContainer("test_quality_changes", "quality_changes")
  396. def hasProperty(key, property):
  397. return key == "layer_height" and property == "value" # Only have the layer_height property set.
  398. quality_changes.hasProperty = hasProperty
  399. global_stack.qualityChanges = quality_changes
  400. assert not global_stack.hasUserValue("infill_sparse_density")
  401. assert global_stack.hasUserValue("layer_height")
  402. assert not global_stack.hasUserValue("")
  403. ## Tests whether inserting a container is properly forbidden.
  404. def test_insertContainer(global_stack):
  405. with pytest.raises(InvalidOperationError):
  406. global_stack.insertContainer(0, unittest.mock.MagicMock())
  407. ## Tests whether removing a container is properly forbidden.
  408. def test_removeContainer(global_stack):
  409. with pytest.raises(InvalidOperationError):
  410. global_stack.removeContainer(unittest.mock.MagicMock())
  411. ## Tests setting definitions by specifying an ID of a definition that exists.
  412. def test_setDefinitionByIdExists(global_stack, container_registry):
  413. global_stack.setDefinitionById("some_definition") #The container registry always has a container with the ID.
  414. ## Tests setting definitions by specifying an ID of a definition that doesn't
  415. # exist.
  416. def test_setDefinitionByIdDoesntExist(global_stack):
  417. with pytest.raises(InvalidContainerError):
  418. global_stack.setDefinitionById("some_definition") #Container registry is empty now.
  419. ## Tests setting definition changes by specifying an ID of a container that
  420. # exists.
  421. def test_setDefinitionChangesByIdExists(global_stack, container_registry):
  422. container_registry.typeMetaData = "definition_changes"
  423. global_stack.setDefinitionChangesById("some_definition_changes") #The container registry always has a container with the ID.
  424. ## Tests setting definition changes by specifying an ID of a container that
  425. # doesn't exist.
  426. def test_setDefinitionChangesByIdDoesntExist(global_stack):
  427. with pytest.raises(InvalidContainerError):
  428. global_stack.setDefinitionChangesById("some_definition_changes") #Container registry is empty now.
  429. ## Tests setting materials by specifying an ID of a material that exists.
  430. def test_setMaterialByIdExists(global_stack, container_registry):
  431. container_registry.typeMetaData = "material"
  432. global_stack.setMaterialById("some_material") #The container registry always has a container with the ID.
  433. ## Tests setting materials by specifying an ID of a material that doesn't
  434. # exist.
  435. def test_setMaterialByIdDoesntExist(global_stack):
  436. with pytest.raises(InvalidContainerError):
  437. global_stack.setMaterialById("some_material") #Container registry is empty now.
  438. ## Tests whether changing the next stack is properly forbidden.
  439. def test_setNextStack(global_stack):
  440. with pytest.raises(InvalidOperationError):
  441. global_stack.setNextStack(unittest.mock.MagicMock())
  442. ## Tests setting properties directly on the global stack.
  443. @pytest.mark.parametrize("key, property, value, output_value", [
  444. ("layer_height", "value", 0.1337, 0.1337),
  445. ("foo", "value", 100, 100),
  446. ("support_enabled", "value", True, True),
  447. ("layer_height", "default_value", 0.1337, 0.1337),
  448. ("layer_height", "is_bright_pink", "of course", "of course")
  449. ])
  450. def test_setPropertyUser(key, property, value, output_value, writable_global_stack):
  451. writable_global_stack.setProperty(key, property, value)
  452. assert writable_global_stack.userChanges.getProperty(key, property) == output_value
  453. ## Tests setting properties on specific containers on the global stack.
  454. @pytest.mark.parametrize("target_container", [
  455. "user",
  456. "quality_changes",
  457. "quality",
  458. "material",
  459. "variant",
  460. "definition_changes",
  461. ])
  462. def test_setPropertyOtherContainers(target_container, writable_global_stack):
  463. #Other parameters that don't need to be varied.
  464. key = "layer_height"
  465. property = "value"
  466. value = 0.1337
  467. output_value = 0.1337
  468. writable_global_stack.setProperty(key, property, value, target_container = target_container)
  469. containers = {
  470. "user": writable_global_stack.userChanges,
  471. "quality_changes": writable_global_stack.qualityChanges,
  472. "quality": writable_global_stack.quality,
  473. "material": writable_global_stack.material,
  474. "variant": writable_global_stack.variant,
  475. "definition_changes": writable_global_stack.definitionChanges,
  476. "definition": writable_global_stack.definition
  477. }
  478. assert containers[target_container].getProperty(key, property) == output_value
  479. ## Tests setting qualities by specifying an ID of a quality that exists.
  480. def test_setQualityByIdExists(global_stack, container_registry):
  481. container_registry.typeMetaData = "quality"
  482. global_stack.setQualityById("some_quality") #The container registry always has a container with the ID.
  483. ## Tests setting qualities by specifying an ID of a quality that doesn't exist.
  484. def test_setQualityByIdDoesntExist(global_stack):
  485. with pytest.raises(InvalidContainerError):
  486. global_stack.setQualityById("some_quality") #Container registry is empty now.
  487. ## Tests setting quality changes by specifying an ID of a quality change that
  488. # exists.
  489. def test_setQualityChangesByIdExists(global_stack, container_registry):
  490. container_registry.typeMetaData = "quality_changes"
  491. global_stack.setQualityChangesById("some_quality_changes") #The container registry always has a container with the ID.
  492. ## Tests setting quality changes by specifying an ID of a quality change that
  493. # doesn't exist.
  494. def test_setQualityChangesByIdDoesntExist(global_stack):
  495. with pytest.raises(InvalidContainerError):
  496. global_stack.setQualityChangesById("some_quality_changes") #Container registry is empty now.
  497. ## Tests setting variants by specifying an ID of a variant that exists.
  498. def test_setVariantByIdExists(global_stack, container_registry):
  499. container_registry.typeMetaData = "variant"
  500. global_stack.setVariantById("some_variant") #The container registry always has a container with the ID.
  501. ## Tests setting variants by specifying an ID of a variant that doesn't exist.
  502. def test_setVariantByIdDoesntExist(global_stack):
  503. with pytest.raises(InvalidContainerError):
  504. global_stack.setVariantById("some_variant") #Container registry is empty now.