Skip to content

Commit

Permalink
tests: Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thulio committed Jan 9, 2019
1 parent e46c69d commit c45ceb9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion google_cloud_logger/__init__.py
Expand Up @@ -73,7 +73,7 @@ def make_exception(self, record):
}

def make_metadata(self, record):
if record.exc_info:
if getattr(record, "exc_info", None):
return {
"userLabels": self.make_user_labels(record),
"exception": self.make_exception(record),
Expand Down
61 changes: 57 additions & 4 deletions test/google_cloud_logger/test_google_cloud_formatter.py
@@ -1,9 +1,35 @@
from collections import OrderedDict

import pytest

from google_cloud_logger import GoogleCloudFormatter

# from https://stackoverflow.com/a/19258720
class FakeCode(object):
def __init__(self, co_filename, co_name):
self.co_filename = co_filename
self.co_name = co_name


class FakeFrame(object):
def __init__(self, f_code, f_globals):
self.f_code = f_code
self.f_globals = f_globals


class FakeTraceback(object):
def __init__(self, frames, line_nums):
if len(frames) != len(line_nums):
raise ValueError("Ya messed up!")
self._frames = frames
self._line_nums = line_nums
self.tb_frame = frames[0]
self.tb_lineno = line_nums[0]

@property
def tb_next(self):
if len(self._frames) > 1:
return FakeTraceback(self._frames[1:], self._line_nums[1:])


@pytest.fixture
def formatter():
Expand Down Expand Up @@ -45,6 +71,25 @@ def record_with_extra_attribute(log_record_factory, mocker):
return record


@pytest.fixture
def record_with_exception(log_record_factory, mocker):
code = FakeCode("module.py", "function")
frame = FakeFrame(code, {})
traceback = FakeTraceback([frame], [1])
data = {
"asctime": "2018-08-30 20:40:57,245",
"filename": "_internal.py",
"funcName": "_log",
"lineno": "88",
"levelname": "WARNING",
"message": "farofa",
"exc_info": (Exception, "ERROR", traceback),
}
record = log_record_factory(**data)
record.getMessage = mocker.Mock(return_value=data["message"])
return record


def test_add_fields(formatter, record, mocker):
log_record = OrderedDict({})
mocker.patch.object(
Expand Down Expand Up @@ -96,14 +141,22 @@ def test_make_metadata(formatter, record):
assert metadata["userLabels"]["extra_field"] == "extra"


def test_make_metadata_with_extra_attribute(
formatter,
record_with_extra_attribute):
def test_make_metadata_with_extra_attribute(formatter, record_with_extra_attribute):
metadata = formatter.make_metadata(record_with_extra_attribute)

assert metadata["userLabels"]["extra_field"] == "extra"


def test_make_metadata_with_exception(formatter, record_with_exception):
metadata = formatter.make_metadata(record_with_exception)

assert metadata["exception"] == {
"class": Exception().__class__,
"message": "ERROR",
"traceback": ' File "module.py", line 1, in function\n',
}


def test_make_source_location(formatter, record):
assert formatter.make_source_location(record) == {
"file": "_internal.py",
Expand Down

0 comments on commit c45ceb9

Please sign in to comment.