<commit_before>
import unittest
import logging

from pymongo.connection import Connection

from mongolog.handlers import MongoHandler

class TestHandler(unittest.TestCase):

    def setUp(self):
        """ Create an empty database that could be used for logging """
        self.db_name = '_mongolog_test'

        self.conn = Connection('localhost')
        self.conn.drop_database(self.db_name)

        self.db = self.conn[self.db_name]
        self.collection = self.db['log']

    def tearDown(self):
        """ Drop used database """
        self.conn.drop_database('_mongolog_test')
        

    def testLogging(self):
        """ Simple logging example """
        log = logging.getLogger('')
        log.setLevel(logging.DEBUG)

        log.addHandler(MongoHandler(self.collection))

        log.debug('test')

        r = self.collection.find_one({'level':'debug', 'msg':'test'})
        self.assertEquals(r['msg'], 'test')
        
    def testLoggingException(self):
        """ Logging example with exception """
        log = logging.getLogger('')
        log.setLevel(logging.DEBUG)

        log.addHandler(MongoHandler(self.collection))

        try:
            1/0
        except ZeroDivisionError:
            log.error('test', exc_info=True)

        r = self.collection.find_one({'level':'error', 'msg':'test'})
        self.assertEquals(r['msg'], 'test')
        assert r['exc_info'].startswith('Traceback')

<commit_msg>Refactor tests, add non RootLogger test case<commit_after>
import unittest
import logging

from pymongo.connection import Connection

from mongolog.handlers import MongoHandler

class MongoLogTestCase(unittest.TestCase):
    """
    Base Test Case
    """
    def setUp(self):
        """ Create an empty database that could be used for logging """
        self.db_name = '_mongolog_test'

        self.conn = Connection('localhost')
        self.conn.drop_database(self.db_name)

        self.db = self.conn[self.db_name]
        self.collection = self.db['log']

    def tearDown(self):
        """ Drop used database """
        self.conn.drop_database('_mongolog_test')


class TestHandler(MongoLogTestCase):

    def testLogging(self):
        """ Simple logging example """
        log = logging.getLogger('example')
        log.setLevel(logging.DEBUG)

        log.addHandler(MongoHandler(self.collection))
        log.debug('test')

        self.assertTrue(self.collection.find_one({'level':'debug', 'msg':'test'}))


    def testLoggingException(self):
        """ Logging example with exception """
        log = logging.getLogger('example')
        log.setLevel(logging.DEBUG)

        log.addHandler(MongoHandler(self.collection))

        try:
            1/0
        except ZeroDivisionError:
            log.error('test zero division', exc_info=True)

        r = self.collection.find_one({'level':'error', 'msg':'test zero division'})
        self.assertTrue(r['exc_info'].startswith('Traceback'))


class TestRootLoggerHandler(MongoLogTestCase):
    """
    Test Handler attached to RootLogger
    """
    def testLogging(self):
        """ Simple logging example """
        log = logging.getLogger('')
        log.setLevel(logging.DEBUG)

        log.addHandler(MongoHandler(self.collection))
        log.debug('test')

        r = self.collection.find_one({'level':'debug', 'msg':'test'})
        self.assertEquals(r['msg'], 'test')

    def testLoggingException(self):
        """ Logging example with exception """
        log = logging.getLogger('')
        log.setLevel(logging.DEBUG)

        log.addHandler(MongoHandler(self.collection))

        try:
            1/0
        except ZeroDivisionError:
            log.error('test zero division', exc_info=True)

        r = self.collection.find_one({'level':'error', 'msg':'test zero division'})
        self.assertTrue(r['exc_info'].startswith('Traceback'))
{'fraction_non_alphanumeric': 0.13118556701030928, 'fraction_numerical': 0.0015463917525773195, 'mean_word_length': 2.771622934888241, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5135305', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1085, 'n_words': 215}