When querying values from Setting, return None if key does not exists. Fixes bug 1387278

Fixes: https://launchpad.net/bugs/1387278
This commit is contained in:
Tomas Groth 2014-11-10 11:51:03 +01:00
parent 6b538995f4
commit 61e42a9782
2 changed files with 20 additions and 4 deletions

View File

@ -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)

View File

@ -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')