TestProfiles.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest.mock import MagicMock
  4. import configparser # To read the profiles.
  5. import os
  6. import os.path
  7. import pytest
  8. from cura.CuraApplication import CuraApplication # To compare against the current SettingVersion.
  9. from UM.Settings.DefinitionContainer import DefinitionContainer
  10. from UM.Settings.InstanceContainer import InstanceContainer
  11. from UM.VersionUpgradeManager import VersionUpgradeManager
  12. def collectAllQualities():
  13. result = []
  14. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "quality"))):
  15. for filename in filenames:
  16. result.append(os.path.join(root, filename))
  17. return result
  18. def collecAllDefinitionIds():
  19. result = []
  20. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions"))):
  21. for filename in filenames:
  22. result.append(os.path.basename(filename).split(".")[0])
  23. return result
  24. def collectAllSettingIds():
  25. VersionUpgradeManager._VersionUpgradeManager__instance = VersionUpgradeManager(MagicMock())
  26. CuraApplication._initializeSettingDefinitions()
  27. definition_container = DefinitionContainer("whatever")
  28. with open(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions", "fdmprinter.def.json"), encoding = "utf-8") as data:
  29. definition_container.deserialize(data.read())
  30. return definition_container.getAllKeys()
  31. def collectAllVariants():
  32. result = []
  33. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "variants"))):
  34. for filename in filenames:
  35. result.append(os.path.join(root, filename))
  36. return result
  37. def collectAllIntents():
  38. result = []
  39. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "intent"))):
  40. for filename in filenames:
  41. result.append(os.path.join(root, filename))
  42. return result
  43. all_definition_ids = collecAllDefinitionIds()
  44. quality_filepaths = collectAllQualities()
  45. all_setting_ids = collectAllSettingIds()
  46. variant_filepaths = collectAllVariants()
  47. intent_filepaths = collectAllIntents()
  48. @pytest.mark.parametrize("file_name", quality_filepaths)
  49. def test_validateQualityProfiles(file_name):
  50. """Attempt to load all the quality profiles."""
  51. try:
  52. with open(file_name, encoding = "utf-8") as data:
  53. serialized = data.read()
  54. result = InstanceContainer._readAndValidateSerialized(serialized)
  55. # Fairly obvious, but all the types here should be of the type quality
  56. assert InstanceContainer.getConfigurationTypeFromSerialized(serialized) == "quality"
  57. # All quality profiles must be linked to an existing definition.
  58. assert result["general"]["definition"] in all_definition_ids
  59. # We don't care what the value is, as long as it's there.
  60. assert result["metadata"].get("quality_type", None) is not None
  61. # Check that all the values that we say something about are known.
  62. if "values" in result:
  63. quality_setting_keys = set(result["values"])
  64. # Prune all the comments from the values
  65. quality_setting_keys = {key for key in quality_setting_keys if not key.startswith("#")}
  66. has_unknown_settings = not quality_setting_keys.issubset(all_setting_ids)
  67. if has_unknown_settings:
  68. print("The following setting(s) %s are defined in the quality %s, but not in fdmprinter.def.json" % ([key for key in quality_setting_keys if key not in all_setting_ids], file_name))
  69. assert False
  70. except Exception as e:
  71. # File can't be read, header sections missing, whatever the case, this shouldn't happen!
  72. print("Got an Exception while reading the file [%s]: %s" % (file_name, e))
  73. assert False
  74. @pytest.mark.parametrize("file_name", intent_filepaths)
  75. def test_validateIntentProfiles(file_name):
  76. try:
  77. with open(file_name, encoding = "utf-8") as f:
  78. serialized = f.read()
  79. result = InstanceContainer._readAndValidateSerialized(serialized)
  80. assert InstanceContainer.getConfigurationTypeFromSerialized(serialized) == "intent", "The intent folder must only contain intent profiles."
  81. assert result["general"]["definition"] in all_definition_ids, "The definition for this intent profile must exist."
  82. assert result["metadata"].get("intent_category", None) is not None, "All intent profiles must have some intent category."
  83. assert result["metadata"].get("quality_type", None) is not None, "All intent profiles must be linked to some quality type."
  84. assert result["metadata"].get("material", None) is not None, "All intent profiles must be linked to some material."
  85. assert result["metadata"].get("variant", None) is not None, "All intent profiles must be linked to some variant."
  86. # Check that all the values that we say something about are known.
  87. if "values" in result:
  88. intent_setting_keys = set(result["values"])
  89. unknown_settings = intent_setting_keys - all_setting_ids
  90. assert len(unknown_settings) == 0, "The settings {setting_list} are defined in the intent {file_name}, but not in fdmprinter.def.json".format(setting_list = unknown_settings, file_name = file_name)
  91. except Exception as e:
  92. # File can't be read, header sections missing, whatever the case, this shouldn't happen!
  93. assert False, "Got an exception while reading the file {file_name}: {err}".format(file_name = file_name, err = str(e))
  94. @pytest.mark.parametrize("file_name", variant_filepaths)
  95. def test_validateVariantProfiles(file_name):
  96. """Attempt to load all the variant profiles."""
  97. try:
  98. with open(file_name, encoding = "utf-8") as data:
  99. serialized = data.read()
  100. result = InstanceContainer._readAndValidateSerialized(serialized)
  101. # Fairly obvious, but all the types here should be of the type quality
  102. assert InstanceContainer.getConfigurationTypeFromSerialized(serialized) == "variant"
  103. # All quality profiles must be linked to an existing definition.
  104. assert result["general"]["definition"] in all_definition_ids
  105. # Check that all the values that we say something about are known.
  106. if "values" in result:
  107. variant_setting_keys = set(result["values"])
  108. # Prune all the comments from the values
  109. variant_setting_keys = {key for key in variant_setting_keys if not key.startswith("#")}
  110. has_unknown_settings = not variant_setting_keys.issubset(all_setting_ids)
  111. if has_unknown_settings:
  112. print("The following setting(s) %s are defined in the variant %s, but not in fdmprinter.def.json" % ([key for key in variant_setting_keys if key not in all_setting_ids], file_name))
  113. assert False
  114. except Exception as e:
  115. # File can't be read, header sections missing, whatever the case, this shouldn't happen!
  116. print("Got an Exception while reading the file [%s]: %s" % (file_name, e))
  117. assert False
  118. @pytest.mark.parametrize("file_name", quality_filepaths + variant_filepaths + intent_filepaths)
  119. def test_versionUpToDate(file_name):
  120. try:
  121. with open(file_name, encoding = "utf-8") as data:
  122. parser = configparser.ConfigParser(interpolation = None)
  123. parser.read(file_name)
  124. assert "general" in parser
  125. assert "version" in parser["general"]
  126. assert int(parser["general"]["version"]) == InstanceContainer.Version
  127. assert "metadata" in parser
  128. assert "setting_version" in parser["metadata"]
  129. assert int(parser["metadata"]["setting_version"]) == CuraApplication.SettingVersion
  130. except Exception as e:
  131. # File can't be read, header sections missing, whatever the case, this shouldn't happen!
  132. print("Got an exception while reading the file {file_name}: {err}".format(file_name = file_name, err = str(e)))
  133. assert False