Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintain project #49

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
Aleksandr Shtaub committed Jan 17, 2024
commit 1481e88e717662609913c4f29dd5330f6d6c9c08
2 changes: 1 addition & 1 deletion src/pylogctx/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def process_request(self, request):
try:
context.update(**extractor(request))
except Exception:
logger.exception()
logger.exception("Can't use extractor")
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.conf import settings

def pytest_configure():
settings.configure()
14 changes: 10 additions & 4 deletions tests/test_celery.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from celery import Celery, current_task
from celery.utils.log import get_task_logger
from celery import VERSION
from mock import patch

import pytest

from pylogctx.celery import LoggingTask

Expand Down Expand Up @@ -46,7 +47,12 @@ def my_task():
logger.info("I log!")
return context.as_dict()

result = my_task.apply()
with pytest.warns(
UserWarning,
match="Method `before_call` without args is deprecated"
):
result = my_task.apply()

if VERSION.major < 4:
result.maybe_reraise()
else:
Expand All @@ -70,8 +76,8 @@ def my_task():
assert not context.as_dict()


@patch.dict('pylogctx.core._adapter_mapping')
def test_adapter():
def test_adapter(mocker):
mocker.patch.dict('pylogctx.core._adapter_mapping')
from pylogctx import context, log_adapter

# To fill save context
Expand Down
19 changes: 9 additions & 10 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
import threading

from mock import patch
import pytest
import six

Expand All @@ -21,7 +20,7 @@ def record():
return LogRecord("foo", "INFO", "foo", 10, "waagh :(", (), None)


@pytest.yield_fixture
@pytest.fixture()
def context():
log_context.update(rid=42)
yield log_context
Expand Down Expand Up @@ -172,8 +171,8 @@ def run(self):
assert 'childField'not in fields


@patch.dict('pylogctx.core._adapter_mapping')
def test_adapter_mro(context):
def test_adapter_mro(mocker, context):
mocker.patch.dict('pylogctx.core._adapter_mapping')
from pylogctx import log_adapter

class Parent(object):
Expand All @@ -198,8 +197,8 @@ def parent_log_maker(instance):
assert child2.name in data


@patch.dict('pylogctx.core._adapter_mapping')
def test_adapter_manager(context):
def test_adapter_manager(mocker, context):
mocker.patch.dict('pylogctx.core._adapter_mapping')
from pylogctx import log_adapter

class Parent(object):
Expand All @@ -223,8 +222,8 @@ def parent_log_maker(instance):
assert child2.name in data


@patch.dict('pylogctx.core._adapter_mapping')
def test_adapter_with_parameters(context):
def test_adapter_with_parameters(mocker, context):
mocker.patch.dict('pylogctx.core._adapter_mapping')
from pylogctx import log_adapter

class Parent(object):
Expand Down Expand Up @@ -257,8 +256,8 @@ def parent_log_maker(instance, with_child=False):
assert 'baby2' in data['child']


@patch.dict('pylogctx.core._adapter_mapping')
def test_adapter_missing(context):
def test_adapter_missing(mocker, context):
mocker.patch.dict('pylogctx.core._adapter_mapping')
with pytest.raises(Exception):
context.update(object())

Expand Down
53 changes: 27 additions & 26 deletions tests/test_django.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.http import HttpResponse
import pytest
from django.core.exceptions import ImproperlyConfigured
from mock import patch, call


from pylogctx.django import (
Expand All @@ -14,11 +13,11 @@


@pytest.fixture()
def request():
def http_request():
return {'rid': 42}


@pytest.yield_fixture
@pytest.fixture()
def context():
log_context.update(rid=42)
yield log_context
Expand All @@ -36,48 +35,50 @@ def _failing_extractor(r):
raise KeyError


def test_middleware_no_extractor(request):
with pytest.raises(ImproperlyConfigured):
ExtractRequestContextMiddleware().process_request(request)
def test_middleware_no_extractor(http_request):
with pytest.raises(AttributeError):
ExtractRequestContextMiddleware(HttpResponse).process_request(http_request)


def test_middleware_extraction_failed(settings, mocker, http_request):
settings.PYLOGCTX_REQUEST_EXTRACTOR = _failing_extractor
logger_mock = mocker.patch('pylogctx.django.logger')

@patch('pylogctx.django.settings',
PYLOGCTX_REQUEST_EXTRACTOR=_failing_extractor)
def test_middleware_extraction_failed(settings, request):
with patch('pylogctx.django.logger') as m:
ExtractRequestContextMiddleware().process_request(request)
assert call.exception() in m.method_calls
ExtractRequestContextMiddleware(HttpResponse).process_request(http_request)
assert mocker.call.exception("Can't use extractor") in logger_mock.method_calls


@patch('pylogctx.django.settings', PYLOGCTX_REQUEST_EXTRACTOR=_extractor)
def test_middleware_context_extracted(settings, request, context):
ExtractRequestContextMiddleware().process_request(request)
def test_middleware_context_extracted(settings, http_request, context):
settings.PYLOGCTX_REQUEST_EXTRACTOR = _extractor

ExtractRequestContextMiddleware(HttpResponse).process_request(http_request)
fields = log_context.as_dict()
assert 'rid' in fields


def test_middleware_context_cleaned_on_response(context):
ExtractRequestContextMiddleware().process_response(None, None)
ExtractRequestContextMiddleware(HttpResponse).process_response(None, None)
assert not log_context.as_dict()


def test_middleware_context_cleaned_on_exception(context):
ExtractRequestContextMiddleware().process_exception(None, None)
ExtractRequestContextMiddleware(HttpResponse).process_exception(None, None)
assert not log_context.as_dict()


@patch.dict('pylogctx.core._adapter_mapping')
def test_middleware_adapter(request, context):
@log_adapter(request.__class__)
def adapter(request):
def test_middleware_adapter(mocker, http_request, context):
mocker.patch.dict('pylogctx.core._adapter_mapping')

@log_adapter(http_request.__class__)
def adapter(http_request):
return {
'djangoRequestId': id(request),
'djangoRequestId': id(http_request),
}

OuterMiddleware().process_request(request)
OuterMiddleware(HttpResponse).process_request(http_request)
fields = log_context.as_dict()
assert 'djangoRequestId' in fields


def test_middleware_missing_adapter(request, context):
OuterMiddleware().process_request(request)
def test_middleware_missing_adapter(http_request, context):
OuterMiddleware(HttpResponse).process_request(http_request)