Let container_registry return a pre-set value for what containers it finds

This makes a lot of tests more simple, without requiring elaborate mocks, patches and fixtures.

Contributes to issue CURA-3497.
This commit is contained in:
Ghostkeeper 2017-04-12 09:40:37 +02:00
parent 479176bc6f
commit 361b58f49c
No known key found for this signature in database
GPG key ID: C5F96EE2BC0F7E75

View file

@ -13,16 +13,20 @@ import cura.Settings.ExtruderStack #The module we're testing.
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.
## Fake container registry that always provides all containers you ask of. ## Fake container registry that always provides all containers you ask of.
@pytest.fixture() @pytest.yield_fixture()
def container_registry(): def container_registry():
registry = unittest.mock.MagicMock() registry = unittest.mock.MagicMock()
def findContainers(id = None): registry.return_value = unittest.mock.NonCallableMagicMock()
if not id: registry.findInstanceContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
return [UM.Settings.ContainerRegistry._EmptyInstanceContainer("test_container")] registry.findDefinitionContainers = lambda *args, registry = registry, **kwargs: [registry.return_value]
else:
return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)] UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = registry
registry.findContainers = findContainers UM.Settings.ContainerStack._containerRegistry = registry
return registry
yield registry
UM.Settings.ContainerRegistry.ContainerRegistry._ContainerRegistry__instance = None
UM.Settings.ContainerStack._containerRegistry = None
## An empty extruder stack to test with. ## An empty extruder stack to test with.
@pytest.fixture() @pytest.fixture()
@ -305,21 +309,19 @@ def test_removeContainer(extruder_stack):
## Tests setting definitions by specifying an ID of a definition that exists. ## Tests setting definitions by specifying an ID of a definition that exists.
def test_setDefinitionByIdExists(extruder_stack, container_registry): def test_setDefinitionByIdExists(extruder_stack, container_registry):
original_container_registry = UM.Settings.ContainerStack._containerRegistry container_registry.return_value = DefinitionContainer(container_id = "some_definition")
UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of. extruder_stack.setDefinitionById("some_definition")
assert extruder_stack.definition.getId() == "some_definition"
extruder_stack.setDefinitionById("some_definition") #The container registry always has a container with the ID.
#Restore.
UM.Settings.ContainerStack._containerRegistry = original_container_registry
## Tests setting definitions by specifying an ID of a definition that doesn't ## Tests setting definitions by specifying an ID of a definition that doesn't
# exist. # exist.
@pytest.mark.skip
def test_setDefinitionByIdDoesntExist(extruder_stack): def test_setDefinitionByIdDoesntExist(extruder_stack):
with pytest.raises(KeyError): with pytest.raises(KeyError):
extruder_stack.setDefinitionById("some_definition") #Container registry is empty now. extruder_stack.setDefinitionById("some_definition") #Container registry is empty now.
## Tests setting materials by specifying an ID of a material that exists. ## Tests setting materials by specifying an ID of a material that exists.
@pytest.mark.skip
def test_setMaterialByIdExists(extruder_stack, container_registry): def test_setMaterialByIdExists(extruder_stack, container_registry):
original_container_registry = UM.Settings.ContainerStack._containerRegistry original_container_registry = UM.Settings.ContainerStack._containerRegistry
UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
@ -331,11 +333,13 @@ def test_setMaterialByIdExists(extruder_stack, container_registry):
## Tests setting materials by specifying an ID of a material that doesn't ## Tests setting materials by specifying an ID of a material that doesn't
# exist. # exist.
@pytest.mark.skip
def test_setMaterialByIdDoesntExist(extruder_stack): def test_setMaterialByIdDoesntExist(extruder_stack):
with pytest.raises(KeyError): with pytest.raises(KeyError):
extruder_stack.setMaterialById("some_material") #Container registry is empty now. extruder_stack.setMaterialById("some_material") #Container registry is empty now.
## Tests setting properties directly on the extruder stack. ## Tests setting properties directly on the extruder stack.
@pytest.mark.skip
@pytest.mark.parametrize("key, property, value, output_value", [ @pytest.mark.parametrize("key, property, value, output_value", [
("layer_height", "value", "0.1337", 0.1337), ("layer_height", "value", "0.1337", 0.1337),
("foo", "value", "100", 100), ("foo", "value", "100", 100),
@ -348,6 +352,7 @@ def test_setPropertyUser(key, property, value, output_value, extruder_stack):
assert extruder_stack.userChanges.getProperty(key, property) == output_value assert extruder_stack.userChanges.getProperty(key, property) == output_value
## Tests setting properties on specific containers on the extruder stack. ## Tests setting properties on specific containers on the extruder stack.
@pytest.mark.skip
@pytest.mark.parametrize("target_container", [ @pytest.mark.parametrize("target_container", [
"user", "user",
"quality_changes", "quality_changes",
@ -376,6 +381,7 @@ def test_setPropertyOtherContainers(target_container, extruder_stack):
assert containers[target_container].getProperty(key, property) == output_value assert containers[target_container].getProperty(key, property) == output_value
## Tests setting qualities by specifying an ID of a quality that exists. ## Tests setting qualities by specifying an ID of a quality that exists.
@pytest.mark.skip
def test_setQualityByIdExists(extruder_stack, container_registry): def test_setQualityByIdExists(extruder_stack, container_registry):
original_container_registry = UM.Settings.ContainerStack._containerRegistry original_container_registry = UM.Settings.ContainerStack._containerRegistry
UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
@ -386,12 +392,14 @@ def test_setQualityByIdExists(extruder_stack, container_registry):
UM.Settings.ContainerStack._containerRegistry = original_container_registry UM.Settings.ContainerStack._containerRegistry = original_container_registry
## Tests setting qualities by specifying an ID of a quality that doesn't exist. ## Tests setting qualities by specifying an ID of a quality that doesn't exist.
@pytest.mark.skip
def test_setQualityByIdDoesntExist(extruder_stack): def test_setQualityByIdDoesntExist(extruder_stack):
with pytest.raises(KeyError): with pytest.raises(KeyError):
extruder_stack.setQualityById("some_quality") #Container registry is empty now. extruder_stack.setQualityById("some_quality") #Container registry is empty now.
## Tests setting quality changes by specifying an ID of a quality change that ## Tests setting quality changes by specifying an ID of a quality change that
# exists. # exists.
@pytest.mark.skip
def test_setQualityChangesByIdExists(extruder_stack, container_registry): def test_setQualityChangesByIdExists(extruder_stack, container_registry):
original_container_registry = UM.Settings.ContainerStack._containerRegistry original_container_registry = UM.Settings.ContainerStack._containerRegistry
UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
@ -403,11 +411,13 @@ def test_setQualityChangesByIdExists(extruder_stack, container_registry):
## Tests setting quality changes by specifying an ID of a quality change that ## Tests setting quality changes by specifying an ID of a quality change that
# doesn't exist. # doesn't exist.
@pytest.mark.skip
def test_setQualityChangesByIdDoesntExist(extruder_stack): def test_setQualityChangesByIdDoesntExist(extruder_stack):
with pytest.raises(KeyError): with pytest.raises(KeyError):
extruder_stack.setQualityChangesById("some_quality_changes") #Container registry is empty now. extruder_stack.setQualityChangesById("some_quality_changes") #Container registry is empty now.
## Tests setting variants by specifying an ID of a variant that exists. ## Tests setting variants by specifying an ID of a variant that exists.
@pytest.mark.skip
def test_setVariantByIdExists(extruder_stack, container_registry): def test_setVariantByIdExists(extruder_stack, container_registry):
original_container_registry = UM.Settings.ContainerStack._containerRegistry original_container_registry = UM.Settings.ContainerStack._containerRegistry
UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of. UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all the profiles you ask of.
@ -418,6 +428,7 @@ def test_setVariantByIdExists(extruder_stack, container_registry):
UM.Settings.ContainerStack._containerRegistry = original_container_registry UM.Settings.ContainerStack._containerRegistry = original_container_registry
## Tests setting variants by specifying an ID of a variant that doesn't exist. ## Tests setting variants by specifying an ID of a variant that doesn't exist.
@pytest.mark.skip
def test_setVariantByIdDoesntExist(extruder_stack): def test_setVariantByIdDoesntExist(extruder_stack):
with pytest.raises(KeyError): with pytest.raises(KeyError):
extruder_stack.setVariantById("some_variant") #Container registry is empty now. extruder_stack.setVariantById("some_variant") #Container registry is empty now.