<commit_before>import glob
import logging
import os
from unittest import TestCase
import mox
from stacktach import stacklog
import __builtin__
from stacktach.stacklog import ExchangeLogger


class StacklogTestCase(TestCase):
    def setUp(self):
        self.mox = mox.Mox()

    def tearDown(self):
        self.mox.UnsetStubs()

    def test_get_logger_should_get_exchange_logger_if_exchange_provided(self):
        filename = 'filename'
        logger = stacklog.get_logger(filename, 'nova')
        self.assertIsInstance(logger, ExchangeLogger)
        for file in glob.glob('{0}.log*'.format(filename)):
            os.remove(file)

    def test_get_logger_should_get_default_logger_if_exchange_not_provided(self):
        filename = 'default_logger'
        logger = stacklog.get_logger(filename)
        self.assertIsInstance(logger, logging.Logger)
        for file in glob.glob('{0}.log*'.format(filename)):
            os.remove(file)


class ExchangeLoggerTestCase(TestCase):
    def setUp(self):
        self.mox = mox.Mox()

    def tearDown(self):
        self.mox.UnsetStubs()

    def _setup_logger_mocks(self, name='name'):
        mock_logger = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(logging, 'getLogger')
        logging.getLogger(stacklog.__name__).AndReturn(mock_logger)
        mock_logger.setLevel(logging.DEBUG)
        self.mox.StubOutClassWithMocks(logging.handlers,
                                       'TimedRotatingFileHandler')
        filename = "{0}.log".format(name)
        handler = logging.handlers.TimedRotatingFileHandler(
            filename, backupCount=3, interval=1, when='midnight')
        self.mox.StubOutClassWithMocks(logging, 'Formatter')
        mock_formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        handler.setFormatter(mock_formatter)
        mock_logger.addHandler(handler)
        mock_logger.handlers = [handler]
        handler.doRollover()
        return mock_logger

    def test_exchange_logger_should_append_exchange_name_to_info(self):
        mock_logger = self._setup_logger_mocks()
        mock_logger.info('exchange: Log %s', 'args', xyz='xyz')
        self.mox.ReplayAll()

        log = ExchangeLogger('exchange', 'name')<commit_msg>Fixing stacklog tests<commit_after>import glob
import logging
import os
from unittest import TestCase
import mox
from stacktach import stacklog
import __builtin__
from stacktach.stacklog import ExchangeLogger


class StacklogTestCase(TestCase):
    def setUp(self):
        self.mox = mox.Mox()

    def tearDown(self):
        self.mox.UnsetStubs()

    def test_get_logger_should_get_exchange_logger_if_exchange_provided(self):
        filename = 'filename'
        logger = stacklog.get_logger(filename, 'nova')
        self.assertIsInstance(logger, ExchangeLogger)
        for file in glob.glob('{0}.log*'.format(filename)):
            os.remove(file)

    def test_get_logger_should_get_default_logger_if_exchange_not_provided(self):
        filename = 'default_logger'
        logger = stacklog.get_logger(filename)
        self.assertIsInstance(logger, logging.Logger)
        for file in glob.glob('{0}.log*'.format(filename)):
            os.remove(file)


class ExchangeLoggerTestCase(TestCase):
    def setUp(self):
        self.mox = mox.Mox()

    def tearDown(self):
        self.mox.UnsetStubs()

    def _setup_logger_mocks(self, name='name'):
        mock_logger = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(logging, 'getLogger')
        logging.getLogger(stacklog.__name__).AndReturn(mock_logger)
        mock_logger.setLevel(logging.DEBUG)
        self.mox.StubOutClassWithMocks(logging.handlers,
                                       'TimedRotatingFileHandler')
        filename = "/tmp/{0}.log".format(name)
        handler = logging.handlers.TimedRotatingFileHandler(
            filename, backupCount=3, interval=1, when='midnight')
        self.mox.StubOutClassWithMocks(logging, 'Formatter')
        mock_formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        handler.setFormatter(mock_formatter)
        mock_logger.addHandler(handler)
        mock_logger.handlers = [handler]
        handler.doRollover()
        return mock_logger

    def test_exchange_logger_should_append_exchange_name_to_info(self):
        mock_logger = self._setup_logger_mocks()
        mock_logger.info('exchange: Log %s', 'args', xyz='xyz')
        self.mox.ReplayAll()

        log = ExchangeLogger('exchange', 'name')
{'fraction_non_alphanumeric': 0.10434402968784108, 'fraction_numerical': 0.0021829294913774284, 'mean_word_length': 3.139114724480578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7560478', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1372, 'n_words': 229}