Changeset - 2a4a63333713
[Not reviewed]
0 2 0
Lance Edgar - 8 years ago 2016-05-17 00:39:04
ledgar@sacfoodcoop.com
Add tests for `RattailConfig.configure_logging()`

Slowly but surely..we'll get there..
2 files changed with 26 insertions and 2 deletions:
0 comments (0 inline, 0 general)
rattail/config.py
Show inline comments
 
@@ -123,7 +123,7 @@ class RattailConfig(object):
 
        if self.usedb:
 
            try:
 
                from rattail.db import Session
 
            except ImportError:
 
            except ImportError: # pragma: no cover
 
                log.warning("config created with `usedb = True`, but can't import "
 
                            "`rattail.db.Session`, so setting `usedb = False` instead",
 
                            exc_info=True)
 
@@ -180,16 +180,19 @@ class RattailConfig(object):
 
        # Coerce all logged timestamps to the local timezone, if possible.
 
        logging.Formatter.converter = TimeConverter(self)
 

	
 
        # Flush all current config to a single file, for input to fileConfig().
 
        path = temp_path(suffix='.conf')
 
        with open(path, 'wt') as f:
 
            self.parser.write(f)
 

	
 
        try:
 
            logging.config.fileConfig(path, disable_existing_loggers=False)
 
        except ConfigParser.NoSectionError as error:
 
            log.warning("tried to configure logging, but got NoSectionError: {0}".format(error))
 
        else:
 
            log.debug("configured logging")
 
        os.remove(path)
 
        finally:
 
            os.remove(path)
 

	
 
    def setdefault(self, section, option, value):
 
        """
rattail/tests/test_config.py
Show inline comments
 
@@ -4,7 +4,9 @@ from __future__ import unicode_literals, absolute_import
 

	
 
import os
 
import unittest
 
import ConfigParser
 

	
 
from mock import patch
 
from fixture import TempIO
 

	
 
from rattail import config
 
@@ -178,3 +180,22 @@ include = "{bogus}" "{app}" "{bogus}" "{site}"
 
        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)
0 comments (0 inline, 0 general)