forked from HypothesisWorks/hypothesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_normalization.py
70 lines (55 loc) · 2.21 KB
/
test_normalization.py
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
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Most of this work is copyright (C) 2013-2020 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# 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/.
#
# END HEADER
from itertools import islice
import pytest
from hypothesis import strategies as st
from hypothesis.errors import UnsatisfiedAssumption
from hypothesis.internal.conjecture.shrinking import dfas
from tests.quality.test_shrinking_order import iter_values
@pytest.fixture
def normalize_kwargs(request):
if request.config.getoption("--hypothesis-learn-to-normalize"):
return {"allowed_to_update": True, "required_successes": 1000}
else:
return {"allowed_to_update": False, "required_successes": 10}
@pytest.mark.parametrize("n", range(10, -1, -1))
@pytest.mark.parametrize(
"strategy",
[st.floats(), st.text(), st.datetimes()],
ids=repr,
)
def test_common_strategies_normalize_small_values(strategy, n, normalize_kwargs):
excluded = list(map(repr, islice(iter_values(strategy, unique_by=repr), n)))
def test_function(data):
try:
v = data.draw(strategy)
except UnsatisfiedAssumption:
data.mark_invalid()
data.output = repr(v)
if repr(v) not in excluded:
data.mark_interesting()
dfas.normalize(repr(strategy), test_function, **normalize_kwargs)
@pytest.mark.parametrize("strategy", [st.emails(), st.complex_numbers()], ids=repr)
def test_harder_strategies_normalize_to_minimal(strategy, normalize_kwargs):
import random
random.seed(0)
def test_function(data):
try:
v = data.draw(strategy)
except UnsatisfiedAssumption:
data.mark_invalid()
data.output = repr(v)
data.mark_interesting()
dfas.normalize(repr(strategy), test_function, **normalize_kwargs)