Ejemplo n.º 1
0
def slice_node(draw):
    lower = draw(hs.one_of(const_node(hs.integers()), hs.none()))
    upper = draw(hs.one_of(const_node(hs.integers()), hs.none()))
    step = draw(hs.one_of(const_node(hs.integers()), hs.none()))
    node = astroid.Slice()
    node.postinit(lower, upper, step)
    return node
Ejemplo n.º 2
0
def _inputs(draw):
    N = draw(st.integers(min_value=0, max_value=5))
    D = draw(st.integers(min_value=1, max_value=5))
    # N, D, data, lambda1, lambda2
    return (
        N,
        D,
        draw(st.lists(
            min_size=N * D,
            max_size=N * D,
            elements=st.one_of(
                st.floats(min_value=-10, max_value=1 - TOLERANCE),
                st.floats(min_value=1 + TOLERANCE, max_value=10))
        )),
        draw(st.lists(
            elements=st.one_of(
                st.floats(min_value=-2, max_value=-TOLERANCE),
                st.floats(min_value=TOLERANCE, max_value=2)),
            min_size=D,
            max_size=D,
        )),
        draw(st.lists(
            elements=st.floats(min_value=-2, max_value=2),
            min_size=D,
            max_size=D,
        )),
    )
Ejemplo n.º 3
0
def _get_strategy_for_field(f):
    # type: (Type[dm.Field]) -> st.SearchStrategy[Any]
    if f.choices:
        choices = []  # type: list
        for value, name_or_optgroup in f.choices:
            if isinstance(name_or_optgroup, (list, tuple)):
                choices.extend(key for key, _ in name_or_optgroup)
            else:
                choices.append(value)
        if isinstance(f, (dm.CharField, dm.TextField)) and f.blank:
            choices.insert(0, u'')
        strategy = st.sampled_from(choices)
    elif type(f) == dm.SlugField:
        strategy = st.text(alphabet=string.ascii_letters + string.digits,
                           min_size=(0 if f.blank else 1),
                           max_size=f.max_length)
    elif type(f) == dm.GenericIPAddressField:
        lookup = {'both': ip4_addr_strings() | ip6_addr_strings(),
                  'ipv4': ip4_addr_strings(), 'ipv6': ip6_addr_strings()}
        strategy = lookup[f.protocol.lower()]
    elif type(f) in (dm.TextField, dm.CharField):
        strategy = st.text(
            alphabet=st.characters(blacklist_characters=u'\x00',
                                   blacklist_categories=('Cs',)),
            min_size=(0 if f.blank else 1),
            max_size=f.max_length,
        )
        # We can infer a vastly more precise strategy by considering the
        # validators as well as the field type.  This is a minimal proof of
        # concept, but we intend to leverage the idea much more heavily soon.
        # See https://github.com/HypothesisWorks/hypothesis-python/issues/1116
        re_validators = [
            v for v in f.validators
            if isinstance(v, validators.RegexValidator) and not v.inverse_match
        ]
        if re_validators:
            regexes = [re.compile(v.regex, v.flags) if isinstance(v.regex, str)
                       else v.regex for v in re_validators]
            # This strategy generates according to one of the regexes, and
            # filters using the others.  It can therefore learn to generate
            # from the most restrictive and filter with permissive patterns.
            # Not maximally efficient, but it makes pathological cases rarer.
            # If you want a challenge: extend https://qntm.org/greenery to
            # compute intersections of the full Python regex language.
            strategy = st.one_of(*[st.from_regex(r) for r in regexes])
    elif type(f) == dm.DecimalField:
        bound = Decimal(10 ** f.max_digits - 1) / (10 ** f.decimal_places)
        strategy = st.decimals(min_value=-bound, max_value=bound,
                               places=f.decimal_places)
    else:
        strategy = field_mappings().get(type(f), st.nothing())
    if f.validators:
        strategy = strategy.filter(validator_to_filter(f))
    if f.null:
        strategy = st.one_of(st.none(), strategy)
    return strategy
Ejemplo n.º 4
0
def resid_strategy(draw, i_kwargs={}):
    """
    strategy for generating a resid.

    Look at the code, to see what we currently support as resid.
    """
    chain = draw(one_of(none(), text(min_size=1, alphabet=ascii_letters)))
    het_flag = " "  # Currently, a non-empty het-flag is not supported!
    resid = draw(integers(**i_kwargs).filter(lambda x: x != 0))
    insertion_code = draw(one_of(just(" "), get_insertion_code()))
    return fgr.RESID(chain, (het_flag, resid, insertion_code))
Ejemplo n.º 5
0
def regex_strategy(regex, fullmatch):
    if not hasattr(regex, "pattern"):
        regex = re.compile(regex)

    is_unicode = isinstance(regex.pattern, text_type)

    parsed = sre_parse.parse(regex.pattern, flags=regex.flags)

    if not parsed:
        if is_unicode:
            return st.text()
        else:
            return st.binary()

    if is_unicode:
        base_padding_strategy = st.text()
        empty = st.just(u"")
        newline = st.just(u"\n")
    else:
        base_padding_strategy = st.binary()
        empty = st.just(b"")
        newline = st.just(b"\n")

    right_pad = base_padding_strategy
    left_pad = base_padding_strategy

    if fullmatch:
        right_pad = empty
    elif parsed[-1][0] == sre.AT:
        if parsed[-1][1] == sre.AT_END_STRING:
            right_pad = empty
        elif parsed[-1][1] == sre.AT_END:
            if regex.flags & re.MULTILINE:
                right_pad = st.one_of(
                    empty, st.builds(operator.add, newline, right_pad)
                )
            else:
                right_pad = st.one_of(empty, newline)
    if fullmatch:
        left_pad = empty
    elif parsed[0][0] == sre.AT:
        if parsed[0][1] == sre.AT_BEGINNING_STRING:
            left_pad = empty
        elif parsed[0][1] == sre.AT_BEGINNING:
            if regex.flags & re.MULTILINE:
                left_pad = st.one_of(empty, st.builds(operator.add, left_pad, newline))
            else:
                left_pad = empty

    base = base_regex_strategy(regex, parsed).filter(regex.search)

    return maybe_pad(regex, base, left_pad, right_pad)
Ejemplo n.º 6
0
def from_typing_type(thing):
    # We start with special-case support for Union and Tuple - the latter
    # isn't actually a generic type.  Support for Callable may be added to
    # this section later.
    # We then explicitly error on non-Generic types, which don't carry enough
    # information to sensibly resolve to strategies at runtime.
    # Finally, we run a variation of the subclass lookup in st.from_type
    # among generic types in the lookup.
    import typing
    # Under 3.6 Union is handled directly in st.from_type, as the argument is
    # not an instance of `type`. However, under Python 3.5 Union *is* a type
    # and we have to handle it here, including failing if it has no parameters.
    if hasattr(thing, '__union_params__'):  # pragma: no cover
        args = sorted(thing.__union_params__ or (), key=type_sorting_key)
        if not args:
            raise ResolutionFailed('Cannot resolve Union of no types.')
        return st.one_of([st.from_type(t) for t in args])
    if isinstance(thing, typing.TupleMeta):
        elem_types = getattr(thing, '__tuple_params__', None) or ()
        elem_types += getattr(thing, '__args__', None) or ()
        if getattr(thing, '__tuple_use_ellipsis__', False) or \
                len(elem_types) == 2 and elem_types[-1] is Ellipsis:
            return st.lists(st.from_type(elem_types[0])).map(tuple)
        return st.tuples(*map(st.from_type, elem_types))
    # Now, confirm that we're dealing with a generic type as we expected
    if not isinstance(thing, typing.GenericMeta):  # pragma: no cover
        raise ResolutionFailed('Cannot resolve %s to a strategy' % (thing,))
    # Parametrised generic types have their __origin__ attribute set to the
    # un-parametrised version, which we need to use in the subclass checks.
    # e.g.:     typing.List[int].__origin__ == typing.List
    mapping = {k: v for k, v in _global_type_lookup.items()
               if isinstance(k, typing.GenericMeta) and
               try_issubclass(k, getattr(thing, '__origin__', None) or thing)}
    if typing.Dict in mapping:
        # The subtype relationships between generic and concrete View types
        # are sometimes inconsistent under Python 3.5, so we pop them out to
        # preserve our invariant that all examples of from_type(T) are
        # instances of type T - and simplify the strategy for abstract types
        # such as Container
        for t in (typing.KeysView, typing.ValuesView, typing.ItemsView):
            mapping.pop(t, None)
    strategies = [v if isinstance(v, st.SearchStrategy) else v(thing)
                  for k, v in mapping.items()
                  if sum(try_issubclass(k, T) for T in mapping) == 1]
    empty = ', '.join(repr(s) for s in strategies if s.is_empty)
    if empty or not strategies:  # pragma: no cover
        raise ResolutionFailed(
            'Could not resolve %s to a strategy; consider using '
            'register_type_strategy' % (empty or thing,))
    return st.one_of(strategies)
Ejemplo n.º 7
0
 def steps(self):
     strategies = []
     for rule in self.rules():
         converted_arguments = {}
         valid = True
         if rule.precondition is not None and not rule.precondition(self):
             continue
         for k, v in sorted(rule.arguments.items()):
             if isinstance(v, Bundle):
                 bundle = self.bundle(v.name)
                 if not bundle:
                     valid = False
                     break
                 else:
                     v = sampled_from(bundle)
             converted_arguments[k] = v
         if valid:
             strategies.append(TupleStrategy((
                 just(rule),
                 FixedKeysDictStrategy(converted_arguments)
             ), tuple))
     if not strategies:
         raise InvalidDefinition(
             u'No progress can be made from state %r' % (self,)
         )
     return one_of(*strategies)
Ejemplo n.º 8
0
def ibm_compatible_floats(draw, min_value=None, max_value=None):
    if min_value is None:
        min_value = MIN_IBM_FLOAT
        
    if max_value is None:
        max_value = MAX_IBM_FLOAT
    
    truncated_min_f = max(min_value, MIN_IBM_FLOAT)
    truncated_max_f = min(max_value, MAX_IBM_FLOAT)

    strategies = []
    if truncated_min_f <= LARGEST_NEGATIVE_NORMAL_IBM_FLOAT <= truncated_max_f:
        strategies.append(floats(truncated_min_f, LARGEST_NEGATIVE_NORMAL_IBM_FLOAT))

    if truncated_min_f <= SMALLEST_POSITIVE_NORMAL_IBM_FLOAT <= truncated_max_f:
        strategies.append(floats(SMALLEST_POSITIVE_NORMAL_IBM_FLOAT, truncated_max_f))

    if truncated_min_f <= 0 <= truncated_max_f:
        strategies.append(just(0.0))

    if len(strategies) == 0:
        strategies.append(floats(truncated_min_f, truncated_max_f))

    ibm = draw(one_of(*strategies))
    return ibm
Ejemplo n.º 9
0
def _get_strategy_for_field(f):
    if f.choices:
        choices = []
        for value, name_or_optgroup in f.choices:
            if isinstance(name_or_optgroup, (list, tuple)):
                choices.extend(key for key, _ in name_or_optgroup)
            else:
                choices.append(value)
        if isinstance(f, (dm.CharField, dm.TextField)) and f.blank:
            choices.insert(0, u'')
        strategy = st.sampled_from(choices)
    elif type(f) == dm.SlugField:
        strategy = st.text(alphabet=string.ascii_letters + string.digits,
                           min_size=(None if f.blank else 1),
                           max_size=f.max_length)
    elif type(f) == dm.GenericIPAddressField:
        lookup = {'both': ip4_addr_strings() | ip6_addr_strings(),
                  'ipv4': ip4_addr_strings(), 'ipv6': ip6_addr_strings()}
        strategy = lookup[f.protocol.lower()]
    elif type(f) in (dm.TextField, dm.CharField):
        strategy = st.text(min_size=(None if f.blank else 1),
                           max_size=f.max_length)
    elif type(f) == dm.DecimalField:
        bound = Decimal(10 ** f.max_digits - 1) / (10 ** f.decimal_places)
        strategy = st.decimals(min_value=-bound, max_value=bound,
                               places=f.decimal_places)
    else:
        strategy = field_mappings().get(type(f), st.nothing())
    if f.validators:
        strategy = strategy.filter(validator_to_filter(f))
    if f.null:
        strategy = st.one_of(st.none(), strategy)
    return strategy
Ejemplo n.º 10
0
 def define_set_strategy(specifier, settings):
     if not specifier:
         return st.sets(max_size=0)
     else:
         with settings:
             return st.sets(st.one_of(
                 *[self(s, settings) for s in specifier]))
Ejemplo n.º 11
0
def _for_text(field):
    # We can infer a vastly more precise strategy by considering the
    # validators as well as the field type.  This is a minimal proof of
    # concept, but we intend to leverage the idea much more heavily soon.
    # See https://github.com/HypothesisWorks/hypothesis-python/issues/1116
    regexes = [
        re.compile(v.regex, v.flags) if isinstance(v.regex, str) else v.regex
        for v in field.validators
        if isinstance(v, django.core.validators.RegexValidator) and not v.inverse_match
    ]
    if regexes:
        # This strategy generates according to one of the regexes, and
        # filters using the others.  It can therefore learn to generate
        # from the most restrictive and filter with permissive patterns.
        # Not maximally efficient, but it makes pathological cases rarer.
        # If you want a challenge: extend https://qntm.org/greenery to
        # compute intersections of the full Python regex language.
        return st.one_of(*[st.from_regex(r) for r in regexes])
    # If there are no (usable) regexes, we use a standard text strategy.
    min_size = 1
    if getattr(field, "blank", False) or not getattr(field, "required", True):
        min_size = 0
    strategy = st.text(
        alphabet=st.characters(
            blacklist_characters=u"\x00", blacklist_categories=("Cs",)
        ),
        min_size=min_size,
        max_size=field.max_length,
    )
    if getattr(field, "required", True):
        strategy = strategy.filter(lambda s: s.strip())
    return strategy
Ejemplo n.º 12
0
def applicatives(substrat):
    return one_of(
        strat.lists(substrat),
        strat.lists(substrat).map(tuple),
        substrat.map(lenses.identity.Identity),
        apply_strat(maybes(), substrat),
    )
Ejemplo n.º 13
0
def models(model, **extra):
    result = {}
    mappings = field_mappings()
    mandatory = set()
    for f in model._meta.concrete_fields:
        if isinstance(f, dm.AutoField):
            continue
        try:
            mapped = mappings[type(f)]
        except KeyError:
            if not f.null:
                mandatory.add(f.name)
            continue
        if f.null:
            mapped = st.one_of(st.none(), mapped)
        result[f.name] = mapped
    missed = {x for x in mandatory if x not in extra}
    if missed:
        raise InvalidArgument((
            u'Missing arguments for mandatory field%s %s for model %s' % (
                u's' if len(missed) > 1 else u'',
                u', '.join(missed),
                model.__name__,
            )))
    result.update(extra)
    # Remove default_values so we don't try to generate anything for those.
    result = {k: v for k, v in result.items() if v is not default_value}
    return ModelStrategy(model, result)
Ejemplo n.º 14
0
def scalar_dtypes():
    # type: () -> st.SearchStrategy[np.dtype]
    """Return a strategy that can return any non-flexible scalar dtype."""
    return st.one_of(boolean_dtypes(),
                     integer_dtypes(), unsigned_integer_dtypes(),
                     floating_dtypes(), complex_number_dtypes(),
                     datetime64_dtypes(), timedelta64_dtypes())
Ejemplo n.º 15
0
def one_of_strategies(xs):
    """Helper function for unioning multiple strategies."""
    xs = tuple(xs)
    if not xs:
        raise ValueError('Cannot join an empty list of strategies')
    from hypothesis.strategies import one_of
    return one_of(xs)
Ejemplo n.º 16
0
def s_codes_extend(codes):
    return s.one_of(
        s.builds(app, codes, codes),
        s.builds(abstract, codes),
        s.builds(join, codes, codes),
        s.builds(QUOTE, codes),
    )
Ejemplo n.º 17
0
def python_number(draw, min_val, max_val):
    return draw(st.one_of(st.floats(min_val,
                                    max_val,
                                    allow_nan=False,
                                    allow_infinity=False),
                          st.integers(min_val,
                                      max_val)))
Ejemplo n.º 18
0
def jenkins_build_results(inQueue=None, builds=None):
    """Create a strategy for generating Jenkins API information for a job.

    :param strategy inQueue: strategy for the inQueue key, or None to use
        the default.
    :param strategy builds: strategy for populating the builds key, or None
        for the default. The special value `NO_BUILDS` will mean that the
        builds key is not in the resulting dict at all.
    :return strategy: a strategy.
    """
    strats = []
    if inQueue is None:
        inQueue = booleans()
        strats.append(just(pmap()))
    without_builds = fixed_dictionaries(dict(
        inQueue=inQueue))
    if builds is None or builds is NO_BUILDS:
        strats.append(without_builds)
    if builds is None:
        builds = lists(jenkins_builds, average_size=1)
    if builds is not NO_BUILDS:
        with_builds = fixed_dictionaries(dict(
            inQueue=inQueue,
            builds=builds,
            property=dictionaries(
                text(max_size=2), text(max_size=2),
                average_size=1, max_size=2)))
        strats.append(with_builds)
    return one_of(*strats)
Ejemplo n.º 19
0
def question_type_and_weight() -> SearchStrategy:
    return one_of(
        tuples(sampled_from(Question.CHOICE_TYPES),
               fixed_decimals()),
        tuples(sampled_from(sorted(set(Question.available_types()) - set(Question.CHOICE_TYPES))),
               just(0))
    )
 def add_optional_field(name, strategy):
     val = draw(one_of(none(), strategy))
     if val is not None:
         event('unit.{}: optional field given value'.format(name))
         result[name] = val
     else:
         event('unit.{}: optional field missing'.format(name))
Ejemplo n.º 21
0
    def steps(self):
        strategies = []
        for rule in self.rules():
            converted_arguments = {}
            valid = True
            if rule.precondition is not None and not rule.precondition(self):
                continue
            for k, v in sorted(rule.arguments.items()):
                if isinstance(v, Bundle):
                    bundle = self.bundle(v.name)
                    if not bundle:
                        valid = False
                        break
                converted_arguments[k] = v
            if valid:
                strategies.append(TupleStrategy((
                    just(rule),
                    FixedKeysDictStrategy(converted_arguments)
                ), tuple))
        if not strategies:
            raise InvalidDefinition(
                u'No progress can be made from state %r' % (self,)
            )

        for name, bundle in self.bundles.items():
            if len(bundle) > 1:
                strategies.append(
                    builds(
                        ShuffleBundle, just(name),
                        lists(integers(0, len(bundle) - 1))))

        return one_of(strategies)
Ejemplo n.º 22
0
def Times(draw, max_value, min_value):
    time = one_of(datetimes(max_value=max_value, min_value=min_value),
                  TimesLeapsecond)

    time = Time(draw(time))

    return time
Ejemplo n.º 23
0
def _get_strategy_for_field(f):
    if isinstance(f, dm.AutoField):
        return default_value
    elif f.choices:
        choices = [value for (value, name) in f.choices]
        if isinstance(f, (dm.CharField, dm.TextField)) and f.blank:
            choices.append(u'')
        strategy = st.sampled_from(choices)
    elif isinstance(f, dm.EmailField):
        return ff.fake_factory(u'email')
    elif type(f) in (dm.TextField, dm.CharField):
        strategy = st.text(min_size=(None if f.blank else 1),
                           max_size=f.max_length)
    elif type(f) == dm.DecimalField:
        m = 10 ** f.max_digits - 1
        div = 10 ** f.decimal_places
        q = Decimal('1.' + ('0' * f.decimal_places))
        strategy = (
            st.integers(min_value=-m, max_value=m)
            .map(lambda n: (Decimal(n) / div).quantize(q)))
    else:
        try:
            strategy = field_mappings()[type(f)]
        except KeyError:
            if f.null:
                return None
            else:
                raise UnmappedFieldError(f)
    if f.validators:
        strategy = strategy.filter(validator_to_filter(f))
    if f.null:
        strategy = st.one_of(st.none(), strategy)
    return strategy
Ejemplo n.º 24
0
def _create_hyp_nested_strategy(simple_class_strategy):
    """
    Create a recursive attrs class.

    Given a strategy for building (simpler) classes, create and return
    a strategy for building classes that have the simpler class as an
    attribute.
    """
    # Use a tuple strategy to combine simple attributes and an attr class.
    def just_class(tup):
        combined_attrs = list(tup[0])
        combined_attrs.append(attr.ib(default=attr.Factory(tup[1])))
        return _create_hyp_class(combined_attrs)

    def list_of_class(tup):
        default = attr.Factory(lambda: [tup[1]()])
        combined_attrs = list(tup[0])
        combined_attrs.append(attr.ib(default=default))
        return _create_hyp_class(combined_attrs)

    def dict_of_class(tup):
        default = attr.Factory(lambda: {"cls": tup[1]()})
        combined_attrs = list(tup[0])
        combined_attrs.append(attr.ib(default=default))
        return _create_hyp_class(combined_attrs)

    return st.one_of(st.tuples(st.lists(simple_attrs), simple_class_strategy)
                     .map(just_class),
                     st.tuples(st.lists(simple_attrs), simple_class_strategy)
                     .map(list_of_class))
Ejemplo n.º 25
0
def models(model, **extra):
    result = {}
    mappings = field_mappings()
    mandatory = set()
    for f in model._meta.concrete_fields:
        if isinstance(f, dm.AutoField):
            continue
        try:
            mapped = mappings[type(f)]
        except KeyError:
            if not f.null:
                mandatory.add(f.name)
            continue
        if f.null:
            mapped = st.one_of(st.none(), mapped)
        result[f.name] = mapped
    missed = {x for x in mandatory if x not in extra}
    if missed:
        raise InvalidArgument((
            'Missing arguments for mandatory field%s %s for model %s' % (
                's' if len(missed) > 1 else '',
                ', '.join(missed),
                model.__name__,
            )))
    for k, v in extra.items():
        if isinstance(v, SearchStrategy):
            result[k] = v
        else:
            result[k] = st.just(v)
    result.update(extra)
    return ModelStrategy(model, result)
Ejemplo n.º 26
0
def unaryop_node(draw, op=hs.one_of(non_bool_unary_op, unary_bool_operator),
                 operand=const_node()):
    op = draw(op)
    operand = draw(operand)
    node = astroid.UnaryOp(op)
    node.postinit(operand)
    return node
Ejemplo n.º 27
0
def format_specifiers(draw):
    """
    Generate a valid format specifier using the rules:

    format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
    fill        ::=  <any character>
    align       ::=  "<" | ">" | "=" | "^"
    sign        ::=  "+" | "-" | " "
    width       ::=  integer
    precision   ::=  integer
    type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

    See https://docs.python.org/2/library/string.html

    :param draw: Let hypothesis draw from other strategies.

    :return: An example format_specifier.
    """
    alphabet_strategy = st.characters(min_codepoint=ord('a'), max_codepoint=ord('z'))
    fill = draw(st.one_of(alphabet_strategy, st.none()))
    align = draw(st.sampled_from(list('<>=^')))
    fill_align = (fill + align or '') if fill else ''

    type_ = draw(st.sampled_from('bcdeEfFgGnosxX%'))
    can_have_sign = type_ in 'deEfFgGnoxX%'
    can_have_comma = type_ in 'deEfFgG%'
    can_have_precision = type_ in 'fFgG'
    can_have_pound = type_ in 'boxX%'
    can_have_zero = type_ in 'oxX'

    sign = draw(st.sampled_from(list('+- ') + [''])) if can_have_sign else ''
    pound = draw(st.sampled_from(('#', '',))) if can_have_pound else ''
    zero = draw(st.sampled_from(('0', '',))) if can_have_zero else ''

    int_strategy = st.integers(min_value=1, max_value=1000)

    width = draw(st.one_of(int_strategy, st.none()))
    width = str(width) if width is not None else ''

    comma = draw(st.sampled_from((',', '',))) if can_have_comma else ''
    if can_have_precision:
        precision = draw(st.one_of(int_strategy, st.none()))
        precision = '.' + str(precision) if precision else ''
    else:
        precision = ''

    return ''.join((fill_align, sign, pound, zero, width, comma, precision, type_,))
Ejemplo n.º 28
0
def s_codes_extend(terms):
    return s.one_of(
        s.builds(APP, terms, terms),
        s.builds(JOIN, terms, terms),
        s.builds(QUOTE, terms),
        s.builds(ABS, terms),
        s.builds(FUN, s_vars, terms),
    )
Ejemplo n.º 29
0
def instance_values() -> SearchStrategy:
    return one_of(
        sane_text(max_size=300, average_size=10),
        integers(),
        booleans(),
        floats(),
        numeric_strings()
    )
Ejemplo n.º 30
0
 def path(cls):
     """
     Strategy for path name generation.
     """
     alphabet = one_of(cls.A_Z(), cls.a_z(), cls.numbers(),
                       cls.punctuation())
     # Create an absolute path from the alphabet by prepending a '/'
     return text(alphabet, average_size=20).map(lambda s: '/' + s)
Ejemplo n.º 31
0
from ErnosCube.orient_enum import OrientEnum
from ErnosCube.face_enum import FaceEnum
from ErnosCube.sticker import Sticker
from ErnosCube.cube import Cube
from ErnosCube.rotation_enum import RotationEnum
from ErnosCube.axis_enum import AxisEnum
from ErnosCube.cube_mutation import CubeMutation

from hypothesis.strategies import sampled_from, builds, one_of
from hypothesis.strategies import lists, integers

orient_enums = sampled_from(list(OrientEnum.__members__.values()))
face_enums = sampled_from(list(FaceEnum.__members__.values()))
stickers = builds(Sticker, face_enums, orient_enums)
sticker_lists = lists(stickers, min_size=1, max_size=3)


def gen_sticker_matrix(n):
    sticker_row = lists(stickers, min_size=n, max_size=n)
    return lists(sticker_row, min_size=n, max_size=n)


sticker_matrices = one_of(gen_sticker_matrix(i) for i in range(1, 3))
cubes = builds(Cube, integers(min_value=1, max_value=5))
cubes_2 = builds(Cube, integers(min_value=2, max_value=2))

axis_enums = sampled_from(list(AxisEnum.__members__.values()))
rotation_enums = sampled_from(list(RotationEnum.__members__.values()))
layers = integers(min_value=-1)
cube_mutations = builds(CubeMutation, axis_enums, rotation_enums, layers)
Ejemplo n.º 32
0
from pymesis import _cache as pymesis_cache

strategies = (
    st.integers(),
    st.text(),
    st.binary(),
    st.booleans(),
    st.characters(),
    st.complex_numbers(allow_nan=False),
    st.dates(),
    st.datetimes(),
    st.decimals(allow_nan=False),
    st.dictionaries(st.text(), st.text()),
    st.floats(allow_nan=False),
    st.fractions(),
    st.functions(),
    st.iterables(st.text()),
    st.none(),
)


@given(st.one_of(strategies))
def test_cache(data):
    pymesis_cache.clear_cache()

    pymesis_cache.add_data(hash("key"), data)
    retrieved_data = pymesis_cache.get_data_if_cached(hash("key"))

    assert retrieved_data == data
        with new_request(context) as request:
            request.session['test'] = 1
            id = request.session._session.id
        with new_request(context) as request:
            assert_same_session(request, id)
            request.session.invalidate()
            assert len(request.session.items()) == 0
            assert_new_session(request, id)
        with new_request(context) as request:
            assert_new_session(request, id)


@given(
    settings=valid_settings(),
    messages=st.lists(elements=st.text(max_size=255), min_size=1, max_size=8),
    queue=st.one_of(st.none(), st.text(max_size=64)),
    wrong_queue=st.text(max_size=64),
    allow_duplicate=st.sampled_from((True, False, None)),
)
def test_isession_flash(
    settings,
    messages,
    queue,
    wrong_queue,
    allow_duplicate,
    ):
    assume(wrong_queue != queue)
    assume(not (wrong_queue == '' and queue is None))
    q_args = {} if queue is None else {'queue': queue}
    flash_args = q_args.copy()
    if allow_duplicate is not None:
Ejemplo n.º 34
0
    import sys, random
    from .test_recompiler import verify

    ALL_PRIMITIVES = [
        'unsigned char',
        'short',
        'int',
        'long',
        'long long',
        'float',
        'double',
        #'long double',   --- on x86 it can give libffi crashes
    ]
    def _make_struct(s):
        return st.lists(s, min_size=1)
    types = st.one_of(st.sampled_from(ALL_PRIMITIVES),
                      st.lists(st.sampled_from(ALL_PRIMITIVES), min_size=1))
    # NB. 'types' could be st.recursive instead, but it doesn't
    # really seem useful

    def draw_primitive(ffi, typename):
        value = random.random() * 2**40
        if typename != 'long double':
            return ffi.cast(typename, value)
        else:
            return value

    TEST_RUN_COUNTER = 0


    @given(st.lists(types), types)
    @settings(max_examples=100, deadline=5000)   # 5000ms
Ejemplo n.º 35
0
def from_typing_type(thing):
    # We start with special-case support for Union and Tuple - the latter
    # isn't actually a generic type.  Support for Callable may be added to
    # this section later.
    # We then explicitly error on non-Generic types, which don't carry enough
    # information to sensibly resolve to strategies at runtime.
    # Finally, we run a variation of the subclass lookup in st.from_type
    # among generic types in the lookup.
    import typing

    # Under 3.6 Union is handled directly in st.from_type, as the argument is
    # not an instance of `type`. However, under Python 3.5 Union *is* a type
    # and we have to handle it here, including failing if it has no parameters.
    if hasattr(thing, "__union_params__"):  # pragma: no cover
        args = sorted(thing.__union_params__ or (), key=type_sorting_key)
        if not args:
            raise ResolutionFailed("Cannot resolve Union of no types.")
        return st.one_of([st.from_type(t) for t in args])
    if getattr(thing, "__origin__", None) == tuple or isinstance(
        thing, getattr(typing, "TupleMeta", ())
    ):
        elem_types = getattr(thing, "__tuple_params__", None) or ()
        elem_types += getattr(thing, "__args__", None) or ()
        if (
            getattr(thing, "__tuple_use_ellipsis__", False)
            or len(elem_types) == 2
            and elem_types[-1] is Ellipsis
        ):
            return st.lists(st.from_type(elem_types[0])).map(tuple)
        elif len(elem_types) == 1 and elem_types[0] == ():
            return st.tuples()  # Empty tuple; see issue #1583
        return st.tuples(*map(st.from_type, elem_types))
    if isinstance(thing, typing.TypeVar):
        if getattr(thing, "__bound__", None) is not None:
            return st.from_type(thing.__bound__)
        if getattr(thing, "__constraints__", None):
            return st.shared(
                st.sampled_from(thing.__constraints__), key="typevar-with-constraint"
            ).flatmap(st.from_type)
        # Constraints may be None or () on various Python versions.
        return st.text()  # An arbitrary type for the typevar
    # Now, confirm that we're dealing with a generic type as we expected
    if not isinstance(thing, typing_root_type):  # pragma: no cover
        raise ResolutionFailed("Cannot resolve %s to a strategy" % (thing,))
    # Parametrised generic types have their __origin__ attribute set to the
    # un-parametrised version, which we need to use in the subclass checks.
    # e.g.:     typing.List[int].__origin__ == typing.List
    mapping = {
        k: v
        for k, v in _global_type_lookup.items()
        if isinstance(k, typing_root_type) and try_issubclass(k, thing)
    }
    if typing.Dict in mapping:
        # The subtype relationships between generic and concrete View types
        # are sometimes inconsistent under Python 3.5, so we pop them out to
        # preserve our invariant that all examples of from_type(T) are
        # instances of type T - and simplify the strategy for abstract types
        # such as Container
        for t in (typing.KeysView, typing.ValuesView, typing.ItemsView):
            mapping.pop(t, None)
    strategies = [
        v if isinstance(v, st.SearchStrategy) else v(thing)
        for k, v in mapping.items()
        if sum(try_issubclass(k, T) for T in mapping) == 1
    ]
    empty = ", ".join(repr(s) for s in strategies if s.is_empty)
    if empty or not strategies:  # pragma: no cover
        raise ResolutionFailed(
            "Could not resolve %s to a strategy; consider using "
            "register_type_strategy" % (empty or thing,)
        )
    return st.one_of(strategies)
Ejemplo n.º 36
0
    for node in mol.nodes:
        H_neighbors = sum(1 for neighbor in mol[node]
                          if mol.nodes[neighbor].get('element') == 'H')
        hcount = mol.nodes[node].get('hcount', 0)
        if H_neighbors + hcount > 9:
            mol.nodes[node]['hcount'] = hcount + H_neighbors - 9


isotope = st.integers(min_value=1)
element = st.sampled_from('C N O S P H'.split())
hcount = st.integers(min_value=0, max_value=9)
charge = st.integers(min_value=-99, max_value=99)
class_ = st.integers(min_value=1)

node_data = st.fixed_dictionaries({
    'isotope': st.one_of(st.none(), isotope),
    'hcount': st.one_of(st.none(), hcount),
    'element': st.one_of(st.none(), element),
    'charge': charge,  # Charge can not be none for comparison reasons
    'aromatic': st.just(False),
    'class': st.one_of(st.none(), class_)
}).map(no_none_value).map(no_hydrogen_hcount)
order = st.sampled_from([1, 2, 3, 0, 4, 1.5])
edge_data = st.fixed_dictionaries({'order': order})

arom_triangle = read_smiles('[*]1[*][*]1')
for edge in arom_triangle.edges:
    arom_triangle.edges[edge]['order'] = 1


class SMILESTest(RuleBasedStateMachine):
Ejemplo n.º 37
0
    bloom1 = bigsi.bloom([kmer])
    bigsi.build([bloom1], ['1234'])
    assert bigsi.lookup(kmer) == {kmer: ['1234']}
    bigsi.insert(bloom1, '1235')
    assert bigsi.lookup(kmer) == {kmer: ['1234', '1235']}
    bigsi.delete_all()


@given(Graph=ST_GRAPH,
       s=ST_SAMPLE_NAME,
       key=st.text(min_size=1),
       value=st.text(min_size=1),
       value2=st.one_of(
           st.text(min_size=1),
           st.dictionaries(keys=st.text(min_size=1),
                           values=st.text(min_size=1)),
           st.lists(st.integers()),
           st.sets(st.integers()),
       ))
@settings(max_examples=2)
def test_add_metadata(Graph, s, key, value, value2):
    kmer = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    bigsi = Graph.create(m=100, force=True)
    bloom1 = bigsi.bloom([kmer])
    bigsi.build([bloom1], [s])
    bigsi.add_sample_metadata(s, key, value)
    assert bigsi.lookup_sample_metadata(s).get(key) == value
    with pytest.raises(ValueError):
        bigsi.add_sample_metadata(s, key, value2)
    assert bigsi.lookup_sample_metadata(s).get(key) == value
    # Key already exists
Ejemplo n.º 38
0
    "min_dims": 15
}, {
    "min_dims": 32
}])
def test_interesting_array_shapes_argument(kwargs):
    nps.array_shapes(**kwargs).example()


@given(nps.scalar_dtypes())
def test_can_generate_scalar_dtypes(dtype):
    assert isinstance(dtype, np.dtype)


@settings(max_examples=100)
@given(
    nps.nested_dtypes(subtype_strategy=st.one_of(nps.scalar_dtypes(
    ), nps.byte_string_dtypes(), nps.unicode_string_dtypes())))
def test_can_generate_compound_dtypes(dtype):
    assert isinstance(dtype, np.dtype)


@settings(max_examples=100)
@given(
    nps.nested_dtypes(subtype_strategy=st.one_of(nps.scalar_dtypes(
    ), nps.byte_string_dtypes(), nps.unicode_string_dtypes())).flatmap(
        lambda dt: nps.arrays(dtype=dt, shape=1)))
def test_can_generate_data_compound_dtypes(arr):
    # This is meant to catch the class of errors which prompted PR #2085
    assert isinstance(arr, np.ndarray)


@given(nps.nested_dtypes())
Ejemplo n.º 39
0
def from_typing_type(thing):
    # We start with special-case support for Union and Tuple - the latter
    # isn't actually a generic type. Then we handle Literal since it doesn't
    # support `isinstance`. Support for Callable may be added to this section
    # later.
    # We then explicitly error on non-Generic types, which don't carry enough
    # information to sensibly resolve to strategies at runtime.
    # Finally, we run a variation of the subclass lookup in st.from_type
    # among generic types in the lookup.
    #
    # Under 3.6 Union is handled directly in st.from_type, as the argument is
    # not an instance of `type`. However, under Python 3.5 Union *is* a type
    # and we have to handle it here, including failing if it has no parameters.
    if hasattr(thing, "__union_params__"):  # pragma: no cover
        args = sorted(thing.__union_params__ or (), key=type_sorting_key)
        if not args:
            raise ResolutionFailed("Cannot resolve Union of no types.")
        return st.one_of([st.from_type(t) for t in args])
    if getattr(thing, "__origin__", None) == tuple or isinstance(
            thing, getattr(typing, "TupleMeta", ())):
        elem_types = getattr(thing, "__tuple_params__", None) or ()
        elem_types += getattr(thing, "__args__", None) or ()
        if (getattr(thing, "__tuple_use_ellipsis__", False)
                or len(elem_types) == 2 and elem_types[-1] is Ellipsis):
            return st.lists(st.from_type(elem_types[0])).map(tuple)
        elif len(elem_types) == 1 and elem_types[0] == ():
            return st.tuples()  # Empty tuple; see issue #1583
        return st.tuples(*map(st.from_type, elem_types))
    if (hasattr(typing, "Final") and getattr(thing, "__origin__", None)
            == typing.Final):  # pragma: no cover  # new in Python 3.8
        return st.one_of([st.from_type(t) for t in thing.__args__])
    if is_typing_literal(thing):  # pragma: no cover  # new in Python 3.8
        args_dfs_stack = list(thing.__args__)
        literals = []
        while args_dfs_stack:
            arg = args_dfs_stack.pop()
            if is_typing_literal(arg):
                args_dfs_stack.extend(reversed(arg.__args__))
            else:
                literals.append(arg)
        return st.sampled_from(literals)
    if isinstance(thing, typing.TypeVar):
        if getattr(thing, "__bound__", None) is not None:
            bound = thing.__bound__
            if isinstance(bound, ForwardRef):
                bound = _try_import_forward_ref(thing, bound)
            strat = unwrap_strategies(st.from_type(bound))
            if not isinstance(strat, OneOfStrategy):
                return strat
            # The bound was a union, or we resolved it as a union of subtypes,
            # so we need to unpack the strategy to ensure consistency across uses.
            # This incantation runs a sampled_from over the strategies inferred for
            # each part of the union, wraps that in shared so that we only generate
            # from one type per testcase, and flatmaps that back to instances.
            return st.shared(st.sampled_from(strat.original_strategies),
                             key="typevar=%r" % (thing, )).flatmap(lambda s: s)
        if getattr(thing, "__constraints__", None):
            return st.shared(st.sampled_from(thing.__constraints__),
                             key="typevar=%r" % (thing, )).flatmap(
                                 st.from_type)
        # Constraints may be None or () on various Python versions.
        return st.text()  # An arbitrary type for the typevar
    # Now, confirm that we're dealing with a generic type as we expected
    if not isinstance(thing, typing_root_type):  # pragma: no cover
        raise ResolutionFailed("Cannot resolve %s to a strategy" % (thing, ))

    # Some "generic" classes are not generic *in* anything - for example both
    # Hashable and Sized have `__args__ == ()` on Python 3.7 or later.
    # (In 3.6 they're just aliases for the collections.abc classes)
    origin = getattr(thing, "__origin__", thing)
    if (typing.Hashable is not collections.abc.Hashable
            and origin in vars(collections.abc).values()
            and len(getattr(thing, "__args__", None) or []) == 0
        ):  # pragma: no cover  # impossible on 3.6 where we measure coverage.
        return st.from_type(origin)

    # Parametrised generic types have their __origin__ attribute set to the
    # un-parametrised version, which we need to use in the subclass checks.
    # e.g.:     typing.List[int].__origin__ == typing.List
    mapping = {
        k: v
        for k, v in _global_type_lookup.items()
        if is_generic_type(k) and try_issubclass(k, thing)
    }
    if typing.Dict in mapping:
        # The subtype relationships between generic and concrete View types
        # are sometimes inconsistent under Python 3.5, so we pop them out to
        # preserve our invariant that all examples of from_type(T) are
        # instances of type T - and simplify the strategy for abstract types
        # such as Container
        for t in (typing.KeysView, typing.ValuesView, typing.ItemsView):
            mapping.pop(t, None)
    if len(mapping) > 1:
        # issubclass treats bytestring as a kind of sequence, which it is,
        # but treating it as such breaks everything else when it is presumed
        # to be a generic sequence or container that could hold any item.
        # Except for sequences of integers, or unions which include integer!
        # See https://github.com/HypothesisWorks/hypothesis/issues/2257
        #
        # This block drops ByteString from the types that can be generated
        # if there is more than one allowed type, and the element type is
        # not either `int` or a Union with `int` as one of its elements.
        elem_type = (getattr(thing, "__args__", None) or ["not int"])[0]
        if getattr(elem_type, "__origin__", None) is typing.Union:
            union_elems = elem_type.__args__
        elif hasattr(elem_type, "__union_params__"):  # pragma: no cover
            union_elems = elem_type.__union_params__  # python 3.5 only
        else:
            union_elems = ()
        if not any(
                isinstance(T, type) and issubclass(int, T)
                for T in list(union_elems) + [elem_type]):
            mapping.pop(typing.ByteString, None)
    strategies = [
        v if isinstance(v, st.SearchStrategy) else v(thing)
        for k, v in mapping.items() if sum(
            try_issubclass(k, T) for T in mapping) == 1
    ]
    empty = ", ".join(repr(s) for s in strategies if s.is_empty)
    if empty or not strategies:
        raise ResolutionFailed(
            "Could not resolve %s to a strategy; consider using "
            "register_type_strategy" % (empty or thing, ))
    return st.one_of(strategies)
Ejemplo n.º 40
0
        return True


test_client = TestClient(app=app)

values_strategy = (
        st.none() |
        st.booleans() |
        st.text() |
        st.integers()
)

order_item_strategy = st.fixed_dictionaries(
    {
        'product': values_strategy,
        'size': st.one_of(st.sampled_from(('small', 'medium', 'big'))) | values_strategy,
        'quantity': values_strategy
    }
)

strategy = st.fixed_dictionaries({
    'order': st.lists(order_item_strategy)
})


@settings(verbosity=Verbosity.verbose, max_examples=500)
@given(strategy)
def test(payload):
    response = test_client.post('/orders', json=payload)
    if is_valid_payload(payload, create_order_schema):
        assert response.status_code == 201
Ejemplo n.º 41
0
These ones pass, just as you'd hope!

"""
import hypothesis.extra.numpy as npst
import hypothesis.strategies as st
from hypothesis import given, settings

import xarray as xr

# Run for a while - arrays are a bigger search space than usual
settings.register_profile("ci", deadline=None)
settings.load_profile("ci")

an_array = npst.arrays(
    dtype=st.one_of(npst.unsigned_integer_dtypes(), npst.integer_dtypes(),
                    npst.floating_dtypes()),
    shape=npst.array_shapes(max_side=3),  # max_side specified for performance
)


@given(st.data(), an_array)
def test_CFMask_coder_roundtrip(data, arr):
    names = data.draw(
        st.lists(st.text(), min_size=arr.ndim, max_size=arr.ndim,
                 unique=True).map(tuple))
    original = xr.Variable(names, arr)
    coder = xr.coding.variables.CFMaskCoder()
    roundtripped = coder.decode(coder.encode(original))
    xr.testing.assert_identical(original, roundtripped)

Ejemplo n.º 42
0
class TestProto:
    @hypothesis.given(
        port=st.integers(min_value=1, max_value=2**(32) - 1),
        request_path=st.text(min_size=1),
        health_path=st.text(min_size=1),
        repository=st.text(min_size=1),
        tag=st.one_of(st.none(), st.text(min_size=1)),
        sha=st.one_of(st.none(), st.text(min_size=1)),
        env_vars=st.one_of(
            st.none(),
            st.dictionaries(
                keys=st.text(min_size=1),
                values=st.text(min_size=1),
            ),
        ),
    )
    def test_from_proto(
        self,
        port,
        request_path,
        health_path,
        repository,
        tag,
        sha,
        env_vars,
    ):
        hypothesis.assume(tag or sha)

        request_path = arg_handler.ensure_starts_with_slash(request_path)
        health_path = arg_handler.ensure_starts_with_slash(health_path)

        msg = RegistryService_pb2.ModelVersion()
        msg.docker_metadata.request_port = port
        msg.docker_metadata.request_path = request_path
        msg.docker_metadata.health_path = health_path
        msg.environment.docker.repository = repository
        if tag is not None:
            msg.environment.docker.tag = tag
        if sha is not None:
            msg.environment.docker.sha = sha
        if env_vars is not None:
            msg.environment.environment_variables.extend(
                [
                    Environment_pb2.EnvironmentVariablesBlob(
                        name=name,
                        value=value
                    )
                    for name, value
                    in env_vars.items()
                ]
            )

        docker_image = DockerImage._from_model_ver_proto(msg)

        assert docker_image.port == port
        assert docker_image.request_path == request_path
        assert docker_image.health_path == health_path
        assert docker_image.repository == repository
        assert docker_image.tag == tag
        assert docker_image.sha == sha
        assert docker_image.env_vars == (env_vars or None)

    @hypothesis.given(
        port=st.integers(min_value=1, max_value=2**(32) - 1),
        request_path=st.text(min_size=1),
        health_path=st.text(min_size=1),
        repository=st.text(min_size=1),
        tag=st.one_of(st.none(), st.text(min_size=1)),
        sha=st.one_of(st.none(), st.text(min_size=1)),
        # pylint: disable=no-value-for-parameter
        env_vars=st.one_of(st.none(), strategies.env_vars()),
    )
    def test_as_proto(
        self,
        port,
        request_path,
        health_path,
        repository,
        tag,
        sha,
        env_vars,
    ):
        hypothesis.assume(tag or sha)

        docker_image = DockerImage(
            port=port,
            request_path=request_path,
            health_path=health_path,
            repository=repository,
            tag=tag,
            sha=sha,
            env_vars=env_vars,
        )
        msg = docker_image._as_model_ver_proto()

        # translate expected values
        request_path = arg_handler.ensure_starts_with_slash(request_path)
        health_path = arg_handler.ensure_starts_with_slash(health_path)
        if isinstance(env_vars, list):
            env_vars = {name: os.environ[name] for name in env_vars}

        assert msg.docker_metadata.request_port == port
        assert msg.docker_metadata.request_path == request_path
        assert msg.docker_metadata.health_path == health_path
        assert msg.environment.docker.repository == repository
        assert msg.environment.docker.tag == (tag or "")
        assert msg.environment.docker.sha == (sha or "")
        assert {
            var.name: var.value for var in msg.environment.environment_variables
        } == (env_vars or {})

    def test_merge_proto(self, model_version, docker_image):
        model_version._fetch_with_no_cache()

        expected_msg = RegistryService_pb2.ModelVersion()
        expected_msg.MergeFrom(model_version._msg)
        expected_msg.MergeFrom(docker_image._as_model_ver_proto())

        docker_image._merge_into_model_ver_proto(model_version._msg)
        assert model_version._msg == expected_msg
Ejemplo n.º 43
0
    def testDistribution(self, dist_name, data):
        seed = test_util.test_seed()
        # Explicitly draw event_dim here to avoid relying on _params_event_ndims
        # later, so this test can support distributions that do not implement the
        # slicing protocol.
        event_dim = data.draw(hps.integers(min_value=2, max_value=6))
        dist = data.draw(
            dhps.distributions(dist_name=dist_name,
                               event_dim=event_dim,
                               enable_vars=True))
        batch_shape = dist.batch_shape
        batch_shape2 = data.draw(
            tfp_hps.broadcast_compatible_shape(batch_shape))
        dist2 = data.draw(
            dhps.distributions(dist_name=dist_name,
                               batch_shape=batch_shape2,
                               event_dim=event_dim,
                               enable_vars=True))
        self.evaluate([var.initializer for var in dist.variables])

        # Check that the distribution passes Variables through to the accessor
        # properties (without converting them to Tensor or anything like that).
        for k, v in six.iteritems(dist.parameters):
            if not tensor_util.is_ref(v):
                continue
            self.assertIs(getattr(dist, k), v)

        # Check that standard statistics do not read distribution parameters more
        # than twice (once in the stat itself and up to once in any validation
        # assertions).
        max_permissible = 2 + extra_tensor_conversions_allowed(dist)
        for stat in sorted(
                data.draw(
                    hps.sets(hps.one_of(
                        map(hps.just, [
                            'covariance', 'entropy', 'mean', 'mode', 'stddev',
                            'variance'
                        ])),
                             min_size=3,
                             max_size=3))):
            hp.note('Testing excessive var usage in {}.{}'.format(
                dist_name, stat))
            try:
                with tfp_hps.assert_no_excessive_var_usage(
                        'statistic `{}` of `{}`'.format(stat, dist),
                        max_permissible=max_permissible):
                    getattr(dist, stat)()

            except NotImplementedError:
                pass

        # Check that `sample` doesn't read distribution parameters more than twice,
        # and that it produces non-None gradients (if the distribution is fully
        # reparameterized).
        with tf.GradientTape() as tape:
            # TDs do bijector assertions twice (once by distribution.sample, and once
            # by bijector.forward).
            max_permissible = 2 + extra_tensor_conversions_allowed(dist)
            with tfp_hps.assert_no_excessive_var_usage(
                    'method `sample` of `{}`'.format(dist),
                    max_permissible=max_permissible):
                sample = dist.sample(seed=seed)
        if dist.reparameterization_type == tfd.FULLY_REPARAMETERIZED:
            grads = tape.gradient(sample, dist.variables)
            for grad, var in zip(grads, dist.variables):
                var_name = var.name.rstrip('_0123456789:')
                if var_name in NO_SAMPLE_PARAM_GRADS.get(dist_name, ()):
                    continue
                if grad is None:
                    raise AssertionError(
                        'Missing sample -> {} grad for distribution {}'.format(
                            var_name, dist_name))

        # Turn off validations, since TODO(b/129271256) log_prob can choke on dist's
        # own samples.  Also, to relax conversion counts for KL (might do >2 w/
        # validate_args).
        dist = dist.copy(validate_args=False)
        dist2 = dist2.copy(validate_args=False)

        # Test that KL divergence reads distribution parameters at most once, and
        # that is produces non-None gradients.
        try:
            for d1, d2 in (dist, dist2), (dist2, dist):
                if dist_name in SKIP_KL_CHECK_DIST_VAR_GRADS:
                    continue
                with tf.GradientTape() as tape:
                    with tfp_hps.assert_no_excessive_var_usage(
                            '`kl_divergence` of (`{}` (vars {}), `{}` (vars {}))'
                            .format(d1, d1.variables, d2, d2.variables),
                            max_permissible=1
                    ):  # No validation => 1 convert per var.
                        kl = d1.kl_divergence(d2)
                wrt_vars = list(d1.variables) + list(d2.variables)
                grads = tape.gradient(kl, wrt_vars)
                for grad, var in zip(grads, wrt_vars):
                    if grad is None and dist_name not in NO_KL_PARAM_GRADS:
                        raise AssertionError(
                            'Missing KL({} || {}) -> {} grad:\n'  # pylint: disable=duplicate-string-formatting-argument
                            '{} vars: {}\n{} vars: {}'.format(
                                d1, d2, var, d1, d1.variables, d2,
                                d2.variables))
        except NotImplementedError:
            # Raised by kl_divergence if no registered KL is found.
            pass

        # Test that log_prob produces non-None gradients, except for distributions
        # on the NO_LOG_PROB_PARAM_GRADS blocklist.
        if dist_name not in NO_LOG_PROB_PARAM_GRADS:
            with tf.GradientTape() as tape:
                lp = dist.log_prob(tf.stop_gradient(sample))
            grads = tape.gradient(lp, dist.variables)
            for grad, var in zip(grads, dist.variables):
                if grad is None:
                    raise AssertionError(
                        'Missing log_prob -> {} grad for distribution {}'.
                        format(var, dist_name))

        # Test that all forms of probability evaluation avoid reading distribution
        # parameters more than once.
        for evaluative in sorted(
                data.draw(
                    hps.sets(hps.one_of(
                        map(hps.just, [
                            'log_prob', 'prob', 'log_cdf', 'cdf',
                            'log_survival_function', 'survival_function'
                        ])),
                             min_size=3,
                             max_size=3))):
            hp.note('Testing excessive var usage in {}.{}'.format(
                dist_name, evaluative))
            try:
                # No validation => 1 convert. But for TD we allow 2:
                # dist.log_prob(bijector.inverse(samp)) + bijector.ildj(samp)
                max_permissible = 2 + extra_tensor_conversions_allowed(dist)
                with tfp_hps.assert_no_excessive_var_usage(
                        'evaluative `{}` of `{}`'.format(evaluative, dist),
                        max_permissible=max_permissible):
                    getattr(dist, evaluative)(sample)
            except NotImplementedError:
                pass
Ejemplo n.º 44
0
import hypothesis.strategies as st
from hypothesis.extra.numpy import (floating_dtypes, integer_dtypes,
                                    unsigned_integer_dtypes)
from rasterio import check_dtype

st_rasterio_dtypes = st.one_of(
    integer_dtypes(),
    unsigned_integer_dtypes(),
    floating_dtypes(),
).filter(check_dtype)
    name = 'yolo'

    meas = Measurement()
    meas.name = name

    meas.register_parameter(DAC.ch1)
    meas.register_parameter(DMM.v1, setpoints=[DAC.ch1])

    with meas.run() as datasaver:
        run_id = datasaver.run_id
        expected_name = fmt.format(name, exp_id, run_id)
        assert datasaver.dataset.table_name == expected_name


@given(wp=hst.one_of(hst.integers(), hst.floats(allow_nan=False), hst.text()))
def test_setting_write_period(empty_temp_db, wp):
    new_experiment('firstexp', sample_name='no sample')
    meas = Measurement()

    if isinstance(wp, str):
        with pytest.raises(ValueError):
            meas.write_period = wp
    elif wp < 1e-3:
        with pytest.raises(ValueError):
            meas.write_period = wp
    else:
        meas.write_period = wp
        assert meas._write_period == wp

        with meas.run() as datasaver:
Ejemplo n.º 46
0
 st.complex_numbers(),
 numbers.Integral:
 st.integers(),
 numbers.Complex:
 st.complex_numbers(),
 slice:
 st.builds(
     slice,
     st.none() | st.integers(),
     st.none() | st.integers(),
     st.none() | st.integers(),
 ),
 range:
 st.one_of(
     st.integers(min_value=0).map(range),
     st.builds(range, st.integers(), st.integers()),
     st.builds(range, st.integers(), st.integers(),
               st.integers().filter(bool)),
 ),
 ipaddress.IPv4Address:
 ip_addresses(v=4),
 ipaddress.IPv6Address:
 ip_addresses(v=6),
 ipaddress.IPv4Interface:
 _networks(32).map(ipaddress.IPv4Interface),
 ipaddress.IPv6Interface:
 _networks(128).map(ipaddress.IPv6Interface),
 ipaddress.IPv4Network:
 st.one_of(
     _networks(32).map(lambda x: ipaddress.IPv4Network(x, strict=False)),
     st.sampled_from(SPECIAL_IPv4_RANGES).map(ipaddress.IPv4Network),
 ),
Ejemplo n.º 47
0
class TestEncodingProperties(object):
    """
    Property-based tests for our integer encoder and decoder.
    """
    @given(integer=integers(min_value=0),
           prefix_bits=integers(min_value=1, max_value=8))
    def test_encode_positive_integer_always_valid(self, integer, prefix_bits):
        """
        So long as the prefix bits are between 1 and 8, any positive integer
        can be represented.
        """
        result = encode_integer(integer, prefix_bits)
        assert isinstance(result, bytearray)
        assert len(result) > 0

    @given(integer=integers(max_value=-1),
           prefix_bits=integers(min_value=1, max_value=8))
    def test_encode_fails_for_negative_integers(self, integer, prefix_bits):
        """
        If the integer to encode is negative, the encoder fails.
        """
        with pytest.raises(ValueError):
            encode_integer(integer, prefix_bits)

    @given(integer=integers(min_value=0),
           prefix_bits=one_of(integers(max_value=0), integers(min_value=9)))
    def test_encode_fails_for_invalid_prefixes(self, integer, prefix_bits):
        """
        If the prefix is out of the range [1,8], the encoder fails.
        """
        with pytest.raises(ValueError):
            encode_integer(integer, prefix_bits)

    @given(prefix_bits=one_of(integers(max_value=0), integers(min_value=9)))
    def test_decode_fails_for_invalid_prefixes(self, prefix_bits):
        """
        If the prefix is out of the range [1,8], the encoder fails.
        """
        with pytest.raises(ValueError):
            decode_integer(b'\x00', prefix_bits)

    @given(data=binary(), prefix_bits=integers(min_value=1, max_value=8))
    def test_decode_either_succeeds_or_raises_error(self, data, prefix_bits):
        """
        Attempting to decode data either returns a positive integer or throws a
        HPACKDecodingError.
        """
        try:
            result, consumed = decode_integer(data, prefix_bits)
        except HPACKDecodingError:
            pass
        else:
            assert isinstance(result, int)
            assert result >= 0
            assert consumed > 0

    @given(integer=integers(min_value=0),
           prefix_bits=integers(min_value=1, max_value=8))
    def test_encode_decode_round_trips(self, integer, prefix_bits):
        """
        Given valid data, the encoder and decoder can round trip.
        """
        encoded_result = encode_integer(integer, prefix_bits)
        decoded_integer, consumed = decode_integer(bytes(encoded_result),
                                                   prefix_bits)
        assert integer == decoded_integer
        assert consumed > 0
Ejemplo n.º 48
0
def shapes(min_dims=0, max_dims=4, min_side=1, max_side=5):
    strategy = hnp.array_shapes(max(1, min_dims), max_dims, min_side, max_side)
    if min_dims < 1:
        strategy = hps.one_of(hps.just(()), strategy)
    return strategy
Ejemplo n.º 49
0
lengths = st.integers(min_value=1, max_value=int(1e7))
small_lengths = st.integers(min_value=1, max_value=int(1e4))

strands = st.sampled_from("+ -".split())
single_strand = st.sampled_from(["+"])
names = st.text("abcdefghijklmnopqrstuvxyz", min_size=1)
scores = st.integers(min_value=0, max_value=256)

datatype = st.sampled_from([pd.Series, np.array, list])

feature_data = st.sampled_from(["ensembl_gtf", "gencode_gtf", "ucsc_bed"])

chromosomes = st.sampled_from(
    ["chr{}".format(str(e)) for e in list(range(1, 23)) + "X Y M".split()])
chromosomes_small = st.sampled_from(["chr1"])
cs = st.one_of(chromosomes, chromosomes_small)

runlengths = data_frames(
    index=indexes(dtype=np.int64, min_size=1, unique=True),
    columns=[
        column("Runs", st.integers(min_value=1, max_value=int(1e7))),
        # must have a min/max on floats because R S4vectors translates too big ones into inf.
        # which is unequal to eg -1.79769e+308 so the tests fail
        column("Values", st.integers(min_value=-int(1e7), max_value=int(1e7)))
    ])

better_dfs_no_min = data_frames(
    index=indexes(dtype=np.int64, min_size=0, unique=True, elements=lengths),
    columns=[
        column("Chromosome", cs),
        column("Start", elements=lengths),
Ejemplo n.º 50
0
def test_one_of_label_is_distinct():
    a = st.integers()
    b = st.booleans()
    assert st.one_of(a, b).label != st.one_of(b, a).label
Ejemplo n.º 51
0
        assert 2 + T(1, 3, 0) == T(3, 5, 2)
        assert 2 * T(1, 3, 0) == T(2, 6, 0)
        assert 2 - T(1, 3, 0) == T(1, -1, 2)
        assert 3 / T(2, 3, 1) == T(1.5, 1, 3)
        assert 2 // T(2, 3, 1) == T(1, 0, 2)


# generated test cases
# using rationals to check math exactly instead of dealing with floating point errors

Rationals = fractions()
PositiveRationals = fractions(min_value=0)
PointStrategy = builds(Point, Rationals, Rationals)
Point3Strategy = builds(Point3, Rationals, Rationals, Rationals)
SizeStrategy = builds(Size, PositiveRationals, PositiveRationals)
ArithmeticTupleStrategy = one_of(PointStrategy, Point3Strategy, SizeStrategy)


@given(ArithmeticTupleStrategy)
def test_unary_operators(o):
    assert +o == o
    assert -o == tuple(-v for v in o)
    assert o == -(-o)
    assert abs(o) == tuple(abs(v) for v in o)


@given(ArithmeticTupleStrategy,
       one_of(integers(min_value=0, max_value=10), none()))
def test_unary_truncation_operators(o, ndigits):
    assume(all(math.isfinite(v)
               for v in o))  # cannot round/truncate/etc inf or nan
Ejemplo n.º 52
0
import hypothesis.strategies as hst
import numpy as np
import pytest
from hypothesis import given

from qcodes.utils.validators import ComplexNumbers
from qcodes.utils.types import numpy_complex


@given(complex_val=hst.complex_numbers())
def test_complex(complex_val):

    n = ComplexNumbers()
    assert str(n) == '<Complex Number>'
    n.validate(complex_val)
    n.validate(complex(complex_val))

    for complex_type in numpy_complex:
        n.validate(complex_type(complex_val))


@given(val=hst.one_of(hst.floats(), hst.integers(), hst.characters()))
def test_complex_raises(val):

    n = ComplexNumbers()

    with pytest.raises(TypeError, match=r"is not complex;"):
        n.validate(val)
Ejemplo n.º 53
0
custom_text = st.text(
    alphabet=st.characters(min_codepoint=0x41, max_codepoint=0x7E))

null_type = st.just(pa.null())
bool_type = st.just(pa.bool_())

binary_type = st.just(pa.binary())
string_type = st.just(pa.string())

signed_integer_types = st.sampled_from(
    [pa.int8(), pa.int16(), pa.int32(),
     pa.int64()])
unsigned_integer_types = st.sampled_from(
    [pa.uint8(), pa.uint16(),
     pa.uint32(), pa.uint64()])
integer_types = st.one_of(signed_integer_types, unsigned_integer_types)

floating_types = st.sampled_from([pa.float16(), pa.float32(), pa.float64()])
decimal_type = st.builds(pa.decimal128,
                         precision=st.integers(min_value=0, max_value=38),
                         scale=st.integers(min_value=0, max_value=38))
numeric_types = st.one_of(integer_types, floating_types, decimal_type)

date_types = st.sampled_from([pa.date32(), pa.date64()])
time_types = st.sampled_from(
    [pa.time32('s'),
     pa.time32('ms'),
     pa.time64('us'),
     pa.time64('ns')])
timestamp_types = st.sampled_from([
    pa.timestamp('s'),
Ejemplo n.º 54
0
def slice_index(
    draw,
    size,
    min_start=None,
    max_start=None,
    min_stop=None,
    max_stop=None,
    min_step=1,
    max_step=2,
    negative_step=True,
):
    """ Hypothesis search strategy: Generate a valid slice-index
    for an axis of a given size. Slices are chosen such that
    most slices will not be empty.

    Examples from this strategy shrink towards `slice(0, 0, 1)`. In the
    case that a negative step size is drawn, start and stop will be flipped
    so that it is less likely to have an empty slice

    Parameters
    ----------
    size : int
        Size of the axis for which the index is drawn
    min_start : int
    max_start : int
    min_stop : int
    max_stop : int
    min_step : int, optional (default=1)
    max_step : int
    negative_step : bool

    Notes
    -----
    `draw` is a parameter reserved by hypothesis, and should not be specified
    by the user.

    Returns
    -------
    hypothesis.searchstrategy.SearchStrategy[slice]
    """
    if not size:
        return slice(None)

    min_start = -size if min_start is None else min_start
    max_start = size - 1 if max_start is None else max_start
    _check_min_max(-math.inf, min_start, max_start, "start")

    min_stop = -size if min_stop is None else min_stop
    max_stop = -size if max_stop is None else max_stop
    _check_min_max(min_start, min_stop, max_stop, "stop")

    _check_min_max(0, min_step, max_step, "step")

    start = draw(st.one_of(st.integers(min_start, max_start - 1), st.none()))
    stop = draw(
        st.one_of(st.integers(start if start is not None else 0, size),
                  st.none()))

    step = draw(st.integers(min_step, max_step))

    if negative_step:
        neg_step = draw(st.booleans())

        if neg_step:
            step *= -1
    return slice(start, stop, step) if step > 0 else slice(stop, start, step)
Ejemplo n.º 55
0
    class ShuffleRoles(TrioAsyncioRuleBasedStateMachine):
        realm_role_strategy = st.one_of(st.just(x) for x in RealmRole)
        User = Bundle("user")

        async def start_backend(self):
            async def _backend_controlled_cb(started_cb):
                async with backend_factory(populated=False) as backend:
                    async with server_factory(backend.handle_client,
                                              backend_addr) as server:
                        await started_cb(backend=backend, server=server)

            return await self.get_root_nursery().start(call_with_control,
                                                       _backend_controlled_cb)

        @property
        def backend(self):
            return self.backend_controller.backend

        @initialize(target=User)
        async def init(self):
            await reset_testbed()
            self.backend_controller = await self.start_backend()

            # Create organization and first user
            self.backend_data_binder = backend_data_binder_factory(
                self.backend)
            await self.backend_data_binder.bind_organization(coolorg, alice)

            # Create realm
            self.realm_id = await realm_factory(self.backend, alice)
            self.current_roles = {alice.user_id: RealmRole.OWNER}
            self.certifs = [ANY]

            self.socks = {}
            return alice

        async def get_sock(self, device):
            try:
                return self.socks[device.user_id]
            except KeyError:
                pass

            async def _start_sock(device,
                                  *,
                                  task_status=trio.TASK_STATUS_IGNORED):
                async with backend_sock_factory(self.backend, device) as sock:
                    task_status.started(sock)
                    await trio.sleep_forever()

            sock = await self.get_root_nursery().start(_start_sock, device)
            self.socks[device.user_id] = sock
            return sock

        @rule(target=User, author=User, role=realm_role_strategy)
        async def give_role_to_new_user(self, author, role):
            # Create new user/device
            new_device = local_device_factory()
            await self.backend_data_binder.bind_device(new_device)
            self.current_roles[new_device.user_id] = None
            # Assign role
            author_sock = await self.get_sock(author)
            if await self._give_role(author_sock, author, new_device, role):
                return new_device
            else:
                return multiple()

        @rule(author=User, recipient=User, role=realm_role_strategy)
        async def change_role_for_existing_user(self, author, recipient, role):
            author_sock = await self.get_sock(author)
            await self._give_role(author_sock, author, recipient, role)

        async def _give_role(self, author_sock, author, recipient, role):
            author_sock = await self.get_sock(author)

            certif = RealmRoleCertificateContent(
                author=author.device_id,
                timestamp=pendulum_now(),
                realm_id=self.realm_id,
                user_id=recipient.user_id,
                role=role,
            ).dump_and_sign(author.signing_key)
            rep = await realm_update_roles(author_sock,
                                           certif,
                                           check_rep=False)
            if author.user_id == recipient.user_id:
                assert rep == {
                    "status": "invalid_data",
                    "reason": "Realm role certificate cannot be self-signed.",
                }

            else:
                owner_only = (RealmRole.OWNER, )
                owner_or_manager = (RealmRole.OWNER, RealmRole.MANAGER)
                existing_recipient_role = self.current_roles[recipient.user_id]
                if existing_recipient_role in owner_or_manager or role in owner_or_manager:
                    allowed_roles = owner_only
                else:
                    allowed_roles = owner_or_manager

                if self.current_roles[author.user_id] in allowed_roles:
                    # print(f"+ {author.user_id} -{role.value}-> {recipient.user_id}")
                    if existing_recipient_role != role:
                        assert rep == {"status": "ok"}
                        self.current_roles[recipient.user_id] = role
                        self.certifs.append(certif)
                    else:
                        assert rep == {"status": "already_granted"}
                else:
                    # print(f"- {author.user_id} -{role.value}-> {recipient.user_id}")
                    assert rep == {"status": "not_allowed"}

            return rep["status"] == "ok"

        @rule(author=User)
        async def get_role_certificates(self, author):
            sock = await self.get_sock(author)
            rep = await realm_get_role_certificates(sock, self.realm_id)
            if self.current_roles.get(author.user_id) is not None:
                assert rep["status"] == "ok"
                assert rep["certificates"] == self.certifs
            else:
                assert rep == {}

        @invariant()
        async def check_current_roles(self):
            try:
                backend = self.backend
            except AttributeError:
                return
            roles = await backend.realm.get_current_roles(
                alice.organization_id, self.realm_id)
            assert roles == {
                k: v
                for k, v in self.current_roles.items() if v is not None
            }
Ejemplo n.º 56
0
def valid_axes(
    ndim: int,
    pos_only: bool = False,
    single_axis_only: bool = False,
    permit_none: bool = True,
    permit_int: bool = True,
    min_dim: int = 0,
    max_dim: Optional[int] = None,
) -> st.SearchStrategy[Union[None, int, Tuple[int, ...]]]:
    """ Hypothesis search strategy: Given array dimensionality, generate valid
    `axis` arguments (including `None`) for numpy's sequential functions.

    Examples from this strategy shrink towards an empty tuple of axes.
    If `single_axis_only=True`, then it shrinks towards 0.

    Parameters
    ----------
    ndim : int
        The dimensionality of the array.

    pos_only : bool, optional (default=False)
        If True, the returned value(s) will be positive.

    single_axis_only : bool, optional (default=False)
        If True, a single integer axis or `None` (assuming `permit_none=True`)
        will be returned.

    permit_none : bool, optional (default=True)
        If True, `None` may be returned instead of a tuple of all of the
        available axes.

    permit_int: bool, optional (default=True)
        If True, the returned value may be an integer

    min_dim : int, optional (default=0)
        The smallest number of entries permitted in the returned tuple of axes

    max_dim : Optional[int]
        The largest number of entries permitted in the returned tuple of axes.
        The defaults is ``ndim``.

    Returns
    -------
    st.SearchStrategy[Union[None, int, Tuple[int, ...]]]

    Examples
    --------
    >>> valid_axes(4).example()
    (0, 1)
    """
    if isinstance(ndim, (tuple, list)):
        ndim = len(ndim)

    single_axis_strat = st.integers(-ndim, ndim - 1) if ndim else st.just(0)

    strats = []

    if permit_none:
        strats.append(st.none())

    if permit_int and min_dim <= 1 and (max_dim is None or 1 <= max_dim):
        strats.append(single_axis_strat)

    if not single_axis_only:
        strats.append(
            hnp.valid_tuple_axes(ndim, min_size=min_dim, max_size=max_dim))

    strat = st.one_of(*strats)
    if pos_only:
        strat = strat.map(lambda x: x if x is None else _to_positive(x, ndim))
    return strat
Ejemplo n.º 57
0
from hypothesis import given
import hypothesis.strategies as st
from harmonious.music import LayerInterval, note_midi, normalize_layers, tone_to_voicing


# notes an octave apart should normalize to the same value when mod by 12.
@given(st.text(alphabet='ABCDEFG', min_size=1, max_size=1),
       st.one_of(st.just(''),
                 st.text(alphabet=('b', '#'), min_size=1, max_size=1)),
       st.integers(min_value=0, max_value=8))
def test_note_midi_octaves_correct(letter, accidental, octave):
    assert note_midi(letter + accidental,
                     -1) == note_midi(letter + accidental, octave) % 12


#rotating the input should not change the output
@given(st.text(alphabet=[x.name for x in LayerInterval]),
       st.integers(min_value=1))
def test_normalize_layers_rotation(layers, rotation):
    assert normalize_layers(layers) == normalize_layers(layers[rotation:] +
                                                        layers[:rotation])


# without a 5, this function degenerates to sorting by value
@given(st.text(alphabet=[x.name for x in LayerInterval if x < 6 and x > 8]))
def test_normalize_layers_without_5_is_sorted(layers):
    assert normalize_layers(layers) == ''.join(sorted(layers))


# the three 5s should normalize to a consistent order
@given(st.text(alphabet='5o+'))
Ejemplo n.º 58
0
def from_typing_type(thing):
    # We start with special-case support for Union and Tuple - the latter
    # isn't actually a generic type. Then we handle Literal since it doesn't
    # support `isinstance`.
    #
    # We then explicitly error on non-Generic types, which don't carry enough
    # information to sensibly resolve to strategies at runtime.
    # Finally, we run a variation of the subclass lookup in `st.from_type`
    # among generic types in the lookup.
    if getattr(thing, "__origin__", None) == tuple or isinstance(
            thing, getattr(typing, "TupleMeta", ())):
        elem_types = getattr(thing, "__tuple_params__", None) or ()
        elem_types += getattr(thing, "__args__", None) or ()
        if (getattr(thing, "__tuple_use_ellipsis__", False)
                or len(elem_types) == 2 and elem_types[-1] is Ellipsis):
            return st.lists(st.from_type(elem_types[0])).map(tuple)
        elif len(elem_types) == 1 and elem_types[0] == ():
            return st.tuples()  # Empty tuple; see issue #1583
        return st.tuples(*map(st.from_type, elem_types))
    if hasattr(typing, "Final") and getattr(thing, "__origin__",
                                            None) == typing.Final:
        return st.one_of([st.from_type(t) for t in thing.__args__])
    if is_typing_literal(thing):
        args_dfs_stack = list(thing.__args__)
        literals = []
        while args_dfs_stack:
            arg = args_dfs_stack.pop()
            if is_typing_literal(arg):
                args_dfs_stack.extend(reversed(arg.__args__))
            else:
                literals.append(arg)
        return st.sampled_from(literals)
    # Now, confirm that we're dealing with a generic type as we expected
    if sys.version_info[:2] < (3, 9) and not isinstance(
            thing, typing_root_type):  # pragma: no cover
        raise ResolutionFailed(f"Cannot resolve {thing} to a strategy")

    # Some "generic" classes are not generic *in* anything - for example both
    # Hashable and Sized have `__args__ == ()` on Python 3.7 or later.
    # (In 3.6 they're just aliases for the collections.abc classes)
    origin = getattr(thing, "__origin__", thing)
    if (typing.Hashable is not collections.abc.Hashable
            and origin in vars(collections.abc).values()
            and len(getattr(thing, "__args__", None) or []) == 0):
        return st.from_type(origin)

    # Parametrised generic types have their __origin__ attribute set to the
    # un-parametrised version, which we need to use in the subclass checks.
    # e.g.:     typing.List[int].__origin__ == typing.List
    mapping = {
        k: v
        for k, v in _global_type_lookup.items()
        if is_generic_type(k) and try_issubclass(k, thing)
    }
    if typing.Dict in mapping or typing.Set in mapping:
        # ItemsView can cause test_lookup.py::test_specialised_collection_types
        # to fail, due to weird isinstance behaviour around the elements.
        mapping.pop(typing.ItemsView, None)
        if sys.version_info[:2] == (3, 6):  # pragma: no cover
            # `isinstance(dict().values(), Container) is False` on py36 only -_-
            mapping.pop(typing.ValuesView, None)
    if typing.Deque in mapping and len(mapping) > 1:
        # Resolving generic sequences to include a deque is more trouble for e.g.
        # the ghostwriter than it's worth, via undefined names in the repr.
        mapping.pop(typing.Deque)
    if len(mapping) > 1:
        # issubclass treats bytestring as a kind of sequence, which it is,
        # but treating it as such breaks everything else when it is presumed
        # to be a generic sequence or container that could hold any item.
        # Except for sequences of integers, or unions which include integer!
        # See https://github.com/HypothesisWorks/hypothesis/issues/2257
        #
        # This block drops ByteString from the types that can be generated
        # if there is more than one allowed type, and the element type is
        # not either `int` or a Union with `int` as one of its elements.
        elem_type = (getattr(thing, "__args__", None) or ["not int"])[0]
        if getattr(elem_type, "__origin__", None) is typing.Union:
            union_elems = elem_type.__args__
        else:
            union_elems = ()
        if not any(
                isinstance(T, type) and issubclass(int, T)
                for T in list(union_elems) + [elem_type]):
            mapping.pop(typing.ByteString, None)
    strategies = [
        v if isinstance(v, st.SearchStrategy) else v(thing)
        for k, v in mapping.items() if sum(
            try_issubclass(k, T) for T in mapping) == 1
    ]
    empty = ", ".join(repr(s) for s in strategies if s.is_empty)
    if empty or not strategies:
        raise ResolutionFailed(
            "Could not resolve %s to a strategy; consider using "
            "register_type_strategy" % (empty or thing, ))
    return st.one_of(strategies)
Ejemplo n.º 59
0
from gc import collect as gc
import traceback
import sys
from time import time
from stricttuple import stricttuple
from collections import deque

__all__ = 'battle_tested', 'fuzz', 'disable_traceback', 'enable_traceback', 'garbage', 'crash_map', 'success_map', 'results', 'stats', 'print_stats'

garbage = (st.binary(), st.booleans(), st.characters(), st.complex_numbers(),
           st.decimals(), st.floats(), st.fractions(), st.integers(),
           st.none(), st.random_module(), st.randoms(), st.text(), st.tuples(),
           st.uuids(), st.dictionaries(keys=st.text(), values=st.text()))
garbage += (
    # iterables
    st.lists(elements=st.one_of(*garbage)),
    st.iterables(elements=st.one_of(*garbage)),
    st.dictionaries(keys=st.text(), values=st.one_of(*garbage)))
garbage = st.one_of(*garbage)


def is_py3():
    return sys.version_info >= (3, 0)


class UniqueCrashContainer(tuple):
    ''' a pretty printable container for crashes '''
    def __repr__(self):
        table = PrettyTable(
            ('exception type', 'arg types', 'location', 'crash message'),
            sortby='exception type')
Ejemplo n.º 60
0
def autocorr(x, t=1):
    """Computes autocorrelation of the given array with a lag of t"""
    return np.round(np.corrcoef(np.array([x[:-t], x[t:]]))[0, 1], 5)


# QUALITY CONTROL AND PREPROCESSING #


@settings(deadline=None)
@given(v=st.one_of(
    st.just("yes"),
    st.just("true"),
    st.just("t"),
    st.just("y"),
    st.just("1"),
    st.just("no"),
    st.just("false"),
    st.just("f"),
    st.just("n"),
    st.just("0"),
))
def test_str2bool(v):
    assert isinstance(deepof.utils.str2bool(v), bool)


@settings(deadline=None)
@given(
    mult=st.integers(min_value=1, max_value=10),
    dframe=data_frames(
        index=range_indexes(min_size=1),
        columns=columns(["X", "y", "likelihood"], dtype=float),