TestCuraContainerRegistry.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os #To find the directory with test files and find the test files.
  4. import pytest #This module contains unit tests.
  5. import unittest.mock #To mock and monkeypatch stuff.
  6. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry #The class we're testing.
  7. from cura.Settings.ExtruderStack import ExtruderStack #Testing for returning the correct types of stacks.
  8. from cura.Settings.GlobalStack import GlobalStack #Testing for returning the correct types of stacks.
  9. from UM.Resources import Resources #Mocking some functions of this.
  10. import UM.Settings.ContainerRegistry #Making empty container stacks.
  11. import UM.Settings.ContainerStack #Setting the container registry here properly.
  12. from UM.Settings.DefinitionContainer import DefinitionContainer #Checking against the DefinitionContainer class.
  13. ## Gives a fresh CuraContainerRegistry instance.
  14. @pytest.fixture()
  15. def container_registry():
  16. return CuraContainerRegistry()
  17. ## Tests whether loading gives objects of the correct type.
  18. @pytest.mark.parametrize("filename, output_class", [
  19. ("ExtruderLegacy.stack.cfg", ExtruderStack),
  20. ("MachineLegacy.stack.cfg", GlobalStack),
  21. ("Left.extruder.cfg", ExtruderStack),
  22. ("Global.global.cfg", GlobalStack),
  23. ("Global.stack.cfg", GlobalStack)
  24. ])
  25. def test_loadTypes(filename, output_class, container_registry):
  26. #Mock some dependencies.
  27. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  28. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
  29. def findContainers(id, container_type = 0):
  30. if id == "some_material" or id == "some_definition":
  31. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  32. else:
  33. return []
  34. container_registry.findContainers = findContainers
  35. mock_definition = unittest.mock.MagicMock()
  36. def findContainer(container_id = "*", container_type = None, type = "*", category = None):
  37. return unittest.mock.MagicMock()
  38. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer", findContainer):
  39. with unittest.mock.patch("os.remove"):
  40. container_registry.load()
  41. #Check whether the resulting type was correct.
  42. stack_id = filename.split(".")[0]
  43. for container in container_registry._containers: #Stupid ContainerRegistry class doesn't expose any way of getting at this except by prodding the privates.
  44. if container.getId() == stack_id: #This is the one we're testing.
  45. assert type(container) == output_class
  46. break
  47. else:
  48. assert False #Container stack with specified ID was not loaded.