From 3b2af5b7eda3275802ba2f83dc7fb902148bfc10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 28 Jun 2016 10:57:51 +0200 Subject: [PATCH] Fix settings unit tests --- src/octoprint/settings.py | 24 ++++++++++++++++++------ tests/settings/test_settings.py | 8 ++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index a15385ea..7fd62e95 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -405,6 +405,9 @@ class HierarchicalChainMap(ChainMap): current[path[-1]] = value def del_by_path(self, path): + if not path: + raise ValueError("Invalid path") + current = self for key in path[:-1]: @@ -693,14 +696,19 @@ class Settings(object): def effective_hash(self): import hashlib hash = hashlib.md5() - hash.update(repr(self.effective)) + hash.update(self.effective_yaml) return hash.hexdigest() + @property + def config_yaml(self): + import yaml + return yaml.safe_dump(self._config) + @property def config_hash(self): import hashlib hash = hashlib.md5() - hash.update(repr(self._config)) + hash.update(self.config_yaml) return hash.hexdigest() @property @@ -1024,7 +1032,7 @@ class Settings(object): return current def _get_value(self, path, asdict=False, config=None, defaults=None, preprocessors=None, merged=False, incl_defaults=True, do_copy=True): - if len(path) == 0: + if not path: raise NoSuchSettingsPath() if config is not None or defaults is not None: @@ -1122,8 +1130,7 @@ class Settings(object): except NoSuchSettingsPath: if error_on_path: raise - else: - return None + return None def getInt(self, path, **kwargs): value = self.get(path, **kwargs) @@ -1204,6 +1211,11 @@ class Settings(object): #~~ remove def remove(self, path, config=None, error_on_path=False): + if not path: + if error_on_path: + raise NoSuchSettingsPath() + return + if config is not None: mappings = [config] + self._overlay_maps + [self._default_map] chain = HierarchicalChainMap(*mappings) @@ -1221,7 +1233,7 @@ class Settings(object): #~~ setter def set(self, path, value, force=False, defaults=None, config=None, preprocessors=None, error_on_path=False): - if len(path) == 0: + if not path: if error_on_path: raise NoSuchSettingsPath() return diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index c7b7a77f..75d528bc 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -436,20 +436,20 @@ class TestSettings(unittest.TestCase): def test_effective_hash(self): with self.mocked_config(): hash = hashlib.md5() - hash.update(repr(self.expected_effective)) + hash.update(yaml.safe_dump(self.expected_effective)) expected_effective_hash = hash.hexdigest() - print(repr(self.expected_effective)) + print(yaml.safe_dump(self.expected_effective)) settings = octoprint.settings.Settings() effective_hash = settings.effective_hash - print(repr(settings.effective)) + print(yaml.safe_dump(settings.effective)) self.assertEqual(expected_effective_hash, effective_hash) def test_config_hash(self): with self.mocked_config(): hash = hashlib.md5() - hash.update(repr(self.config)) + hash.update(yaml.safe_dump(self.config)) expected_config_hash = hash.hexdigest() settings = octoprint.settings.Settings()