File: test_exceptiongroup.py

package info (click to toggle)
python-hypothesis 6.138.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,272 kB
  • sloc: python: 62,853; ruby: 1,107; sh: 253; makefile: 41; javascript: 6
file content (179 lines) | stat: -rw-r--r-- 6,354 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

from contextlib import suppress

import pytest

from hypothesis import given, strategies as st
from hypothesis.errors import Flaky, FlakyBackendFailure, FlakyFailure, Frozen, StopTest
from hypothesis.internal.compat import BaseExceptionGroup, ExceptionGroup
from hypothesis.strategies import DataObject


def test_discard_frozen() -> None:
    @given(st.data())
    def discard_frozen(data: DataObject) -> None:
        # Accessing .conjecture_data is internal API. Other possible ways of freezing
        # data might go through ConjectureRunner.cached_test_function_ir or
        # ConjectureRunner.test_function
        data.conjecture_data.freeze()
        # Raising Frozen doesn't actually do anything, what matters is
        # whether the data is frozen.
        raise ExceptionGroup("", [Frozen()])

    discard_frozen()


def test_discard_multiple_frozen() -> None:
    @given(st.data())
    def discard_multiple_frozen(data: DataObject) -> None:
        data.conjecture_data.freeze()
        raise ExceptionGroup("", [Frozen(), Frozen()])

    discard_multiple_frozen()


def test_user_error_and_frozen() -> None:
    @given(st.data())
    def user_error_and_frozen(data: DataObject) -> None:
        raise ExceptionGroup("", [Frozen(), TypeError()])

    with pytest.raises(ExceptionGroup) as excinfo:
        user_error_and_frozen()
    e = excinfo.value
    assert isinstance(e, ExceptionGroup)
    assert len(e.exceptions) == 2
    assert isinstance(e.exceptions[0], Frozen)
    assert isinstance(e.exceptions[1], TypeError)


def test_user_error_and_stoptest() -> None:
    # if the code base had "proper" handling of exceptiongroups, the StopTest would
    # probably be handled by an except*.
    # TODO: which I suppose is an argument in favor of stripping it??
    @given(st.data())
    def user_error_and_stoptest(data: DataObject) -> None:
        raise BaseExceptionGroup(
            "", [StopTest(data.conjecture_data.testcounter), TypeError()]
        )

    with pytest.raises(BaseExceptionGroup) as excinfo:
        user_error_and_stoptest()
    e = excinfo.value
    assert isinstance(e, BaseExceptionGroup)
    assert len(e.exceptions) == 2
    assert isinstance(e.exceptions[0], StopTest)
    assert isinstance(e.exceptions[1], TypeError)


def test_lone_user_error() -> None:
    # we don't want to unwrap exceptiongroups, since they might contain
    # useful debugging info
    @given(st.data())
    def lone_user_error(data: DataObject) -> None:
        raise ExceptionGroup("foo", [TypeError()])

    with pytest.raises(ExceptionGroup) as excinfo:
        lone_user_error()
    e = excinfo.value
    assert isinstance(e, ExceptionGroup)
    assert len(e.exceptions) == 1
    assert isinstance(e.exceptions[0], TypeError)


def test_nested_stoptest() -> None:
    @given(st.data())
    def nested_stoptest(data: DataObject) -> None:
        raise BaseExceptionGroup(
            "",
            [BaseExceptionGroup("", [StopTest(data.conjecture_data.testcounter)])],
        )

    nested_stoptest()


def test_frozen_and_stoptest() -> None:
    # frozen+stoptest => strip frozen and let engine handle StopTest
    # actually.. I don't think I've got a live repo for this either.
    @given(st.data())
    def frozen_and_stoptest(data: DataObject) -> None:
        raise BaseExceptionGroup(
            "", [StopTest(data.conjecture_data.testcounter), Frozen()]
        )

    frozen_and_stoptest()


def test_multiple_stoptest_1() -> None:
    # multiple stoptest, reraise the one with lowest testcounter
    @given(st.data())
    def multiple_stoptest(data: DataObject) -> None:
        c = data.conjecture_data.testcounter
        raise BaseExceptionGroup("", [StopTest(c), StopTest(c + 1)])

    multiple_stoptest()


def test_multiple_stoptest_2() -> None:
    # the lower value is raised, which does not match data.conjecture_data.testcounter
    # so it is not handled by the engine
    @given(st.data())
    def multiple_stoptest_2(data: DataObject) -> None:
        c = data.conjecture_data.testcounter
        raise BaseExceptionGroup("", [StopTest(c), StopTest(c - 1)])

    with pytest.raises(StopTest):
        multiple_stoptest_2()


def test_stoptest_and_hypothesisexception() -> None:
    # current code raises the first hypothesisexception and throws away stoptest
    @given(st.data())
    def stoptest_and_hypothesisexception(data: DataObject) -> None:
        c = data.conjecture_data.testcounter
        raise BaseExceptionGroup("", [StopTest(c), Flaky()])

    with pytest.raises(Flaky):
        stoptest_and_hypothesisexception()


def test_multiple_hypothesisexception() -> None:
    # this can happen in several ways, see nocover/test_exceptiongroup.py
    @given(st.data())
    def stoptest_and_hypothesisexception(data: DataObject) -> None:
        c = data.conjecture_data.testcounter
        raise BaseExceptionGroup("", [StopTest(c), Flaky()])

    with pytest.raises(Flaky):
        stoptest_and_hypothesisexception()


@pytest.mark.parametrize("ExceptionClass", [FlakyFailure, FlakyBackendFailure])
def test_exceptiongroups_reconstruct_original_type(ExceptionClass):
    # contextlib.suppress uses .split, and if ExceptionClass does not implement
    # .derive, .split will return a standard ExceptionGroup instead of
    # ExceptionClass.
    # see https://github.com/python/cpython/issues/119287
    class UnrelatedException(Exception):
        pass

    with pytest.raises(ExceptionClass):
        with suppress(UnrelatedException):
            raise ExceptionClass("exception message", [Exception()])


@pytest.mark.parametrize("ExceptionClass", [FlakyFailure, FlakyBackendFailure])
def test_derived_exception_group(ExceptionClass):
    exception = ExceptionClass("exception message", [Exception()])
    # we don't expect a match, we just want to test .derive.
    match, rest = exception.split(())
    assert match is None
    assert isinstance(rest, ExceptionClass)