mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-06 14:37:29 -06:00
Add test for read() function
This is complex. I don't want to get into the actual translations from the DoD here. Contributes to issue CURA-5929.
This commit is contained in:
parent
7e87a303cb
commit
7bd55f5064
2 changed files with 75 additions and 1 deletions
|
@ -2,8 +2,16 @@
|
|||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
import configparser # An input for some functions we're testing.
|
||||
import os.path # To find the integration test .ini files.
|
||||
import pytest # To register tests with.
|
||||
import unittest.mock # To mock the application, plug-in and container registry out.
|
||||
|
||||
import UM.Application # To mock the application out.
|
||||
import UM.PluginRegistry # To mock the plug-in registry out.
|
||||
import UM.Settings.ContainerRegistry # To mock the container registry out.
|
||||
import UM.Settings.InstanceContainer # To intercept the serialised data from the read() function.
|
||||
|
||||
import LegacyProfileReader as LegacyProfileReaderModule # To get the directory of the module.
|
||||
from LegacyProfileReader import LegacyProfileReader # The module we're testing.
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -120,4 +128,63 @@ def test_prepareLocalsNoSectionError(legacy_profile_reader, parser_data, default
|
|||
parser.read_dict(parser_data)
|
||||
|
||||
with pytest.raises(configparser.NoSectionError):
|
||||
legacy_profile_reader.prepareLocals(parser, "profile", defaults)
|
||||
legacy_profile_reader.prepareLocals(parser, "profile", defaults)
|
||||
|
||||
intercepted_data = ""
|
||||
|
||||
@pytest.mark.parametrize("file_name", ["normal_case.ini"])
|
||||
def test_read(legacy_profile_reader, file_name):
|
||||
# Mock out all dependencies. Quite a lot!
|
||||
global_stack = unittest.mock.MagicMock()
|
||||
global_stack.getProperty = unittest.mock.MagicMock(return_value = 1) # For machine_extruder_count setting.
|
||||
def getMetaDataEntry(key, default_value = ""):
|
||||
if key == "quality_definition":
|
||||
return "mocked_quality_definition"
|
||||
if key == "has_machine_quality":
|
||||
return "True"
|
||||
global_stack.definition.getMetaDataEntry = getMetaDataEntry
|
||||
global_stack.definition.getId = unittest.mock.MagicMock(return_value = "mocked_global_definition")
|
||||
application = unittest.mock.MagicMock()
|
||||
application.getGlobalContainerStack = unittest.mock.MagicMock(return_value = global_stack)
|
||||
application_getInstance = unittest.mock.MagicMock(return_value = application)
|
||||
container_registry = unittest.mock.MagicMock()
|
||||
container_registry_getInstance = unittest.mock.MagicMock(return_value = container_registry)
|
||||
container_registry.uniqueName = unittest.mock.MagicMock(return_value = "Imported Legacy Profile")
|
||||
container_registry.findDefinitionContainers = unittest.mock.MagicMock(return_value = [global_stack.definition])
|
||||
UM.Settings.InstanceContainer.setContainerRegistry(container_registry)
|
||||
plugin_registry = unittest.mock.MagicMock()
|
||||
plugin_registry_getInstance = unittest.mock.MagicMock(return_value = plugin_registry)
|
||||
plugin_registry.getPluginPath = unittest.mock.MagicMock(return_value = os.path.dirname(LegacyProfileReaderModule.__file__))
|
||||
|
||||
# Mock out the resulting InstanceContainer so that we can intercept the data before it's passed through the version upgrader.
|
||||
def deserialize(self, data): # Intercepts the serialised data that we'd perform the version upgrade from when deserializing.
|
||||
global intercepted_data
|
||||
intercepted_data = data
|
||||
|
||||
parser = configparser.ConfigParser()
|
||||
parser.read_string(data)
|
||||
self._metadata["position"] = parser["metadata"]["position"]
|
||||
def duplicate(self, new_id, new_name):
|
||||
self._metadata["id"] = new_id
|
||||
self._metadata["name"] = new_name
|
||||
return self
|
||||
|
||||
with unittest.mock.patch.object(UM.Application.Application, "getInstance", application_getInstance):
|
||||
with unittest.mock.patch.object(UM.Settings.ContainerRegistry.ContainerRegistry, "getInstance", container_registry_getInstance):
|
||||
with unittest.mock.patch.object(UM.PluginRegistry.PluginRegistry, "getInstance", plugin_registry_getInstance):
|
||||
with unittest.mock.patch.object(UM.Settings.InstanceContainer.InstanceContainer, "deserialize", deserialize):
|
||||
with unittest.mock.patch.object(UM.Settings.InstanceContainer.InstanceContainer, "duplicate", duplicate):
|
||||
result = legacy_profile_reader.read(os.path.join(os.path.dirname(__file__), file_name))
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
# Let's see what's inside the actual output file that we generated.
|
||||
parser = configparser.ConfigParser()
|
||||
parser.read_string(intercepted_data)
|
||||
assert parser["general"]["definition"] == "mocked_quality_definition"
|
||||
assert parser["general"]["version"] == "4" # Yes, before we upgraded.
|
||||
assert parser["general"]["name"] == "Imported Legacy Profile" # Because we overwrote uniqueName.
|
||||
assert parser["metadata"]["type"] == "quality_changes"
|
||||
assert parser["metadata"]["quality_type"] == "normal"
|
||||
assert parser["metadata"]["position"] == "0"
|
||||
assert parser["metadata"]["setting_version"] == "5" # Yes, before we upgraded.
|
7
plugins/LegacyProfileReader/tests/normal_case.ini
Normal file
7
plugins/LegacyProfileReader/tests/normal_case.ini
Normal file
|
@ -0,0 +1,7 @@
|
|||
[profile]
|
||||
foo = bar
|
||||
boo = far
|
||||
fill_overlap = 3
|
||||
|
||||
[alterations]
|
||||
some = values
|
Loading…
Add table
Add a link
Reference in a new issue