def test__validate_attrs(self): valid_keys = { 'collection': 'a-collection', 'timestamp': 'a-timestamp', 'unit': 'a-unit', 'comment': 'a-comment', } invalid_keys = collections.OrderedDict(( ('not-valid', 'is-not-valid'), ('some-key', 'is-not-a-valid-key'), )) self.patch_object(logging, 'logger', name='mock_logging') ev = logging.EventLogger('a-logger') ev._validate_attrs(valid_keys) self.mock_logging.warning.assert_not_called() ev._validate_attrs(invalid_keys) self.mock_logging.warning.assert_called_once_with( mock.ANY, 'not-valid,some-key', mock.ANY) all_kwargs = collections.OrderedDict(valid_keys) all_kwargs.update(invalid_keys) self.mock_logging.reset_mock() ev._validate_attrs(all_kwargs) self.mock_logging.warning.assert_called_once_with( mock.ANY, 'not-valid,some-key', mock.ANY)
def test_get_logger_instance(self): self.patch_object(logging, 'LoggerInstance', name='mock_LoggerInstance') self.mock_LoggerInstance.return_value = 'a-logger-instance' ev = logging.EventLogger('a-logger') self.assertEqual(ev.get_logger_instance(thing='hello'), 'a-logger-instance') self.mock_LoggerInstance.assert_called_once_with(ev, thing='hello')
def test_add_writers(self): self.patch_object(logging, 'logger', name='mock_logging') mock_writer1 = mock.Mock() mock_writer2 = mock.Mock() ev = logging.EventLogger('a-logger') ev.add_writers(mock_writer1, mock_writer2) self.assertEqual(ev._writers, [mock_writer1, mock_writer2]) self.mock_logging.warning.assert_not_called() ev.add_writers(mock_writer1) self.assertEqual(ev._writers, [mock_writer1, mock_writer2]) self.mock_logging.warning.assert_called_once_with( mock.ANY, [mock_writer1])
def test__log_with_invalid_event(self): self.patch('datetime.datetime', name='mock_datetime') self.mock_datetime.now.return_value = 'a-timestamp' ev = logging.EventLogger('a-logger') self.patch_object(ev, '_validate_attrs', name='mock_ev__validate_attrs') mock_writer1 = mock.Mock() ev._writers = [mock_writer1] with self.assertRaises(AssertionError): ev._log('not-an-allowed-event') self.mock_datetime.now.assert_not_called() self.mock_ev__validate_attrs.assert_not_called() mock_writer1.write.assert_not_called()
def test__log_with_valid_span(self): self.patch('uuid.uuid4', name='mock_uuid4') self.mock_uuid4.return_value = 'my-uuid' self.patch('datetime.datetime', name='mock_datetime') self.mock_datetime.now.return_value = 'a-timestamp' ev = logging.EventLogger('a-logger') self.patch_object(ev, '_validate_attrs', name='mock_ev__validate_attrs') mock_writer1 = mock.Mock() ev._writers = [mock_writer1] span = logging.span(ev) ev._log(logging.Events.START_TEST, span=span) self.mock_datetime.now.assert_called_once_with() self.mock_ev__validate_attrs.assert_called_once_with( dict(event='start', timestamp='a-timestamp', uuid='my-uuid')) mock_writer1.write.assert_called_once_with(newline=True, event='start', timestamp='a-timestamp', uuid='my-uuid')
def test__log_with_invalid_span_and_timestamp(self): self.patch('datetime.datetime', name='mock_datetime') self.mock_datetime.now.return_value = 'a-timestamp' ev = logging.EventLogger('a-logger') self.patch_object(ev, '_validate_attrs', name='mock_ev__validate_attrs') mock_writer1 = mock.Mock() ev._writers = [mock_writer1] mock_span = mock.Mock() ev._log(logging.Events.START_TEST, timestamp='my-timestamp', span=mock_span) self.mock_datetime.now.assert_not_called() self.mock_ev__validate_attrs.assert_called_once_with( dict(event='start', timestamp='my-timestamp', span=mock_span)) mock_writer1.write.assert_called_once_with(newline=True, event='start', timestamp='my-timestamp', span=mock_span)
def test_init(self): ev = logging.EventLogger('a-logger') self.assertEqual(ev.name, 'a-logger') self.assertEqual(ev._writers, [])