File: test_argument_validation.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 (146 lines) | stat: -rw-r--r-- 5,526 bytes parent folder | download | duplicates (2)
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
# 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/.

import re
from datetime import datetime, timedelta

import pandas as pd
import pytest

from hypothesis import given, strategies as st
from hypothesis.errors import InvalidArgument
from hypothesis.extra import pandas as pdst
from hypothesis.extra.pandas.impl import IntegerDtype

from tests.common.arguments import argument_validation_test, e
from tests.common.debug import check_can_generate_examples, find_any
from tests.common.utils import checks_deprecated_behaviour

BAD_ARGS = [
    e(pdst.data_frames),
    e(pdst.data_frames, pdst.columns(1, dtype="not a dtype")),
    e(pdst.data_frames, pdst.columns(1, elements="not a strategy")),
    e(pdst.data_frames, pdst.columns([[]])),
    e(pdst.data_frames, [], index=[]),
    e(pdst.data_frames, [], rows=st.fixed_dictionaries({"A": st.just(1)})),
    e(pdst.data_frames, pdst.columns(1)),
    e(pdst.data_frames, pdst.columns(1, dtype=float, fill=1)),
    e(pdst.data_frames, pdst.columns(1, dtype=float, elements=1)),
    e(pdst.data_frames, pdst.columns(1, fill=1, dtype=float)),
    e(pdst.data_frames, pdst.columns(["A", "A"], dtype=float)),
    pytest.param(
        *e(pdst.data_frames, pdst.columns(1, elements=st.none(), dtype=int)),
        marks=pytest.mark.skipif(IntegerDtype, reason="works with integer NA"),
    ),
    e(pdst.data_frames, pdst.columns(1, elements=st.text(), dtype=int)),
    e(pdst.data_frames, 1),
    e(pdst.data_frames, [1]),
    e(pdst.data_frames, pdst.columns(1, dtype="category")),
    e(
        pdst.data_frames,
        pdst.columns(["A"], dtype=bool),
        rows=st.tuples(st.booleans(), st.booleans()),
    ),
    e(
        pdst.data_frames,
        pdst.columns(1, elements=st.booleans()),
        rows=st.tuples(st.booleans()),
    ),
    e(pdst.data_frames, rows=st.integers(), index=pdst.range_indexes(0, 0)),
    e(pdst.data_frames, rows=st.integers(), index=pdst.range_indexes(1, 1)),
    e(pdst.data_frames, pdst.columns(1, dtype=int), rows=st.integers()),
    e(
        pdst.data_frames,
        columns=pdst.columns(["a", "b"], dtype=str, elements=st.text()),
        rows=st.just({"a": "x"}),
        index=pdst.indexes(dtype=int, min_size=1),
    ),
    e(
        pdst.data_frames,
        columns=pdst.columns(["a", "b"], dtype=str, elements=st.text()),
        rows=st.just(["x"]),
        index=pdst.indexes(dtype=int, min_size=1),
    ),
    e(pdst.indexes),
    e(pdst.indexes, dtype="category"),
    e(pdst.indexes, dtype="not a dtype"),
    e(pdst.indexes, elements="not a strategy"),
    e(pdst.indexes, elements=st.text(), dtype=float),
    pytest.param(
        *e(pdst.indexes, elements=st.none(), dtype=int),
        marks=pytest.mark.skipif(IntegerDtype, reason="works with integer NA"),
    ),
    e(pdst.indexes, elements=st.text(), dtype=int),
    e(pdst.indexes, elements=st.just("9" * 30), dtype=int),  # OverflowError handling
    e(pdst.indexes, elements=st.integers(0, 10), dtype=st.sampled_from([int, float])),
    e(pdst.indexes, dtype=int, max_size=0, min_size=1),
    e(pdst.indexes, dtype=int, unique="true"),
    e(pdst.indexes, dtype=int, min_size="0"),
    e(pdst.indexes, dtype=int, max_size="1"),
    e(pdst.range_indexes, 1, 0),
    e(pdst.range_indexes, min_size="0"),
    e(pdst.range_indexes, max_size="1"),
    e(pdst.range_indexes, name=""),
    e(pdst.series),
    e(pdst.series, dtype="not a dtype"),
    e(pdst.series, elements="not a strategy"),
    pytest.param(
        *e(pdst.series, elements=st.none(), dtype=int),
        marks=pytest.mark.skipif(IntegerDtype, reason="works with integer NA"),
    ),
    e(pdst.series, elements=st.text(), dtype=int),
    e(pdst.series, dtype="category"),
    e(pdst.series, index="not a strategy"),
]


test_raise_invalid_argument = argument_validation_test(BAD_ARGS)

lo, hi = pd.Timestamp(2017, 1, 1), pd.Timestamp(2084, 12, 21)


@given(st.datetimes(min_value=lo, max_value=hi))
def test_timestamp_as_datetime_bounds(dt):
    # Would have caught https://github.com/HypothesisWorks/hypothesis/issues/2406
    assert isinstance(dt, datetime)
    assert lo <= dt <= hi
    assert not isinstance(dt, pd.Timestamp)


@checks_deprecated_behaviour
def test_confusing_object_dtype_aliases():
    check_can_generate_examples(
        pdst.series(elements=st.tuples(st.integers()), dtype=tuple)
    )


@pytest.mark.skipif(
    not IntegerDtype, reason="Nullable types not available in this version of Pandas"
)
def test_pandas_nullable_types_class():
    st = pdst.series(dtype=pd.core.arrays.integer.Int8Dtype)
    with pytest.raises(
        InvalidArgument, match="Otherwise it would be treated as dtype=object"
    ):
        find_any(st, lambda s: s.isna().any())


@pytest.mark.parametrize(
    "dtype_,expected_unit",
    [
        (datetime, "datetime64[ns]"),
        (pd.Timestamp, "datetime64[ns]"),
        (timedelta, "timedelta64[ns]"),
        (pd.Timedelta, "timedelta64[ns]"),
    ],
)
def test_invalid_datetime_or_timedelta_dtype_raises_error(dtype_, expected_unit):
    with pytest.raises(InvalidArgument, match=re.escape(expected_unit)):
        check_can_generate_examples(pdst.series(dtype=dtype_))