Add test for global stack getting user changes

Tests fail right now because running the test actually deletes files. Got to fix that.

Contributes to issue CURA-3497.
This commit is contained in:
Ghostkeeper 2017-03-22 15:40:13 +01:00
parent c5768d89dc
commit 49fad35d28
No known key found for this signature in database
GPG key ID: C5F96EE2BC0F7E75
7 changed files with 50 additions and 7 deletions

View file

@ -11,6 +11,7 @@ from cura.Settings.GlobalStack import GlobalStack #Testing for returning the cor
from UM.Resources import Resources #Mocking some functions of this.
import UM.Settings.ContainerRegistry #Making empty container stacks.
import UM.Settings.ContainerStack #Setting the container registry here properly.
from UM.Settings.DefinitionContainer import DefinitionContainer #Checking against the DefinitionContainer class.
## Gives a fresh CuraContainerRegistry instance.
@pytest.fixture()
@ -30,12 +31,16 @@ def test_loadTypes(filename, output_class, container_registry):
UM.Settings.ContainerStack.setContainerRegistry(container_registry)
Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
def findContainers(id, container_type = 0):
if id == "empty_material":
if id == "some_material" or id == "some_definition":
return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
else:
return []
container_registry.findContainers = findContainers
mock_definition = unittest.mock.MagicMock()
def findContainer(container_id = "*", container_type = None, type = "*", category = None):
return unittest.mock.MagicMock()
with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer", findContainer):
container_registry.load()
#Check whether the resulting type was correct.

View file

@ -0,0 +1,33 @@
# Copyright (c) 2017 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.
import os.path #To find the test files.
import pytest #This module contains unit tests.
import unittest.mock #To monkeypatch some mocks in place of dependencies.
from cura.Settings.GlobalStack import GlobalStack #The module we're testing.
from UM.Settings.DefinitionContainer import DefinitionContainer #To test against the class DefinitionContainer.
import UM.Settings.ContainerRegistry
## Tests whether the user changes are being read properly from a global stack.
@pytest.mark.parametrize("filename, user_changes_id", [
("Global.global.cfg", "empty"),
("Global.stack.cfg", "empty"),
("MachineLegacy.stack.cfg", "empty")
])
def test_deserializeUserChanges(filename, user_changes_id):
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)) as file_handle:
serialized = file_handle.read()
stack = GlobalStack("TestStack")
#Mock the loading of the instances.
def findContainer(container_id = "*", container_type = None, type = None, category = "*"):
if id == "some_material":
return UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)
if container_type == DefinitionContainer:
return unittest.mock.MagicMock()
stack.findContainer = findContainer
stack.deserialize(serialized)
assert stack.userChanges.getId() == user_changes_id

View file

@ -7,4 +7,5 @@ id = ExtruderLegacy
type = extruder
[containers]
0 = empty_material
3 = some_material
6 = some_definition

View file

@ -4,4 +4,5 @@ name = Global
id = Global
[containers]
0 = empty_material
3 = some_material
6 = some_definition

View file

@ -4,4 +4,5 @@ name = Global
id = Global
[containers]
0 = empty_material
3 = some_material
6 = some_definition

View file

@ -4,4 +4,5 @@ name = Left
id = Left
[containers]
0 = empty_material
3 = some_material
6 = some_definition

View file

@ -7,4 +7,5 @@ id = MachineLegacy
type = machine
[containers]
0 = empty_material
3 = some_material
6 = some_definition