TestVersionUpgrade27to30.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 parse the resulting config files.
  7. import pytest #To register tests with.
  8. import VersionUpgrade27to30 #The module we're testing.
  9. ## Creates an instance of the upgrader to test with.
  10. @pytest.fixture
  11. def upgrader():
  12. return VersionUpgrade27to30.VersionUpgrade27to30()
  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. ## Tests the technique that gets the version number from CFG files.
  60. #
  61. # \param data The parametrised data to test with. It contains a test name
  62. # to debug with, the serialised contents of a CFG file and the correct
  63. # version number in that CFG file.
  64. # \param upgrader The instance of the upgrade class to test.
  65. @pytest.mark.parametrize("data", test_cfg_version_good_data)
  66. def test_cfgVersionGood(data, upgrader):
  67. version = upgrader.getCfgVersion(data["file_data"])
  68. assert version == data["version"]
  69. test_cfg_version_bad_data = [
  70. {
  71. "test_name": "Empty",
  72. "file_data": "",
  73. "exception": configparser.Error #Explicitly not specified further which specific error we're getting, because that depends on the implementation of configparser.
  74. },
  75. {
  76. "test_name": "No General",
  77. "file_data": """[values]
  78. layer_height = 0.1337
  79. """,
  80. "exception": configparser.Error
  81. },
  82. {
  83. "test_name": "No Version",
  84. "file_data": """[general]
  85. true = false
  86. """,
  87. "exception": configparser.Error
  88. },
  89. {
  90. "test_name": "Not a Number",
  91. "file_data": """[general]
  92. version = not-a-text-version-number
  93. """,
  94. "exception": ValueError
  95. },
  96. {
  97. "test_name": "Setting Value NaN",
  98. "file_data": """[general]
  99. version = 4
  100. [metadata]
  101. setting_version = latest_or_something
  102. """,
  103. "exception": ValueError
  104. },
  105. {
  106. "test_name": "Major-Minor",
  107. "file_data": """[general]
  108. version = 1.2
  109. """,
  110. "exception": ValueError
  111. }
  112. ]
  113. ## Tests whether getting a version number from bad CFG files gives an
  114. # exception.
  115. #
  116. # \param data The parametrised data to test with. It contains a test name
  117. # to debug with, the serialised contents of a CFG file and the class of
  118. # exception it needs to throw.
  119. # \param upgrader The instance of the upgrader to test.
  120. @pytest.mark.parametrize("data", test_cfg_version_bad_data)
  121. def test_cfgVersionBad(data, upgrader):
  122. with pytest.raises(data["exception"]):
  123. upgrader.getCfgVersion(data["file_data"])
  124. test_translate_theme_data = [
  125. (
  126. "Original Cura theme",
  127. """[general]
  128. version = 4
  129. theme = cura
  130. [metadata]
  131. setting_version = 2
  132. """,
  133. "cura-light"
  134. ),
  135. (
  136. "No theme",
  137. """[general]
  138. version = 4
  139. [metadata]
  140. setting_version = 2
  141. """,
  142. None #Indicates that the theme should be absent in the new file.
  143. )
  144. ]
  145. ## Tests whether the theme is properly translated.
  146. @pytest.mark.parametrize("test_name, file_data, new_theme", test_translate_theme_data)
  147. def test_translateTheme(test_name, file_data, new_theme, upgrader):
  148. #Read old file.
  149. original_parser = configparser.ConfigParser(interpolation = None)
  150. original_parser.read_string(file_data)
  151. #Perform the upgrade.
  152. _, upgraded_stacks = upgrader.upgradePreferences(file_data, "<string>")
  153. upgraded_stack = upgraded_stacks[0]
  154. parser = configparser.ConfigParser(interpolation = None)
  155. parser.read_string(upgraded_stack)
  156. #Check whether the theme was properly translated.
  157. if not new_theme:
  158. assert "theme" not in parser["general"]
  159. else:
  160. assert "theme" in parser["general"]
  161. assert parser["general"]["theme"] == new_theme