TestCuraContainerRegistry.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 shutil #To copy files to make a temporary file.
  6. import unittest.mock #To mock and monkeypatch stuff.
  7. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry #The class we're testing.
  8. from cura.Settings.ExtruderStack import ExtruderStack #Testing for returning the correct types of stacks.
  9. from cura.Settings.GlobalStack import GlobalStack #Testing for returning the correct types of stacks.
  10. from UM.Resources import Resources #Mocking some functions of this.
  11. import UM.Settings.ContainerRegistry #Making empty container stacks.
  12. import UM.Settings.ContainerStack #Setting the container registry here properly.
  13. ## Gives a fresh CuraContainerRegistry instance.
  14. @pytest.fixture()
  15. def container_registry():
  16. return CuraContainerRegistry()
  17. def teardown():
  18. #If the temporary file for the legacy file rename test still exists, remove it.
  19. temporary_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
  20. if os.path.isfile(temporary_file):
  21. os.remove(temporary_file)
  22. ## Tests whether loading gives objects of the correct type.
  23. @pytest.mark.parametrize("filename, output_class", [
  24. ("ExtruderLegacy.stack.cfg", ExtruderStack),
  25. ("MachineLegacy.stack.cfg", GlobalStack),
  26. ("Left.extruder.cfg", ExtruderStack),
  27. ("Global.global.cfg", GlobalStack),
  28. ("Global.stack.cfg", GlobalStack)
  29. ])
  30. def test_loadTypes(filename, output_class, container_registry):
  31. #Mock some dependencies.
  32. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  33. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", filename)]) #Return just this tested file.
  34. def findContainers(id, container_type = 0):
  35. if id == "some_instance" or id == "some_definition":
  36. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  37. else:
  38. return []
  39. container_registry.findContainers = findContainers
  40. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  41. with unittest.mock.patch("os.remove"):
  42. container_registry.load()
  43. #Check whether the resulting type was correct.
  44. stack_id = filename.split(".")[0]
  45. for container in container_registry._containers: #Stupid ContainerRegistry class doesn't expose any way of getting at this except by prodding the privates.
  46. if container.getId() == stack_id: #This is the one we're testing.
  47. assert type(container) == output_class
  48. break
  49. else:
  50. assert False #Container stack with specified ID was not loaded.
  51. ## Tests whether loading a legacy file moves the upgraded file properly.
  52. def test_loadLegacyFileRenamed(container_registry):
  53. #Create a temporary file for the registry to load.
  54. temp_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
  55. temp_file_source = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "MachineLegacy.stack.cfg")
  56. shutil.copyfile(temp_file_source, temp_file)
  57. #Mock some dependencies.
  58. UM.Settings.ContainerStack.setContainerRegistry(container_registry)
  59. Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [temp_file]) #Return a temporary file that we'll make for this test.
  60. def findContainers(id, container_type = 0):
  61. return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
  62. container_registry.findContainers = findContainers
  63. with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
  64. container_registry.load()
  65. assert not os.path.isfile(temp_file)
  66. new_filename = os.path.splitext(os.path.splitext(temp_file)[0])[0] + ".global.cfg"
  67. assert os.path.isfile(new_filename)