| 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 collections |
| 12 |
import decimal |
| 13 |
import enum |
| 14 |
import fractions |
| 15 |
import math |
| 16 |
import warnings |
| 17 |
from datetime import date, datetime, time, timedelta |
| 18 |
from ipaddress import IPv4Network, IPv6Network |
| 19 |
|
| 20 |
import pytest |
| 21 |
|
| 22 |
from hypothesis import Phase, given, settings, strategies as st |
| 23 |
from hypothesis.errors import HypothesisWarning, InvalidArgument |
| 24 |
from hypothesis.vendor.pretty import pretty |
| 25 |
|
| 26 |
from tests.common.debug import check_can_generate_examples, minimal |
| 27 |
from tests.common.utils import Why, xfail_on_crosshair |
| 28 |
|
| 29 |
# Use `pretty` instead of `repr` for building test names, so that set and dict |
| 30 |
# parameters print consistently across multiple worker processes with different |
| 31 |
# PYTHONHASHSEED values. |
| 32 |
|
| 33 |
|
| 34 |
def fn_test(*fnkwargs): |
| 35 |
fnkwargs = list(fnkwargs) |
| 36 |
return pytest.mark.parametrize( |
| 37 |
("fn", "args"), |
| 38 |
fnkwargs, |
| 39 |
ids=[ |
| 40 |
"{}({})".format(fn.__name__, ", ".join(map(pretty, args))) |
| 41 |
for fn, args in fnkwargs |
| 42 |
], |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
def fn_ktest(*fnkwargs): |
| 47 |
fnkwargs = list(fnkwargs) |
| 48 |
return pytest.mark.parametrize( |
| 49 |
("fn", "kwargs"), |
| 50 |
fnkwargs, |
| 51 |
ids=[f"{fn.__name__}(**{pretty(kwargs)})" for fn, kwargs in fnkwargs], |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
@fn_ktest( |
| 56 |
(st.integers, {"min_value": math.nan}), |
| 57 |
(st.integers, {"min_value": 2, "max_value": 1}), |
| 58 |
(st.integers, {"min_value": math.nan}), |
| 59 |
(st.integers, {"max_value": math.nan}), |
| 60 |
(st.integers, {"min_value": decimal.Decimal("1.5")}), |
| 61 |
(st.integers, {"max_value": decimal.Decimal("1.5")}), |
| 62 |
(st.integers, {"min_value": -1.5, "max_value": -0.5}), |
| 63 |
(st.integers, {"min_value": 0.1, "max_value": 0.2}), |
| 64 |
(st.dates, {"min_value": "fish"}), |
| 65 |
(st.dates, {"max_value": "fish"}), |
| 66 |
(st.dates, {"min_value": date(2017, 8, 22), "max_value": date(2017, 8, 21)}), |
| 67 |
(st.datetimes, {"min_value": "fish"}), |
| 68 |
(st.datetimes, {"max_value": "fish"}), |
| 69 |
(st.datetimes, {"allow_imaginary": 0}), |
| 70 |
( |
| 71 |
st.datetimes, |
| 72 |
{"min_value": datetime(2017, 8, 22), "max_value": datetime(2017, 8, 21)}, |
| 73 |
), |
| 74 |
(st.decimals, {"min_value": math.nan}), |
| 75 |
(st.decimals, {"max_value": math.nan}), |
| 76 |
(st.decimals, {"min_value": 2, "max_value": 1}), |
| 77 |
(st.decimals, {"max_value": "-snan"}), |
| 78 |
(st.decimals, {"max_value": complex(1, 2)}), |
| 79 |
(st.decimals, {"places": -1}), |
| 80 |
(st.decimals, {"places": 0.5}), |
| 81 |
(st.decimals, {"max_value": 0.0, "min_value": 1.0}), |
| 82 |
(st.decimals, {"min_value": 1.0, "max_value": 0.0}), |
| 83 |
(st.decimals, {"min_value": 0.0, "max_value": 1.0, "allow_infinity": True}), |
| 84 |
(st.decimals, {"min_value": "inf"}), |
| 85 |
(st.decimals, {"max_value": "-inf"}), |
| 86 |
(st.decimals, {"min_value": "-inf", "allow_infinity": False}), |
| 87 |
(st.decimals, {"max_value": "inf", "allow_infinity": False}), |
| 88 |
(st.decimals, {"min_value": complex(1, 2)}), |
| 89 |
(st.decimals, {"min_value": "0.1", "max_value": "0.9", "places": 0}), |
| 90 |
(st.decimals, {"min_value": fractions.Fraction(1, 3)}), |
| 91 |
(st.decimals, {"max_value": fractions.Fraction(2, 3), "places": 1}), |
| 92 |
( |
| 93 |
st.dictionaries, |
| 94 |
{"keys": st.booleans(), "values": st.booleans(), "min_size": 10, "max_size": 1}, |
| 95 |
), |
| 96 |
(st.floats, {"min_value": math.nan}), |
| 97 |
(st.floats, {"max_value": math.nan}), |
| 98 |
(st.floats, {"min_value": complex(1, 2)}), |
| 99 |
(st.floats, {"max_value": complex(1, 2)}), |
| 100 |
(st.floats, {"exclude_min": None}), |
| 101 |
(st.floats, {"exclude_max": None}), |
| 102 |
(st.floats, {"exclude_min": True}), # because min_value=None |
| 103 |
(st.floats, {"exclude_max": True}), # because max_value=None |
| 104 |
(st.floats, {"min_value": 1.8, "width": 32}), |
| 105 |
(st.floats, {"max_value": 1.8, "width": 32}), |
| 106 |
(st.fractions, {"min_value": 2, "max_value": 1}), |
| 107 |
(st.fractions, {"min_value": math.nan}), |
| 108 |
(st.fractions, {"max_value": math.nan}), |
| 109 |
(st.fractions, {"max_denominator": 0}), |
| 110 |
(st.fractions, {"max_denominator": 1.5}), |
| 111 |
(st.fractions, {"min_value": complex(1, 2)}), |
| 112 |
(st.fractions, {"min_value": "1/3", "max_value": "1/2", "max_denominator": 2}), |
| 113 |
(st.fractions, {"min_value": "0", "max_value": "1/3", "max_denominator": 2}), |
| 114 |
(st.fractions, {"min_value": "1/3", "max_value": "1/3", "max_denominator": 2}), |
| 115 |
(st.lists, {"elements": st.integers(), "min_size": 10, "max_size": 9}), |
| 116 |
(st.lists, {"elements": st.integers(), "min_size": -10, "max_size": -9}), |
| 117 |
(st.lists, {"elements": st.integers(), "max_size": -9}), |
| 118 |
(st.lists, {"elements": st.integers(), "min_size": -10}), |
| 119 |
(st.lists, {"elements": st.integers(), "min_size": math.nan}), |
| 120 |
(st.lists, {"elements": st.nothing(), "max_size": 1}), |
| 121 |
(st.lists, {"elements": "hi"}), |
| 122 |
(st.lists, {"elements": st.integers(), "unique_by": 1}), |
| 123 |
(st.lists, {"elements": st.integers(), "unique_by": ()}), |
| 124 |
(st.lists, {"elements": st.integers(), "unique_by": (1,)}), |
| 125 |
(st.lists, {"elements": st.sampled_from([0, 1]), "min_size": 3, "unique": True}), |
| 126 |
(st.lists, {"elements": st.none(), "min_size": 100_000}), |
| 127 |
(st.lists, {"elements": st.none(), "min_size": 100_000, "unique": True}), |
| 128 |
( |
| 129 |
st.lists, |
| 130 |
{"elements": st.sampled_from([1, 2]), "min_size": 100_000, "unique": True}, |
| 131 |
), |
| 132 |
(st.text, {"min_size": 10, "max_size": 9}), |
| 133 |
(st.text, {"alphabet": [1]}), |
| 134 |
(st.text, {"alphabet": ["abc"]}), |
| 135 |
(st.text, {"alphabet": st.just("abc")}), |
| 136 |
(st.text, {"alphabet": st.sampled_from(["abc", "def"])}), |
| 137 |
(st.text, {"alphabet": st.just(123)}), |
| 138 |
(st.text, {"alphabet": st.sampled_from([123, 456])}), |
| 139 |
(st.text, {"alphabet": st.builds(lambda: "abc")}), |
| 140 |
(st.text, {"alphabet": st.builds(lambda: 123)}), |
| 141 |
(st.text, {"alphabet": "abc", "min_size": 100_000}), |
| 142 |
(st.from_regex, {"regex": 123}), |
| 143 |
(st.from_regex, {"regex": b"abc", "alphabet": "abc"}), |
| 144 |
(st.from_regex, {"regex": b"abc", "alphabet": b"def"}), |
| 145 |
(st.from_regex, {"regex": "abc", "alphabet": "def"}), |
| 146 |
(st.from_regex, {"regex": "aa|bb", "alphabet": "c"}), |
| 147 |
(st.from_regex, {"regex": "[abc]", "alphabet": "def"}), |
| 148 |
(st.from_regex, {"regex": "[ab]x[de]", "alphabet": "abcdef"}), |
| 149 |
(st.from_regex, {"regex": "...", "alphabet": st.builds(lambda: "a")}), |
| 150 |
(st.from_regex, {"regex": "abc", "alphabet": st.sampled_from("def")}), |
| 151 |
(st.from_regex, {"regex": "abc", "alphabet": st.characters(min_codepoint=128)}), |
| 152 |
(st.from_regex, {"regex": "abc", "alphabet": 123}), |
| 153 |
(st.binary, {"min_size": 10, "max_size": 9}), |
| 154 |
(st.floats, {"min_value": math.nan}), |
| 155 |
(st.floats, {"min_value": "0"}), |
| 156 |
(st.floats, {"max_value": "0"}), |
| 157 |
(st.floats, {"min_value": 0.0, "max_value": -0.0}), |
| 158 |
(st.floats, {"min_value": 0.0, "max_value": 1.0, "allow_infinity": True}), |
| 159 |
(st.floats, {"max_value": 0.0, "min_value": 1.0}), |
| 160 |
(st.floats, {"min_value": 0.0, "allow_nan": True}), |
| 161 |
(st.floats, {"max_value": 0.0, "allow_nan": True}), |
| 162 |
(st.floats, {"min_value": 0.0, "max_value": 1.0, "allow_infinity": True}), |
| 163 |
(st.floats, {"min_value": math.inf, "allow_infinity": False}), |
| 164 |
(st.floats, {"max_value": -math.inf, "allow_infinity": False}), |
| 165 |
(st.complex_numbers, {"min_magnitude": None}), |
| 166 |
(st.complex_numbers, {"min_magnitude": math.nan}), |
| 167 |
(st.complex_numbers, {"max_magnitude": math.nan}), |
| 168 |
(st.complex_numbers, {"max_magnitude": complex(1, 2)}), |
| 169 |
(st.complex_numbers, {"min_magnitude": -1}), |
| 170 |
(st.complex_numbers, {"max_magnitude": -1}), |
| 171 |
(st.complex_numbers, {"min_magnitude": 3, "max_magnitude": 2}), |
| 172 |
(st.complex_numbers, {"max_magnitude": 2, "allow_infinity": True}), |
| 173 |
(st.complex_numbers, {"max_magnitude": 2, "allow_nan": True}), |
| 174 |
(st.complex_numbers, {"width": None}), |
| 175 |
# Conceivable mistake when misunderstanding width for individual component widths: |
| 176 |
(st.complex_numbers, {"width": 16}), |
| 177 |
# Unsupported as of now: |
| 178 |
(st.complex_numbers, {"width": 196}), |
| 179 |
(st.complex_numbers, {"width": 256}), |
| 180 |
(st.fixed_dictionaries, {"mapping": "fish"}), |
| 181 |
(st.fixed_dictionaries, {"mapping": {1: "fish"}}), |
| 182 |
(st.fixed_dictionaries, {"mapping": {}, "optional": "fish"}), |
| 183 |
(st.fixed_dictionaries, {"mapping": {}, "optional": {1: "fish"}}), |
| 184 |
(st.fixed_dictionaries, {"mapping": {}, "optional": collections.OrderedDict()}), |
| 185 |
(st.fixed_dictionaries, {"mapping": {1: st.none()}, "optional": {1: st.none()}}), |
| 186 |
(st.dictionaries, {"keys": st.integers(), "values": 1}), |
| 187 |
(st.dictionaries, {"keys": 1, "values": st.integers()}), |
| 188 |
(st.text, {"alphabet": "", "min_size": 1}), |
| 189 |
(st.timedeltas, {"min_value": "fish"}), |
| 190 |
(st.timedeltas, {"max_value": "fish"}), |
| 191 |
( |
| 192 |
st.timedeltas, |
| 193 |
{"min_value": timedelta(hours=1), "max_value": timedelta(minutes=1)}, |
| 194 |
), |
| 195 |
(st.times, {"min_value": "fish"}), |
| 196 |
(st.times, {"max_value": "fish"}), |
| 197 |
(st.times, {"min_value": time(2, 0), "max_value": time(1, 0)}), |
| 198 |
(st.uuids, {"version": 6}), |
| 199 |
(st.characters, {"min_codepoint": -1}), |
| 200 |
(st.characters, {"min_codepoint": "1"}), |
| 201 |
(st.characters, {"max_codepoint": -1}), |
| 202 |
(st.characters, {"max_codepoint": "1"}), |
| 203 |
(st.characters, {"categories": []}), |
| 204 |
(st.characters, {"categories": ["Nd"], "exclude_categories": ["Nd"]}), |
| 205 |
(st.characters, {"whitelist_categories": ["Nd"], "blacklist_categories": ["Nd"]}), |
| 206 |
(st.characters, {"include_characters": "a", "blacklist_characters": "b"}), |
| 207 |
(st.characters, {"codec": 100}), |
| 208 |
(st.characters, {"codec": "this is not a valid codec name"}), |
| 209 |
(st.characters, {"codec": "ascii", "include_characters": "é"}), |
| 210 |
(st.characters, {"codec": "utf-8", "categories": "Cs"}), |
| 211 |
(st.slices, {"size": None}), |
| 212 |
(st.slices, {"size": "chips"}), |
| 213 |
(st.slices, {"size": -1}), |
| 214 |
(st.slices, {"size": 2.3}), |
| 215 |
(st.sampled_from, {"elements": ()}), |
| 216 |
(st.ip_addresses, {"v": "4"}), |
| 217 |
(st.ip_addresses, {"v": 4.0}), |
| 218 |
(st.ip_addresses, {"v": 5}), |
| 219 |
(st.ip_addresses, {"v": 4, "network": "::/64"}), |
| 220 |
(st.ip_addresses, {"v": 6, "network": "127.0.0.0/8"}), |
| 221 |
(st.ip_addresses, {"network": b"127.0.0.0/8"}), # only unicode strings are valid |
| 222 |
(st.ip_addresses, {"network": b"::/64"}), |
| 223 |
(st.randoms, {"use_true_random": "False"}), |
| 224 |
(st.randoms, {"note_method_calls": "True"}), |
| 225 |
) |
| 226 |
def test_validates_keyword_arguments(fn, kwargs): |
| 227 |
with pytest.raises(InvalidArgument): |
| 228 |
check_can_generate_examples(fn(**kwargs)) |
| 229 |
|
| 230 |
|
| 231 |
@fn_ktest( |
| 232 |
(st.integers, {"min_value": 0}), |
| 233 |
(st.integers, {"min_value": 11}), |
| 234 |
(st.integers, {"min_value": 11, "max_value": 100}), |
| 235 |
(st.integers, {"max_value": 0}), |
| 236 |
(st.integers, {"min_value": -2, "max_value": -1}), |
| 237 |
(st.decimals, {"min_value": 1.0, "max_value": 1.5}), |
| 238 |
(st.decimals, {"min_value": "1.0", "max_value": "1.5"}), |
| 239 |
(st.decimals, {"min_value": decimal.Decimal("1.5")}), |
| 240 |
(st.decimals, {"max_value": 1.0, "min_value": -1.0, "allow_infinity": False}), |
| 241 |
(st.decimals, {"min_value": 1.0, "allow_nan": False}), |
| 242 |
(st.decimals, {"max_value": 1.0, "allow_nan": False}), |
| 243 |
(st.decimals, {"max_value": 1.0, "min_value": -1.0, "allow_nan": False}), |
| 244 |
(st.decimals, {"min_value": "-inf"}), |
| 245 |
(st.decimals, {"max_value": "inf"}), |
| 246 |
(st.decimals, {"min_value": fractions.Fraction(3, 20)}), |
| 247 |
(st.decimals, {"max_value": fractions.Fraction(1, 8), "places": 10}), |
| 248 |
(st.fractions, {"min_value": -1, "max_value": 1, "max_denominator": 1000}), |
| 249 |
(st.fractions, {"min_value": 1, "max_value": 1}), |
| 250 |
(st.fractions, {"min_value": 1, "max_value": 1, "max_denominator": 2}), |
| 251 |
(st.fractions, {"min_value": 1.0}), |
| 252 |
(st.fractions, {"min_value": decimal.Decimal("1.0")}), |
| 253 |
(st.fractions, {"min_value": fractions.Fraction(1, 2)}), |
| 254 |
(st.fractions, {"min_value": "1/2", "max_denominator": 2}), |
| 255 |
(st.fractions, {"max_value": "1/2", "max_denominator": 3}), |
| 256 |
(st.lists, {"elements": st.nothing(), "max_size": 0}), |
| 257 |
(st.lists, {"elements": st.integers()}), |
| 258 |
(st.lists, {"elements": st.integers(), "max_size": 5}), |
| 259 |
(st.lists, {"elements": st.booleans(), "min_size": 5}), |
| 260 |
(st.lists, {"elements": st.booleans(), "min_size": 5, "max_size": 10}), |
| 261 |
(st.sets, {"min_size": 10, "max_size": 10, "elements": st.integers()}), |
| 262 |
(st.booleans, {}), |
| 263 |
(st.just, {"value": "hi"}), |
| 264 |
(st.integers, {"min_value": 12, "max_value": 12}), |
| 265 |
(st.floats, {}), |
| 266 |
(st.floats, {"min_value": 1.0}), |
| 267 |
(st.floats, {"max_value": 1.0}), |
| 268 |
(st.floats, {"min_value": math.inf}), |
| 269 |
(st.floats, {"max_value": -math.inf}), |
| 270 |
(st.floats, {"max_value": 1.0, "min_value": -1.0}), |
| 271 |
(st.floats, {"max_value": 1.0, "min_value": -1.0, "allow_infinity": False}), |
| 272 |
(st.floats, {"min_value": 1.0, "allow_nan": False}), |
| 273 |
(st.floats, {"max_value": 1.0, "allow_nan": False}), |
| 274 |
(st.floats, {"max_value": 1.0, "min_value": -1.0, "allow_nan": False}), |
| 275 |
(st.complex_numbers, {}), |
| 276 |
(st.complex_numbers, {"min_magnitude": 3, "max_magnitude": 3}), |
| 277 |
(st.complex_numbers, {"max_magnitude": 0}), |
| 278 |
(st.complex_numbers, {"allow_nan": True}), |
| 279 |
(st.complex_numbers, {"allow_nan": True, "allow_infinity": True}), |
| 280 |
(st.complex_numbers, {"allow_nan": True, "allow_infinity": False}), |
| 281 |
(st.complex_numbers, {"allow_nan": False}), |
| 282 |
(st.complex_numbers, {"allow_nan": False, "allow_infinity": True}), |
| 283 |
(st.complex_numbers, {"allow_nan": False, "allow_infinity": False}), |
| 284 |
(st.complex_numbers, {"max_magnitude": math.inf, "allow_infinity": True}), |
| 285 |
(st.complex_numbers, {"width": 32}), |
| 286 |
(st.complex_numbers, {"width": 64}), |
| 287 |
(st.complex_numbers, {"width": 128}), |
| 288 |
(st.sampled_from, {"elements": [1]}), |
| 289 |
(st.sampled_from, {"elements": [1, 2, 3]}), |
| 290 |
(st.fixed_dictionaries, {"mapping": {1: st.integers()}}), |
| 291 |
(st.dictionaries, {"keys": st.booleans(), "values": st.integers()}), |
| 292 |
(st.text, {"alphabet": "abc"}), |
| 293 |
(st.text, {"alphabet": set("abc")}), |
| 294 |
(st.text, {"alphabet": ""}), |
| 295 |
(st.text, {"alphabet": st.just("a")}), |
| 296 |
(st.text, {"alphabet": st.sampled_from("abc")}), |
| 297 |
(st.text, {"alphabet": st.builds(lambda: "a")}), |
| 298 |
(st.characters, {"codec": "ascii"}), |
| 299 |
(st.characters, {"codec": "latin1"}), |
| 300 |
(st.characters, {"categories": ["N"]}), |
| 301 |
(st.characters, {"exclude_categories": []}), |
| 302 |
(st.characters, {"whitelist_characters": "a", "codec": "ascii"}), |
| 303 |
(st.characters, {"blacklist_characters": "a"}), |
| 304 |
(st.characters, {"whitelist_categories": ["Nd"]}), |
| 305 |
(st.characters, {"blacklist_categories": ["Nd"]}), |
| 306 |
(st.from_regex, {"regex": "abc", "alphabet": "abc"}), |
| 307 |
(st.from_regex, {"regex": "abc", "alphabet": "abcdef"}), |
| 308 |
(st.from_regex, {"regex": "[abc]", "alphabet": "abcdef"}), |
| 309 |
(st.from_regex, {"regex": "[a-f]", "alphabet": "abef"}), |
| 310 |
(st.from_regex, {"regex": "[a-d]", "alphabet": "def"}), |
| 311 |
(st.from_regex, {"regex": "[f-z]", "alphabet": "def"}), |
| 312 |
(st.from_regex, {"regex": "abc", "alphabet": st.sampled_from("abc")}), |
| 313 |
(st.from_regex, {"regex": "abc", "alphabet": st.characters(codec="ascii")}), |
| 314 |
(st.ip_addresses, {}), |
| 315 |
(st.ip_addresses, {"v": 4}), |
| 316 |
(st.ip_addresses, {"v": 6}), |
| 317 |
(st.ip_addresses, {"network": "127.0.0.0/8"}), |
| 318 |
(st.ip_addresses, {"network": "::/64"}), |
| 319 |
(st.ip_addresses, {"v": 4, "network": "127.0.0.0/8"}), |
| 320 |
(st.ip_addresses, {"v": 6, "network": "::/64"}), |
| 321 |
(st.ip_addresses, {"network": IPv4Network("127.0.0.0/8")}), |
| 322 |
(st.ip_addresses, {"network": IPv6Network("::/64")}), |
| 323 |
(st.ip_addresses, {"v": 4, "network": IPv4Network("127.0.0.0/8")}), |
| 324 |
(st.ip_addresses, {"v": 6, "network": IPv6Network("::/64")}), |
| 325 |
) |
| 326 |
def test_produces_valid_examples_from_keyword(fn, kwargs): |
| 327 |
check_can_generate_examples(fn(**kwargs)) |
| 328 |
|
| 329 |
|
| 330 |
@fn_test( |
| 331 |
(st.one_of, (1,)), |
| 332 |
(st.one_of, (1, st.integers())), |
| 333 |
(st.tuples, (1,)), |
| 334 |
) |
| 335 |
def test_validates_args(fn, args): |
| 336 |
with pytest.raises(InvalidArgument): |
| 337 |
check_can_generate_examples(fn(*args)) |
| 338 |
|
| 339 |
|
| 340 |
@fn_test( |
| 341 |
(st.one_of, (st.booleans(), st.tuples(st.booleans()))), |
| 342 |
(st.one_of, (st.booleans(),)), |
| 343 |
(st.text, ()), |
| 344 |
(st.binary, ()), |
| 345 |
(st.builds, (lambda x, y: x + y, st.integers(), st.integers())), |
| 346 |
) |
| 347 |
def test_produces_valid_examples_from_args(fn, args): |
| 348 |
check_can_generate_examples(fn(*args)) |
| 349 |
|
| 350 |
|
| 351 |
def test_build_class_with_target_kwarg(): |
| 352 |
NamedTupleWithTargetField = collections.namedtuple("Something", ["target"]) |
| 353 |
check_can_generate_examples( |
| 354 |
st.builds(NamedTupleWithTargetField, target=st.integers()) |
| 355 |
) |
| 356 |
|
| 357 |
|
| 358 |
def test_builds_raises_with_no_target(): |
| 359 |
with pytest.raises(TypeError): |
| 360 |
check_can_generate_examples(st.builds()) |
| 361 |
|
| 362 |
|
| 363 |
@pytest.mark.parametrize("non_callable", [1, "abc", st.integers()]) |
| 364 |
def test_builds_raises_if_non_callable_as_target_kwarg(non_callable): |
| 365 |
with pytest.raises(TypeError): |
| 366 |
check_can_generate_examples(st.builds(target=non_callable)) |
| 367 |
|
| 368 |
|
| 369 |
@pytest.mark.parametrize("non_callable", [1, "abc", st.integers()]) |
| 370 |
def test_builds_raises_if_non_callable_as_first_arg(non_callable): |
| 371 |
# If there are any positional arguments, then the target (which must be |
| 372 |
# callable) must be specified as the first one. |
| 373 |
with pytest.raises(InvalidArgument): |
| 374 |
check_can_generate_examples(st.builds(non_callable, target=lambda x: x)) |
| 375 |
|
| 376 |
|
| 377 |
def test_tuples_raise_error_on_bad_kwargs(): |
| 378 |
with pytest.raises(TypeError): |
| 379 |
st.tuples(stuff="things") |
| 380 |
|
| 381 |
|
| 382 |
@given(st.lists(st.booleans(), min_size=10, max_size=10)) |
| 383 |
def test_has_specified_length(xs): |
| 384 |
assert len(xs) == 10 |
| 385 |
|
| 386 |
|
| 387 |
@given(st.integers(max_value=100)) |
| 388 |
@settings(max_examples=100) |
| 389 |
def test_has_upper_bound(x): |
| 390 |
assert x <= 100 |
| 391 |
|
| 392 |
|
| 393 |
@given(st.integers(min_value=100)) |
| 394 |
def test_has_lower_bound(x): |
| 395 |
assert x >= 100 |
| 396 |
|
| 397 |
|
| 398 |
@given(st.integers(min_value=1, max_value=2)) |
| 399 |
def test_is_in_bounds(x): |
| 400 |
assert 1 <= x <= 2 |
| 401 |
|
| 402 |
|
| 403 |
@given(st.fractions(min_value=-1, max_value=1, max_denominator=1000)) |
| 404 |
def test_fraction_is_in_bounds(x): |
| 405 |
assert -1 <= x <= 1 |
| 406 |
assert abs(x.denominator) <= 1000 |
| 407 |
|
| 408 |
|
| 409 |
@given(st.fractions(min_value=fractions.Fraction(1, 2))) |
| 410 |
def test_fraction_gt_positive(x): |
| 411 |
assert fractions.Fraction(1, 2) <= x |
| 412 |
|
| 413 |
|
| 414 |
@given(st.fractions(max_value=fractions.Fraction(-1, 2))) |
| 415 |
def test_fraction_lt_negative(x): |
| 416 |
assert x <= fractions.Fraction(-1, 2) |
| 417 |
|
| 418 |
|
| 419 |
@given(st.decimals(min_value=-1.5, max_value=1.5)) |
| 420 |
def test_decimal_is_in_bounds(x): |
| 421 |
assert decimal.Decimal("-1.5") <= x <= decimal.Decimal("1.5") |
| 422 |
|
| 423 |
|
| 424 |
def test_float_can_find_max_value_inf(): |
| 425 |
assert minimal(st.floats(max_value=math.inf), math.isinf) == float("inf") |
| 426 |
assert minimal(st.floats(min_value=0.0), math.isinf) == math.inf |
| 427 |
|
| 428 |
|
| 429 |
def test_float_can_find_min_value_inf(): |
| 430 |
minimal(st.floats(), lambda x: x < 0 and math.isinf(x)) |
| 431 |
minimal(st.floats(min_value=-math.inf, max_value=0.0), math.isinf) |
| 432 |
|
| 433 |
|
| 434 |
def test_can_find_none_list(): |
| 435 |
assert minimal(st.lists(st.none()), lambda x: len(x) >= 3) == [None] * 3 |
| 436 |
|
| 437 |
|
| 438 |
def test_fractions(): |
| 439 |
assert minimal(st.fractions(), lambda f: f >= 1) == 1 |
| 440 |
|
| 441 |
|
| 442 |
def test_decimals(): |
| 443 |
assert minimal(st.decimals(), lambda f: f.is_finite() and f >= 1) == 1 |
| 444 |
|
| 445 |
|
| 446 |
@xfail_on_crosshair( |
| 447 |
Why.undiscovered |
| 448 |
) # (SampledFromStrategy.calc_label() hashes a symbolic int) |
| 449 |
def test_non_float_decimal(): |
| 450 |
minimal(st.decimals(), lambda d: d.is_finite() and decimal.Decimal(float(d)) != d) |
| 451 |
|
| 452 |
|
| 453 |
def test_produces_dictionaries_of_at_least_minimum_size(): |
| 454 |
t = minimal( |
| 455 |
st.dictionaries(st.booleans(), st.integers(), min_size=2), |
| 456 |
) |
| 457 |
assert t == {False: 0, True: 0} |
| 458 |
|
| 459 |
|
| 460 |
@given(st.dictionaries(st.integers(), st.integers(), max_size=5)) |
| 461 |
@settings(max_examples=50) |
| 462 |
def test_dictionaries_respect_size(d): |
| 463 |
assert len(d) <= 5 |
| 464 |
|
| 465 |
|
| 466 |
@given(st.dictionaries(st.integers(), st.integers(), max_size=0)) |
| 467 |
@settings(max_examples=50) |
| 468 |
def test_dictionaries_respect_zero_size(d): |
| 469 |
assert len(d) <= 5 |
| 470 |
|
| 471 |
|
| 472 |
@given(st.lists(st.none(), max_size=5)) |
| 473 |
def test_none_lists_respect_max_size(ls): |
| 474 |
assert len(ls) <= 5 |
| 475 |
|
| 476 |
|
| 477 |
@given(st.lists(st.none(), max_size=5, min_size=1)) |
| 478 |
def test_none_lists_respect_max_and_min_size(ls): |
| 479 |
assert 1 <= len(ls) <= 5 |
| 480 |
|
| 481 |
|
| 482 |
@given(st.iterables(st.integers(), max_size=5, min_size=1)) |
| 483 |
def test_iterables_are_exhaustible(it): |
| 484 |
for _ in it: |
| 485 |
pass |
| 486 |
with pytest.raises(StopIteration): |
| 487 |
next(it) |
| 488 |
|
| 489 |
|
| 490 |
def test_minimal_iterable(): |
| 491 |
assert list(minimal(st.iterables(st.integers()))) == [] |
| 492 |
|
| 493 |
|
| 494 |
@pytest.mark.parametrize("parameter_name", ["min_value", "max_value"]) |
| 495 |
@pytest.mark.parametrize("value", [-1, 0, 1]) |
| 496 |
def test_no_infinity_for_min_max_values(value, parameter_name): |
| 497 |
kwargs = {"allow_infinity": False, parameter_name: value} |
| 498 |
|
| 499 |
@given(st.floats(**kwargs)) |
| 500 |
def test_not_infinite(xs): |
| 501 |
assert not math.isinf(xs) |
| 502 |
|
| 503 |
test_not_infinite() |
| 504 |
|
| 505 |
|
| 506 |
@pytest.mark.parametrize("parameter_name", ["min_value", "max_value"]) |
| 507 |
@pytest.mark.parametrize("value", [-1, 0, 1]) |
| 508 |
def test_no_nan_for_min_max_values(value, parameter_name): |
| 509 |
kwargs = {"allow_nan": False, parameter_name: value} |
| 510 |
|
| 511 |
@given(st.floats(**kwargs)) |
| 512 |
def test_not_nan(xs): |
| 513 |
assert not math.isnan(xs) |
| 514 |
|
| 515 |
test_not_nan() |
| 516 |
|
| 517 |
|
| 518 |
class Sneaky: |
| 519 |
"""It's like a strategy, but it's not a strategy.""" |
| 520 |
|
| 521 |
is_empty = False |
| 522 |
depth = 0 |
| 523 |
label = 0 |
| 524 |
|
| 525 |
def do_draw(self, data): |
| 526 |
pass |
| 527 |
|
| 528 |
def validate(self): |
| 529 |
pass |
| 530 |
|
| 531 |
|
| 532 |
@pytest.mark.parametrize("value", [5, Sneaky()]) |
| 533 |
@pytest.mark.parametrize("label", [None, "not a strategy"]) |
| 534 |
@given(data=st.data()) |
| 535 |
def test_data_explicitly_rejects_non_strategies(data, value, label): |
| 536 |
with pytest.raises(InvalidArgument): |
| 537 |
data.draw(value, label=label) |
| 538 |
|
| 539 |
|
| 540 |
@given(st.integers().filter(bool).filter(lambda x: x % 3)) |
| 541 |
def test_chained_filter(x): |
| 542 |
assert x |
| 543 |
assert x % 3 |
| 544 |
|
| 545 |
|
| 546 |
def test_chained_filter_tracks_all_conditions(): |
| 547 |
s = st.integers().filter(bool).filter(lambda x: x % 3) |
| 548 |
assert len(s.wrapped_strategy.flat_conditions) == 2 |
| 549 |
|
| 550 |
|
| 551 |
@pytest.mark.parametrize("version", [4, 6]) |
| 552 |
@given(data=st.data()) |
| 553 |
def test_ipaddress_from_network_is_always_correct_version(data, version): |
| 554 |
ip = data.draw(st.ip_addresses(v=version), label="address") |
| 555 |
assert ip.version == version |
| 556 |
|
| 557 |
|
| 558 |
@given(data=st.data(), network=st.from_type(IPv4Network) | st.from_type(IPv6Network)) |
| 559 |
def test_ipaddress_from_network_is_always_in_network(data, network): |
| 560 |
ip = data.draw(st.ip_addresses(network=network), label="address") |
| 561 |
assert ip in network |
| 562 |
assert ip.version == network.version |
| 563 |
|
| 564 |
|
| 565 |
class AnEnum(enum.Enum): |
| 566 |
a = 1 |
| 567 |
|
| 568 |
|
| 569 |
def requires_arg(value): |
| 570 |
"""Similar to the enum.Enum.__call__ method.""" |
| 571 |
|
| 572 |
|
| 573 |
@given(st.data()) |
| 574 |
def test_builds_error_messages(data): |
| 575 |
# If we call them directly, we get a simple TypeError in both cases |
| 576 |
with pytest.raises(TypeError): |
| 577 |
requires_arg() |
| 578 |
with pytest.raises(TypeError): |
| 579 |
AnEnum() |
| 580 |
# But we have an improved error message if you try to build an Enum |
| 581 |
assert issubclass(InvalidArgument, TypeError) # it's a valid substitution |
| 582 |
with pytest.raises(TypeError): # which only applies to enums |
| 583 |
data.draw(st.builds(requires_arg)) |
| 584 |
with pytest.raises( |
| 585 |
InvalidArgument, |
| 586 |
match=r".* try using sampled_from\(.+\) instead of builds\(.+\)", |
| 587 |
): |
| 588 |
data.draw(st.builds(AnEnum)) |
| 589 |
# and sampled_from() does in fact work |
| 590 |
data.draw(st.sampled_from(AnEnum)) |
| 591 |
|
| 592 |
|
| 593 |
@pytest.mark.parametrize( |
| 594 |
"strat_a,strat_b", |
| 595 |
[ |
| 596 |
pytest.param( |
| 597 |
st.integers(), |
| 598 |
st.integers(0), |
| 599 |
marks=pytest.mark.xfail( |
| 600 |
# this is the exception raised by failed pytest.warns(), |
| 601 |
# ref https://github.com/pytest-dev/pytest/issues/8928 |
| 602 |
raises=pytest.fail.Exception, |
| 603 |
strict=True, |
| 604 |
reason="constraints not checked", |
| 605 |
), |
| 606 |
), |
| 607 |
(st.builds(int), st.builds(float)), |
| 608 |
(st.none(), st.integers()), |
| 609 |
( |
| 610 |
st.composite(lambda draw: draw(st.none()))(), |
| 611 |
st.composite(lambda draw: draw(st.integers()))(), |
| 612 |
), |
| 613 |
], |
| 614 |
) |
| 615 |
def test_incompatible_shared_strategies_warns(strat_a, strat_b): |
| 616 |
shared_a = st.shared(strat_a, key="share") |
| 617 |
shared_b = st.shared(strat_b, key="share") |
| 618 |
|
| 619 |
@given(shared_a, shared_b) |
| 620 |
@settings(max_examples=10, phases=[Phase.generate]) |
| 621 |
def test_it(a, b): |
| 622 |
assert a == b |
| 623 |
|
| 624 |
with pytest.warns(HypothesisWarning, match="Different strategies"): |
| 625 |
test_it() |
| 626 |
|
| 627 |
|
| 628 |
@st.composite |
| 629 |
def _composite1(draw): |
| 630 |
return draw(st.integers()) |
| 631 |
|
| 632 |
|
| 633 |
@st.composite |
| 634 |
def _composite2(draw): |
| 635 |
return draw(st.integers()) |
| 636 |
|
| 637 |
|
| 638 |
@pytest.mark.parametrize( |
| 639 |
"strat_a,strat_b", |
| 640 |
[ |
| 641 |
(st.floats(allow_nan=False), st.floats(allow_nan=False)), |
| 642 |
(st.builds(float), st.builds(float)), |
| 643 |
(_composite1(), _composite1()), |
| 644 |
( |
| 645 |
st.floats(allow_nan=False, allow_infinity=False), |
| 646 |
st.floats(allow_nan=False, allow_infinity=0), |
| 647 |
), |
| 648 |
(_composite1(), _composite2()), |
| 649 |
pytest.param( |
| 650 |
st.integers().flatmap(st.just), |
| 651 |
st.integers(), |
| 652 |
marks=pytest.mark.xfail( |
| 653 |
raises=HypothesisWarning, |
| 654 |
strict=True, |
| 655 |
reason="really different (but compatible)", |
| 656 |
), |
| 657 |
), |
| 658 |
], |
| 659 |
) |
| 660 |
def test_compatible_shared_strategies_do_not_warn(strat_a, strat_b): |
| 661 |
shared_a = st.shared(strat_a, key="share") |
| 662 |
shared_b = st.shared(strat_b, key="share") |
| 663 |
|
| 664 |
@given(shared_a, shared_b) |
| 665 |
@settings(max_examples=10, phases=[Phase.generate]) |
| 666 |
def test_it(a, b): |
| 667 |
assert a == b |
| 668 |
|
| 669 |
with warnings.catch_warnings(): |
| 670 |
warnings.simplefilter("error", HypothesisWarning) |
| 671 |
test_it() |
| 672 |
|
| 673 |
|
| 674 |
def test_compatible_nested_shared_strategies_do_not_warn(): |
| 675 |
shared_a = st.shared(st.integers(), key="share") |
| 676 |
shared_b = st.shared(st.integers(), key="share") |
| 677 |
shared_c = st.shared(shared_a, key="nested_share") |
| 678 |
shared_d = st.shared(shared_b, key="nested_share") |
| 679 |
|
| 680 |
@given(shared_a, shared_b, shared_c, shared_d) |
| 681 |
@settings(max_examples=10, phases=[Phase.generate]) |
| 682 |
def test_it(a, b, c, d): |
| 683 |
assert a == b == c == d |
| 684 |
|
| 685 |
test_it() |