Debian Codegraph @

hypothesis-python/tests/cover/test_targeting.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 from operator import itemgetter
12
13 import pytest
14
15 from hypothesis import example, given, strategies as st, target
16 from hypothesis.control import current_build_context
17 from hypothesis.errors import InvalidArgument
18
19
20 @example(0.0, "this covers the branch where context.data is None")
21 @given(
22 observation=st.integers() | st.floats(allow_nan=False, allow_infinity=False),
23 label=st.text(),
24 )
25 def test_allowed_inputs_to_target(observation, label):
26 target(observation, label=label)
27
28
29 @given(
30 observation=st.integers(min_value=1)
31 | st.floats(min_value=1, allow_nan=False, allow_infinity=False),
32 label=st.sampled_from(["a", "few", "labels"]),
33 )
34 def test_allowed_inputs_to_target_fewer_labels(observation, label):
35 target(observation, label=label)
36
37
38 @given(st.floats(min_value=1, max_value=10))
39 def test_target_without_label(observation):
40 target(observation)
41
42
43 @given(
44 st.lists(
45 st.tuples(st.floats(allow_nan=False, allow_infinity=False), st.text()),
46 min_size=1,
47 unique_by=itemgetter(1),
48 )
49 )
50 def test_multiple_target_calls(args):
51 for observation, label in args:
52 target(observation, label=label)
53
54
55 @given(
56 st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=11, max_size=20)
57 )
58 def test_respects_max_pool_size(observations):
59 """Using many examples of several labels like this stresses the
60 pool-size logic and internal assertions in TargetSelector.
61 """
62 for i, obs in enumerate(observations):
63 target(obs, label=str(i))
64
65
66 def everything_except(type_):
67 # Note: we would usually stick to faster traditional or parametrized
68 # tests to check that invalid inputs are rejected, but for `target()`
69 # we need to use `@given` (to validate arguments instead of context)
70 # so we might as well apply this neat recipe.
71 return (
72 st.from_type(type)
73 .flatmap(st.from_type)
74 .filter(lambda x: not isinstance(x, type_))
75 )
76
77
78 @example(float("nan"), "")
79 @example(float("inf"), "")
80 @example(float("-inf"), "")
81 @example("1", "Non-float observations are invalid")
82 @example(0.0, ["a list of strings is not a valid label"])
83 @given(observation=everything_except((float, int)), label=everything_except(str))
84 def test_disallowed_inputs_to_target(observation, label):
85 with pytest.raises(InvalidArgument):
86 target(observation, label=label)
87
88
89 def test_cannot_target_outside_test():
90 with pytest.raises(InvalidArgument):
91 target(1.0, label="example label")
92
93
94 @given(st.none())
95 def test_cannot_target_same_label_twice(_):
96 if current_build_context().data.provider.avoid_realization:
97 pytest.skip("target() is a noop to avoid realizing arguments")
98 target(0.0, label="label")
99 with pytest.raises(InvalidArgument):
100 target(1.0, label="label")
101
102
103 @given(st.none())
104 def test_cannot_target_default_label_twice(_):
105 target(0.0)
106 with pytest.raises(InvalidArgument):
107 target(1.0)