TestProfiles.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest.mock import MagicMock
  4. import pytest
  5. from UM.Settings.DefinitionContainer import DefinitionContainer
  6. from UM.Settings.InstanceContainer import InstanceContainer
  7. import os
  8. import os.path
  9. from UM.VersionUpgradeManager import VersionUpgradeManager
  10. from cura.CuraApplication import CuraApplication
  11. def collectAllQualities():
  12. result = []
  13. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "quality"))):
  14. for filename in filenames:
  15. result.append(os.path.join(root, filename))
  16. return result
  17. def collecAllDefinitionIds():
  18. result = []
  19. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions"))):
  20. for filename in filenames:
  21. result.append(os.path.basename(filename).split(".")[0])
  22. return result
  23. def collectAllSettingIds():
  24. VersionUpgradeManager._VersionUpgradeManager__instance = VersionUpgradeManager(MagicMock())
  25. CuraApplication._initializeSettingDefinitions()
  26. definition_container = DefinitionContainer("whatever")
  27. with open(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "definitions", "fdmprinter.def.json"), encoding="utf-8") as data:
  28. definition_container.deserialize(data.read())
  29. return definition_container.getAllKeys()
  30. def collectAllVariants():
  31. result = []
  32. for root, directories, filenames in os.walk(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources", "variants"))):
  33. for filename in filenames:
  34. result.append(os.path.join(root, filename))
  35. return result
  36. all_definition_ids = collecAllDefinitionIds()
  37. quality_filepaths = collectAllQualities()
  38. all_setting_ids = collectAllSettingIds()
  39. variant_filepaths = collectAllVariants()
  40. ## Atempt to load all the quality types
  41. @pytest.mark.parametrize("file_name", quality_filepaths)
  42. def test_validateQualityProfiles(file_name):
  43. try:
  44. with open(file_name, encoding="utf-8") as data:
  45. serialized = data.read()
  46. result = InstanceContainer._readAndValidateSerialized(serialized)
  47. # Fairly obvious, but all the types here should be of the type quality
  48. assert InstanceContainer.getConfigurationTypeFromSerialized(serialized) == "quality"
  49. # All quality profiles must be linked to an existing definition.
  50. assert result["general"]["definition"] in all_definition_ids
  51. # We don't care what the value is, as long as it's there.
  52. assert result["metadata"].get("quality_type", None) is not None
  53. # Check that all the values that we say something about are known.
  54. if "values" in result:
  55. quality_setting_keys = set(result["values"])
  56. # Prune all the comments from the values
  57. quality_setting_keys = {key for key in quality_setting_keys if not key.startswith("#")}
  58. has_unknown_settings = not quality_setting_keys.issubset(all_setting_ids)
  59. if has_unknown_settings:
  60. 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))
  61. assert False
  62. except Exception as e:
  63. # File can't be read, header sections missing, whatever the case, this shouldn't happen!
  64. print("Got an Exception while reading he file [%s]: %s" % (file_name, e))
  65. assert False
  66. ## Attempt to load all the quality types
  67. @pytest.mark.parametrize("file_name", variant_filepaths)
  68. def test_validateVariantProfiles(file_name):
  69. try:
  70. with open(file_name, encoding="utf-8") as data:
  71. serialized = data.read()
  72. result = InstanceContainer._readAndValidateSerialized(serialized)
  73. # Fairly obvious, but all the types here should be of the type quality
  74. assert InstanceContainer.getConfigurationTypeFromSerialized(serialized) == "variant"
  75. # All quality profiles must be linked to an existing definition.
  76. assert result["general"]["definition"] in all_definition_ids
  77. # Check that all the values that we say something about are known.
  78. if "values" in result:
  79. variant_setting_keys = set(result["values"])
  80. # Prune all the comments from the values
  81. variant_setting_keys = {key for key in variant_setting_keys if not key.startswith("#")}
  82. has_unknown_settings = not variant_setting_keys.issubset(all_setting_ids)
  83. if has_unknown_settings:
  84. 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))
  85. assert False
  86. except Exception as e:
  87. # File can't be read, header sections missing, whatever the case, this shouldn't happen!
  88. print("Got an Exception while reading he file [%s]: %s" % (file_name, e))
  89. assert False