Skip to content

Commit

Permalink
Test '_BaseEntry' directly, drop tests for methodless subclasses.
Browse files Browse the repository at this point in the history
Addresses:
#1578 (comment)
  • Loading branch information
tseaver committed Mar 22, 2016
1 parent 7c52009 commit fd8965c
Showing 1 changed file with 10 additions and 217 deletions.
227 changes: 10 additions & 217 deletions gcloud/logging/test_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
import unittest2


class TestTextEntry(unittest2.TestCase):
class Test_BaseEntry(unittest2.TestCase):

PROJECT = 'PROJECT'
LOGGER_NAME = 'LOGGER_NAME'

def _getTargetClass(self):
from gcloud.logging.entries import TextEntry
return TextEntry
from gcloud.logging.entries import _BaseEntry

class _Dummy(_BaseEntry):
_PAYLOAD_KEY = 'dummyPayload'

return _Dummy

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
Expand Down Expand Up @@ -53,7 +57,7 @@ def test_from_api_repr_missing_data_no_loggers(self):
PAYLOAD = 'PAYLOAD'
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'textPayload': PAYLOAD,
'dummyPayload': PAYLOAD,
'logName': LOG_NAME,
}
klass = self._getTargetClass()
Expand All @@ -76,7 +80,7 @@ def test_from_api_repr_w_loggers_no_logger_match(self):
TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'textPayload': PAYLOAD,
'dummyPayload': PAYLOAD,
'logName': LOG_NAME,
'insertId': IID,
'timestamp': TIMESTAMP,
Expand All @@ -103,218 +107,7 @@ def test_from_api_repr_w_loggers_w_logger_match(self):
TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'textPayload': PAYLOAD,
'logName': LOG_NAME,
'insertId': IID,
'timestamp': TIMESTAMP,
}
LOGGER = object()
loggers = {LOG_NAME: LOGGER}
klass = self._getTargetClass()
entry = klass.from_api_repr(API_REPR, client, loggers=loggers)
self.assertEqual(entry.payload, PAYLOAD)
self.assertEqual(entry.insert_id, IID)
self.assertEqual(entry.timestamp, NOW)
self.assertTrue(entry.logger is LOGGER)


class TestStructEntry(unittest2.TestCase):

PROJECT = 'PROJECT'
LOGGER_NAME = 'LOGGER_NAME'

def _getTargetClass(self):
from gcloud.logging.entries import StructEntry
return StructEntry

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_ctor_defaults(self):
PAYLOAD = {'message': 'MESSAGE', 'weather': 'partly cloudy'}
logger = _Logger(self.LOGGER_NAME, self.PROJECT)
entry = self._makeOne(PAYLOAD, logger)
self.assertEqual(entry.payload, PAYLOAD)
self.assertTrue(entry.logger is logger)
self.assertTrue(entry.insert_id is None)
self.assertTrue(entry.timestamp is None)

def test_ctor_explicit(self):
import datetime
PAYLOAD = {'message': 'MESSAGE', 'weather': 'partly cloudy'}
IID = 'IID'
TIMESTAMP = datetime.datetime.now()
logger = _Logger(self.LOGGER_NAME, self.PROJECT)
entry = self._makeOne(PAYLOAD, logger, IID, TIMESTAMP)
self.assertEqual(entry.payload, PAYLOAD)
self.assertTrue(entry.logger is logger)
self.assertEqual(entry.insert_id, IID)
self.assertEqual(entry.timestamp, TIMESTAMP)

def test_from_api_repr_missing_data_no_loggers(self):
client = _Client(self.PROJECT)
PAYLOAD = {'message': 'MESSAGE', 'weather': 'partly cloudy'}
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'jsonPayload': PAYLOAD,
'logName': LOG_NAME,
}
klass = self._getTargetClass()
entry = klass.from_api_repr(API_REPR, client)
self.assertEqual(entry.payload, PAYLOAD)
self.assertTrue(entry.insert_id is None)
self.assertTrue(entry.timestamp is None)
logger = entry.logger
self.assertTrue(isinstance(logger, _Logger))
self.assertTrue(logger.client is client)
self.assertEqual(logger.name, self.LOGGER_NAME)

def test_from_api_repr_w_loggers_no_logger_match(self):
from datetime import datetime
from gcloud._helpers import UTC
client = _Client(self.PROJECT)
PAYLOAD = {'message': 'MESSAGE', 'weather': 'partly cloudy'}
IID = 'IID'
NOW = datetime.utcnow().replace(tzinfo=UTC)
TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'jsonPayload': PAYLOAD,
'logName': LOG_NAME,
'insertId': IID,
'timestamp': TIMESTAMP,
}
loggers = {}
klass = self._getTargetClass()
entry = klass.from_api_repr(API_REPR, client, loggers=loggers)
self.assertEqual(entry.payload, PAYLOAD)
self.assertEqual(entry.insert_id, IID)
self.assertEqual(entry.timestamp, NOW)
logger = entry.logger
self.assertTrue(isinstance(logger, _Logger))
self.assertTrue(logger.client is client)
self.assertEqual(logger.name, self.LOGGER_NAME)
self.assertEqual(loggers, {LOG_NAME: logger})

def test_from_api_repr_w_loggers_w_logger_match(self):
from datetime import datetime
from gcloud._helpers import UTC
client = _Client(self.PROJECT)
PAYLOAD = {'message': 'MESSAGE', 'weather': 'partly cloudy'}
IID = 'IID'
NOW = datetime.utcnow().replace(tzinfo=UTC)
TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'jsonPayload': PAYLOAD,
'logName': LOG_NAME,
'insertId': IID,
'timestamp': TIMESTAMP,
}
LOGGER = object()
loggers = {LOG_NAME: LOGGER}
klass = self._getTargetClass()
entry = klass.from_api_repr(API_REPR, client, loggers=loggers)
self.assertEqual(entry.payload, PAYLOAD)
self.assertEqual(entry.insert_id, IID)
self.assertEqual(entry.timestamp, NOW)
self.assertTrue(entry.logger is LOGGER)


class TestProtobufEntry(unittest2.TestCase):

PROJECT = 'PROJECT'
LOGGER_NAME = 'LOGGER_NAME'

def _getTargetClass(self):
from gcloud.logging.entries import ProtobufEntry
return ProtobufEntry

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_ctor_defaults(self):
PAYLOAD = {'@type': 'type.googleapis.com/testing.example',
'message': 'MESSAGE', 'weather': 'partly cloudy'}
logger = _Logger(self.LOGGER_NAME, self.PROJECT)
entry = self._makeOne(PAYLOAD, logger)
self.assertEqual(entry.payload, PAYLOAD)
self.assertTrue(entry.logger is logger)
self.assertTrue(entry.insert_id is None)
self.assertTrue(entry.timestamp is None)

def test_ctor_explicit(self):
import datetime
PAYLOAD = {'@type': 'type.googleapis.com/testing.example',
'message': 'MESSAGE', 'weather': 'partly cloudy'}
IID = 'IID'
TIMESTAMP = datetime.datetime.now()
logger = _Logger(self.LOGGER_NAME, self.PROJECT)
entry = self._makeOne(PAYLOAD, logger, IID, TIMESTAMP)
self.assertEqual(entry.payload, PAYLOAD)
self.assertTrue(entry.logger is logger)
self.assertEqual(entry.insert_id, IID)
self.assertEqual(entry.timestamp, TIMESTAMP)

def test_from_api_repr_missing_data_no_loggers(self):
client = _Client(self.PROJECT)
PAYLOAD = {'@type': 'type.googleapis.com/testing.example',
'message': 'MESSAGE', 'weather': 'partly cloudy'}
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'protoPayload': PAYLOAD,
'logName': LOG_NAME,
}
klass = self._getTargetClass()
entry = klass.from_api_repr(API_REPR, client)
self.assertEqual(entry.payload, PAYLOAD)
self.assertTrue(entry.insert_id is None)
self.assertTrue(entry.timestamp is None)
logger = entry.logger
self.assertTrue(isinstance(logger, _Logger))
self.assertTrue(logger.client is client)
self.assertEqual(logger.name, self.LOGGER_NAME)

def test_from_api_repr_w_loggers_no_logger_match(self):
from datetime import datetime
from gcloud._helpers import UTC
client = _Client(self.PROJECT)
PAYLOAD = {'@type': 'type.googleapis.com/testing.example',
'message': 'MESSAGE', 'weather': 'partly cloudy'}
IID = 'IID'
NOW = datetime.utcnow().replace(tzinfo=UTC)
TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'protoPayload': PAYLOAD,
'logName': LOG_NAME,
'insertId': IID,
'timestamp': TIMESTAMP,
}
loggers = {}
klass = self._getTargetClass()
entry = klass.from_api_repr(API_REPR, client, loggers=loggers)
self.assertEqual(entry.payload, PAYLOAD)
self.assertEqual(entry.insert_id, IID)
self.assertEqual(entry.timestamp, NOW)
logger = entry.logger
self.assertTrue(isinstance(logger, _Logger))
self.assertTrue(logger.client is client)
self.assertEqual(logger.name, self.LOGGER_NAME)
self.assertEqual(loggers, {LOG_NAME: logger})

def test_from_api_repr_w_loggers_w_logger_match(self):
from datetime import datetime
from gcloud._helpers import UTC
client = _Client(self.PROJECT)
PAYLOAD = {'@type': 'type.googleapis.com/testing.example',
'message': 'MESSAGE', 'weather': 'partly cloudy'}
IID = 'IID'
NOW = datetime.utcnow().replace(tzinfo=UTC)
TIMESTAMP = _datetime_to_rfc3339_w_nanos(NOW)
LOG_NAME = 'projects/%s/logs/%s' % (self.PROJECT, self.LOGGER_NAME)
API_REPR = {
'protoPayload': PAYLOAD,
'dummyPayload': PAYLOAD,
'logName': LOG_NAME,
'insertId': IID,
'timestamp': TIMESTAMP,
Expand Down

0 comments on commit fd8965c

Please sign in to comment.