mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-21 21:58:01 -06:00
Add tests for deserialising a stack with user changes
Code is mostly copied over from the global stack, which should work in a similar way. Contributes to issue CURA-3497.
This commit is contained in:
parent
e881465050
commit
dea86ca535
3 changed files with 78 additions and 1 deletions
|
@ -1,16 +1,52 @@
|
|||
# Copyright (c) 2017 Ultimaker B.V.
|
||||
# Cura is released under the terms of the AGPLv3 or higher.
|
||||
|
||||
import os.path #To find the test stack files.
|
||||
import pytest #This module contains automated tests.
|
||||
import unittest.mock #For the mocking and monkeypatching functionality.
|
||||
|
||||
import UM.Settings.ContainerRegistry #To create empty instance containers.
|
||||
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.
|
||||
import cura.Settings.ExtruderStack #The module we're testing.
|
||||
from cura.Settings.Exceptions import InvalidOperationError #To check whether the correct exceptions are raised.
|
||||
|
||||
## Fake container registry that always provides all containers you ask of.
|
||||
@pytest.fixture()
|
||||
def container_registry():
|
||||
registry = unittest.mock.MagicMock()
|
||||
def findContainers(id = None):
|
||||
if not id:
|
||||
return [UM.Settings.ContainerRegistry._EmptyInstanceContainer("test_container")]
|
||||
else:
|
||||
return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
|
||||
registry.findContainers = findContainers
|
||||
return registry
|
||||
|
||||
## An empty extruder stack to test with.
|
||||
@pytest.fixture()
|
||||
def extruder_stack() -> cura.Settings.ExtruderStack.ExtruderStack:
|
||||
return cura.Settings.ExtruderStack.ExtruderStack
|
||||
return cura.Settings.ExtruderStack.ExtruderStack("TestStack")
|
||||
|
||||
## Place-in function for findContainer that finds only containers that start
|
||||
# with "some_".
|
||||
def findSomeContainers(container_id = "*", container_type = None, type = None, category = "*"):
|
||||
if container_id.startswith("some_"):
|
||||
return UM.Settings.ContainerRegistry._EmptyInstanceContainer(container_id)
|
||||
if container_type == DefinitionContainer:
|
||||
definition_mock = unittest.mock.MagicMock()
|
||||
definition_mock.getId = unittest.mock.MagicMock(return_value = "some_definition") #getId returns some_definition.
|
||||
return definition_mock
|
||||
|
||||
## Helper function to read the contents of a container stack in the test
|
||||
# stack folder.
|
||||
#
|
||||
# \param filename The name of the file in the "stacks" folder to read from.
|
||||
# \return The contents of that file.
|
||||
def readStack(filename):
|
||||
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)) as file_handle:
|
||||
serialized = file_handle.read()
|
||||
return serialized
|
||||
|
||||
#############################START OF TEST CASES################################
|
||||
|
||||
|
@ -19,6 +55,26 @@ def test_addContainer(extruder_stack):
|
|||
with pytest.raises(InvalidOperationError):
|
||||
extruder_stack.addContainer(unittest.mock.MagicMock())
|
||||
|
||||
@pytest.mark.parametrize("filename, user_changes_id", [
|
||||
("Left.extruder.cfg", "empty"),
|
||||
("ExtruderLegacy.stack.cfg", "empty"),
|
||||
("OnlyUser.extruder.cfg", "some_instance"),
|
||||
("Complete.extruder.cfg", "some_user_changes")
|
||||
])
|
||||
def test_deserializeUserChanges(filename, user_changes_id, container_registry, extruder_stack):
|
||||
serialized = readStack(filename)
|
||||
|
||||
#Mock the loading of the instance containers.
|
||||
extruder_stack.findContainer = findSomeContainers
|
||||
original_container_registry = UM.Settings.ContainerStack._containerRegistry
|
||||
UM.Settings.ContainerStack._containerRegistry = container_registry #Always has all profiles you ask of.
|
||||
|
||||
extruder_stack.deserialize(serialized)
|
||||
assert extruder_stack.userChanges.getId() == user_changes_id
|
||||
|
||||
#Restore.
|
||||
UM.Settings.ContainerStack._containerRegistry = original_container_registry
|
||||
|
||||
## Tests whether inserting a container is properly forbidden.
|
||||
def test_insertContainer(extruder_stack):
|
||||
with pytest.raises(InvalidOperationError):
|
||||
|
|
13
tests/Settings/stacks/Complete.extruder.cfg
Normal file
13
tests/Settings/stacks/Complete.extruder.cfg
Normal file
|
@ -0,0 +1,13 @@
|
|||
[general]
|
||||
version = 3
|
||||
name = Complete
|
||||
id = Complete
|
||||
|
||||
[containers]
|
||||
0 = some_user_changes
|
||||
1 = some_quality_changes
|
||||
2 = some_quality
|
||||
3 = some_material
|
||||
4 = some_variant
|
||||
5 = some_definition_changes
|
||||
6 = some_definition
|
8
tests/Settings/stacks/OnlyUser.extruder.cfg
Normal file
8
tests/Settings/stacks/OnlyUser.extruder.cfg
Normal file
|
@ -0,0 +1,8 @@
|
|||
[general]
|
||||
version = 3
|
||||
name = Only User
|
||||
id = OnlyUser
|
||||
|
||||
[containers]
|
||||
0 = some_instance
|
||||
6 = some_definition
|
Loading…
Add table
Add a link
Reference in a new issue