TestGlobalStack.py 33 KB

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