File: helpers.py

package info (click to toggle)
python-structlog 25.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,568 kB
  • sloc: python: 8,890; makefile: 138
file content (46 lines) | stat: -rw-r--r-- 975 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
35
36
37
38
39
40
41
42
43
44
45
46
# SPDX-License-Identifier: MIT OR Apache-2.0
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the MIT License.  See the LICENSE file in the root of this
# repository for complete details.

"""
Shared test utilities.
"""

from unittest.mock import Mock, seal

from structlog._log_levels import NAME_TO_LEVEL


stdlib_log_methods = [m for m in NAME_TO_LEVEL if m != "notset"]


class CustomError(Exception):
    """
    Custom exception for testing purposes.
    """


def stub(**kwargs):
    """
    Create a restrictive mock that prevents new attributes after creation.
    Similar to pretend.stub().
    """
    m = Mock(**kwargs)
    seal(m)

    return m


def raiser(exception):
    """
    Create a mock that raises the given exception when called.
    """
    return Mock(side_effect=exception)


def call_recorder(func):
    """
    Create a mock that records calls while executing func.
    """
    return Mock(side_effect=func)