Tweak app.make_rattail_config()
to allow caller to define some settings.
This is mostly for the sake of tests etc.
This commit is contained in:
parent
2f5f9c8c3c
commit
4f5c0e6bd8
|
@ -55,22 +55,28 @@ def make_rattail_config(settings):
|
||||||
"""
|
"""
|
||||||
Make a Rattail config object from the given settings.
|
Make a Rattail config object from the given settings.
|
||||||
"""
|
"""
|
||||||
# Initialize rattail config and embed it in the settings dict, to make it
|
rattail_config = settings.get('rattail_config')
|
||||||
# available to web requests later.
|
if not rattail_config:
|
||||||
path = settings.get('edbob.config')
|
|
||||||
if not path or not os.path.exists(path):
|
|
||||||
raise ConfigurationError("Please set 'edbob.config' in [app:main] section of config "
|
|
||||||
"to the path of your config file. Lame, but necessary.")
|
|
||||||
edbob.init('rattail', path)
|
|
||||||
log.info("using rattail config file: {0}".format(path))
|
|
||||||
rattail_config = RattailConfig(edbob.config)
|
|
||||||
settings['rattail_config'] = rattail_config
|
|
||||||
|
|
||||||
# Load all Rattail database engines from config, and store in settings
|
# Initialize rattail config and embed it in the settings dict, to make it
|
||||||
# dict. This is necessary e.g. in the case of a host server, to have
|
# available to web requests later.
|
||||||
# access to its subordinate store servers.
|
path = settings.get('edbob.config')
|
||||||
rattail_engines = get_engines(rattail_config)
|
if not path or not os.path.exists(path):
|
||||||
settings['rattail_engines'] = rattail_engines
|
raise ConfigurationError("Please set 'edbob.config' in [app:main] section of config "
|
||||||
|
"to the path of your config file. Lame, but necessary.")
|
||||||
|
edbob.init('rattail', path)
|
||||||
|
log.info("using rattail config file: {0}".format(path))
|
||||||
|
rattail_config = RattailConfig(edbob.config)
|
||||||
|
settings['rattail_config'] = rattail_config
|
||||||
|
|
||||||
|
rattail_engines = settings.get('rattail_engines')
|
||||||
|
if not rattail_engines:
|
||||||
|
|
||||||
|
# Load all Rattail database engines from config, and store in settings
|
||||||
|
# dict. This is necessary e.g. in the case of a host server, to have
|
||||||
|
# access to its subordinate store servers.
|
||||||
|
rattail_engines = get_engines(rattail_config)
|
||||||
|
settings['rattail_engines'] = rattail_engines
|
||||||
|
|
||||||
# Configure the database session classes. Note that most of the time we'll
|
# Configure the database session classes. Note that most of the time we'll
|
||||||
# be using the Tailbone Session, but occasionally (e.g. within batch
|
# be using the Tailbone Session, but occasionally (e.g. within batch
|
||||||
|
|
8
tests/data/tailbone.conf
Normal file
8
tests/data/tailbone.conf
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
[app:main]
|
||||||
|
edbob.config = %(here)s/tailbone.conf
|
||||||
|
|
||||||
|
[rattail.db]
|
||||||
|
keys = default, store
|
||||||
|
default.url = sqlite://
|
||||||
|
store.url = sqlite://
|
42
tests/test_app.py
Normal file
42
tests/test_app.py
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
|
||||||
|
from rattail.config import RattailConfig
|
||||||
|
from rattail.exceptions import ConfigurationError
|
||||||
|
from rattail.db import Session as RattailSession
|
||||||
|
|
||||||
|
from tailbone import app
|
||||||
|
from tailbone.db import Session as TailboneSession
|
||||||
|
|
||||||
|
|
||||||
|
class TestRattailConfig(TestCase):
|
||||||
|
|
||||||
|
config_path = os.path.abspath(
|
||||||
|
os.path.join(os.path.dirname(__file__), 'data', 'tailbone.conf'))
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
# may or may not be necessary depending on test
|
||||||
|
TailboneSession.remove()
|
||||||
|
|
||||||
|
def test_settings_arg_must_include_config_path_by_default(self):
|
||||||
|
# error raised if path not provided
|
||||||
|
self.assertRaises(ConfigurationError, app.make_rattail_config, {})
|
||||||
|
# get a config object if path provided
|
||||||
|
result = app.make_rattail_config({'edbob.config': self.config_path})
|
||||||
|
self.assertTrue(isinstance(result, RattailConfig))
|
||||||
|
|
||||||
|
def test_settings_arg_may_override_config_and_engines(self):
|
||||||
|
rattail_config = RattailConfig()
|
||||||
|
engine = create_engine('sqlite://')
|
||||||
|
result = app.make_rattail_config({
|
||||||
|
'rattail_config': rattail_config,
|
||||||
|
'rattail_engines': {'default': engine}})
|
||||||
|
self.assertTrue(result is rattail_config)
|
||||||
|
self.assertTrue(RattailSession.kw['bind'] is engine)
|
||||||
|
self.assertTrue(TailboneSession.bind is engine)
|
Loading…
Reference in a new issue