TestVersionUpgrade25to26.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. import sys
  5. sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
  6. import configparser #To check whether the appropriate exceptions are raised.
  7. import pytest #To register tests with.
  8. import VersionUpgrade25to26 #The module we're testing.
  9. ## Creates an instance of the upgrader to test with.
  10. @pytest.fixture
  11. def upgrader():
  12. return VersionUpgrade25to26.VersionUpgrade25to26()
  13. test_cfg_version_good_data = [
  14. {
  15. "test_name": "Simple",
  16. "file_data": """[general]
  17. version = 1
  18. """,
  19. "version": 1000000
  20. },
  21. {
  22. "test_name": "Other Data Around",
  23. "file_data": """[nonsense]
  24. life = good
  25. [general]
  26. version = 3
  27. [values]
  28. layer_height = 0.12
  29. infill_sparse_density = 42
  30. """,
  31. "version": 3000000
  32. },
  33. {
  34. "test_name": "Negative Version", #Why not?
  35. "file_data": """[general]
  36. version = -20
  37. """,
  38. "version": -20000000
  39. },
  40. {
  41. "test_name": "Setting Version",
  42. "file_data": """[general]
  43. version = 1
  44. [metadata]
  45. setting_version = 1
  46. """,
  47. "version": 1000001
  48. },
  49. {
  50. "test_name": "Negative Setting Version",
  51. "file_data": """[general]
  52. version = 1
  53. [metadata]
  54. setting_version = -3
  55. """,
  56. "version": 999997
  57. }
  58. ]
  59. test_upgrade_preferences_removed_settings_data = [
  60. {
  61. "test_name": "Removed Setting",
  62. "file_data": """[general]
  63. visible_settings = baby;you;know;how;I;like;to;start_layers_at_same_position
  64. """,
  65. },
  66. {
  67. "test_name": "No Removed Setting",
  68. "file_data": """[general]
  69. visible_settings = baby;you;now;how;I;like;to;eat;chocolate;muffins
  70. """
  71. },
  72. {
  73. "test_name": "No Visible Settings Key",
  74. "file_data": """[general]
  75. cura = cool
  76. """
  77. },
  78. {
  79. "test_name": "No General Category",
  80. "file_data": """[foos]
  81. foo = bar
  82. """
  83. }
  84. ]
  85. ## Tests whether the settings that should be removed are removed for the 2.6
  86. # version of preferences.
  87. @pytest.mark.parametrize("data", test_upgrade_preferences_removed_settings_data)
  88. def test_upgradePreferencesRemovedSettings(data, upgrader):
  89. #Get the settings from the original file.
  90. original_parser = configparser.ConfigParser(interpolation = None)
  91. original_parser.read_string(data["file_data"])
  92. settings = set()
  93. if original_parser.has_section("general") and "visible_settings" in original_parser["general"]:
  94. settings = set(original_parser["general"]["visible_settings"].split(";"))
  95. #Perform the upgrade.
  96. _, upgraded_preferences = upgrader.upgradePreferences(data["file_data"], "<string>")
  97. upgraded_preferences = upgraded_preferences[0]
  98. #Find whether the removed setting is removed from the file now.
  99. settings -= VersionUpgrade25to26._removed_settings
  100. parser = configparser.ConfigParser(interpolation = None)
  101. parser.read_string(upgraded_preferences)
  102. assert (parser.has_section("general") and "visible_settings" in parser["general"]) == (len(settings) > 0) #If there are settings, there must also be a preference.
  103. if settings:
  104. assert settings == set(parser["general"]["visible_settings"].split(";"))
  105. test_upgrade_instance_container_removed_settings_data = [
  106. {
  107. "test_name": "Removed Setting",
  108. "file_data": """[values]
  109. layer_height = 0.1337
  110. start_layers_at_same_position = True
  111. """
  112. },
  113. {
  114. "test_name": "No Removed Setting",
  115. "file_data": """[values]
  116. oceans_number = 11
  117. """
  118. },
  119. {
  120. "test_name": "No Values Category",
  121. "file_data": """[general]
  122. type = instance_container
  123. """
  124. }
  125. ]
  126. ## Tests whether the settings that should be removed are removed for the 2.6
  127. # version of instance containers.
  128. @pytest.mark.parametrize("data", test_upgrade_instance_container_removed_settings_data)
  129. def test_upgradeInstanceContainerRemovedSettings(data, upgrader):
  130. #Get the settings from the original file.
  131. original_parser = configparser.ConfigParser(interpolation = None)
  132. original_parser.read_string(data["file_data"])
  133. settings = set()
  134. if original_parser.has_section("values"):
  135. settings = set(original_parser["values"])
  136. #Perform the upgrade.
  137. _, upgraded_container = upgrader.upgradeInstanceContainer(data["file_data"], "<string>")
  138. upgraded_container = upgraded_container[0]
  139. #Find whether the forbidden setting is still in the container.
  140. settings -= VersionUpgrade25to26._removed_settings
  141. parser = configparser.ConfigParser(interpolation = None)
  142. parser.read_string(upgraded_container)
  143. assert parser.has_section("values") == (len(settings) > 0) #If there are settings, there must also be the values category.
  144. if settings:
  145. assert settings == set(parser["values"])