2023-10-28 17:48:37 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
|
2024-07-04 08:00:42 -05:00
|
|
|
import pytest
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2024-08-27 20:26:22 -05:00
|
|
|
from wuttjamaican import conf as mod
|
2025-08-30 21:25:44 -05:00
|
|
|
|
2024-08-27 20:26:22 -05:00
|
|
|
# TODO: get rid of this eventually
|
2023-10-28 17:48:37 -05:00
|
|
|
from wuttjamaican import conf
|
|
|
|
from wuttjamaican.exc import ConfigurationError
|
2023-11-20 09:57:38 -06:00
|
|
|
from wuttjamaican.app import AppHandler
|
2025-08-08 22:41:33 -05:00
|
|
|
from wuttjamaican.testing import FileTestCase, ConfigTestCase
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
|
2024-08-27 19:10:50 -05:00
|
|
|
class TestWuttaConfig(FileTestCase):
|
2024-11-23 15:33:00 -06:00
|
|
|
def make_config(self, **kwargs):
|
|
|
|
return mod.WuttaConfig(**kwargs)
|
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
def test_contstructor_basic(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.appname, "wutta")
|
2023-10-28 17:48:37 -05:00
|
|
|
self.assertEqual(config.files_read, [])
|
|
|
|
|
|
|
|
def test_constructor_valid_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
myfile = self.write_file("my.conf", "")
|
2023-10-28 17:48:37 -05:00
|
|
|
config = conf.WuttaConfig(files=[myfile])
|
|
|
|
self.assertEqual(len(config.files_read), 1)
|
|
|
|
self.assertEqual(config.files_read[0], myfile)
|
|
|
|
|
|
|
|
def test_constructor_missing_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
invalid = os.path.join(self.tempdir, "invalid.conf")
|
2023-10-28 17:48:37 -05:00
|
|
|
self.assertRaises(FileNotFoundError, conf.WuttaConfig, files=[invalid])
|
|
|
|
|
|
|
|
def test_constructor_required_files_are_present(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
first = self.write_file(
|
|
|
|
"first.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[foo]
|
|
|
|
bar = 1
|
|
|
|
baz = A
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
second = self.write_file(
|
|
|
|
"second.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta.config]
|
|
|
|
require = %(here)s/first.conf
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
baz = B
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
config = conf.WuttaConfig(files=[second])
|
|
|
|
self.assertEqual(len(config.files_read), 2)
|
|
|
|
# nb. files_read listing is in order of "priority" which is
|
|
|
|
# same the as order in which files were initially read
|
|
|
|
self.assertEqual(config.files_read[0], second)
|
|
|
|
self.assertEqual(config.files_read[1], first)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo.bar"), "1")
|
|
|
|
self.assertEqual(config.get("foo.baz"), "B")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_constructor_required_files_are_missing(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
second = self.write_file(
|
|
|
|
"second.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta.config]
|
|
|
|
require = %(here)s/first.conf
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
baz = B
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
self.assertRaises(FileNotFoundError, conf.WuttaConfig, files=[second])
|
|
|
|
|
|
|
|
def test_constructor_included_files_are_present(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
first = self.write_file(
|
|
|
|
"first.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[foo]
|
|
|
|
bar = 1
|
|
|
|
baz = A
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
second = self.write_file(
|
|
|
|
"second.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta.config]
|
|
|
|
include = %(here)s/first.conf
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
baz = B
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
config = conf.WuttaConfig(files=[second])
|
|
|
|
self.assertEqual(len(config.files_read), 2)
|
|
|
|
# nb. files_read listing is in order of "priority" which is
|
|
|
|
# same the as order in which files were initially read
|
|
|
|
self.assertEqual(config.files_read[0], second)
|
|
|
|
self.assertEqual(config.files_read[1], first)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo.bar"), "1")
|
|
|
|
self.assertEqual(config.get("foo.baz"), "B")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_constructor_included_files_are_missing(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
second = self.write_file(
|
|
|
|
"second.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta.config]
|
|
|
|
include = %(here)s/first.conf
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
baz = B
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
config = conf.WuttaConfig(files=[second])
|
|
|
|
self.assertEqual(len(config.files_read), 1)
|
|
|
|
self.assertEqual(config.files_read[0], second)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo.bar"))
|
|
|
|
self.assertEqual(config.get("foo.baz"), "B")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2024-12-18 12:33:17 -06:00
|
|
|
def test_files_only_read_once(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
base = self.write_file(
|
|
|
|
"base.conf",
|
|
|
|
"""
|
2024-12-18 12:33:17 -06:00
|
|
|
[foo]
|
|
|
|
bar = 1
|
|
|
|
baz = A
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2024-12-18 12:33:17 -06:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
middle = self.write_file(
|
|
|
|
"middle.conf",
|
|
|
|
"""
|
2024-12-18 12:33:17 -06:00
|
|
|
[wutta.config]
|
|
|
|
require = %(here)s/base.conf
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
baz = B
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2024-12-18 12:33:17 -06:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
top = self.write_file(
|
|
|
|
"top.conf",
|
|
|
|
"""
|
2024-12-18 12:33:17 -06:00
|
|
|
[wutta.config]
|
|
|
|
require = %(here)s/middle.conf
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
baz = C
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2024-12-18 12:33:17 -06:00
|
|
|
|
|
|
|
config = conf.WuttaConfig(files=[top, middle, base])
|
|
|
|
self.assertEqual(len(config.files_read), 3)
|
|
|
|
# nb. files_read listing is in order of "priority" which is
|
|
|
|
# same the as order in which files were initially read
|
|
|
|
self.assertEqual(config.files_read[0], top)
|
|
|
|
self.assertEqual(config.files_read[1], middle)
|
|
|
|
self.assertEqual(config.files_read[2], base)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo.bar"), "1")
|
|
|
|
self.assertEqual(config.get("foo.baz"), "C")
|
2024-12-18 12:33:17 -06:00
|
|
|
|
2023-11-19 20:36:51 -06:00
|
|
|
def test_prioritized_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
first = self.write_file(
|
|
|
|
"first.conf",
|
|
|
|
"""\
|
2023-11-19 20:36:51 -06:00
|
|
|
[foo]
|
|
|
|
bar = 1
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-11-19 20:36:51 -06:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
second = self.write_file(
|
|
|
|
"second.conf",
|
|
|
|
"""\
|
2023-11-19 20:36:51 -06:00
|
|
|
[wutta.config]
|
|
|
|
require = %(here)s/first.conf
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-11-19 20:36:51 -06:00
|
|
|
|
|
|
|
config = conf.WuttaConfig(files=[second])
|
|
|
|
files = config.get_prioritized_files()
|
|
|
|
self.assertEqual(len(files), 2)
|
|
|
|
self.assertEqual(files[0], second)
|
|
|
|
self.assertEqual(files[1], first)
|
|
|
|
|
2024-12-18 13:43:19 -06:00
|
|
|
def test_default_vars_interpolated(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
myconf = self.write_file(
|
|
|
|
"my.conf",
|
|
|
|
"""
|
2024-12-18 13:43:19 -06:00
|
|
|
[foo]
|
|
|
|
bar = %(here)s/bar.txt
|
|
|
|
baz = %(__file__)s
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2024-12-18 13:43:19 -06:00
|
|
|
|
|
|
|
config = conf.WuttaConfig(files=[myconf])
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo.bar"), f"{self.tempdir}/bar.txt")
|
|
|
|
self.assertEqual(config.get("foo.baz"), myconf)
|
2024-12-18 13:43:19 -06:00
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
def test_constructor_defaults(self):
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
self.assertEqual(config.defaults, {})
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo"))
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.WuttaConfig(defaults={"foo": "bar"})
|
|
|
|
self.assertEqual(config.defaults, {"foo": "bar"})
|
|
|
|
self.assertEqual(config.get("foo"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_constructor_db_flags(self):
|
2024-07-04 08:00:42 -05:00
|
|
|
try:
|
|
|
|
# nb. we don't need this import but the test will not
|
|
|
|
# behave correctly unless the lib is installed
|
|
|
|
import sqlalchemy
|
|
|
|
except ImportError:
|
|
|
|
pytest.skip("test is not relevant without sqlalchemy")
|
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
myfile = self.write_file(
|
|
|
|
"my.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta.config]
|
|
|
|
usedb = true
|
|
|
|
preferdb = true
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# flags are off by default
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
self.assertFalse(config.usedb)
|
|
|
|
self.assertFalse(config.preferdb)
|
|
|
|
|
|
|
|
# but may override via constructor
|
|
|
|
config = conf.WuttaConfig(usedb=True, preferdb=True)
|
|
|
|
self.assertTrue(config.usedb)
|
|
|
|
self.assertTrue(config.preferdb)
|
|
|
|
|
|
|
|
# and also may override via config file
|
|
|
|
config = conf.WuttaConfig(files=[myfile])
|
|
|
|
self.assertTrue(config.usedb)
|
|
|
|
self.assertTrue(config.preferdb)
|
|
|
|
|
|
|
|
def test_constructor_db_not_supported(self):
|
2024-07-04 08:00:42 -05:00
|
|
|
try:
|
|
|
|
# nb. we don't need this import but the test will not
|
|
|
|
# behave correctly unless the lib is installed
|
|
|
|
import sqlalchemy
|
|
|
|
except ImportError:
|
|
|
|
pytest.skip("test is not relevant without sqlalchemy")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# flags are off by default
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
self.assertFalse(config.usedb)
|
|
|
|
self.assertFalse(config.preferdb)
|
|
|
|
|
|
|
|
# but caller may enable the flags (if sqlalchemy available)
|
|
|
|
config = conf.WuttaConfig(usedb=True, preferdb=True)
|
|
|
|
self.assertTrue(config.usedb)
|
|
|
|
self.assertTrue(config.preferdb)
|
|
|
|
|
|
|
|
# but db flags are force-disabled if sqlalchemy not available,
|
|
|
|
# regardless of flag values caller provides...
|
|
|
|
|
|
|
|
orig_import = __import__
|
|
|
|
|
|
|
|
def mock_import(name, *args, **kwargs):
|
2025-08-30 21:25:44 -05:00
|
|
|
if name == "wuttjamaican.db":
|
2023-10-28 17:48:37 -05:00
|
|
|
raise ImportError
|
|
|
|
return orig_import(name, *args, **kwargs)
|
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("builtins.__import__", side_effect=mock_import):
|
2023-10-28 17:48:37 -05:00
|
|
|
config = conf.WuttaConfig(usedb=True, preferdb=True)
|
|
|
|
self.assertFalse(config.usedb)
|
|
|
|
self.assertFalse(config.preferdb)
|
|
|
|
|
|
|
|
def test_constructor_may_configure_logging(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
myfile = self.write_file(
|
|
|
|
"my.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta.config]
|
|
|
|
configure_logging = true
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(conf.WuttaConfig, "_configure_logging") as method:
|
2023-10-28 17:48:37 -05:00
|
|
|
# no logging config by default
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
method.assert_not_called()
|
|
|
|
|
|
|
|
# but may override via constructor
|
|
|
|
method.reset_mock()
|
|
|
|
config = conf.WuttaConfig(configure_logging=True)
|
|
|
|
method.assert_called_once()
|
|
|
|
|
|
|
|
# and also may override via config file
|
|
|
|
method.reset_mock()
|
|
|
|
config = conf.WuttaConfig(files=[myfile])
|
|
|
|
method.assert_called_once()
|
|
|
|
|
|
|
|
def test_constructor_configures_logging(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
myfile = self.write_file(
|
|
|
|
"my.conf",
|
|
|
|
"""\
|
2023-10-28 17:48:37 -05:00
|
|
|
[wutta]
|
|
|
|
timezone.default = America/Chicago
|
|
|
|
|
|
|
|
[wutta.config]
|
|
|
|
configure_logging = true
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("wuttjamaican.conf.logging") as logging:
|
2023-10-28 17:48:37 -05:00
|
|
|
# basic constructor attempts logging config
|
|
|
|
config = conf.WuttaConfig(configure_logging=True)
|
|
|
|
logging.config.fileConfig.assert_called_once()
|
|
|
|
|
|
|
|
# if logging config fails, error is *not* raised
|
|
|
|
logging.config.fileConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
logging.config.fileConfig.side_effect = configparser.NoSectionError(
|
|
|
|
"logging"
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
config = conf.WuttaConfig(configure_logging=True)
|
|
|
|
logging.config.fileConfig.assert_called_once()
|
|
|
|
|
|
|
|
# and it works if we specify config file
|
|
|
|
logging.config.fileConfig.reset_mock()
|
|
|
|
config = conf.WuttaConfig(files=[myfile])
|
|
|
|
logging.config.fileConfig.assert_called_once()
|
|
|
|
|
2024-07-04 06:21:38 -05:00
|
|
|
def test_config_has_no_app_after_init(self):
|
|
|
|
# initial config should *not* have an app yet, otherwise
|
|
|
|
# extensions cannot specify a default app handler
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-31 11:42:30 -05:00
|
|
|
self.assertIsNone(config._app)
|
2024-07-04 06:21:38 -05:00
|
|
|
|
|
|
|
# but after that we can get an app okay
|
|
|
|
app = config.get_app()
|
2025-08-31 11:42:30 -05:00
|
|
|
self.assertIsNotNone(app)
|
2024-07-04 06:21:38 -05:00
|
|
|
self.assertIs(app, config._app)
|
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
def test_setdefault(self):
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
|
|
|
|
# value is empty by default
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo"))
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# but we can change that by setting default
|
2025-08-30 21:25:44 -05:00
|
|
|
config.setdefault("foo", "bar")
|
|
|
|
self.assertEqual(config.get("foo"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# also, value is returned when we set default
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("baz"))
|
|
|
|
self.assertEqual(config.setdefault("baz", "blarg"), "blarg")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_get_require_with_default(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertRaises(ValueError, config.get, "foo", require=True, default="bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_get_require_missing(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertRaises(ConfigurationError, config.get, "foo", require=True)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_get_with_default(self):
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
# nb. returns None if no default specified
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo"))
|
|
|
|
self.assertEqual(config.get("foo", default="bar"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_get_from_db(self):
|
2024-07-04 08:00:42 -05:00
|
|
|
try:
|
|
|
|
import sqlalchemy as sa
|
|
|
|
from wuttjamaican.db import Session
|
|
|
|
except ImportError:
|
|
|
|
pytest.skip("test is not relevant without sqlalchemy")
|
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
# minimal config, but at least it needs db cxn info
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.WuttaConfig(defaults={"wutta.db.default.url": "sqlite://"})
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
session = Session()
|
|
|
|
|
|
|
|
# setup table for testing
|
2025-08-30 21:25:44 -05:00
|
|
|
session.execute(
|
|
|
|
sa.text(
|
|
|
|
"""
|
2023-10-28 17:48:37 -05:00
|
|
|
create table setting (
|
|
|
|
name varchar(255) primary key,
|
|
|
|
value text
|
|
|
|
);
|
2025-08-30 21:25:44 -05:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
# setting not yet defined
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get_from_db("foo"))
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# insert setting value to db
|
|
|
|
session.execute(sa.text("insert into setting values ('foo', 'bar')"))
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
# now setting returns a value
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get_from_db("foo"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# also works if we provide the session
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get_from_db("foo", session=session), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
session.close()
|
|
|
|
|
|
|
|
def test_get_default(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo"))
|
|
|
|
self.assertEqual(config.get("foo", default="bar"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_get_require(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo"))
|
|
|
|
self.assertRaises(ConfigurationError, config.get, "foo", require=True)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_get_require_message(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("foo"))
|
2023-10-28 17:48:37 -05:00
|
|
|
try:
|
2025-08-30 21:25:44 -05:00
|
|
|
config.get("foo", require=True, message="makin stuff up")
|
2023-10-28 17:48:37 -05:00
|
|
|
except ConfigurationError as error:
|
|
|
|
self.assertIn("makin stuff up", str(error))
|
|
|
|
|
|
|
|
def test_get_preferdb(self):
|
2024-07-04 08:00:42 -05:00
|
|
|
try:
|
|
|
|
import sqlalchemy as sa
|
|
|
|
from wuttjamaican.db import Session
|
|
|
|
except ImportError:
|
|
|
|
pytest.skip("test is not relevant without sqlalchemy")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# start out with a default value
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.WuttaConfig(
|
|
|
|
defaults={"wutta.db.default.url": "sqlite://", "foo": "bar"}
|
|
|
|
)
|
|
|
|
self.assertEqual(config.get("foo"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
session = Session()
|
|
|
|
|
|
|
|
# setup table for testing
|
2025-08-30 21:25:44 -05:00
|
|
|
session.execute(
|
|
|
|
sa.text(
|
|
|
|
"""
|
2023-10-28 17:48:37 -05:00
|
|
|
create table setting (
|
|
|
|
name varchar(255) primary key,
|
|
|
|
value text
|
|
|
|
);
|
2025-08-30 21:25:44 -05:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
session.execute(sa.text("insert into setting values ('foo', 'baz')"))
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
# we did not specify usedb=True, so original default is still returned
|
|
|
|
self.assertFalse(config.usedb)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo"), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# usedb but no preferdb means original default is still returned
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo", usedb=True), "bar")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# but preferdb should mean newer db value is returned
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.get("foo", usedb=True, preferdb=True), "baz")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# try a different key to ensure db fallback works if no default present
|
|
|
|
session.execute(sa.text("insert into setting values ('blarg', 'blitz')"))
|
|
|
|
session.commit()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get("blarg"))
|
|
|
|
self.assertEqual(config.get("blarg", usedb=True), "blitz")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
session.close()
|
|
|
|
|
2023-10-29 22:41:33 -05:00
|
|
|
def test_get_ambiguous(self):
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
|
|
|
|
# value is returned if key is not ambiguous
|
2025-08-30 21:25:44 -05:00
|
|
|
config.setdefault("foo", "bar")
|
|
|
|
self.assertEqual(config.get("foo"), "bar")
|
2023-10-29 22:41:33 -05:00
|
|
|
|
|
|
|
# but None is returned if key is ambiguous
|
2025-08-30 21:25:44 -05:00
|
|
|
config.setdefault("foo.bar", "baz")
|
|
|
|
self.assertIsNone(config.get("foo"))
|
2023-10-29 22:41:33 -05:00
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
def test_require(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertRaises(ConfigurationError, config.require, "foo")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2023-11-19 20:36:51 -06:00
|
|
|
def test_get_bool(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertFalse(config.get_bool("foo.bar"))
|
|
|
|
config.setdefault("foo.bar", "true")
|
|
|
|
self.assertTrue(config.get_bool("foo.bar"))
|
2023-11-19 20:36:51 -06:00
|
|
|
|
|
|
|
def test_get_int(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get_int("foo.bar"))
|
|
|
|
config.setdefault("foo.bar", "42")
|
|
|
|
self.assertEqual(config.get_int("foo.bar"), 42)
|
2023-11-19 20:36:51 -06:00
|
|
|
|
|
|
|
def test_get_list(self):
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIsNone(config.get_list("foo.bar"))
|
|
|
|
config.setdefault("foo.bar", "hello world")
|
|
|
|
self.assertEqual(config.get_list("foo.bar"), ["hello", "world"])
|
2023-11-19 20:36:51 -06:00
|
|
|
|
2024-11-23 15:33:00 -06:00
|
|
|
def test_parse_bool_null(self):
|
|
|
|
config = self.make_config()
|
|
|
|
self.assertIsNone(config.parse_bool(None))
|
|
|
|
|
|
|
|
def test_parse_bool_bool(self):
|
|
|
|
config = self.make_config()
|
|
|
|
self.assertTrue(config.parse_bool(True))
|
|
|
|
self.assertFalse(config.parse_bool(False))
|
|
|
|
|
|
|
|
def test_parse_bool_string_true(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertTrue(config.parse_bool("true"))
|
|
|
|
self.assertTrue(config.parse_bool("yes"))
|
|
|
|
self.assertTrue(config.parse_bool("y"))
|
|
|
|
self.assertTrue(config.parse_bool("on"))
|
|
|
|
self.assertTrue(config.parse_bool("1"))
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_bool_string_false(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertFalse(config.parse_bool("false"))
|
|
|
|
self.assertFalse(config.parse_bool("no"))
|
|
|
|
self.assertFalse(config.parse_bool("n"))
|
|
|
|
self.assertFalse(config.parse_bool("off"))
|
|
|
|
self.assertFalse(config.parse_bool("0"))
|
2024-11-23 15:33:00 -06:00
|
|
|
# nb. assume false for unrecognized input
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertFalse(config.parse_bool("whatever-else"))
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_list_null(self):
|
|
|
|
config = self.make_config()
|
|
|
|
value = config.parse_list(None)
|
|
|
|
self.assertIsInstance(value, list)
|
|
|
|
self.assertEqual(len(value), 0)
|
|
|
|
|
|
|
|
def test_parse_list_list_instance(self):
|
|
|
|
config = self.make_config()
|
|
|
|
mylist = []
|
|
|
|
value = config.parse_list(mylist)
|
|
|
|
self.assertIs(value, mylist)
|
|
|
|
|
|
|
|
def test_parse_list_single_value(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list("foo")
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 1)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_list_single_value_padded_by_spaces(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list(" foo ")
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 1)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_list_slash_is_not_a_separator(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list("/dev/null")
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 1)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "/dev/null")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_list_multiple_values_separated_by_whitespace(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list("foo bar baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 3)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
|
|
|
self.assertEqual(value[1], "bar")
|
|
|
|
self.assertEqual(value[2], "baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_list_multiple_values_separated_by_commas(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list("foo,bar,baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 3)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
|
|
|
self.assertEqual(value[1], "bar")
|
|
|
|
self.assertEqual(value[2], "baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
|
|
|
def test_parse_list_multiple_values_separated_by_whitespace_and_commas(self):
|
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list(" foo, bar baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 3)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
|
|
|
self.assertEqual(value[1], "bar")
|
|
|
|
self.assertEqual(value[2], "baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
def test_parse_list_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(
|
|
|
|
self,
|
|
|
|
):
|
2024-11-23 15:33:00 -06:00
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list(
|
|
|
|
"""
|
2024-11-23 15:33:00 -06:00
|
|
|
foo
|
|
|
|
"C:\\some path\\with spaces\\and, a comma",
|
|
|
|
baz
|
2025-08-30 21:25:44 -05:00
|
|
|
"""
|
|
|
|
)
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 3)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
|
|
|
self.assertEqual(value[1], "C:\\some path\\with spaces\\and, a comma")
|
|
|
|
self.assertEqual(value[2], "baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
def test_parse_list_multiple_values_separated_by_whitespace_and_commas_with_single_quotes(
|
|
|
|
self,
|
|
|
|
):
|
2024-11-23 15:33:00 -06:00
|
|
|
config = self.make_config()
|
2025-08-30 21:25:44 -05:00
|
|
|
value = config.parse_list(
|
|
|
|
"""
|
2024-11-23 15:33:00 -06:00
|
|
|
foo
|
|
|
|
'C:\\some path\\with spaces\\and, a comma',
|
|
|
|
baz
|
2025-08-30 21:25:44 -05:00
|
|
|
"""
|
|
|
|
)
|
2024-11-23 15:33:00 -06:00
|
|
|
self.assertEqual(len(value), 3)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(value[0], "foo")
|
|
|
|
self.assertEqual(value[1], "C:\\some path\\with spaces\\and, a comma")
|
|
|
|
self.assertEqual(value[2], "baz")
|
2024-11-23 15:33:00 -06:00
|
|
|
|
2023-11-20 09:57:38 -06:00
|
|
|
def test_get_app(self):
|
2024-07-04 07:12:22 -05:00
|
|
|
# default handler
|
2023-11-20 09:57:38 -06:00
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(config.default_app_handler_spec, "wuttjamaican.app:AppHandler")
|
2023-11-20 09:57:38 -06:00
|
|
|
app = config.get_app()
|
|
|
|
self.assertIsInstance(app, AppHandler)
|
2024-07-04 07:12:22 -05:00
|
|
|
# nb. make extra sure we didn't get a subclass
|
2023-11-20 09:57:38 -06:00
|
|
|
self.assertIs(type(app), AppHandler)
|
|
|
|
|
2024-07-04 07:12:22 -05:00
|
|
|
# custom default handler
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
config.default_app_handler_spec = "tests.test_conf:CustomAppHandler"
|
2024-07-04 07:12:22 -05:00
|
|
|
app = config.get_app()
|
|
|
|
self.assertIsInstance(app, CustomAppHandler)
|
|
|
|
|
|
|
|
def test_get_engine_maker(self):
|
2024-07-04 08:00:42 -05:00
|
|
|
try:
|
|
|
|
from wuttjamaican.db.conf import make_engine_from_config
|
|
|
|
except ImportError:
|
|
|
|
pytest.skip("test is not relevant without sqlalchemy")
|
2024-07-04 07:12:22 -05:00
|
|
|
|
|
|
|
# default func
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(
|
|
|
|
config.default_engine_maker_spec,
|
|
|
|
"wuttjamaican.db.conf:make_engine_from_config",
|
|
|
|
)
|
2024-07-04 07:12:22 -05:00
|
|
|
make_engine = config.get_engine_maker()
|
|
|
|
self.assertIs(make_engine, make_engine_from_config)
|
|
|
|
|
|
|
|
# custom default func
|
|
|
|
config = conf.WuttaConfig()
|
2025-08-30 21:25:44 -05:00
|
|
|
config.default_engine_maker_spec = (
|
|
|
|
"tests.test_conf:custom_make_engine_from_config"
|
|
|
|
)
|
2024-07-04 07:12:22 -05:00
|
|
|
make_engine = config.get_engine_maker()
|
|
|
|
self.assertIs(make_engine, custom_make_engine_from_config)
|
|
|
|
|
2024-07-11 18:19:38 -05:00
|
|
|
def test_production(self):
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
|
|
|
|
# false if not defined
|
|
|
|
self.assertFalse(config.production())
|
|
|
|
|
|
|
|
# but config may specify
|
2025-08-30 21:25:44 -05:00
|
|
|
config.setdefault("wutta.production", "true")
|
2024-07-11 18:19:38 -05:00
|
|
|
self.assertTrue(config.production())
|
|
|
|
|
2024-07-04 07:12:22 -05:00
|
|
|
|
|
|
|
class CustomAppHandler(AppHandler):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-07-04 08:00:42 -05:00
|
|
|
def custom_make_engine_from_config():
|
2024-07-04 07:12:22 -05:00
|
|
|
pass
|
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2023-11-24 18:45:10 -06:00
|
|
|
class TestWuttaConfigExtension(TestCase):
|
|
|
|
def test_basic(self):
|
|
|
|
config = conf.WuttaConfig()
|
|
|
|
ext = conf.WuttaConfigExtension()
|
|
|
|
self.assertIsNone(ext.key)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertEqual(repr(ext), "WuttaConfigExtension(key=None)")
|
2023-11-24 18:45:10 -06:00
|
|
|
|
|
|
|
|
2023-10-28 17:48:37 -05:00
|
|
|
class TestGenericDefaultFiles(TestCase):
|
|
|
|
def test_linux(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
files = conf.generic_default_files("wuttatest")
|
2023-10-28 17:48:37 -05:00
|
|
|
self.assertIsInstance(files, list)
|
|
|
|
self.assertTrue(len(files) > 1)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIn("/etc/wuttatest.conf", files)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_win32(self):
|
|
|
|
win32com = MagicMock()
|
2025-08-30 21:25:44 -05:00
|
|
|
win32com.shell.SHGetSpecialFolderPath.return_value = r"C:" + os.sep
|
|
|
|
with patch.dict("sys.modules", **{"win32com.shell": win32com}):
|
|
|
|
with patch("wuttjamaican.conf.sys", platform="win32"):
|
|
|
|
files = conf.generic_default_files("wuttatest")
|
2023-10-28 17:48:37 -05:00
|
|
|
self.assertIsInstance(files, list)
|
|
|
|
self.assertTrue(len(files) > 1)
|
2025-08-30 21:25:44 -05:00
|
|
|
self.assertIn(os.path.join("C:", "wuttatest.conf"), files)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_win32_broken(self):
|
|
|
|
orig_import = __import__
|
|
|
|
|
|
|
|
def mock_import(name, *args, **kwargs):
|
2025-08-30 21:25:44 -05:00
|
|
|
if name == "win32com.shell":
|
2023-10-28 17:48:37 -05:00
|
|
|
raise ImportError
|
|
|
|
return orig_import(name, *args, **kwargs)
|
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("builtins.__import__", side_effect=mock_import):
|
|
|
|
with patch("wuttjamaican.conf.sys", platform="win32"):
|
|
|
|
files = conf.generic_default_files("wuttatest")
|
2023-10-28 17:48:37 -05:00
|
|
|
self.assertIsInstance(files, list)
|
|
|
|
self.assertEqual(len(files), 0)
|
|
|
|
|
|
|
|
|
2024-08-27 19:10:50 -05:00
|
|
|
class TestGetConfigPaths(FileTestCase):
|
2023-11-21 22:25:45 -06:00
|
|
|
def test_winsvc(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
myconf = self.write_file(
|
|
|
|
"my.conf",
|
|
|
|
"""
|
2023-11-21 22:25:45 -06:00
|
|
|
[wutta.config]
|
|
|
|
winsvc.RattailFileMonitor = /path/to/other/file
|
2025-08-30 21:25:44 -05:00
|
|
|
""",
|
|
|
|
)
|
2023-11-21 22:25:45 -06:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
files = conf.get_config_paths(files=[myconf], winsvc="RattailFileMonitor")
|
|
|
|
self.assertEqual(files, ["/path/to/other/file"])
|
2023-11-21 22:25:45 -06:00
|
|
|
|
2024-05-28 22:55:53 -05:00
|
|
|
def test_nonexistent_default_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
files = conf.get_config_paths(
|
|
|
|
files=None,
|
|
|
|
env_files_name="IGNORE_THIS",
|
|
|
|
default_files=["/this/does/not/exist"],
|
|
|
|
)
|
2024-05-28 22:55:53 -05:00
|
|
|
self.assertEqual(files, [])
|
|
|
|
|
2023-11-21 22:25:45 -06:00
|
|
|
|
2024-08-27 19:10:50 -05:00
|
|
|
class TestMakeConfig(FileTestCase):
|
2023-10-28 17:48:37 -05:00
|
|
|
# nb. we use appname='wuttatest' in this suite to avoid any
|
|
|
|
# "valid" default config files, env vars etc. which may be present
|
|
|
|
# on the dev machine
|
|
|
|
|
|
|
|
def test_generic_default_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
generic = self.write_file("generic.conf", "")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("wuttjamaican.conf.generic_default_files") as generic_default_files:
|
|
|
|
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
2023-10-28 17:48:37 -05:00
|
|
|
# generic files are used if nothing is specified
|
|
|
|
generic_default_files.return_value = [generic]
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest")
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# make sure empty defaults works too
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
generic_default_files.return_value = []
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest")
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_specify_default_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
generic = self.write_file("generic.conf", "")
|
|
|
|
myfile = self.write_file("my.conf", "")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("wuttjamaican.conf.generic_default_files") as generic_default_files:
|
|
|
|
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
2023-10-28 17:48:37 -05:00
|
|
|
# generic defaults are used if nothing specified
|
|
|
|
generic_default_files.return_value = [generic]
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest")
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify single default file
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest", default_files=myfile)
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.assert_not_called()
|
2025-08-30 21:25:44 -05:00
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify default files as list
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest", default_files=[myfile])
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.assert_not_called()
|
2025-08-30 21:25:44 -05:00
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify default files as callable
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(
|
|
|
|
appname="wuttatest", default_files=lambda appname: [myfile]
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.assert_not_called()
|
2025-08-30 21:25:44 -05:00
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_specify_plus_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
generic = self.write_file("generic.conf", "")
|
|
|
|
myfile = self.write_file("my.conf", "")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("wuttjamaican.conf.generic_default_files") as generic_default_files:
|
|
|
|
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.return_value = [generic]
|
|
|
|
|
|
|
|
# no plus files by default
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest")
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify single plus file
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest", plus_files=myfile)
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify plus files as list
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest", plus_files=[myfile])
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify plus files via env
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(
|
|
|
|
appname="wuttatest", env={"WUTTATEST_CONFIG_PLUS_FILES": myfile}
|
|
|
|
)
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_specify_primary_files(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
generic = self.write_file("generic.conf", "")
|
|
|
|
myfile = self.write_file("my.conf", "")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch("wuttjamaican.conf.generic_default_files") as generic_default_files:
|
|
|
|
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.return_value = [generic]
|
|
|
|
|
|
|
|
# generic files by default
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(appname="wuttatest")
|
|
|
|
generic_default_files.assert_called_once_with("wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify single primary file (nb. no default files)
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(myfile, appname="wuttatest")
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.assert_not_called()
|
2025-08-30 21:25:44 -05:00
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify primary files as list
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config([myfile], appname="wuttatest")
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.assert_not_called()
|
2025-08-30 21:25:44 -05:00
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# can specify primary files via env
|
|
|
|
generic_default_files.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = conf.make_config(
|
|
|
|
appname="wuttatest", env={"WUTTATEST_CONFIG_FILES": myfile}
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
generic_default_files.assert_not_called()
|
2025-08-30 21:25:44 -05:00
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
def test_extensions(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
generic = self.write_file("generic.conf", "")
|
|
|
|
myfile = self.write_file("my.conf", "")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
2025-08-30 21:25:44 -05:00
|
|
|
with patch.object(mod, "WuttaConfig") as WuttaConfig:
|
|
|
|
with patch.object(mod, "load_entry_points") as load_entry_points:
|
2023-10-28 17:48:37 -05:00
|
|
|
# no entry points loaded if extend=False
|
2025-08-30 21:25:44 -05:00
|
|
|
config = mod.make_config(appname="wuttatest", extend=False)
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
2023-10-28 17:48:37 -05:00
|
|
|
load_entry_points.assert_not_called()
|
|
|
|
|
|
|
|
# confirm entry points for default appname
|
|
|
|
load_entry_points.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = mod.make_config([], appname="wutta")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[], appname="wutta", usedb=None, preferdb=None
|
|
|
|
)
|
|
|
|
load_entry_points.assert_called_once_with("wutta.config.extensions")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# confirm entry points for custom appname
|
|
|
|
load_entry_points.reset_mock()
|
|
|
|
WuttaConfig.reset_mock()
|
2025-08-30 21:25:44 -05:00
|
|
|
config = mod.make_config(appname="wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
|
|
|
load_entry_points.assert_called_once_with("wuttatest.config.extensions")
|
2023-10-28 17:48:37 -05:00
|
|
|
|
|
|
|
# confirm extensions are invoked
|
|
|
|
load_entry_points.reset_mock()
|
|
|
|
foo_obj = MagicMock()
|
|
|
|
foo_cls = MagicMock(return_value=foo_obj)
|
2025-08-30 21:25:44 -05:00
|
|
|
load_entry_points.return_value = {"foo": foo_cls}
|
2023-10-28 17:48:37 -05:00
|
|
|
WuttaConfig.reset_mock()
|
|
|
|
testconfig = MagicMock()
|
|
|
|
WuttaConfig.return_value = testconfig
|
2025-08-30 21:25:44 -05:00
|
|
|
config = mod.make_config(appname="wuttatest")
|
|
|
|
WuttaConfig.assert_called_once_with(
|
|
|
|
[], appname="wuttatest", usedb=None, preferdb=None
|
|
|
|
)
|
|
|
|
load_entry_points.assert_called_once_with("wuttatest.config.extensions")
|
2023-10-28 17:48:37 -05:00
|
|
|
foo_cls.assert_called_once_with()
|
|
|
|
foo_obj.configure.assert_called_once_with(testconfig)
|
2024-08-27 20:26:22 -05:00
|
|
|
foo_obj.startup.assert_called_once_with(testconfig)
|
2025-08-08 22:41:33 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TestWuttaConfigProfile(ConfigTestCase):
|
|
|
|
def make_profile(self, key):
|
|
|
|
return mod.WuttaConfigProfile(self.config, key)
|
|
|
|
|
|
|
|
def test_section(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
profile = self.make_profile("default")
|
|
|
|
self.assertRaises(NotImplementedError, getattr, profile, "section")
|
2025-08-08 22:41:33 -05:00
|
|
|
|
|
|
|
def test_get_str(self):
|
2025-08-30 21:25:44 -05:00
|
|
|
self.config.setdefault("wutta.telemetry.default.submit_url", "/nodes/telemetry")
|
|
|
|
with patch.object(mod.WuttaConfigProfile, "section", new="wutta.telemetry"):
|
|
|
|
profile = self.make_profile("default")
|
|
|
|
self.assertEqual(profile.section, "wutta.telemetry")
|
|
|
|
url = profile.get_str("submit_url")
|
|
|
|
self.assertEqual(url, "/nodes/telemetry")
|