| 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 |
import functools |
| 12 |
import random |
| 13 |
import sys |
| 14 |
from collections import defaultdict, namedtuple |
| 15 |
from dataclasses import dataclass |
| 16 |
from typing import Any |
| 17 |
|
| 18 |
import pytest |
| 19 |
|
| 20 |
from hypothesis import given, strategies as st |
| 21 |
from hypothesis.errors import InvalidArgument, Unsatisfiable |
| 22 |
from hypothesis.internal.conjecture.data import ConjectureData |
| 23 |
from hypothesis.internal.reflection import get_pretty_function_description |
| 24 |
from hypothesis.strategies._internal.utils import to_jsonable |
| 25 |
from hypothesis.utils.conventions import UniqueIdentifier |
| 26 |
|
| 27 |
from tests.common.debug import assert_simple_property, check_can_generate_examples |
| 28 |
from tests.common.utils import checks_deprecated_behaviour |
| 29 |
|
| 30 |
|
| 31 |
def test_or_errors_when_given_non_strategy(): |
| 32 |
bools = st.tuples(st.booleans()) |
| 33 |
with pytest.raises(ValueError): |
| 34 |
bools | "foo" |
| 35 |
|
| 36 |
|
| 37 |
SomeNamedTuple = namedtuple("SomeNamedTuple", ("a", "b")) |
| 38 |
|
| 39 |
|
| 40 |
def last(xs): |
| 41 |
t = None |
| 42 |
for x in xs: |
| 43 |
t = x |
| 44 |
return t |
| 45 |
|
| 46 |
|
| 47 |
def test_just_strategy_uses_repr(): |
| 48 |
class WeirdRepr: |
| 49 |
def __repr__(self): |
| 50 |
return "ABCDEFG" |
| 51 |
|
| 52 |
assert repr(st.just(WeirdRepr())) == f"just({WeirdRepr()!r})" |
| 53 |
|
| 54 |
|
| 55 |
def test_just_strategy_does_not_draw(): |
| 56 |
data = ConjectureData.for_choices([]) |
| 57 |
s = st.just("hello") |
| 58 |
assert s.do_draw(data) == "hello" |
| 59 |
|
| 60 |
|
| 61 |
def test_none_strategy_does_not_draw(): |
| 62 |
data = ConjectureData.for_choices([]) |
| 63 |
s = st.none() |
| 64 |
assert s.do_draw(data) is None |
| 65 |
|
| 66 |
|
| 67 |
def test_can_map(): |
| 68 |
s = st.integers().map(pack=lambda t: "foo") |
| 69 |
assert_simple_property(s, lambda v: v == "foo") |
| 70 |
|
| 71 |
|
| 72 |
def test_example_raises_unsatisfiable_when_too_filtered(): |
| 73 |
with pytest.raises(Unsatisfiable): |
| 74 |
check_can_generate_examples(st.integers().filter(lambda x: False)) |
| 75 |
|
| 76 |
|
| 77 |
def nameless_const(x): |
| 78 |
def f(u, v): |
| 79 |
return u |
| 80 |
|
| 81 |
return functools.partial(f, x) |
| 82 |
|
| 83 |
|
| 84 |
def test_can_map_nameless(): |
| 85 |
f = nameless_const(2) |
| 86 |
assert get_pretty_function_description(f) in repr(st.integers().map(f)) |
| 87 |
|
| 88 |
|
| 89 |
def test_can_flatmap_nameless(): |
| 90 |
f = nameless_const(st.just(3)) |
| 91 |
assert get_pretty_function_description(f) in repr(st.integers().flatmap(f)) |
| 92 |
|
| 93 |
|
| 94 |
def test_flatmap_with_invalid_expand(): |
| 95 |
with pytest.raises(InvalidArgument): |
| 96 |
check_can_generate_examples(st.just(100).flatmap(lambda n: "a")) |
| 97 |
|
| 98 |
|
| 99 |
_bad_random_strategy = st.lists(st.integers(), min_size=1).map(random.choice) |
| 100 |
|
| 101 |
|
| 102 |
@checks_deprecated_behaviour |
| 103 |
def test_use_of_global_random_is_deprecated_in_given(): |
| 104 |
check_can_generate_examples(_bad_random_strategy) |
| 105 |
|
| 106 |
|
| 107 |
@checks_deprecated_behaviour |
| 108 |
def test_use_of_global_random_is_deprecated_in_interactive_draws(): |
| 109 |
@given(st.data()) |
| 110 |
def inner(d): |
| 111 |
d.draw(_bad_random_strategy) |
| 112 |
|
| 113 |
inner() |
| 114 |
|
| 115 |
|
| 116 |
def test_jsonable(): |
| 117 |
assert to_jsonable(object(), avoid_realization=True) == "<symbolic>" |
| 118 |
assert isinstance(to_jsonable(object(), avoid_realization=False), str) |
| 119 |
|
| 120 |
|
| 121 |
@dataclass |
| 122 |
class HasDefaultDict: |
| 123 |
x: defaultdict |
| 124 |
|
| 125 |
|
| 126 |
def test_jsonable_defaultdict(): |
| 127 |
obj = HasDefaultDict(defaultdict(list)) |
| 128 |
obj.x["a"] = [42] |
| 129 |
assert to_jsonable(obj, avoid_realization=False) == {"x": {"a": [42]}} |
| 130 |
|
| 131 |
|
| 132 |
def test_jsonable_namedtuple(): |
| 133 |
Obj = namedtuple("Obj", ("x")) |
| 134 |
obj = Obj(10) |
| 135 |
assert to_jsonable(obj, avoid_realization=False) == {"x": 10} |
| 136 |
|
| 137 |
|
| 138 |
def test_jsonable_small_ints_are_ints(): |
| 139 |
n = 2**62 |
| 140 |
for avoid in (True, False): |
| 141 |
assert isinstance(to_jsonable(n, avoid_realization=avoid), int) |
| 142 |
assert to_jsonable(n, avoid_realization=avoid) == n |
| 143 |
|
| 144 |
|
| 145 |
def test_jsonable_large_ints_are_floats(): |
| 146 |
n = 2**63 |
| 147 |
assert isinstance(to_jsonable(n, avoid_realization=False), float) |
| 148 |
assert to_jsonable(n, avoid_realization=False) == float(n) |
| 149 |
assert to_jsonable(n, avoid_realization=True) == "<symbolic>" |
| 150 |
|
| 151 |
|
| 152 |
def test_jsonable_very_large_ints(): |
| 153 |
# previously caused OverflowError when casting to float. |
| 154 |
n = 2**1024 |
| 155 |
assert to_jsonable(n, avoid_realization=False) == sys.float_info.max |
| 156 |
assert to_jsonable(n, avoid_realization=True) == "<symbolic>" |
| 157 |
|
| 158 |
|
| 159 |
@dataclass |
| 160 |
class HasCustomJsonFormat: |
| 161 |
x: str |
| 162 |
|
| 163 |
def to_json(self): |
| 164 |
return "surprise!" |
| 165 |
|
| 166 |
|
| 167 |
def test_jsonable_override(): |
| 168 |
obj = HasCustomJsonFormat("expected") |
| 169 |
assert to_jsonable(obj, avoid_realization=False) == "surprise!" |
| 170 |
assert to_jsonable(obj, avoid_realization=True) == "<symbolic>" |
| 171 |
|
| 172 |
|
| 173 |
@dataclass |
| 174 |
class Inner: |
| 175 |
value: int |
| 176 |
|
| 177 |
def to_json(self): |
| 178 |
return "custom" |
| 179 |
|
| 180 |
|
| 181 |
@dataclass |
| 182 |
class Outer: |
| 183 |
inner: Inner |
| 184 |
|
| 185 |
|
| 186 |
def test_jsonable_to_json_nested(): |
| 187 |
obj = Outer(Inner(42)) |
| 188 |
assert to_jsonable(obj, avoid_realization=False) == {"inner": "custom"} |
| 189 |
assert to_jsonable(obj, avoid_realization=True) == "<symbolic>" |
| 190 |
|
| 191 |
|
| 192 |
recursive_list = [] |
| 193 |
recursive_list.append(recursive_list) |
| 194 |
|
| 195 |
recursive_dict = {} |
| 196 |
recursive_dict["a"] = recursive_dict |
| 197 |
|
| 198 |
mutual1 = [] |
| 199 |
mutual2 = [mutual1] |
| 200 |
mutual1.append(mutual2) |
| 201 |
|
| 202 |
shared = UniqueIdentifier("shared") |
| 203 |
|
| 204 |
|
| 205 |
@dataclass |
| 206 |
class A: |
| 207 |
a: Any |
| 208 |
b: Any |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.parametrize( |
| 212 |
"obj, value", |
| 213 |
[ |
| 214 |
(recursive_list, ["[...]"]), |
| 215 |
(recursive_dict, {"a": "{...}"}), |
| 216 |
(mutual1, [["[...]"]]), |
| 217 |
(mutual2, [["[...]"]]), |
| 218 |
# same id object in different fields. no cycle |
| 219 |
(A(a=shared, b=shared), {"a": "shared", "b": "shared"}), |
| 220 |
(A(a=recursive_list, b=recursive_dict), {"a": ["[...]"], "b": {"a": "{...}"}}), |
| 221 |
], |
| 222 |
) |
| 223 |
def test_to_jsonable_handles_reference_cycles(obj, value): |
| 224 |
assert to_jsonable(obj, avoid_realization=False) == value |
| 225 |
|
| 226 |
|
| 227 |
def test_deferred_strategy_draw(): |
| 228 |
strategy = st.deferred(lambda: st.integers()) |
| 229 |
assert strategy.do_draw(ConjectureData.for_choices([0])) == 0 |