TestDefinitionContainer.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. all_meshes = os.listdir(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "meshes"))
  14. all_images = os.listdir(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "images"))
  15. @pytest.fixture
  16. def definition_container():
  17. uid = str(uuid.uuid4())
  18. result = UM.Settings.DefinitionContainer.DefinitionContainer(uid)
  19. assert result.getId() == uid
  20. return result
  21. ## Tests all definition containers
  22. @pytest.mark.parametrize("file_name", machine_filepaths)
  23. def test_validateMachineDefinitionContainer(file_name, definition_container):
  24. if file_name == "fdmprinter.def.json" or file_name == "fdmextruder.def.json":
  25. return # Stop checking, these are root files.
  26. definition_path = os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions")
  27. assertIsDefinitionValid(definition_container, definition_path, file_name)
  28. def assertIsDefinitionValid(definition_container, path, file_name):
  29. with open(os.path.join(path, file_name), encoding = "utf-8") as data:
  30. json = data.read()
  31. parser, is_valid = definition_container.readAndValidateSerialized(json)
  32. assert is_valid #The definition has invalid JSON structure.
  33. metadata = DefinitionContainer.deserializeMetadata(json, "whatever")
  34. # If the definition defines a platform file, it should be in /resources/meshes/
  35. if "platform" in metadata[0]:
  36. assert metadata[0]["platform"] in all_meshes
  37. if "platform_texture" in metadata[0]:
  38. assert metadata[0]["platform_texture"] in all_images