Beispiel #1
0
    def test_write_to_stream(self):
        stream = io.StringIO()
        bsh = BufferStreamHandler(stream)
        teststring = 'string'
        bsh.write_to_stream(teststring)

        self.assertEqual(stream.getvalue().strip(), teststring)
Beispiel #2
0
    def test_emit_different_error_level_writes_immeditely_string(self):
        stream = io.StringIO()
        bsh = BufferStreamHandler(stream)
        rec = logging.LogRecord('test_name', logging.ERROR, 'pathname', 123,
                                'error message', (), None)
        bsh.emit(rec)

        self.assertEqual(len(bsh.message_buffer['items']), 1)
        self.assertEqual(stream.getvalue().strip(), 'error message')
Beispiel #3
0
    def test_emit_string(self):
        stream = io.StringIO()
        bsh = BufferStreamHandler(stream)
        rec = logging.LogRecord('test_name', logging.INFO, 'pathname', 123,
                                'string', (), None)
        # setting to empty, seems something else is writing to it somehow
        bsh.emit(rec)

        self.assertEqual(len(bsh.message_buffer['items']), 1)
        self.assertEqual(stream.getvalue().strip(), '')
Beispiel #4
0
    def test_flush_buffer_to_stream_empty_items_key(self):
        stream = io.StringIO()
        bsh = BufferStreamHandler(stream)
        messages = {'items': []}
        bsh.message_buffer = messages
        bsh.flush_buffer_to_stream()

        # what is returned is the string representation of the buffer, which
        # should always be an array of strings. Opted to hard code the expected
        # value to higlight clearly what is expected rather than str(messages)
        self.assertEqual(stream.getvalue().strip(), '')
Beispiel #5
0
    def test_flush_buffer_to_stream_empty_object(self):
        stream = io.StringIO()
        bsh = BufferStreamHandler(stream)
        messages = {'items': [{}]}
        bsh.message_buffer = messages
        bsh.flush_buffer_to_stream()

        # what is returned is the string representation of the buffer, which
        # should always be an array of strings. Opted to hard code the expected
        # value to higlight clearly what is expected rather than str(messages)
        self.assertEqual(stream.getvalue().strip(), '{"items": [{}]}')

        try:
            json.loads(stream.getvalue().strip())
        except Exception:  # pylint: disable=broad-except
            self.fail("the outputted object is not valid json")