From 61e42a9782ff852f17bb04f247a1ea5d480e2eda Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 10 Nov 2014 11:51:03 +0100 Subject: [PATCH] When querying values from Setting, return None if key does not exists. Fixes bug 1387278 Fixes: https://launchpad.net/bugs/1387278 --- openlp/core/common/settings.py | 12 ++++++++---- tests/functional/openlp_core_common/test_settings.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index abbf09ec3..624179826 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -431,10 +431,14 @@ class Settings(QtCore.QSettings): :param key: The key to return the value from. """ # if group() is not empty the group has not been specified together with the key. - if self.group(): - default_value = Settings.__default_settings__[self.group() + '/' + key] - else: - default_value = Settings.__default_settings__[key] + try: + if self.group(): + default_value = Settings.__default_settings__[self.group() + '/' + key] + else: + default_value = Settings.__default_settings__[key] + except KeyError: + log.warning('Key "%s" was not found in settings, returning None!' % key) + return None setting = super(Settings, self).value(key, default_value) return self._convert_value(setting, default_value) diff --git a/tests/functional/openlp_core_common/test_settings.py b/tests/functional/openlp_core_common/test_settings.py index ea4bcf849..bcd901a81 100644 --- a/tests/functional/openlp_core_common/test_settings.py +++ b/tests/functional/openlp_core_common/test_settings.py @@ -115,3 +115,15 @@ class TestSettings(TestCase, TestMixin): # THEN the new value is returned when re-read self.assertEqual('very short', Settings().value('test/extend'), 'The saved value should be returned') + + def settings_nonexisting_test(self): + """ + Test the Settings on query for non-existing value + """ + # GIVEN: A new Settings setup + + # WHEN reading a setting that doesn't exists + does_not_exist_value = Settings().value('core/does not exists') + + # THEN None should be returned + self.assertEqual(does_not_exist_value, None, 'The value should be None')