| """Tests for cloud_logging.""" |
| |
| from __future__ import absolute_import |
| import unittest |
| from . import cloud_logging |
| |
| try: |
| from unittest import mock # pylint: disable=wrong-import-order |
| except ImportError: |
| import mock |
| |
| class StrictAttributesTest(unittest.TestCase): |
| |
| def setUp(self): |
| self.valid_attributes = { |
| 'status': cloud_logging.StrictAttributes.DEFAULT_STRING, |
| 'count': cloud_logging.StrictAttributes.DEFAULT_INT, |
| } |
| |
| @cloud_logging.StrictAttributes(self.valid_attributes) |
| class Collector(cloud_logging.LogEvent): |
| """Extends LogEvent.""" |
| |
| self.collector = Collector('test_logger') |
| |
| def testVerifySubclass(self): |
| """Verify that output class is a subclass of LogEvent.""" |
| self.assertIsInstance(self.collector, cloud_logging.LogEvent) |
| |
| def testAddAttribute(self): |
| """Add a valid attribute to the dict of _attributes.""" |
| self.collector.AddAttribute('status', 'SUCCESS') |
| |
| self.assertIn('status', self.collector._attributes) |
| self.assertEqual(self.collector._attributes['status'], 'SUCCESS') |
| |
| def testAddInvalidAttribute(self): |
| """Attempt to add an invalid attribute.""" |
| self.collector.AddAttribute('invalid', True) |
| self.assertNotIn('invalid', self.collector._attributes) |
| |
| @mock.patch.object(cloud_logging.LogEvent, 'Log') |
| def testLog(self, mock_log): |
| self.collector.Log() |
| mock_log.assert_called_once() |
| for attribute in self.valid_attributes: |
| self.assertIn(attribute, self.collector._attributes) |
| |
| |
| if __name__ == '__main__': |
| unittest.main() |