def test_register_generic_typing_strats(): # I don't expect anyone to do this, but good to check it works as expected try: # We register sets for the abstract sequence type, which masks subtypes # from supertype resolution but not direct resolution st.register_type_strategy( typing.Sequence, types._global_type_lookup[typing.Set] ) @given(from_type(typing.Sequence[int])) def inner_1(ex): assert isinstance(ex, set) @given(from_type(typing.Container[int])) def inner_2(ex): assert not isinstance(ex, typing.Sequence) @given(from_type(typing.List[int])) def inner_3(ex): assert isinstance(ex, list) inner_1() inner_2() inner_3() finally: types._global_type_lookup.pop(typing.Sequence) st.from_type.__clear_cache()
def test_cannot_register_empty(): # Cannot register and did not register with pytest.raises(InvalidArgument): st.register_type_strategy(UnknownType, st.nothing()) fails = st.from_type(UnknownType) with pytest.raises(ResolutionFailed): fails.example() assert UnknownType not in types._global_type_lookup
def test_custom_type_resolution_with_function_non_strategy(): try: st.register_type_strategy(UnknownType, lambda _: None) with pytest.raises(ResolutionFailed): st.from_type(UnknownType).example() with pytest.raises(ResolutionFailed): st.from_type(ParentUnknownType).example() finally: types._global_type_lookup.pop(UnknownType)
def test_custom_type_resolution_with_function(): sentinel = object() try: st.register_type_strategy(UnknownType, lambda _: st.just(sentinel)) assert st.from_type(UnknownType).example() is sentinel assert st.from_type(ParentUnknownType).example() is sentinel finally: types._global_type_lookup.pop(UnknownType) st.from_type.__clear_cache()
def test_lookup_overrides_defaults(typ): sentinel = object() try: strat = types._global_type_lookup[typ] st.register_type_strategy(typ, st.just(sentinel)) assert st.from_type(typ).example() is sentinel finally: st.register_type_strategy(typ, strat) st.from_type.__clear_cache() assert st.from_type(typ).example() is not sentinel
def test_custom_type_resolution(): sentinel = object() try: st.register_type_strategy(UnknownType, st.just(sentinel)) assert st.from_type(UnknownType).example() is sentinel # Also covered by registration of child class assert st.from_type(ParentUnknownType).example() is sentinel finally: types._global_type_lookup.pop(UnknownType) st.from_type.__clear_cache() assert UnknownType not in types._global_type_lookup
def test_errors_if_generic_resolves_empty(): try: st.register_type_strategy(UnknownType, lambda _: st.nothing()) fails_1 = st.from_type(UnknownType) with pytest.raises(ResolutionFailed): fails_1.example() fails_2 = st.from_type(ParentUnknownType) with pytest.raises(ResolutionFailed): fails_2.example() finally: types._global_type_lookup.pop(UnknownType) st.from_type.__clear_cache()
def test_lookup_overrides_defaults(): sentinel = object() try: st.register_type_strategy(int, st.just(sentinel)) @given(from_type(typing.List[int])) def inner_1(ex): assert all(elem is sentinel for elem in ex) inner_1() finally: st.register_type_strategy(int, st.integers()) st.from_type.__clear_cache() @given(from_type(typing.List[int])) def inner_2(ex): assert all(isinstance(elem, int) for elem in ex) inner_2()
def _hypothesis_setup_hook(): import hypothesis.strategies as st st.register_type_strategy(MyCustomType, st.integers(min_value=0).map(MyCustomType))
def test_can_register_NewType(): Name = typing.NewType("Name", str) st.register_type_strategy(Name, st.just("Eric Idle")) assert st.from_type(Name).example() == "Eric Idle"
def test_lookup_values_are_strategies(): with pytest.raises(InvalidArgument): st.register_type_strategy(int, 42) assert 42 not in types._global_type_lookup.values()
def test_generic_origin_with_type_args(generic, strategy): with pytest.raises(InvalidArgument): st.register_type_strategy(generic, strategy) assert generic not in types._global_type_lookup
import libcst from hypothesis import infer, strategies as st, target from hypothesmith.syntactic import identifiers # For some nodes, we just need to ensure that they use the appropriate regex # pattern instead of allowing literally any string. for node_type, pattern in { libcst.Float: FLOATNUMBER_RE, libcst.Integer: INTNUMBER_RE, libcst.Imaginary: IMAGNUMBER_RE, libcst.SimpleWhitespace: libcst._nodes.whitespace.SIMPLE_WHITESPACE_RE, }.items(): _strategy = st.builds(node_type, st.from_regex(pattern, fullmatch=True)) st.register_type_strategy(node_type, _strategy) # type-ignore comments are special in the 3.8+ (typed) ast, so boost their chances) _comments = st.from_regex(libcst._nodes.whitespace.COMMENT_RE, fullmatch=True) st.register_type_strategy( libcst.Comment, st.builds(libcst.Comment, _comments | st.just("# type: ignore"))) # `from_type()` has less laziness than other strategies, we we register for these # foundational node types *before* referring to them in other strategies. st.register_type_strategy(libcst.Name, st.builds(libcst.Name, identifiers())) st.register_type_strategy(libcst.SimpleString, st.builds(libcst.SimpleString, st.text().map(repr))) # Ensure that ImportAlias uses Attribute nodes composed only of Name nodes.
from hypothesis import strategies as st from to_cover.module import Example st.register_type_strategy(Example, st.builds(Example))
currencies: SearchStrategy[Currency] = from_type(Currency), quantities: SearchStrategy[Decimal] = cashAmounts(), ) -> SearchStrategy[AccountBalance]: # Generate a unique set of currencies, then a list of cash amounts to match. return builds( AccountBalance, cash=sets(currencies).flatmap(lambda keys: lists( quantities, min_size=len(keys), max_size=len(keys)).map( lambda values: { currency: Cash(currency=currency, quantity=qty) for currency, qty in zip(keys, values) })), ) register_type_strategy(Cash, cash()) register_type_strategy(Bond, bonds()) register_type_strategy(Stock, stocks()) register_type_strategy(Option, options()) register_type_strategy(FutureOption, futuresOptions()) register_type_strategy(Future, futures()) register_type_strategy( Forex, lists(from_type(Currency), min_size=2, max_size=2, unique=True).flatmap(lambda cx: forex( baseCurrency=just(cx[0]), quoteCurrency=just(cx[1]), exchange=optionals(exchanges()), )), )
@strategies.composite def points(draw): label = strategies.text() return Point(draw(coordinates), draw(label)) @strategies.composite def boxes(draw): label = strategies.text() a = draw(coordinates) b = draw(coordinates) xyxy = (min(a[0], b[0]), min(a[1], b[1]), max(a[0], b[0]), max(a[1], b[1])) return BoundingBox(xyxy, draw(label)) strategies.register_type_strategy(Polygon, polygons()) strategies.register_type_strategy(Point, points()) strategies.register_type_strategy(BoundingBox, boxes()) @strategies.composite def image_array(draw, image_size=None): if image_size is None: size = draw( strategies.tuples( strategies.integers(min_value=40, max_value=1000), strategies.integers(min_value=40, max_value=1000), )) else: size = image_size size = size[::-1]
from hypothesis import strategies as st from vcr import VCR from vcr.request import Request as VcrRequest from vcr.stubs import httpx_stubs import webknossos as wk from webknossos.client.context import _clear_all_context_caches from .constants import TESTDATA_DIR, TESTOUTPUT_DIR ### HYPOTHESIS STRATEGIES (library to test many combinations for data class input) _vec3_int_strategy = st.builds(wk.Vec3Int, st.integers(), st.integers(), st.integers()) st.register_type_strategy(wk.Vec3Int, _vec3_int_strategy) _positive_vec3_int_strategy = st.builds( wk.Vec3Int, st.integers(min_value=0), st.integers(min_value=0), st.integers(min_value=0), ) st.register_type_strategy( wk.BoundingBox, st.builds(wk.BoundingBox, _positive_vec3_int_strategy, _positive_vec3_int_strategy), ) _mag_strategy = st.builds(
if k not in skip } specific = { "line_length": st.integers(0, 200), "wrap_length": st.integers(0, 200), "indent": st.integers(0, 20).map(lambda n: n * " "), "default_section": st.sampled_from(sorted(isort.settings.KNOWN_SECTION_MAPPING)), "force_grid_wrap": st.integers(0, 20), "profile": st.sampled_from(sorted(isort.settings.profiles)), "py_version": st.sampled_from(("auto",) + isort.settings.VALID_PY_TARGETS), } kwargs = {**inferred_kwargs, **specific} return st.fixed_dictionaries({}, optional=kwargs).map(_as_config) st.register_type_strategy(isort.Config, configs()) CODE_SNIPPET = """ '''Taken from bottle.py Copyright (c) 2009-2018, Marcel Hellkamp. License: MIT (see LICENSE for details) ''' # Lots of stdlib and builtin differences. if py3k: import http.client as httplib import _thread as thread from urllib.parse import urljoin, SplitResult as UrlSplitResult from urllib.parse import urlencode, quote as urlquote, unquote as urlunquote urlunquote = functools.partial(urlunquote, encoding='latin1') from http.cookies import SimpleCookie, Morsel, CookieError
from myrtlespeech.model.cnn import MaskConv1d from myrtlespeech.model.cnn import MaskConv2d from myrtlespeech.model.cnn import out_lens from myrtlespeech.model.cnn import pad_same from myrtlespeech.model.cnn import PaddingMode # Fixtures and Strategies ----------------------------------------------------- @st.composite def padding_modes(draw) -> st.SearchStrategy[PaddingMode]: """A strategy for :py:class:`PaddingMode`.""" return draw(st.sampled_from(PaddingMode)) st.register_type_strategy(PaddingMode, padding_modes) @st.composite def mask_conv1ds(draw) -> st.SearchStrategy[MaskConv1d]: """Returns a SearchStrategy for MaskConv1d.""" in_channels = draw(st.integers(1, 8)) out_channels = draw(st.integers(1, 8)) kernel_size = draw(st.integers(1, 5)) stride = draw(st.integers(1, 5)) dilation = draw(st.integers(1, 5)) padding_mode = draw(padding_modes()) return MaskConv1d( in_channels=in_channels, out_channels=out_channels,
from hypothesis import strategies as st from decimal import Decimal import datetime as dt from enum import Enum from . import _strategies as _st # Tests often want to compare for equality, and there's no good way to do this with NaNs breaking it. :-( st.register_type_strategy(Decimal, st.decimals(allow_nan=False)) st.register_type_strategy(float, st.floats(allow_nan=False)) def type_value_pairs(base): @st.composite def tv_pairs(draw): typ = draw(base) try: val = draw(st.from_type(typ)) except Exception as exc: exc.args += (typ, ) raise return (typ, val) return tv_pairs() atoms = st.sampled_from([ type(None), bool, int,
from oef.query import Eq, NotEq, Lt, LtEq, Gt, GtEq, Range, In, NotIn, And, Or, Constraint, Query, Not, Distance from oef.schema import AttributeSchema, DataModel, Description, Location, ATTRIBUTE_TYPES integers_32 = integers(min_value=-2**32 + 1, max_value=2**32 - 1) floats_no_nan = floats(allow_nan=False) @composite def locations(draw): latitude = draw(floats(min_value=-90.0, max_value=90.0)) longitude = draw(floats(min_value=-180.0, max_value=180.0)) return Location(latitude, longitude) register_type_strategy(Location, locations()) strategies_by_type = { int: integers_32, float: floats_no_nan, bool: booleans(), str: text(), Location: locations() } attribute_schema_types = sampled_from(ATTRIBUTE_TYPES.__args__ + (bool, )) attribute_schema_values = one_of(integers_32, floats_no_nan, text(), booleans(), locations()) ordered_values = one_of(integers_32, floats_no_nan, text()) set_values = one_of(lists(integers_32), lists(floats_no_nan), lists(text()), lists(booleans()), lists(locations))
""" dt = draw( st.datetimes(min_value=datetime.datetime(2000, 1, 1), max_value=datetime.datetime(2100, 1, 1))) ts = clamp(int(dt.timestamp()), -2147483648, 2147483647) t = struct.pack("i", ts) r = draw(st.binary(min_size=5, max_size=5)) c = draw(st.binary(min_size=3, max_size=3)) return str(ObjectId((t + r + c).hex())) st.register_type_strategy( model.Date, st.builds( model.Date, st.integers(min_value=1, max_value=12), # month st.integers(min_value=2000, max_value=2100) # year )) st.register_type_strategy( model.ChildField, st.builds( model.ChildField, st.from_type(model.Date), # date st.text(alphabet=printable, max_size=160) # name )) st.register_type_strategy( model.Document, st.builds(
def test_lookup_keys_are_types(): with pytest.raises(InvalidArgument): st.register_type_strategy("int", st.integers()) assert "int" not in types._global_type_lookup
from hypothesis.strategies import (builds, from_type, one_of, register_type_strategy) from tests import helpers _L = TypeVar('_L') _R = TypeVar('_R') @adt class Either(Generic[_L, _R]): LEFT: Case[_L] RIGHT: Case[_R] register_type_strategy( Either, one_of(builds(Either.LEFT, helpers.any_types), builds(Either.RIGHT, helpers.any_types))) class TestEither(unittest.TestCase): def test_left(self) -> None: e: Either[int, str] = Either.LEFT(5) self.assertEqual(e, Either.LEFT(5)) self.assertEqual(e.left(), 5) self.assertNotEqual(e, Either.RIGHT(5)) with self.assertRaises(AttributeError): e.right() self.assertEqual( e.match(left=lambda n: n + 1, right=helpers.invalidPatternMatch),
"ci", # Hypothesis timing checks are tuned for scalars by default, so we bump # them from 200ms to 500ms per test case as the global default. If this # is too short for a specific test, (a) try to make it faster, and (b) # if it really is slow add `@settings(deadline=...)` with a working value, # or `deadline=None` to entirely disable timeouts for that test. deadline=500, suppress_health_check=(hypothesis.HealthCheck.too_slow, ), ) hypothesis.settings.load_profile("ci") # Registering these strategies makes them globally available via st.from_type, # which is use for offsets in tests/tseries/offsets/test_offsets_properties.py for name in "MonthBegin MonthEnd BMonthBegin BMonthEnd".split(): cls = getattr(pd.tseries.offsets, name) st.register_type_strategy( cls, st.builds(cls, n=st.integers(-99, 99), normalize=st.booleans())) for name in "YearBegin YearEnd BYearBegin BYearEnd".split(): cls = getattr(pd.tseries.offsets, name) st.register_type_strategy( cls, st.builds( cls, n=st.integers(-5, 5), normalize=st.booleans(), month=st.integers(min_value=1, max_value=12), ), ) for name in "QuarterBegin QuarterEnd BQuarterBegin BQuarterEnd".split(): cls = getattr(pd.tseries.offsets, name)
pass else: def is_valid_email(s: str) -> bool: # Hypothesis' st.emails() occasionally generates emails like [email protected] # that are invalid according to email-validator, so we filter those out. try: email_validator.validate_email(s, check_deliverability=False) return True except email_validator.EmailNotValidError: # pragma: no cover return False # Note that these strategies deliberately stay away from any tricky Unicode # or other encoding issues; we're just trying to generate *something* valid. st.register_type_strategy( pydantic.EmailStr, st.emails().filter(is_valid_email)) # type: ignore[arg-type] st.register_type_strategy( pydantic.NameEmail, st.builds( '{} <{}>'.format, # type: ignore[arg-type] st.from_regex('[A-Za-z0-9_]+( [A-Za-z0-9_]+){0,5}', fullmatch=True), st.emails().filter(is_valid_email), ), ) # PyObject - dotted names, in this case taken from the math module. st.register_type_strategy( pydantic.PyObject, # type: ignore[arg-type] st.sampled_from([
class Foo: def __init__(self, x): pass class Bar(Foo): pass class Baz(Foo): pass st.register_type_strategy(Bar, st.builds(Bar, st.integers())) st.register_type_strategy(Baz, st.builds(Baz, st.integers())) @pytest.mark.parametrize( "var,expected", [ (typing.TypeVar("V"), object), (typing.TypeVar("V", bound=int), int), (typing.TypeVar("V", bound=Foo), (Bar, Baz)), (typing.TypeVar("V", bound=typing.Union[int, str]), (int, str)), (typing.TypeVar("V", int, str), (int, str)), ], ) @settings(suppress_health_check=[HealthCheck.too_slow]) @given(data=st.data())
def test_lookup_keys_are_types(): with pytest.raises(InvalidArgument): st.register_type_strategy('int', st.integers()) assert 'int' not in types._global_type_lookup
from returns.context import ( RequiresContext, RequiresContextFutureResult, RequiresContextIOResult, RequiresContextResult, ) from returns.contrib.hypothesis.containers import strategy_from_container from returns.future import Future, FutureResult from returns.io import IO, IOResult from returns.maybe import Maybe from returns.primitives.laws import Lawful from returns.result import Result #: Our types that we register in hypothesis to be working with ``st.from_type`` REGISTERED_TYPES: Sequence[Type[Lawful]] = ( Result, Maybe, IO, IOResult, Future, FutureResult, RequiresContext, RequiresContextResult, RequiresContextIOResult, RequiresContextFutureResult, ) for type_ in REGISTERED_TYPES: st.register_type_strategy(type_, strategy_from_container(type_))
from hypothesis.strategies._internal.types import _global_type_lookup from libcst._nodes.expression import ExpressionPosition from libcst._nodes.statement import _INDENT_WHITESPACE_RE from hypothesmith.syntactic import identifiers # For some nodes, we just need to ensure that they use the appropriate regex # pattern instead of allowing literally any string. for node_type, pattern in { libcst.Float: FLOATNUMBER_RE, libcst.Integer: INTNUMBER_RE, libcst.Imaginary: IMAGNUMBER_RE, libcst.SimpleWhitespace: libcst._nodes.whitespace.SIMPLE_WHITESPACE_RE, }.items(): _strategy = st.builds(node_type, st.from_regex(pattern, fullmatch=True)) st.register_type_strategy(node_type, _strategy) # type-ignore comments are special in the 3.8+ (typed) ast, so boost their chances) _comments = st.from_regex(libcst._nodes.whitespace.COMMENT_RE, fullmatch=True) st.register_type_strategy( libcst.Comment, st.builds(libcst.Comment, _comments | st.just("# type: ignore"))) # `from_type()` has less laziness than other strategies, we we register for these # foundational node types *before* referring to them in other strategies. st.register_type_strategy(libcst.Name, st.builds(libcst.Name, identifiers())) st.register_type_strategy(libcst.SimpleString, st.builds(libcst.SimpleString, st.text().map(repr))) # Ensure that ImportAlias uses Attribute nodes composed only of Name nodes.
cls = draw(sampled_from([list, tuple, set, frozenset])) return cls( draw(iterables(getattr(type, '__args__', object), max_size=10))) return strategy() def _sequence_strategy(type: Type[Sequence]) -> SearchStrategy[Sequence]: @composite def strategy(draw) -> Sequence: cls = draw(sampled_from([list, tuple])) return cls(draw(_iterable_strategy(type))) return strategy() def _simple_value_word_node_strategy( type: Type[SimpleValueWordNode], ) -> SearchStrategy[SimpleValueWordNode]: @composite def strategy(draw) -> SimpleValueWordNode: cls = draw( sampled_from([NotImplWordNode, NoneWordNode, EllipsisWordNode])) return cls(draw(from_type(Token))) return strategy() register_type_strategy(Iterable, _iterable_strategy) register_type_strategy(Sequence, _sequence_strategy) register_type_strategy(SimpleValueWordNode, _simple_value_word_node_strategy)
@st.composite def _embargo_end(draw, time=Times(max_value=datetime.datetime( datetime.datetime.utcnow().year, 1, 1, 0, 0), min_value=datetime.datetime(1981, 1, 1, 0, 0)), delta=TimeDelta()): t1 = draw(time) t2 = t1 + draw(delta) assume(t2 < Time.now()) return a.dkist.EmbargoEndTime(t1, t2) for attr_type in DKISTDatasetClient.register_values(): st.register_type_strategy(attr_type, _generate_from_register_values) st.register_type_strategy(a.Time, time_attr()) st.register_type_strategy(a.Wavelength, _unit_range) st.register_type_strategy(a.dkist.SpectralSampling, _unit_range) st.register_type_strategy(a.dkist.TemporalSampling, _unit_range) st.register_type_strategy(a.dkist.SpatialSampling, _unit_range) st.register_type_strategy(a.dkist.BrowseMovie, _browse_movie()) st.register_type_strategy(a.dkist.FriedParameter, _unit_range) st.register_type_strategy(a.dkist.PolarimetricAccuracy, _unit_range) st.register_type_strategy(a.dkist.ExposureTime, _unit_range) st.register_type_strategy(a.dkist.EmbargoEndTime, _embargo_end()) @settings(suppress_health_check=[HealthCheck.too_slow]) @st.composite
def test_non_runtime_type_cannot_be_registered(non_runtime_type): with pytest.raises(InvalidArgument, match="there is no such thing as a runtime instance"): st.register_type_strategy(non_runtime_type, st.none())
else: return pytest.importorskip("mock") # ---------------------------------------------------------------- # Global setup for tests using Hypothesis from hypothesis import strategies as st # Registering these strategies makes them globally available via st.from_type, # which is use for offsets in tests/tseries/offsets/test_offsets_properties.py for name in 'MonthBegin MonthEnd BMonthBegin BMonthEnd'.split(): cls = getattr(pd.tseries.offsets, name) st.register_type_strategy(cls, st.builds( cls, n=st.integers(-99, 99), normalize=st.booleans(), )) for name in 'YearBegin YearEnd BYearBegin BYearEnd'.split(): cls = getattr(pd.tseries.offsets, name) st.register_type_strategy(cls, st.builds( cls, n=st.integers(-5, 5), normalize=st.booleans(), month=st.integers(min_value=1, max_value=12), )) for name in 'QuarterBegin QuarterEnd BQuarterBegin BQuarterEnd'.split(): cls = getattr(pd.tseries.offsets, name) st.register_type_strategy(cls, st.builds(
def test_not_supported(): with pytest.raises(NotImplementedError): parse_input(5, Callable[[int], set]) def test_collections(): assert parse_input([1, 2, 3], frozenset) == frozenset((1, 2, 3)) with pytest.raises(ValidationError): parse_input([{1}, 2, 3], frozenset) with pytest.raises(ValidationError): parse_input("X", frozenset) register_type_strategy(Any, booleans()) def invert_db(db): db = dataclasses.asdict(db) db['flags'] = list(map(dataclasses.asdict, db['flags'])) def invert_row(row): # row = dataclasses.asdict(row) row['values'] = list(row['values']) return row db['rows'] = list(map(invert_row, db['rows'])) db['metadata'] = dict(db['metadata']) db['size'] = db['size'].name db['attributes'] = list(m for m in Attributes.__members__