TestVersionUpgrade25to26.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To check whether the appropriate exceptions are raised.
  4. import pytest #To register tests with.
  5. import VersionUpgrade25to26 #The module we're testing.
  6. ## Creates an instance of the upgrader to test with.
  7. @pytest.fixture
  8. def upgrader():
  9. return VersionUpgrade25to26.VersionUpgrade25to26()
  10. test_cfg_version_good_data = [
  11. {
  12. "test_name": "Simple",
  13. "file_data": """[general]
  14. version = 1
  15. """,
  16. "version": 1000000
  17. },
  18. {
  19. "test_name": "Other Data Around",
  20. "file_data": """[nonsense]
  21. life = good
  22. [general]
  23. version = 3
  24. [values]
  25. layer_height = 0.12
  26. infill_sparse_density = 42
  27. """,
  28. "version": 3000000
  29. },
  30. {
  31. "test_name": "Negative Version", #Why not?
  32. "file_data": """[general]
  33. version = -20
  34. """,
  35. "version": -20000000
  36. },
  37. {
  38. "test_name": "Setting Version",
  39. "file_data": """[general]
  40. version = 1
  41. [metadata]
  42. setting_version = 1
  43. """,
  44. "version": 1000001
  45. },
  46. {
  47. "test_name": "Negative Setting Version",
  48. "file_data": """[general]
  49. version = 1
  50. [metadata]
  51. setting_version = -3
  52. """,
  53. "version": 999997
  54. }
  55. ]
  56. ## Tests the technique that gets the version number from CFG files.
  57. #
  58. # \param data The parametrised data to test with. It contains a test name
  59. # to debug with, the serialised contents of a CFG file and the correct
  60. # version number in that CFG file.
  61. # \param upgrader The instance of the upgrade class to test.
  62. @pytest.mark.parametrize("data", test_cfg_version_good_data)
  63. def test_cfgVersionGood(data, upgrader):
  64. version = upgrader.getCfgVersion(data["file_data"])
  65. assert version == data["version"]
  66. test_cfg_version_bad_data = [
  67. {
  68. "test_name": "Empty",
  69. "file_data": "",
  70. "exception": configparser.Error #Explicitly not specified further which specific error we're getting, because that depends on the implementation of configparser.
  71. },
  72. {
  73. "test_name": "No General",
  74. "file_data": """[values]
  75. layer_height = 0.1337
  76. """,
  77. "exception": configparser.Error
  78. },
  79. {
  80. "test_name": "No Version",
  81. "file_data": """[general]
  82. true = false
  83. """,
  84. "exception": configparser.Error
  85. },
  86. {
  87. "test_name": "Not a Number",
  88. "file_data": """[general]
  89. version = not-a-text-version-number
  90. """,
  91. "exception": ValueError
  92. },
  93. {
  94. "test_name": "Setting Value NaN",
  95. "file_data": """[general]
  96. version = 4
  97. [metadata]
  98. setting_version = latest_or_something
  99. """,
  100. "exception": ValueError
  101. },
  102. {
  103. "test_name": "Major-Minor",
  104. "file_data": """[general]
  105. version = 1.2
  106. """,
  107. "exception": ValueError
  108. }
  109. ]
  110. ## Tests whether getting a version number from bad CFG files gives an
  111. # exception.
  112. #
  113. # \param data The parametrised data to test with. It contains a test name
  114. # to debug with, the serialised contents of a CFG file and the class of
  115. # exception it needs to throw.
  116. # \param upgrader The instance of the upgrader to test.
  117. @pytest.mark.parametrize("data", test_cfg_version_bad_data)
  118. def test_cfgVersionBad(data, upgrader):
  119. with pytest.raises(data["exception"]):
  120. upgrader.getCfgVersion(data["file_data"])
  121. test_upgrade_preferences_removed_settings_data = [
  122. {
  123. "test_name": "Removed Setting",
  124. "file_data": """[general]
  125. visible_settings = baby;you;know;how;I;like;to;start_layers_at_same_position
  126. """,
  127. },
  128. {
  129. "test_name": "No Removed Setting",
  130. "file_data": """[general]
  131. visible_settings = baby;you;now;how;I;like;to;eat;chocolate;muffins
  132. """
  133. },
  134. {
  135. "test_name": "No Visible Settings Key",
  136. "file_data": """[general]
  137. cura = cool
  138. """
  139. },
  140. {
  141. "test_name": "No General Category",
  142. "file_data": """[foos]
  143. foo = bar
  144. """
  145. }
  146. ]
  147. ## Tests whether the settings that should be removed are removed for the 2.6
  148. # version of preferences.
  149. @pytest.mark.parametrize("data", test_upgrade_preferences_removed_settings_data)
  150. def test_upgradePreferencesRemovedSettings(data, upgrader):
  151. #Get the settings from the original file.
  152. original_parser = configparser.ConfigParser(interpolation = None)
  153. original_parser.read_string(data["file_data"])
  154. settings = set()
  155. if original_parser.has_section("general") and "visible_settings" in original_parser["general"]:
  156. settings = set(original_parser["general"]["visible_settings"].split(";"))
  157. #Perform the upgrade.
  158. _, upgraded_preferences = upgrader.upgradePreferences(data["file_data"], "<string>")
  159. upgraded_preferences = upgraded_preferences[0]
  160. #Find whether the removed setting is removed from the file now.
  161. settings -= VersionUpgrade25to26._removed_settings
  162. parser = configparser.ConfigParser(interpolation = None)
  163. parser.read_string(upgraded_preferences)
  164. assert (parser.has_section("general") and "visible_settings" in parser["general"]) == (len(settings) > 0) #If there are settings, there must also be a preference.
  165. if settings:
  166. assert settings == set(parser["general"]["visible_settings"].split(";"))
  167. test_upgrade_instance_container_removed_settings_data = [
  168. {
  169. "test_name": "Removed Setting",
  170. "file_data": """[values]
  171. layer_height = 0.1337
  172. start_layers_at_same_position = True
  173. """
  174. },
  175. {
  176. "test_name": "No Removed Setting",
  177. "file_data": """[values]
  178. oceans_number = 11
  179. """
  180. },
  181. {
  182. "test_name": "No Values Category",
  183. "file_data": """[general]
  184. type = instance_container
  185. """
  186. }
  187. ]
  188. ## Tests whether the settings that should be removed are removed for the 2.6
  189. # version of instance containers.
  190. @pytest.mark.parametrize("data", test_upgrade_instance_container_removed_settings_data)
  191. def test_upgradeInstanceContainerRemovedSettings(data, upgrader):
  192. #Get the settings from the original file.
  193. original_parser = configparser.ConfigParser(interpolation = None)
  194. original_parser.read_string(data["file_data"])
  195. settings = set()
  196. if original_parser.has_section("values"):
  197. settings = set(original_parser["values"])
  198. #Perform the upgrade.
  199. _, upgraded_container = upgrader.upgradeInstanceContainer(data["file_data"], "<string>")
  200. upgraded_container = upgraded_container[0]
  201. #Find whether the forbidden setting is still in the container.
  202. settings -= VersionUpgrade25to26._removed_settings
  203. parser = configparser.ConfigParser(interpolation = None)
  204. parser.read_string(upgraded_container)
  205. assert parser.has_section("values") == (len(settings) > 0) #If there are settings, there must also be the values category.
  206. if settings:
  207. assert settings == set(parser["values"])