TestGlobalStack.py 29 KB

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