|
| 1 | +""" Test class for the serializers module |
| 2 | +""" |
| 3 | +import unittest |
| 4 | +import logging |
| 5 | +import datetime |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import decimal |
| 9 | + |
| 10 | +sys.path.insert(0, os.path.abspath('.')) |
| 11 | +from cmreslogging.serializers import CMRESSerializer |
| 12 | + |
| 13 | + |
| 14 | +class CMRESSerializerTestCase(unittest.TestCase): |
| 15 | + """ CMRESSerializer test class |
| 16 | + """ |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + """ Set up the test |
| 20 | +
|
| 21 | + Set up the log and the formatter to get asctime and exc_text fields |
| 22 | + """ |
| 23 | + self.log = logging.getLogger("MyTestCase") |
| 24 | + self.formatter = logging.Formatter('%(asctime)s') |
| 25 | + |
| 26 | + def tearDown(self): |
| 27 | + """ Delete the log and the formatter |
| 28 | + """ |
| 29 | + del self.log |
| 30 | + del self.formatter |
| 31 | + |
| 32 | + def test_dumps_classic_log(self): |
| 33 | + """ Test the classic log serialization |
| 34 | + """ |
| 35 | + serializer = CMRESSerializer() |
| 36 | + record = self.log.makeRecord(name=self.log.name, |
| 37 | + level=logging.INFO, |
| 38 | + fn=self.__class__.__name__, |
| 39 | + lno=58, msg="dump_classic_log", |
| 40 | + args=None, |
| 41 | + exc_info=False, |
| 42 | + func=None, |
| 43 | + extra=None) |
| 44 | + self.formatter.format(record) |
| 45 | + for value in record.__dict__.values(): |
| 46 | + try: |
| 47 | + serializer.dumps(value) |
| 48 | + except TypeError: |
| 49 | + self.fail("Serializer raised a TypeError exception") |
| 50 | + |
| 51 | + def test_dumps_exception_log(self): |
| 52 | + """ Test the exception log serialization with the exc_info field |
| 53 | + """ |
| 54 | + serializer = CMRESSerializer() |
| 55 | + try: |
| 56 | + bad_idea = 1/0 |
| 57 | + except ZeroDivisionError: |
| 58 | + record = self.log.makeRecord(name=self.log.name, |
| 59 | + level=logging.ERROR, |
| 60 | + fn=self.__class__.__name__, |
| 61 | + lno=58, msg="dump_exception_log", |
| 62 | + args=None, |
| 63 | + exc_info=sys.exc_info(), |
| 64 | + func=None, |
| 65 | + extra=None) |
| 66 | + self.formatter.format(record) |
| 67 | + for value in record.__dict__.values(): |
| 68 | + try: |
| 69 | + serializer.dumps(value) |
| 70 | + except TypeError: |
| 71 | + self.fail("Serializer raised a TypeError exception") |
| 72 | + |
| 73 | + def test_dumps_log_with_extras_and_args(self): |
| 74 | + """ Test the log serialization with arguments and extras complex parameters |
| 75 | + """ |
| 76 | + serializer = CMRESSerializer() |
| 77 | + record = self.log.makeRecord(name=self.log.name, |
| 78 | + level=logging.ERROR, |
| 79 | + fn=self.__class__.__name__, |
| 80 | + lno=58, msg="dump_%s_log", |
| 81 | + args="args", |
| 82 | + exc_info=False, |
| 83 | + func=None, |
| 84 | + extra={'complexvalue1': datetime.date.today(), |
| 85 | + 'complexvalue2': decimal.Decimal('3.0')}) |
| 86 | + self.formatter.format(record) |
| 87 | + for value in record.__dict__.values(): |
| 88 | + try: |
| 89 | + serializer.dumps(value) |
| 90 | + except TypeError: |
| 91 | + self.fail("Serializer raised a TypeError exception") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + unittest.main() |
0 commit comments