diff --git a/cura/Settings/GlobalStack.py b/cura/Settings/GlobalStack.py index e2f7df41ea..517b45eb98 100755 --- a/cura/Settings/GlobalStack.py +++ b/cura/Settings/GlobalStack.py @@ -23,6 +23,7 @@ from .CuraContainerStack import CuraContainerStack if TYPE_CHECKING: from cura.Settings.ExtruderStack import ExtruderStack + ## Represents the Global or Machine stack and its related containers. # class GlobalStack(CuraContainerStack): diff --git a/tests/Settings/TestGlobalStack.py b/tests/Settings/TestGlobalStack.py index f8052aa4bb..0f1579f78b 100755 --- a/tests/Settings/TestGlobalStack.py +++ b/tests/Settings/TestGlobalStack.py @@ -15,6 +15,7 @@ import UM.Settings.SettingDefinition #To add settings to the definition. from cura.Settings.cura_empty_instance_containers import empty_container + ## Gets an instance container with a specified container type. # # \param container_type The type metadata for the instance container. @@ -24,22 +25,27 @@ def getInstanceContainer(container_type) -> InstanceContainer: container.setMetaDataEntry("type", container_type) return container + class DefinitionContainerSubClass(DefinitionContainer): def __init__(self): super().__init__(container_id = "SubDefinitionContainer") + class InstanceContainerSubClass(InstanceContainer): def __init__(self, container_type): super().__init__(container_id = "SubInstanceContainer") self.setMetaDataEntry("type", container_type) + #############################START OF TEST CASES################################ + ## Tests whether adding a container is properly forbidden. def test_addContainer(global_stack): with pytest.raises(InvalidOperationError): global_stack.addContainer(unittest.mock.MagicMock()) + ## Tests adding extruders to the global stack. def test_addExtruder(global_stack): mock_definition = unittest.mock.MagicMock() @@ -67,6 +73,7 @@ def test_addExtruder(global_stack): # global_stack.addExtruder(unittest.mock.MagicMock()) assert len(global_stack.extruders) == 2 #Didn't add the faulty extruder. + #Tests setting user changes profiles to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"), @@ -77,6 +84,7 @@ def test_constrainUserChangesInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.userChanges = container + #Tests setting user changes profiles. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "user"), @@ -85,6 +93,7 @@ def test_constrainUserChangesInvalid(container, global_stack): def test_constrainUserChangesValid(container, global_stack): global_stack.userChanges = container #Should not give an error. + #Tests setting quality changes profiles to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"), @@ -95,6 +104,7 @@ def test_constrainQualityChangesInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.qualityChanges = container + #Test setting quality changes profiles. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "quality_changes"), @@ -103,6 +113,7 @@ def test_constrainQualityChangesInvalid(container, global_stack): def test_constrainQualityChangesValid(container, global_stack): global_stack.qualityChanges = container #Should not give an error. + #Tests setting quality profiles to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"), @@ -113,6 +124,7 @@ def test_constrainQualityInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.quality = container + #Test setting quality profiles. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "quality"), @@ -121,6 +133,7 @@ def test_constrainQualityInvalid(container, global_stack): def test_constrainQualityValid(container, global_stack): global_stack.quality = container #Should not give an error. + #Tests setting materials to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"), @@ -131,6 +144,7 @@ def test_constrainMaterialInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.material = container + #Test setting materials. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "material"), @@ -139,6 +153,7 @@ def test_constrainMaterialInvalid(container, global_stack): def test_constrainMaterialValid(container, global_stack): global_stack.material = container #Should not give an error. + #Tests setting variants to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"), @@ -149,6 +164,7 @@ def test_constrainVariantInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.variant = container + #Test setting variants. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "variant"), @@ -157,6 +173,7 @@ def test_constrainVariantInvalid(container, global_stack): def test_constrainVariantValid(container, global_stack): global_stack.variant = container #Should not give an error. + #Tests setting definition changes profiles to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong container type"), @@ -167,6 +184,7 @@ def test_constrainDefinitionChangesInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.definitionChanges = container + #Test setting definition changes profiles. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "definition_changes"), @@ -175,6 +193,7 @@ def test_constrainDefinitionChangesInvalid(container, global_stack): def test_constrainDefinitionChangesValid(container, global_stack): global_stack.definitionChanges = container #Should not give an error. + #Tests setting definitions to invalid containers. @pytest.mark.parametrize("container", [ getInstanceContainer(container_type = "wrong class"), @@ -184,6 +203,7 @@ def test_constrainDefinitionInvalid(container, global_stack): with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. global_stack.definition = container + #Test setting definitions. @pytest.mark.parametrize("container", [ DefinitionContainer(container_id = "DefinitionContainer"), @@ -192,6 +212,7 @@ def test_constrainDefinitionInvalid(container, global_stack): def test_constrainDefinitionValid(container, global_stack): global_stack.definition = container #Should not give an error. + ## Tests whether deserialising completes the missing containers with empty ones. The initial containers are just the # definition and the definition_changes (that cannot be empty after CURA-5281) def test_deserializeCompletesEmptyContainers(global_stack): @@ -207,6 +228,7 @@ def test_deserializeCompletesEmptyContainers(global_stack): continue assert global_stack.getContainer(container_type_index) == empty_container #All others need to be empty. + ## Tests whether an instance container with the wrong type gets removed when deserialising. def test_deserializeRemovesWrongInstanceContainer(global_stack): global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type") @@ -217,6 +239,7 @@ def test_deserializeRemovesWrongInstanceContainer(global_stack): assert global_stack.quality == global_stack._empty_instance_container #Replaced with empty. + ## Tests whether a container with the wrong class gets removed when deserialising. def test_deserializeRemovesWrongContainerClass(global_stack): global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class") @@ -227,6 +250,7 @@ def test_deserializeRemovesWrongContainerClass(global_stack): assert global_stack.quality == global_stack._empty_instance_container #Replaced with empty. + ## Tests whether an instance container in the definition spot results in an error. def test_deserializeWrongDefinitionClass(global_stack): global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class. @@ -235,6 +259,7 @@ def test_deserializeWrongDefinitionClass(global_stack): with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container. global_stack.deserialize("") + ## Tests whether an instance container with the wrong type is moved into the correct slot by deserialising. def test_deserializeMoveInstanceContainer(global_stack): global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot. @@ -246,6 +271,7 @@ def test_deserializeMoveInstanceContainer(global_stack): assert global_stack.quality == empty_container assert global_stack.material != empty_container + ## Tests whether a definition container in the wrong spot is moved into the correct spot by deserialising. def test_deserializeMoveDefinitionContainer(global_stack): global_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot. @@ -256,6 +282,7 @@ def test_deserializeMoveDefinitionContainer(global_stack): assert global_stack.material == empty_container assert global_stack.definition != empty_container + ## Tests whether getProperty properly applies the stack-like behaviour on its containers. def test_getPropertyFallThrough(global_stack): #A few instance container mocks to put in the stack. @@ -298,6 +325,7 @@ def test_getPropertyFallThrough(global_stack): global_stack.userChanges = mock_layer_heights[container_indexes.UserChanges] assert global_stack.getProperty("layer_height", "value") == container_indexes.UserChanges + ## In definitions, test whether having no resolve allows us to find the value. def test_getPropertyNoResolveInDefinition(global_stack): value = unittest.mock.MagicMock() #Just sets the value for bed temperature. @@ -307,6 +335,7 @@ def test_getPropertyNoResolveInDefinition(global_stack): global_stack.definition = value assert global_stack.getProperty("material_bed_temperature", "value") == 10 #No resolve, so fall through to value. + ## In definitions, when the value is asked and there is a resolve function, it must get the resolve first. def test_getPropertyResolveInDefinition(global_stack): resolve_and_value = unittest.mock.MagicMock() #Sets the resolve and value for bed temperature. @@ -316,6 +345,7 @@ def test_getPropertyResolveInDefinition(global_stack): global_stack.definition = resolve_and_value assert global_stack.getProperty("material_bed_temperature", "value") == 7.5 #Resolve wins in the definition. + ## In instance containers, when the value is asked and there is a resolve function, it must get the value first. def test_getPropertyResolveInInstance(global_stack): container_indices = cura.Settings.CuraContainerStack._ContainerIndexes @@ -342,6 +372,7 @@ def test_getPropertyResolveInInstance(global_stack): global_stack.userChanges = instance_containers[container_indices.UserChanges] assert global_stack.getProperty("material_bed_temperature", "value") == 5 + ## Tests whether the value in instances gets evaluated before the resolve in definitions. def test_getPropertyInstancesBeforeResolve(global_stack): value = unittest.mock.MagicMock() #Sets just the value. @@ -356,6 +387,7 @@ def test_getPropertyInstancesBeforeResolve(global_stack): assert global_stack.getProperty("material_bed_temperature", "value") == 10 + ## Tests whether the hasUserValue returns true for settings that are changed in the user-changes container. def test_hasUserValueUserChanges(global_stack): container = unittest.mock.MagicMock() @@ -367,6 +399,7 @@ def test_hasUserValueUserChanges(global_stack): assert not global_stack.hasUserValue("infill_sparse_density") assert not global_stack.hasUserValue("") + ## Tests whether the hasUserValue returns true for settings that are changed in the quality-changes container. def test_hasUserValueQualityChanges(global_stack): container = unittest.mock.MagicMock() @@ -378,6 +411,7 @@ def test_hasUserValueQualityChanges(global_stack): assert not global_stack.hasUserValue("infill_sparse_density") assert not global_stack.hasUserValue("") + ## Tests whether a container in some other place on the stack is correctly not recognised as user value. def test_hasNoUserValue(global_stack): container = unittest.mock.MagicMock() @@ -387,21 +421,25 @@ def test_hasNoUserValue(global_stack): assert not global_stack.hasUserValue("layer_height") #However this container is quality, so it's not a user value. + ## Tests whether inserting a container is properly forbidden. def test_insertContainer(global_stack): with pytest.raises(InvalidOperationError): global_stack.insertContainer(0, unittest.mock.MagicMock()) + ## Tests whether removing a container is properly forbidden. def test_removeContainer(global_stack): with pytest.raises(InvalidOperationError): global_stack.removeContainer(unittest.mock.MagicMock()) + ## Tests whether changing the next stack is properly forbidden. def test_setNextStack(global_stack): with pytest.raises(InvalidOperationError): global_stack.setNextStack(unittest.mock.MagicMock()) + ## Tests setting properties directly on the global stack. @pytest.mark.parametrize("key, property, value", [ ("layer_height", "value", 0.1337), @@ -415,6 +453,7 @@ def test_setPropertyUser(key, property, value, global_stack): user_changes.getMetaDataEntry = unittest.mock.MagicMock(return_value = "user") global_stack.userChanges = user_changes - global_stack.setProperty(key, property, value) #The actual test. + global_stack.setProperty(key, property, value) # The actual test. - global_stack.userChanges.setProperty.assert_called_once_with(key, property, value, None, False) #Make sure that the user container gets a setProperty call. \ No newline at end of file + # Make sure that the user container gets a setProperty call. + global_stack.userChanges.setProperty.assert_called_once_with(key, property, value, None, False) \ No newline at end of file