-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from plathanus-tech/12-no-way-of-deactivating-…
…notifications-from-being-sent Allow notifications to not be sent on `DEBUG=True`
- Loading branch information
Showing
7 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
APM_REQUEST_SAVE_QUERY_STRING = True | ||
|
||
APM_NOTIFY_USING_CELERY = False | ||
APM_NOTIFY_ON_DEBUG_TRUE = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any | ||
|
||
from django.test.client import RequestFactory | ||
import pytest | ||
|
||
from djapm.apm.contrib import _contribute_to_request | ||
from djapm.apm.types import ApmRequest, PatchedHttpRequest | ||
|
||
from tests.types import ApmRequestFactory | ||
|
||
|
||
@pytest.fixture | ||
def apm_rf(rf: RequestFactory, admin_user) -> ApmRequestFactory: | ||
"""Request factory for a APM view, that calls `_contribute_to_request`""" | ||
|
||
def wrapper( | ||
method: str, url: str, view: Any, drf_req: bool = False, user=None | ||
) -> PatchedHttpRequest: | ||
func = getattr(rf, method.lower()) | ||
req = func(url) | ||
req.user = user or admin_user | ||
|
||
rest_req = None | ||
if drf_req: | ||
rest_req = ApmRequest(req) | ||
|
||
_contribute_to_request(req, view=view, logger_name=None, rest_request=rest_req) | ||
return req | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/test_apm/test_middlewares/test_error_trace_middleware.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import warnings | ||
|
||
from django.urls import reverse | ||
|
||
from djapm.apm.middlewares import ErrorTraceMiddleware | ||
from polls.views import get_polls | ||
from tests.types import ApmRequestFactory | ||
|
||
|
||
def test_process_exception_warns_when_debug_true_and_notify_on_debug_true_is_false( | ||
settings, apm_rf: ApmRequestFactory | ||
): | ||
settings.DEBUG = True | ||
settings.APM_NOTIFY_ON_DEBUG_TRUE = False | ||
|
||
middleware = ErrorTraceMiddleware(lambda r: r) | ||
|
||
with warnings.catch_warnings(record=True) as w: | ||
request = apm_rf("GET", reverse("polls-list"), get_polls, drf_req=True) | ||
middleware.process_exception(request, ValueError("Oops, A exception occurred")) | ||
|
||
assert len(w) == 1, "No warnings were emitted" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from typing import Any, Protocol | ||
|
||
from djapm.apm.types import PatchedHttpRequest | ||
|
||
|
||
class ApmRequestFactory(Protocol): | ||
def __call__( | ||
self, method: str, url: str, view: Any, drf_req: bool = False, user=None | ||
) -> PatchedHttpRequest: | ||
... |