Browse Source

Merge pull request #6261 from maxkoryukov/develop

Improved tests for settings
pull/6303/head
Ruud Burger 9 years ago
parent
commit
2f47c9b8b9
  1. 18
      couchpotato/core/settings.py
  2. 53
      couchpotato/core/settings_test.py

18
couchpotato/core/settings.py

@ -130,14 +130,15 @@ class Settings(object):
def get(self, option = '', section = 'core', default = None, type = None): def get(self, option = '', section = 'core', default = None, type = None):
if self.isOptionMeta(section, option): if self.isOptionMeta(section, option):
self.log.warning('set::option "%s.%s" cancelled, since it is a META option', (section, option)) self.log.warning('get::option "%s.%s" cancelled, since it is a META option', (section, option))
return None return None
tp = type
try: try:
type = self.getType(section, option) if not type else type tp = self.getType(section, option) if not tp else tp
if hasattr(self, 'get%s' % type.capitalize()): if hasattr(self, 'get%s' % tp.capitalize()):
return getattr(self, 'get%s' % type.capitalize())(section, option) return getattr(self, 'get%s' % tp.capitalize())(section, option)
else: else:
return self.getUnicode(section, option) return self.getUnicode(section, option)
@ -179,6 +180,7 @@ class Settings(object):
def getDirectories(self, section, option): def getDirectories(self, section, option):
value = self.p.get(section, option) value = self.p.get(section, option)
if value: if value:
return map(str.strip, str.split(value, self.directories_delimiter)) return map(str.strip, str.split(value, self.directories_delimiter))
return [] return []
@ -256,10 +258,10 @@ class Settings(object):
self.types[section][option] = type self.types[section][option] = type
def getType(self, section, option): def getType(self, section, option):
type = None tp = None
try: type = self.types[section][option] try: tp = self.types[section][option]
except: type = 'unicode' if not type else type except: tp = 'unicode' if not tp else tp
return type return tp
def addOptions(self, section_name, options): def addOptions(self, section_name, options):
# no additional actions (related to ro-rw options) are required here # no additional actions (related to ro-rw options) are required here

53
couchpotato/core/settings_test.py

@ -1,16 +1,18 @@
import mock import mock
from mock import patch, Mock from mock import patch, Mock, MagicMock
import unittest import unittest
from unittest import TestCase from unittest import TestCase
from couchpotato.core.settings import Settings from couchpotato.core.settings import Settings
class DoNotUseMe: class DoNotUseMe:
""" Do not use this class, it is just for storing Mock ' s of Settings-class """ Do not use this class, it is just for storing Mock ' s of Settings-class
Usage: Usage:
Select appropriate Mocks and copy-paste them to your test-method Select appropriate Mocks and copy-paste them to your test-method
""" """
def __do_not_call(self): def __do_not_call(self):
# s = Settings # s = Settings
s = Mock() s = Mock()
@ -28,7 +30,31 @@ class DoNotUseMe:
s.p.getboolean = Mock(return_value=True) s.p.getboolean = Mock(return_value=True)
s.p.has_option = Mock s.p.has_option = Mock
class SettingsCommon(TestCase):
def setUp(self):
self.s = Settings()
def test_get_directories(self):
s = self.s
raw = ' /some/directory ::/another/dir '
exp = ['/some/directory', '/another/dir']
sec = 'sec'
opt = 'opt'
s.types[sec] = {}
s.types[sec][opt] = 'directories'
s.p = MagicMock()
s.p.get.return_value = raw
act = s.get(option = opt, section = sec)
self.assertEqual(act, exp)
class SettingsSaveWritableNonWritable(TestCase): class SettingsSaveWritableNonWritable(TestCase):
def setUp(self): def setUp(self):
self.s = Settings() self.s = Settings()
@ -44,7 +70,7 @@ class SettingsSaveWritableNonWritable(TestCase):
section = 'core' section = 'core'
option = 'option_non_exist_be_sure' option = 'option_non_exist_be_sure'
value = "1000" value = "1000"
params = { 'section' : section, 'name' : option, 'value' : value } params = {'section': section, 'name': option, 'value': value}
# call method: # call method:
env_mock = Mock() env_mock = Mock()
@ -65,7 +91,6 @@ class SettingsSaveWritableNonWritable(TestCase):
# check, that Settings tried to save my value: # check, that Settings tried to save my value:
mock_set.assert_called_with(section, option, value) mock_set.assert_called_with(section, option, value)
def test_save_non_writable(self): def test_save_non_writable(self):
s = self.s s = self.s
@ -79,7 +104,7 @@ class SettingsSaveWritableNonWritable(TestCase):
section = 'core' section = 'core'
option = 'option_non_exist_be_sure' option = 'option_non_exist_be_sure'
value = "1000" value = "1000"
params = { 'section' : section, 'name' : option, 'value' : value } params = {'section': section, 'name': option, 'value': value}
# call method: # call method:
env_mock = Mock() env_mock = Mock()
@ -88,7 +113,6 @@ class SettingsSaveWritableNonWritable(TestCase):
with patch.dict('sys.modules', {'couchpotato.environment.Env': env_mock}): with patch.dict('sys.modules', {'couchpotato.environment.Env': env_mock}):
result = s.saveView(**params) result = s.saveView(**params)
self.assertIsInstance(s, Settings) self.assertIsInstance(s, Settings)
self.assertIsInstance(result, dict) self.assertIsInstance(result, dict)
self.assertFalse(result['success']) self.assertFalse(result['success'])
@ -101,7 +125,6 @@ class SettingsSaveWritableNonWritable(TestCase):
mock_is_w.assert_called_with(section, option) mock_is_w.assert_called_with(section, option)
class OptionMetaSuite(TestCase): class OptionMetaSuite(TestCase):
""" tests for ro rw hidden options """ """ tests for ro rw hidden options """
@ -123,11 +146,11 @@ class OptionMetaSuite(TestCase):
s.p.getboolean = Mock(return_value=True) s.p.getboolean = Mock(return_value=True)
# there is no META-record for our option: # there is no META-record for our option:
s.p.has_option = Mock(side_effect=lambda s,o: not (s==section and o==option_meta) ) s.p.has_option = Mock(side_effect=lambda s, o: not (s == section and o == option_meta))
# by default all options are writable and readable # by default all options are writable and readable
self.assertTrue ( s.isOptionWritable(section, option) ) self.assertTrue(s.isOptionWritable(section, option))
self.assertTrue ( s.isOptionReadable(section, option) ) self.assertTrue(s.isOptionReadable(section, option))
def test_non_writable(self): def test_non_writable(self):
s = self.s s = self.s
@ -135,16 +158,16 @@ class OptionMetaSuite(TestCase):
section = 'core' section = 'core'
option = 'url' option = 'url'
def mock_get_meta_ro (s,o): def mock_get_meta_ro(s, o):
if (s==section and o==option_meta): if (s == section and o == option_meta):
return 'ro' return 'ro'
return 11 return 11
option_meta = option + self.meta option_meta = option + self.meta
# setup mock # setup mock
s.p.has_option = Mock( return_value = True ) s.p.has_option = Mock(return_value=True)
s.p.get = Mock( side_effect = mock_get_meta_ro) s.p.get = Mock(side_effect=mock_get_meta_ro)
# by default all options are writable and readable # by default all options are writable and readable
self.assertFalse ( s.isOptionWritable(section, option) ) self.assertFalse(s.isOptionWritable(section, option))
self.assertTrue ( s.isOptionReadable(section, option) ) self.assertTrue(s.isOptionReadable(section, option))

Loading…
Cancel
Save