TestDefinitionContainer.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import pytest #This module contains automated tests.
  4. import UM.Settings.ContainerRegistry #To create empty instance containers.
  5. import UM.Settings.ContainerStack #To set the container registry the container stacks use.
  6. from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer.
  7. import os
  8. import os.path
  9. import uuid
  10. from UM.Resources import Resources
  11. Resources.addSearchPath(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources")))
  12. machine_filepaths = os.listdir(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions"))
  13. @pytest.fixture
  14. def definition_container():
  15. uid = str(uuid.uuid4())
  16. result = UM.Settings.DefinitionContainer.DefinitionContainer(uid)
  17. assert result.getId() == uid
  18. return result
  19. ## Tests all definition containers
  20. @pytest.mark.parametrize("file_name", machine_filepaths)
  21. def test_validateMachineDefinitionContainer(file_name, definition_container):
  22. if file_name == "fdmprinter.def.json" or file_name == "fdmextruder.def.json":
  23. return # Stop checking, these are root files.
  24. definition_path = os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions")
  25. assert isDefinitionValid(definition_container, definition_path, file_name)
  26. def isDefinitionValid(definition_container, path, file_name):
  27. with open(os.path.join(path, file_name), encoding = "utf-8") as data:
  28. json = data.read()
  29. parser, is_valid = definition_container.readAndValidateSerialized(json)
  30. if not is_valid:
  31. print("The definition '{0}', has invalid data.".format(file_name))
  32. return is_valid