TestLegacyProfileReader.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser # An input for some functions we're testing.
  4. import os.path # To find the integration test .ini files.
  5. import pytest # To register tests with.
  6. import unittest.mock # To mock the application, plug-in and container registry out.
  7. import UM.Application # To mock the application out.
  8. import UM.PluginRegistry # To mock the plug-in registry out.
  9. import UM.Settings.ContainerRegistry # To mock the container registry out.
  10. import UM.Settings.InstanceContainer # To intercept the serialised data from the read() function.
  11. import LegacyProfileReader as LegacyProfileReaderModule # To get the directory of the module.
  12. from LegacyProfileReader import LegacyProfileReader # The module we're testing.
  13. @pytest.fixture
  14. def legacy_profile_reader():
  15. return LegacyProfileReader()
  16. test_prepareDefaultsData = [
  17. {
  18. "defaults":
  19. {
  20. "foo": "bar"
  21. },
  22. "cheese": "delicious"
  23. },
  24. {
  25. "cat": "fluffy",
  26. "dog": "floofy"
  27. }
  28. ]
  29. @pytest.mark.parametrize("input", test_prepareDefaultsData)
  30. def test_prepareDefaults(legacy_profile_reader, input):
  31. output = legacy_profile_reader.prepareDefaults(input)
  32. if "defaults" in input:
  33. assert input["defaults"] == output
  34. else:
  35. assert output == {}
  36. test_prepareLocalsData = [
  37. ( # Ordinary case.
  38. { # Parser data.
  39. "profile":
  40. {
  41. "layer_height": "0.2",
  42. "infill_density": "30"
  43. }
  44. },
  45. { # Defaults.
  46. "layer_height": "0.1",
  47. "infill_density": "20",
  48. "line_width": "0.4"
  49. }
  50. ),
  51. ( # Empty data.
  52. { # Parser data.
  53. "profile":
  54. {
  55. }
  56. },
  57. { # Defaults.
  58. }
  59. ),
  60. ( # All defaults.
  61. { # Parser data.
  62. "profile":
  63. {
  64. }
  65. },
  66. { # Defaults.
  67. "foo": "bar",
  68. "boo": "far"
  69. }
  70. ),
  71. ( # Multiple config sections.
  72. { # Parser data.
  73. "some_other_name":
  74. {
  75. "foo": "bar"
  76. },
  77. "profile":
  78. {
  79. "foo": "baz" #Not the same as in some_other_name
  80. }
  81. },
  82. { # Defaults.
  83. "foo": "bla"
  84. }
  85. )
  86. ]
  87. @pytest.mark.parametrize("parser_data, defaults", test_prepareLocalsData)
  88. def test_prepareLocals(legacy_profile_reader, parser_data, defaults):
  89. parser = configparser.ConfigParser()
  90. parser.read_dict(parser_data)
  91. output = legacy_profile_reader.prepareLocals(parser, "profile", defaults)
  92. assert set(defaults.keys()) <= set(output.keys()) # All defaults must be in there.
  93. assert set(parser_data["profile"]) <= set(output.keys()) # All overwritten values must be in there.
  94. for key in output:
  95. if key in parser_data["profile"]:
  96. assert output[key] == parser_data["profile"][key] # If overwritten, must be the overwritten value.
  97. else:
  98. assert output[key] == defaults[key] # Otherwise must be equal to the default.
  99. test_prepareLocalsNoSectionErrorData = [
  100. ( # Section does not exist.
  101. { # Parser data.
  102. "some_other_name":
  103. {
  104. "foo": "bar"
  105. },
  106. },
  107. { # Defaults.
  108. "foo": "baz"
  109. }
  110. )
  111. ]
  112. ## Test cases where a key error is expected.
  113. @pytest.mark.parametrize("parser_data, defaults", test_prepareLocalsNoSectionErrorData)
  114. def test_prepareLocalsNoSectionError(legacy_profile_reader, parser_data, defaults):
  115. parser = configparser.ConfigParser()
  116. parser.read_dict(parser_data)
  117. with pytest.raises(configparser.NoSectionError):
  118. legacy_profile_reader.prepareLocals(parser, "profile", defaults)
  119. intercepted_data = ""
  120. @pytest.mark.parametrize("file_name", ["normal_case.ini"])
  121. def test_read(legacy_profile_reader, file_name):
  122. # Mock out all dependencies. Quite a lot!
  123. global_stack = unittest.mock.MagicMock()
  124. global_stack.getProperty = unittest.mock.MagicMock(return_value = 1) # For machine_extruder_count setting.
  125. def getMetaDataEntry(key, default_value = ""):
  126. if key == "quality_definition":
  127. return "mocked_quality_definition"
  128. if key == "has_machine_quality":
  129. return "True"
  130. global_stack.definition.getMetaDataEntry = getMetaDataEntry
  131. global_stack.definition.getId = unittest.mock.MagicMock(return_value = "mocked_global_definition")
  132. application = unittest.mock.MagicMock()
  133. application.getGlobalContainerStack = unittest.mock.MagicMock(return_value = global_stack)
  134. application_getInstance = unittest.mock.MagicMock(return_value = application)
  135. container_registry = unittest.mock.MagicMock()
  136. container_registry_getInstance = unittest.mock.MagicMock(return_value = container_registry)
  137. container_registry.uniqueName = unittest.mock.MagicMock(return_value = "Imported Legacy Profile")
  138. container_registry.findDefinitionContainers = unittest.mock.MagicMock(return_value = [global_stack.definition])
  139. UM.Settings.InstanceContainer.setContainerRegistry(container_registry)
  140. plugin_registry = unittest.mock.MagicMock()
  141. plugin_registry_getInstance = unittest.mock.MagicMock(return_value = plugin_registry)
  142. plugin_registry.getPluginPath = unittest.mock.MagicMock(return_value = os.path.dirname(LegacyProfileReaderModule.__file__))
  143. # Mock out the resulting InstanceContainer so that we can intercept the data before it's passed through the version upgrader.
  144. def deserialize(self, data): # Intercepts the serialised data that we'd perform the version upgrade from when deserializing.
  145. global intercepted_data
  146. intercepted_data = data
  147. parser = configparser.ConfigParser()
  148. parser.read_string(data)
  149. self._metadata["position"] = parser["metadata"]["position"]
  150. def duplicate(self, new_id, new_name):
  151. self._metadata["id"] = new_id
  152. self._metadata["name"] = new_name
  153. return self
  154. with unittest.mock.patch.object(UM.Application.Application, "getInstance", application_getInstance):
  155. with unittest.mock.patch.object(UM.Settings.ContainerRegistry.ContainerRegistry, "getInstance", container_registry_getInstance):
  156. with unittest.mock.patch.object(UM.PluginRegistry.PluginRegistry, "getInstance", plugin_registry_getInstance):
  157. with unittest.mock.patch.object(UM.Settings.InstanceContainer.InstanceContainer, "deserialize", deserialize):
  158. with unittest.mock.patch.object(UM.Settings.InstanceContainer.InstanceContainer, "duplicate", duplicate):
  159. result = legacy_profile_reader.read(os.path.join(os.path.dirname(__file__), file_name))
  160. assert len(result) == 1
  161. # Let's see what's inside the actual output file that we generated.
  162. parser = configparser.ConfigParser()
  163. parser.read_string(intercepted_data)
  164. assert parser["general"]["definition"] == "mocked_quality_definition"
  165. assert parser["general"]["version"] == "4" # Yes, before we upgraded.
  166. assert parser["general"]["name"] == "Imported Legacy Profile" # Because we overwrote uniqueName.
  167. assert parser["metadata"]["type"] == "quality_changes"
  168. assert parser["metadata"]["quality_type"] == "normal"
  169. assert parser["metadata"]["position"] == "0"
  170. assert parser["metadata"]["setting_version"] == "5" # Yes, before we upgraded.