Files
@ bf0f52d92bc6
Branch filter:
Location: rattail-project/rattail/rattail/tests/test_config.py
bf0f52d92bc6
6.7 KiB
text/x-python
Test commit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | # -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import os
import unittest
from six.moves import configparser
from mock import patch
from fixture import TempIO
from rattail import config
class TestParseBoolFunc(unittest.TestCase):
def test_none(self):
self.assertIsNone(config.parse_bool(None))
def test_true(self):
self.assertIs(config.parse_bool(True), True)
def test_false(self):
self.assertIs(config.parse_bool(False), False)
def test_string(self):
self.assertTrue(config.parse_bool('true'))
self.assertTrue(config.parse_bool('yes'))
self.assertTrue(config.parse_bool('on'))
self.assertTrue(config.parse_bool('1'))
self.assertFalse(config.parse_bool('false'))
self.assertFalse(config.parse_bool('no'))
self.assertFalse(config.parse_bool('off'))
self.assertFalse(config.parse_bool('0'))
class TestParseListFunc(unittest.TestCase):
def test_none(self):
value = config.parse_list(None)
self.assertEqual(len(value), 0)
def test_single_value(self):
value = config.parse_list(u'foo')
self.assertEqual(len(value), 1)
self.assertEqual(value[0], u'foo')
def test_single_value_padded_by_spaces(self):
value = config.parse_list(u' foo ')
self.assertEqual(len(value), 1)
self.assertEqual(value[0], u'foo')
def test_slash_is_not_a_separator(self):
value = config.parse_list(u'/dev/null')
self.assertEqual(len(value), 1)
self.assertEqual(value[0], u'/dev/null')
def test_multiple_values_separated_by_whitespace(self):
value = config.parse_list(u'foo bar baz')
self.assertEqual(len(value), 3)
self.assertEqual(value[0], u'foo')
self.assertEqual(value[1], u'bar')
self.assertEqual(value[2], u'baz')
def test_multiple_values_separated_by_commas(self):
value = config.parse_list(u'foo,bar,baz')
self.assertEqual(len(value), 3)
self.assertEqual(value[0], u'foo')
self.assertEqual(value[1], u'bar')
self.assertEqual(value[2], u'baz')
def test_multiple_values_separated_by_whitespace_and_commas(self):
value = config.parse_list(u' foo, bar baz')
self.assertEqual(len(value), 3)
self.assertEqual(value[0], u'foo')
self.assertEqual(value[1], u'bar')
self.assertEqual(value[2], u'baz')
def test_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(self):
value = config.parse_list("""
foo
"C:\\some path\\with spaces\\and, a comma",
baz
""")
self.assertEqual(len(value), 3)
self.assertEqual(value[0], u'foo')
self.assertEqual(value[1], u'C:\\some path\\with spaces\\and, a comma')
self.assertEqual(value[2], u'baz')
class TestRattailConfig(unittest.TestCase):
def setUp(self):
self.tempio = TempIO()
def tearDown(self):
del self.tempio
def setup_files(self):
self.site_path = self.tempio.putfile('site.conf', """
[rattail]
""")
self.host_path = self.tempio.putfile('host.conf', """
[rattail.config]
include = "{}"
""".format(self.site_path))
self.app_path = self.tempio.putfile('app.conf', """
[rattail.config]
include = "{}"
""".format(self.host_path))
self.custom_path = self.tempio.putfile('custom.conf', """
[rattail.config]
include = "%(here)s/app.conf"
""")
def test_init_defaults(self):
cfg = config.RattailConfig()
self.assertEqual(cfg.files_requested, [])
self.assertEqual(cfg.files_read, [])
self.assertIsNone(cfg._session_factory)
def test_init_params(self):
self.setup_files()
# files
cfg = config.RattailConfig()
self.assertEqual(cfg.files_requested, [])
self.assertEqual(cfg.files_read, [])
cfg = config.RattailConfig(files=[self.site_path])
self.assertEqual(cfg.files_requested, [self.site_path])
self.assertEqual(cfg.files_read, [self.site_path])
# usedb
cfg = config.RattailConfig()
self.assertFalse(cfg.usedb)
cfg = config.RattailConfig(usedb=True)
self.assertTrue(cfg.usedb)
# preferdb
cfg = config.RattailConfig()
self.assertFalse(cfg.preferdb)
cfg = config.RattailConfig(preferdb=True)
self.assertTrue(cfg.preferdb)
def test_read_file_with_recurse(self):
self.setup_files()
cfg = config.RattailConfig()
cfg.read_file(self.custom_path, recurse=True)
self.assertEqual(cfg.files_requested, [self.custom_path, self.app_path, self.host_path, self.site_path])
self.assertEqual(cfg.files_read, [self.site_path, self.host_path, self.app_path, self.custom_path])
def test_read_file_once_only(self):
self.setup_files()
another_path = self.tempio.putfile('another.conf', """
[rattail.config]
include = "{custom}" "{site}" "{app}" "{site}" "{custom}"
""".format(custom=self.custom_path, app=self.app_path, site=self.site_path))
cfg = config.RattailConfig()
cfg.read_file(another_path, recurse=True)
self.assertEqual(cfg.files_requested, [another_path, self.custom_path, self.app_path, self.host_path, self.site_path])
self.assertEqual(cfg.files_read, [self.site_path, self.host_path, self.app_path, self.custom_path, another_path])
def test_read_file_skip_missing(self):
self.setup_files()
bogus_path = '/tmp/does-not/exist'
self.assertFalse(os.path.exists(bogus_path))
another_path = self.tempio.putfile('another.conf', """
[rattail.config]
include = "{bogus}" "{app}" "{bogus}" "{site}"
""".format(bogus=bogus_path, app=self.app_path, site=self.site_path))
cfg = config.RattailConfig()
cfg.read_file(another_path, recurse=True)
self.assertEqual(cfg.files_requested, [another_path, bogus_path, self.app_path, self.host_path, self.site_path])
self.assertEqual(cfg.files_read, [self.site_path, self.host_path, self.app_path, another_path])
@patch('rattail.config.logging.config.fileConfig')
def test_configure_logging(self, fileConfig):
cfg = config.RattailConfig()
# logging not configured by default
cfg.configure_logging()
self.assertFalse(fileConfig.called)
# but config option can enable it
cfg.set('rattail.config', 'configure_logging', 'true')
cfg.configure_logging()
self.assertEqual(fileConfig.call_count, 1)
fileConfig.reset_mock()
# invalid logging config is ignored
fileConfig.side_effect = configparser.NoSectionError('loggers')
cfg.configure_logging()
self.assertEqual(fileConfig.call_count, 1)
|