File: test_exception_handler.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (91 lines) | stat: -rw-r--r-- 2,557 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import sys

from strawberry.exceptions import MissingFieldAnnotationError
from strawberry.exceptions.handler import (
    reset_exception_handler,
    setup_exception_handler,
    strawberry_exception_handler,
)


def test_exception_handler(mocker):
    print_mock = mocker.patch("rich.print", autospec=True)

    class Query:
        abc: int

    exception = MissingFieldAnnotationError("abc", Query)

    strawberry_exception_handler(MissingFieldAnnotationError, exception, None)

    assert print_mock.call_args == mocker.call(exception)


def test_exception_handler_other_exceptions(mocker):
    print_mock = mocker.patch("rich.print", autospec=True)
    original_exception_mock = mocker.patch(
        "strawberry.exceptions.handler.sys.__excepthook__", autospec=True
    )

    exception = ValueError("abc")

    strawberry_exception_handler(ValueError, exception, None)

    assert print_mock.called is False
    assert original_exception_mock.call_args == mocker.call(ValueError, exception, None)


def test_exception_handler_uses_original_when_rich_is_not_installed(mocker):
    original_exception_mock = mocker.patch(
        "strawberry.exceptions.handler.sys.__excepthook__", autospec=True
    )

    mocker.patch.dict("sys.modules", {"rich": None})

    class Query:
        abc: int

    exception = MissingFieldAnnotationError("abc", Query)

    strawberry_exception_handler(MissingFieldAnnotationError, exception, None)

    assert original_exception_mock.call_args == mocker.call(
        MissingFieldAnnotationError, exception, None
    )


def test_exception_handler_uses_original_when_libcst_is_not_installed(mocker):
    original_exception_mock = mocker.patch(
        "strawberry.exceptions.handler.sys.__excepthook__", autospec=True
    )

    mocker.patch.dict("sys.modules", {"libcst": None})

    class Query:
        abc: int

    exception = MissingFieldAnnotationError("abc", Query)

    strawberry_exception_handler(MissingFieldAnnotationError, exception, None)

    assert original_exception_mock.call_args == mocker.call(
        MissingFieldAnnotationError, exception, None
    )


def test_setup_install_handler(mocker):
    reset_exception_handler()
    setup_exception_handler()

    assert sys.excepthook == strawberry_exception_handler


def test_setup_does_not_install_handler_when_disabled_via_env(mocker):
    reset_exception_handler()

    mocker.patch.dict(os.environ, {"STRAWBERRY_DISABLE_RICH_ERRORS": "true"})

    setup_exception_handler()

    assert sys.excepthook != strawberry_exception_handler