File: test_middleware.py

package info (click to toggle)
sentry-python 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,004 kB
  • sloc: python: 55,908; makefile: 114; sh: 111; xml: 2
file content (34 lines) | stat: -rw-r--r-- 1,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import Optional

import pytest

from sentry_sdk.integrations.django.middleware import _wrap_middleware


def _sync_capable_middleware_factory(sync_capable):
    # type: (Optional[bool]) -> type
    """Create a middleware class with a sync_capable attribute set to the value passed to the factory.
    If the factory is called with None, the middleware class will not have a sync_capable attribute.
    """
    sc = sync_capable  # rename so we can set sync_capable in the class

    class TestMiddleware:
        nonlocal sc
        if sc is not None:
            sync_capable = sc

    return TestMiddleware


@pytest.mark.parametrize(
    ("middleware", "sync_capable"),
    (
        (_sync_capable_middleware_factory(True), True),
        (_sync_capable_middleware_factory(False), False),
        (_sync_capable_middleware_factory(None), True),
    ),
)
def test_wrap_middleware_sync_capable_attribute(middleware, sync_capable):
    wrapped_middleware = _wrap_middleware(middleware, "test_middleware")

    assert wrapped_middleware.sync_capable is sync_capable