def test_empty_format_spec(self): m = Message( twiggy.levels.DEBUG, '', { 'shirt': lambda: 42, 'name': 'jose' }, Message._default_options, args=[lambda: "Mister"], kwargs={'who': lambda: "Funnypants"}, ) assert m.text == '' assert m.name == 'jose' assert m.fields['shirt'] == 42
def test_trace_error_without_error(self): opts = Message._default_options.copy() opts['trace'] = 'error' m = Message( twiggy.levels.DEBUG, "Hello {0} {who}", { 'shirt': lambda: 42, 'name': 'jose' }, opts, args=[lambda: "Mister"], kwargs={'who': lambda: "Funnypants"}, ) assert m.traceback is None
def test_json_formatter_is_able_to_deal_with_utf8_fields(): stream = io.BytesIO() output = JsonOutput(stream=stream) message = Message( INFO, u'Некий текст'.encode('utf-8'), { 'time': time.gmtime(), 'blah': u'минор'.encode('utf-8') }, _default_fields, (), {}, ) output.output(message) result = stream.getvalue() assert isinstance(result, six.binary_type)
def test_no_kwargs(self): m = Message(twiggy.levels.DEBUG, "Hello {0}", { 'shirt': 42, 'name': 'jose' }, Message._default_options, args=['Mister'], kwargs={}) assert m.fields == { 'shirt': 42, 'name': 'jose', 'level': twiggy.levels.DEBUG } assert m.traceback is None assert m.text == "Hello Mister"
def test_json_formatter_is_able_to_deal_with_unicode_fields(): stream = io.StringIO() output = JsonOutput(stream=stream) message = Message( INFO, u'Некий текст', { 'time': time.gmtime(), 'blah': u'минор' }, _default_fields, (), {}, ) output.output(message) result = stream.getvalue() assert isinstance(result, six.text_type)
def test_json_formatter_is_able_to_deal_with_unicode_fields(): # logger usually writes it's output to some file and should # encode data into some binary encoding, for example utf-8 stream = io.BytesIO() output = JsonOutput(stream=stream) message = Message( INFO, u'Некий текст', { 'time': time.gmtime(), 'blah': u'минор' }, _default_fields, (), {}, ) output.output(message) result = stream.getvalue() assert isinstance(result, six.binary_type)
def test_no_args(self): m = Message( twiggy.levels.DEBUG, "Hello {who}", { 'shirt': 42, 'name': 'jose' }, Message._default_options, args=(), kwargs={'who': "Funnypants"}, ) assert m.fields == { 'shirt': 42, 'name': 'jose', 'level': twiggy.levels.DEBUG } assert m.traceback is None assert m.text == "Hello Funnypants"
def test_trace_tuple(self): opts = Message._default_options.copy() try: 1 / 0 except ZeroDivisionError: opts['trace'] = sys.exc_info() m = Message( twiggy.levels.DEBUG, "Hello {0} {who}", { 'shirt': lambda: 42, 'name': 'jose' }, opts, args=[lambda: "Mister"], kwargs={'who': lambda: "Funnypants"}, ) assert m.traceback.startswith('Traceback (most recent call last):') assert 'ZeroDivisionError:' in m.traceback
def test_callables(self): m = Message( twiggy.levels.DEBUG, "Hello {0} {who}", { 'shirt': lambda: 42, 'name': 'jose' }, Message._default_options, args=[lambda: "Mister"], kwargs={'who': lambda: "Funnypants"}, ) assert m.fields == { 'shirt': 42, 'name': 'jose', 'level': twiggy.levels.DEBUG } assert m.traceback is None assert m.text == "Hello Mister Funnypants"
def test_trace_error_with_error(self): opts = Message._default_options.copy() opts['trace'] = 'error' try: 1 / 0 except ZeroDivisionError: m = Message( twiggy.levels.DEBUG, "Hello {0} {who}", { 'shirt': lambda: 42, 'name': 'jose' }, opts, args=[lambda: "Mister"], kwargs={'who': lambda: "Funnypants"}, ) assert m.traceback.startswith('Traceback (most recent call last):') assert m.traceback.endswith( 'ZeroDivisionError: integer division or modulo by zero\n')
def test_json_formatter_does_not_dump_long_as_string(): # https://github.com/svetlyak40wt/twiggy-goodies/issues/3 if sys.version_info.major == 2: # this test makes sense only for python 2.x # because there is no separation between integer and long # in python 3 stream = io.BytesIO() output = JsonOutput(stream=stream) message = Message( INFO, u'Какой-то текст сообщения', { 'time': time.gmtime(), 'request_id': long(1234), }, _default_fields, (), {}, ) output.output(message) result = stream.getvalue() assert '"request_id": 1234' in result.decode('utf-8')
def test_braces_alias(self): opts = Message._default_options.copy() opts['style'] = '{}' m = Message( twiggy.levels.DEBUG, "Hello {who}", { 'shirt': 42, 'name': 'jose' }, opts, args=(), kwargs={'who': "Funnypants"}, ) assert m.fields == { 'shirt': 42, 'name': 'jose', 'level': twiggy.levels.DEBUG } assert m.traceback is None assert m.text == "Hello Funnypants"