|
|
@ -1,16 +1,18 @@ |
|
|
|
import mock |
|
|
|
from mock import patch, Mock |
|
|
|
from mock import patch, Mock, MagicMock |
|
|
|
import unittest |
|
|
|
from unittest import TestCase |
|
|
|
|
|
|
|
from couchpotato.core.settings import Settings |
|
|
|
|
|
|
|
|
|
|
|
class DoNotUseMe: |
|
|
|
""" Do not use this class, it is just for storing Mock ' s of Settings-class |
|
|
|
|
|
|
|
Usage: |
|
|
|
Select appropriate Mocks and copy-paste them to your test-method |
|
|
|
""" |
|
|
|
|
|
|
|
def __do_not_call(self): |
|
|
|
# s = Settings |
|
|
|
s = Mock() |
|
|
@ -27,8 +29,32 @@ class DoNotUseMe: |
|
|
|
s.p = Mock() |
|
|
|
s.p.getboolean = Mock(return_value=True) |
|
|
|
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): |
|
|
|
|
|
|
|
def setUp(self): |
|
|
|
self.s = Settings() |
|
|
|
|
|
|
@ -44,8 +70,8 @@ class SettingsSaveWritableNonWritable(TestCase): |
|
|
|
section = 'core' |
|
|
|
option = 'option_non_exist_be_sure' |
|
|
|
value = "1000" |
|
|
|
params = { 'section' : section, 'name' : option, 'value' : value } |
|
|
|
|
|
|
|
params = {'section': section, 'name': option, 'value': value} |
|
|
|
|
|
|
|
# call method: |
|
|
|
env_mock = Mock() |
|
|
|
|
|
|
@ -65,7 +91,6 @@ class SettingsSaveWritableNonWritable(TestCase): |
|
|
|
# check, that Settings tried to save my value: |
|
|
|
mock_set.assert_called_with(section, option, value) |
|
|
|
|
|
|
|
|
|
|
|
def test_save_non_writable(self): |
|
|
|
s = self.s |
|
|
|
|
|
|
@ -79,8 +104,8 @@ class SettingsSaveWritableNonWritable(TestCase): |
|
|
|
section = 'core' |
|
|
|
option = 'option_non_exist_be_sure' |
|
|
|
value = "1000" |
|
|
|
params = { 'section' : section, 'name' : option, 'value' : value } |
|
|
|
|
|
|
|
params = {'section': section, 'name': option, 'value': value} |
|
|
|
|
|
|
|
# call method: |
|
|
|
env_mock = Mock() |
|
|
|
|
|
|
@ -88,7 +113,6 @@ class SettingsSaveWritableNonWritable(TestCase): |
|
|
|
with patch.dict('sys.modules', {'couchpotato.environment.Env': env_mock}): |
|
|
|
result = s.saveView(**params) |
|
|
|
|
|
|
|
|
|
|
|
self.assertIsInstance(s, Settings) |
|
|
|
self.assertIsInstance(result, dict) |
|
|
|
self.assertFalse(result['success']) |
|
|
@ -101,14 +125,13 @@ class SettingsSaveWritableNonWritable(TestCase): |
|
|
|
mock_is_w.assert_called_with(section, option) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OptionMetaSuite(TestCase): |
|
|
|
""" tests for ro rw hidden options """ |
|
|
|
|
|
|
|
def setUp(self): |
|
|
|
self.s = Settings() |
|
|
|
self.meta = self.s.optionMetaSuffix() |
|
|
|
|
|
|
|
|
|
|
|
# hide real config-parser: |
|
|
|
self.s.p = Mock() |
|
|
|
|
|
|
@ -121,13 +144,13 @@ class OptionMetaSuite(TestCase): |
|
|
|
option_meta = option + self.meta |
|
|
|
# setup mock |
|
|
|
s.p.getboolean = Mock(return_value=True) |
|
|
|
|
|
|
|
# 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) ) |
|
|
|
|
|
|
|
# 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)) |
|
|
|
|
|
|
|
# by default all options are writable and readable |
|
|
|
self.assertTrue ( s.isOptionWritable(section, option) ) |
|
|
|
self.assertTrue ( s.isOptionReadable(section, option) ) |
|
|
|
self.assertTrue(s.isOptionWritable(section, option)) |
|
|
|
self.assertTrue(s.isOptionReadable(section, option)) |
|
|
|
|
|
|
|
def test_non_writable(self): |
|
|
|
s = self.s |
|
|
@ -135,16 +158,16 @@ class OptionMetaSuite(TestCase): |
|
|
|
section = 'core' |
|
|
|
option = 'url' |
|
|
|
|
|
|
|
def mock_get_meta_ro (s,o): |
|
|
|
if (s==section and o==option_meta): |
|
|
|
def mock_get_meta_ro(s, o): |
|
|
|
if (s == section and o == option_meta): |
|
|
|
return 'ro' |
|
|
|
return 11 |
|
|
|
|
|
|
|
option_meta = option + self.meta |
|
|
|
# setup mock |
|
|
|
s.p.has_option = Mock( return_value = True ) |
|
|
|
s.p.get = Mock( side_effect = mock_get_meta_ro) |
|
|
|
s.p.has_option = Mock(return_value=True) |
|
|
|
s.p.get = Mock(side_effect=mock_get_meta_ro) |
|
|
|
|
|
|
|
# by default all options are writable and readable |
|
|
|
self.assertFalse ( s.isOptionWritable(section, option) ) |
|
|
|
self.assertTrue ( s.isOptionReadable(section, option) ) |
|
|
|
self.assertFalse(s.isOptionWritable(section, option)) |
|
|
|
self.assertTrue(s.isOptionReadable(section, option)) |
|
|
|