TestDefinitionContainer.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import json # To check files for unnecessarily overridden properties.
  4. import os
  5. import os.path
  6. import pytest #This module contains automated tests.
  7. from typing import Any, Dict
  8. import uuid
  9. import UM.Settings.ContainerRegistry #To create empty instance containers.
  10. import UM.Settings.ContainerStack #To set the container registry the container stacks use.
  11. from UM.Settings.DefinitionContainer import DefinitionContainer #To check against the class of DefinitionContainer.
  12. from UM.Resources import Resources
  13. Resources.addSearchPath(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources")))
  14. machine_filepaths = os.listdir(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions"))
  15. all_meshes = os.listdir(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "meshes"))
  16. all_images = os.listdir(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "images"))
  17. @pytest.fixture
  18. def definition_container():
  19. uid = str(uuid.uuid4())
  20. result = UM.Settings.DefinitionContainer.DefinitionContainer(uid)
  21. assert result.getId() == uid
  22. return result
  23. ## Tests all definition containers
  24. @pytest.mark.parametrize("file_name", machine_filepaths)
  25. def test_validateMachineDefinitionContainer(file_name, definition_container):
  26. if file_name == "fdmprinter.def.json" or file_name == "fdmextruder.def.json":
  27. return # Stop checking, these are root files.
  28. definition_path = os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions")
  29. assertIsDefinitionValid(definition_container, definition_path, file_name)
  30. def assertIsDefinitionValid(definition_container, path, file_name):
  31. with open(os.path.join(path, file_name), encoding = "utf-8") as data:
  32. json = data.read()
  33. parser, is_valid = definition_container.readAndValidateSerialized(json)
  34. assert is_valid #The definition has invalid JSON structure.
  35. metadata = DefinitionContainer.deserializeMetadata(json, "whatever")
  36. # If the definition defines a platform file, it should be in /resources/meshes/
  37. if "platform" in metadata[0]:
  38. assert metadata[0]["platform"] in all_meshes
  39. if "platform_texture" in metadata[0]:
  40. assert metadata[0]["platform_texture"] in all_images
  41. ## Tests whether setting values are not being hidden by parent containers.
  42. #
  43. # When a definition container defines a "default_value" but inherits from a
  44. # definition that defines a "value", the "default_value" is ineffective. This
  45. # test fails on those things.
  46. @pytest.mark.parametrize("file_name", machine_filepaths)
  47. def test_validateOverridingDefaultValue(file_name):
  48. definition_path = os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions", file_name)
  49. with open(definition_path, encoding = "utf-8") as f:
  50. doc = json.load(f)
  51. if "inherits" not in doc:
  52. return # We only want to check for documents where the inheritance overrides the children. If there's no inheritance, this can't happen so it's fine.
  53. if "overrides" not in doc:
  54. return # No settings are being overridden. No need to check anything.
  55. parent_settings = getInheritedSettings(doc["inherits"])
  56. for key, val in doc["overrides"].items():
  57. if "value" in parent_settings[key]:
  58. assert "default_value" not in val, "Unnecessary default_value in {file_name}".format(file_name = file_name) # If there is a value in the parent settings, then the default_value is not effective.
  59. def getInheritedSettings(definition_id: str) -> Dict[str, Any]:
  60. definition_path = os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions", definition_id + ".def.json")
  61. with open(definition_path, encoding = "utf-8") as f:
  62. doc = json.load(f)
  63. result = {}
  64. if "inherits" in doc: # Recursive inheritance.
  65. result.update(getInheritedSettings(doc["inherits"]))
  66. if "settings" in doc:
  67. result.update(flattenSettings(doc["settings"]))
  68. if "overrides" in doc:
  69. result = merge_dicts(result, doc["overrides"])
  70. return result
  71. def flattenSettings(settings) -> Dict[str, Any]:
  72. result = {}
  73. for entry, contents in settings.items():
  74. if "children" in contents:
  75. result.update(flattenSettings(contents["children"]))
  76. del contents["children"]
  77. result[entry] = contents
  78. return result
  79. def merge_dicts(base: Dict[str, Any], overrides: Dict[str, Any]) -> Dict[str, Any]:
  80. result = {}
  81. result.update(base)
  82. for key, val in overrides.items():
  83. if key not in result:
  84. result[key] = val
  85. continue
  86. if isinstance(result[key], dict) and isinstance(val, dict):
  87. result[key] = merge_dicts(result[key], val)
  88. else:
  89. result[key] = val
  90. return result