TestVersionUpgrade27to30.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To parse the resulting config files.
  4. import pytest #To register tests with.
  5. import VersionUpgrade27to30 #The module we're testing.
  6. ## Creates an instance of the upgrader to test with.
  7. @pytest.fixture
  8. def upgrader():
  9. return VersionUpgrade27to30.VersionUpgrade27to30()
  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_translate_theme_data = [
  122. (
  123. "Original Cura theme",
  124. """[general]
  125. version = 4
  126. theme = cura
  127. [metadata]
  128. setting_version = 2
  129. """,
  130. "cura-light"
  131. ),
  132. (
  133. "No theme",
  134. """[general]
  135. version = 4
  136. [metadata]
  137. setting_version = 2
  138. """,
  139. None #Indicates that the theme should be absent in the new file.
  140. )
  141. ]
  142. ## Tests whether the theme is properly translated.
  143. @pytest.mark.parametrize("test_name, file_data, new_theme", test_translate_theme_data)
  144. def test_translateTheme(test_name, file_data, new_theme, upgrader):
  145. #Read old file.
  146. original_parser = configparser.ConfigParser(interpolation = None)
  147. original_parser.read_string(file_data)
  148. #Perform the upgrade.
  149. _, upgraded_stacks = upgrader.upgradePreferences(file_data, "<string>")
  150. upgraded_stack = upgraded_stacks[0]
  151. parser = configparser.ConfigParser(interpolation = None)
  152. parser.read_string(upgraded_stack)
  153. #Check whether the theme was properly translated.
  154. if not new_theme:
  155. assert "theme" not in parser["general"]
  156. else:
  157. assert "theme" in parser["general"]
  158. assert parser["general"]["theme"] == new_theme