Fix tests in the ExtruderStack.

Contributes to CURA-5628.
This commit is contained in:
Diego Prado Gesto 2018-08-14 15:40:11 +02:00
parent e1fd9b03a4
commit b85950b128
3 changed files with 49 additions and 59 deletions

View file

@ -4,37 +4,14 @@
import pytest #This module contains automated tests. import pytest #This module contains automated tests.
import unittest.mock #For the mocking and monkeypatching functionality. import unittest.mock #For the mocking and monkeypatching functionality.
import cura.CuraApplication import cura.Settings.CuraContainerStack #To get the list of container types.
import UM.Settings.ContainerRegistry #To create empty instance containers. import UM.Settings.ContainerRegistry #To create empty instance containers.
import UM.Settings.ContainerStack #To set the container registry the container stacks use. import UM.Settings.ContainerStack #To set the container registry the container stacks use.
from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer. from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer.
from UM.Settings.InstanceContainer import InstanceContainer #To check against the class of InstanceContainer. from UM.Settings.InstanceContainer import InstanceContainer #To check against the class of InstanceContainer.
import cura.Settings.ExtruderStack #The module we're testing. from UM.Settings.EmptyInstanceContainer import empty_container
from cura.Settings.Exceptions import InvalidContainerError, InvalidOperationError #To check whether the correct exceptions are raised. from cura.Settings.Exceptions import InvalidContainerError, InvalidOperationError #To check whether the correct exceptions are raised.
from cura.Settings.ExtruderManager import ExtruderManager from cura.Settings.ExtruderManager import ExtruderManager
from cura.Settings.GlobalStack import GlobalStack
## Fake container registry that always provides all containers you ask of.
# @pytest.yield_fixture()
# def container_registry():
# registry = unittest.mock.MagicMock()
# registry.return_value = unittest.mock.NonCallableMagicMock()
# registry.findInstanceContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
# registry.findDefinitionContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
#
# UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = registry
# UM.Settings.ContainerStack._containerRegistry = registry
#
# yield registry
#
# UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = None
# UM.Settings.ContainerStack._containerRegistry = None
## An empty extruder stack to test with.
@pytest.fixture()
def extruder_stack() -> cura.Settings.ExtruderStack.ExtruderStack:
return cura.Settings.ExtruderStack.ExtruderStack("TestStack")
## Gets an instance container with a specified container type. ## Gets an instance container with a specified container type.
# #
@ -151,12 +128,30 @@ def test_constrainVariantInvalid(container, extruder_stack):
def test_constrainVariantValid(container, extruder_stack): def test_constrainVariantValid(container, extruder_stack):
extruder_stack.variant = container #Should not give an error. extruder_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"),
getInstanceContainer(container_type = "material"), #Existing, but still wrong type.
DefinitionContainer(container_id = "wrong class")
])
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"),
InstanceContainerSubClass(container_type = "definition_changes")
])
def test_constrainDefinitionChangesValid(container, global_stack):
global_stack.definitionChanges = container #Should not give an error.
#Tests setting definitions to invalid containers. #Tests setting definitions to invalid containers.
@pytest.mark.parametrize("container", [ @pytest.mark.parametrize("container", [
getInstanceContainer(container_type = "wrong class"), getInstanceContainer(container_type = "wrong class"),
getInstanceContainer(container_type = "material"), #Existing, but still wrong class. getInstanceContainer(container_type = "material"), #Existing, but still wrong class.
]) ])
def test_constrainVariantInvalid(container, extruder_stack): def test_constrainDefinitionInvalid(container, extruder_stack):
with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
extruder_stack.definition = container extruder_stack.definition = container
@ -168,23 +163,22 @@ def test_constrainVariantInvalid(container, extruder_stack):
def test_constrainDefinitionValid(container, extruder_stack): def test_constrainDefinitionValid(container, extruder_stack):
extruder_stack.definition = container #Should not give an error. extruder_stack.definition = container #Should not give an error.
## Tests whether deserialising completes the missing containers with empty ## Tests whether deserialising completes the missing containers with empty ones.
# ones. def test_deserializeCompletesEmptyContainers(extruder_stack):
@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. extruder_stack._containers = [DefinitionContainer(container_id = "definition"), extruder_stack.definitionChanges] #Set the internal state of this stack manually.
def test_deserializeCompletesEmptyContainers(extruder_stack: cura.Settings.ExtruderStack):
extruder_stack._containers = [DefinitionContainer(container_id = "definition")] #Set the internal state of this stack manually.
with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
extruder_stack.deserialize("") extruder_stack.deserialize("")
assert len(extruder_stack.getContainers()) == len(cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap) #Needs a slot for every type. assert len(extruder_stack.getContainers()) == len(cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap) #Needs a slot for every type.
for container_type_index in cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap: for container_type_index in cura.Settings.CuraContainerStack._ContainerIndexes.IndexTypeMap:
if container_type_index == cura.Settings.CuraContainerStack._ContainerIndexes.Definition: #We're not checking the definition. if container_type_index in \
(cura.Settings.CuraContainerStack._ContainerIndexes.Definition,
cura.Settings.CuraContainerStack._ContainerIndexes.DefinitionChanges): # We're not checking the definition or definition_changes
continue continue
assert extruder_stack.getContainer(container_type_index).getId() == "empty" #All others need to be empty. assert extruder_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 ## Tests whether an instance container with the wrong type gets removed when deserialising.
# deserialising.
def test_deserializeRemovesWrongInstanceContainer(extruder_stack): def test_deserializeRemovesWrongInstanceContainer(extruder_stack):
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type") extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "wrong type")
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition") extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
@ -194,8 +188,7 @@ def test_deserializeRemovesWrongInstanceContainer(extruder_stack):
assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
## Tests whether a container with the wrong class gets removed when ## Tests whether a container with the wrong class gets removed when deserialising.
# deserialising.
def test_deserializeRemovesWrongContainerClass(extruder_stack): def test_deserializeRemovesWrongContainerClass(extruder_stack):
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class") extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = DefinitionContainer(container_id = "wrong class")
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition") extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
@ -205,8 +198,7 @@ def test_deserializeRemovesWrongContainerClass(extruder_stack):
assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty. assert extruder_stack.quality == extruder_stack._empty_instance_container #Replaced with empty.
## Tests whether an instance container in the definition spot results in an ## Tests whether an instance container in the definition spot results in an error.
# error.
def test_deserializeWrongDefinitionClass(extruder_stack): def test_deserializeWrongDefinitionClass(extruder_stack):
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = getInstanceContainer(container_type = "definition") #Correct type but wrong class.
@ -214,8 +206,7 @@ def test_deserializeWrongDefinitionClass(extruder_stack):
with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container. with pytest.raises(UM.Settings.ContainerStack.InvalidContainerStackError): #Must raise an error that there is no definition container.
extruder_stack.deserialize("") extruder_stack.deserialize("")
## Tests whether an instance container with the wrong type is moved into the ## Tests whether an instance container with the wrong type is moved into the correct slot by deserialising.
# correct slot by deserialising.
def test_deserializeMoveInstanceContainer(extruder_stack): def test_deserializeMoveInstanceContainer(extruder_stack):
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Quality] = getInstanceContainer(container_type = "material") #Not in the correct spot.
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition") extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Definition] = DefinitionContainer(container_id = "some definition")
@ -223,26 +214,21 @@ def test_deserializeMoveInstanceContainer(extruder_stack):
with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
extruder_stack.deserialize("") extruder_stack.deserialize("")
assert extruder_stack.quality.getId() == "empty" assert extruder_stack.quality == empty_container
assert extruder_stack.material.getId() != "empty" assert extruder_stack.material != empty_container
from UM.Settings.Validator import Validator
## Tests whether a definition container in the wrong spot is moved into the ## Tests whether a definition container in the wrong spot is moved into the correct spot by deserialising.
# correct spot by deserialising.
@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.
def test_deserializeMoveDefinitionContainer(extruder_stack): def test_deserializeMoveDefinitionContainer(extruder_stack):
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot. extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.Material] = DefinitionContainer(container_id = "some definition") #Not in the correct spot.
with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize. with unittest.mock.patch("UM.Settings.ContainerStack.ContainerStack.deserialize", unittest.mock.MagicMock()): #Prevent calling super().deserialize.
extruder_stack.deserialize("") extruder_stack.deserialize("")
assert extruder_stack.material.getId() == "empty" assert extruder_stack.material == empty_container
assert extruder_stack.definition.getId() != "empty" assert extruder_stack.definition != empty_container
UM.Settings.ContainerStack._containerRegistry = None ## Tests whether getProperty properly applies the stack-like behaviour on its containers.
def test_getPropertyFallThrough(global_stack, extruder_stack):
## Tests whether getProperty properly applies the stack-like behaviour on its
# containers.
def test_getPropertyFallThrough(extruder_stack):
# ExtruderStack.setNextStack calls registerExtruder for backward compatibility, but we do not need a complete extruder manager # ExtruderStack.setNextStack calls registerExtruder for backward compatibility, but we do not need a complete extruder manager
ExtruderManager._ExtruderManager__instance = unittest.mock.MagicMock() ExtruderManager._ExtruderManager__instance = unittest.mock.MagicMock()
@ -272,8 +258,7 @@ def test_getPropertyFallThrough(extruder_stack):
with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking. with unittest.mock.patch("cura.Settings.CuraContainerStack.DefinitionContainer", unittest.mock.MagicMock): #To guard against the type checking.
extruder_stack.definition = mock_layer_heights[container_indices.Definition] #There's a layer height in here! extruder_stack.definition = mock_layer_heights[container_indices.Definition] #There's a layer height in here!
stack = GlobalStack("PyTest GlobalStack") extruder_stack.setNextStack(global_stack)
extruder_stack.setNextStack(stack)
assert extruder_stack.getProperty("layer_height", "value") == container_indices.Definition assert extruder_stack.getProperty("layer_height", "value") == container_indices.Definition
extruder_stack.variant = mock_layer_heights[container_indices.Variant] extruder_stack.variant = mock_layer_heights[container_indices.Variant]
@ -312,4 +297,4 @@ def test_setPropertyUser(key, property, value, extruder_stack):
extruder_stack.setProperty(key, property, value) #The actual test. extruder_stack.setProperty(key, property, value) #The actual test.
extruder_stack.userChanges.setProperty.assert_called_once_with(key, property, value) #Make sure that the user container gets a setProperty call. extruder_stack.userChanges.setProperty.assert_called_once_with(key, property, value, None, False) #Make sure that the user container gets a setProperty call.

View file

@ -179,7 +179,7 @@ def test_constrainDefinitionChangesValid(container, global_stack):
getInstanceContainer(container_type = "wrong class"), getInstanceContainer(container_type = "wrong class"),
getInstanceContainer(container_type = "material"), #Existing, but still wrong class. getInstanceContainer(container_type = "material"), #Existing, but still wrong class.
]) ])
def test_constrainVariantInvalid(container, global_stack): def test_constrainDefinitionInvalid(container, global_stack):
with pytest.raises(InvalidContainerError): #Invalid container, should raise an error. with pytest.raises(InvalidContainerError): #Invalid container, should raise an error.
global_stack.definition = container global_stack.definition = container

View file

@ -35,4 +35,9 @@ def global_stack() -> GlobalStack:
## An empty extruder stack to test with. ## An empty extruder stack to test with.
@pytest.fixture() @pytest.fixture()
def extruder_stack() -> ExtruderStack: def extruder_stack() -> ExtruderStack:
return ExtruderStack("TestExtruderStack") extruder_stack= ExtruderStack("TestExtruderStack")
# There is a restriction here that the definition changes cannot be an empty container. Added in CURA-5281
definition_changes_container = InstanceContainer(container_id = "InstanceContainer")
definition_changes_container.setMetaDataEntry("type", "definition_changes")
extruder_stack._containers[cura.Settings.CuraContainerStack._ContainerIndexes.DefinitionChanges] = definition_changes_container
return extruder_stack