Python hypothesis.strategies.lists() Examples

The following are 30 code examples of hypothesis.strategies.lists(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module hypothesis.strategies , or try the search function .
Example #1
Source File: tough-bonus-problems.py    From escape-from-automanual-testing with GNU Affero General Public License v3.0 6 votes vote down vote up
def from_schema(schema):
    """Returns a strategy for objects that match the given schema."""
    check_schema(schema)
    # TODO: actually handle constraints on number/string/array schemas
    return dict(
        null=st.none(),
        bool=st.booleans(),
        number=st.floats(allow_nan=False),
        string=st.text(),
        array=st.lists(st.nothing()),
    )[schema["type"]]


# `@st.composite` is one way to write this - another would be to define a
# bare function, and `return st.one_of(st.none(), st.booleans(), ...)` so
# each strategy can be defined individually.  Use whichever seems more
# natural to you - the important thing in tests is usually readability! 
Example #2
Source File: strategies.py    From brownie with MIT License 6 votes vote down vote up
def _array_strategy(
    abi_type: BasicType,
    min_length: ArrayLengthType = 1,
    max_length: ArrayLengthType = 8,
    unique: bool = False,
    **kwargs: Any,
) -> SearchStrategy:
    if abi_type.arrlist[-1]:
        min_len = max_len = abi_type.arrlist[-1][0]
    else:
        dynamic_len = len([i for i in abi_type.arrlist if not i])
        min_len = _get_array_length("min_length", min_length, dynamic_len)
        max_len = _get_array_length("max_length", max_length, dynamic_len)
    if abi_type.item_type.is_array:
        kwargs.update(min_length=min_length, max_length=max_length, unique=unique)
    base_strategy = strategy(abi_type.item_type.to_type_str(), **kwargs)
    strat = st.lists(base_strategy, min_size=min_len, max_size=max_len, unique=unique)
    # swap 'size' for 'length' in the repr
    repr_ = "length".join(strat.__repr__().rsplit("size", maxsplit=2))
    strat._LazyStrategy__representation = repr_  # type: ignore
    return strat 
Example #3
Source File: test_matching.py    From xapi-profiles with Apache License 2.0 6 votes vote down vote up
def pattern_to_statements(pattern):
    if isinstance(pattern, template):
        return lists(just(pattern), min_size=1, max_size=1)
    rule, value = pattern
    if rule == 'sequence':
        return tuples(*map(pattern_to_statements, value)).map(unpack_list).map(list)
    elif rule == 'alternates':
        return one_of(*map(pattern_to_statements, value))
    elif rule == 'zeroOrMore':
        return lists(pattern_to_statements(value)).map(unpack_list).map(list)
    elif rule == 'oneOrMore':
        return lists(pattern_to_statements(value), min_size=1).map(unpack_list).map(list)
    elif rule == 'optional':
        return lists(pattern_to_statements(value), min_size=0, max_size=1).map(unpack_list).map(list)
    else:
        raise Exception("impossible!", rule)



# this replicates the current scorm pattern, a realistic example of medium
# complexity. Note it has repeated elements, just not in ambiguous ways. 
Example #4
Source File: concat_thread_test.py    From pypeln with MIT License 6 votes vote down vote up
def test_concat_basic(nums: tp.List[int]):

    nums_py = list(map(lambda x: x + 1, nums))
    nums_py1 = list(map(lambda x: x ** 2, nums_py))
    nums_py2 = list(map(lambda x: -x, nums_py))
    nums_py = nums_py1 + nums_py2

    nums_pl = pl.thread.map(lambda x: x + 1, nums)
    nums_pl1 = pl.thread.map(lambda x: x ** 2, nums_pl)
    nums_pl2 = pl.thread.map(lambda x: -x, nums_pl)
    nums_pl = pl.thread.concat([nums_pl1, nums_pl2])

    assert sorted(nums_pl) == sorted(nums_py)


# @hp.given(nums=st.lists(st.integers()))
# @hp.settings(max_examples=MAX_EXAMPLES) 
Example #5
Source File: concat_task_test.py    From pypeln with MIT License 6 votes vote down vote up
def test_concat_basic_2(nums: tp.List[int]):

    nums_py = list(map(lambda x: x + 1, nums))
    nums_py1 = list(map(lambda x: x ** 2, nums_py))
    nums_py2 = list(map(lambda x: -x, nums_py))
    nums_py = nums_py1 + nums_py2

    nums_pl = pl.task.map(lambda x: x + 1, nums)
    nums_pl1 = pl.task.map(lambda x: x ** 2, nums_pl)
    nums_pl2 = pl.task.map(lambda x: -x, nums_pl)
    nums_pl = await pl.task.concat([nums_pl1, nums_pl2])

    assert sorted(nums_pl) == sorted(nums_py)

# @hp.given(nums=st.lists(st.integers()))
# @hp.settings(max_examples=MAX_EXAMPLES) 
Example #6
Source File: concat_process_test.py    From pypeln with MIT License 6 votes vote down vote up
def test_concat_basic(nums: tp.List[int]):

    nums_py = list(map(lambda x: x + 1, nums))
    nums_py1 = list(map(lambda x: x ** 2, nums_py))
    nums_py2 = list(map(lambda x: -x, nums_py))
    nums_py = nums_py1 + nums_py2

    nums_pl = pl.process.map(lambda x: x + 1, nums)
    nums_pl1 = pl.process.map(lambda x: x ** 2, nums_pl)
    nums_pl2 = pl.process.map(lambda x: -x, nums_pl)
    nums_pl = pl.process.concat([nums_pl1, nums_pl2])

    assert sorted(nums_pl) == sorted(nums_py)


# @hp.given(nums=st.lists(st.integers()))
# @hp.settings(max_examples=MAX_EXAMPLES) 
Example #7
Source File: test_parse.py    From eliot with Apache License 2.0 6 votes vote down vote up
def action_structures(draw):
    """
    A Hypothesis strategy that creates a tree of L{ActionStructure} and
    L{unicode}.
    """
    tree = draw(st.recursive(labels, st.lists, max_leaves=20))

    def to_structure(tree_or_message):
        if isinstance(tree_or_message, list):
            return ActionStructure(
                type=draw(labels),
                failed=draw(st.booleans()),
                children=[to_structure(o) for o in tree_or_message],
            )
        else:
            return tree_or_message

    return to_structure(tree) 
Example #8
Source File: fuzzer.py    From fuzz-lightyear with Apache License 2.0 6 votes vote down vote up
def _fuzz_array(
    parameter: Dict[str, Any],
    required: bool = False,
) -> SearchStrategy:
    item = parameter['items']
    required = parameter.get('required', required)

    # TODO: Handle `oneOf`
    strategy = st.lists(
        elements=_fuzz_parameter(item, required=required),
        min_size=parameter.get(
            'minItems',
            0 if not required else 1,
        ),
        max_size=parameter.get('maxItems', None),
    )
    if not required:
        return st.one_of(st.none(), strategy)

    return strategy 
Example #9
Source File: test_browser.py    From crocoite with MIT License 6 votes vote down vote up
def fixedDicts (fixed, dynamic):
    return st.builds (lambda x, y: x.update (y), st.fixed_dictionaries (fixed), st.lists (dynamic)) 
Example #10
Source File: custom_hypothesis_support.py    From pyta with GNU General Public License v3.0 6 votes vote down vote up
def arguments_node(draw, annotated=False):
    n = draw(hs.integers(min_value=1, max_value=5))
    args = draw(hs.lists(name_node(None), min_size=n, max_size=n))
    if annotated:
        annotations = draw(hs.lists(name_node(annotation), min_size=n, max_size=n))
    else:
        annotations = None
    node = astroid.Arguments()
    node.postinit(
        args,
        None,
        None,
        None,
        annotations
    )
    return node 
Example #11
Source File: __init__.py    From wellpathpy with GNU Lesser General Public License v3.0 6 votes vote down vote up
def same_len_lists(draw, min_value = None, max_value = None):
    """Draw random arrays of equal lengths

    One precondition of the list version of spherical() is that its inputs must
    be of equal length.
    """
    n = draw(integers(min_value = 0, max_value = 50))
    fixlen = lists(
        floats(
            min_value = min_value,
            max_value = max_value,
            allow_nan = False,
            allow_infinity = False
        ),
        min_size = n,
        max_size = n,
    )
    fixnp = fixlen.map(np.array)
    return (draw(fixnp), draw(fixnp), draw(fixnp)) 
Example #12
Source File: testtools.py    From flocker with Apache License 2.0 6 votes vote down vote up
def node_uuid_pool_strategy(draw, min_number_of_nodes=1):
    """
    A strategy to create a pool of node uuids.

    :param min_number_of_nodes: The minimum number of nodes to create.

    :returns: A strategy to create an iterable of node uuids.
    """
    max_number_of_nodes = max(min_number_of_nodes, 10)
    return draw(
        st.lists(
            uuids(),
            min_size=min_number_of_nodes,
            max_size=max_number_of_nodes
        )
    ) 
Example #13
Source File: test_cli.py    From habitipy with MIT License 5 votes vote down vote up
def test_data(draw):
    can_overlap = draw(booleans())
    all_tasks = draw(task_lists)
    if all_tasks:
        i = draw(integers(min_value=0,max_value=len(all_tasks)))
        user_tasks = all_tasks[:i]
        more_tasks = all_tasks[i:]
    else:
        user_tasks = []
        more_tasks = []
    index_lists = lists(
        one_of(
            index_id_alias(len(all_tasks)),
            integer_range(0, len(all_tasks) - 1)),
        min_size=1)
    arguments = draw(lists(index_lists, min_size=1))
    arguments_strings = []
    task_ids = []
    for indexes in arguments:
        arg = ''
        for index in indexes:
            comma = ',' if arg else ''
            if isinstance(index, tuple):
                task_ids.extend(map(lambda x: all_tasks[x]['_id'], range(index[0],index[1]+1)))
                arg += '{comma}{0}-{1}'.format(index[0] + 1, index[1] + 1, comma=comma)
            elif isinstance(index, dict):
                task_ids.append(all_tasks[index['i']]['_id'])
                if index['type'] == 'index':
                    arg += '{comma}{i}'.format(i=index['i'] + 1, comma=comma)
                elif index['type'] == 'id':
                    arg += '{comma}{i}'.format(i=all_tasks[index['i']]['_id'], comma=comma)
                elif index['type'] == 'alias':
                    arg += '{comma}{i}'.format(i=all_tasks[index['i']]['alias'], comma=comma)
        arguments_strings.append(arg)
    if not can_overlap:
        task_ids = list(set(task_ids))
    return (can_overlap, user_tasks, more_tasks, all_tasks, arguments_strings, task_ids, arguments) 
Example #14
Source File: graph_test.py    From pyaptly with GNU Affero General Public License v3.0 5 votes vote down vote up
def provide_require_st(draw, filter_=True):  # pragma: no cover
    commands = draw(range_intagers_st)
    provides = draw(
        st.lists(
            st.lists(range_intagers_st, max_size=10),
            min_size = commands,
            max_size = commands
        ),
    )
    is_func = draw(
        st.lists(
            st.booleans(),
            min_size = commands,
            max_size = commands
        )
    )
    provides_set = set()
    for command in provides:
        provides_set.update(command)
    requires = []
    if provides_set:
        for command in provides:
            if command:
                max_prov = max(command)
            else:
                max_prov = 0
            if filter_:
                provides_filter = [x for x in provides_set if x > max_prov]
            else:
                provides_filter = provides_set
            if provides_filter:
                sample = st.sampled_from(provides_filter)
                requires.append(draw(st.lists(sample, max_size=10)))
            else:
                requires.append([])
    else:
        requires = [[]] * commands
    return (provides, requires, is_func) 
Example #15
Source File: test_MarketAnalysis.py    From MikaLendingBot with MIT License 5 votes vote down vote up
def random_dates(min_len, max_len):
    max_year = datetime.datetime.now().year
    return lists(datetimes(min_year=2016, max_year=max_year), min_size=min_len, max_size=max_len).map(sorted).example() 
Example #16
Source File: test_MarketAnalysis.py    From MikaLendingBot with MIT License 5 votes vote down vote up
def random_rates():
    return lists(floats(min_value=0.00001, max_value=100, allow_nan=False, allow_infinity=False), min_size=0, max_size=100).example() 
Example #17
Source File: chunk_strategies.py    From py-ssz with MIT License 5 votes vote down vote up
def elements_st(draw, size=None, **kwargs):
    if size is None:
        size = draw(element_size_st())
    return draw(st.lists(element_st(size), **kwargs)) 
Example #18
Source File: test_explode.py    From KiField with MIT License 5 votes vote down vote up
def random_references(draw):
    '''generates random sorted lists of references with the same prefix like ['IASDHAH1', 'IASDHAH1569', ...]'''
    prefix = st.just(draw(random_prefix()))
    parts = draw(st.lists(random_reference(prefix = prefix)))
    parts.sort()
    return list(map(toRef, parts)) 
Example #19
Source File: test_explode.py    From KiField with MIT License 5 votes vote down vote up
def totally_random_references(draw):
    '''generates random sorted lists of references like ['IASDHAH1', 'ZKJDJAD1569', ...]'''
    parts = draw(st.lists(random_reference()))
    parts.sort()
    return list(map(toRef, parts)) 
Example #20
Source File: test_explode.py    From KiField with MIT License 5 votes vote down vote up
def random_prefix(draw):
    #limited to unicode letters, see
    #https://en.wikipedia.org/wiki/Unicode_character_property#General_Category
    categories = ['Ll', 'Lt', 'Lm', 'Lo']
    characters = st.lists(st.characters(whitelist_categories=categories), min_size = 1)
    prefix = st.text(alphabet = draw(characters), min_size = 1)
    return draw(prefix) 
Example #21
Source File: chunk_strategies.py    From py-ssz with MIT License 5 votes vote down vote up
def chunk_updates_st(draw, num_original_elements, element_size):
    indices = draw(
        st.lists(
            st.integers(min_value=0, max_value=num_original_elements - 1), unique=True
        )
    )
    elements = draw(
        st.lists(element_st(element_size), min_size=len(indices), max_size=len(indices))
    )
    return dict(zip(indices, elements)) 
Example #22
Source File: hashable_strategies.py    From py-ssz with MIT License 5 votes vote down vote up
def bitlist_value_st(max_size=None):
    return st.builds(tuple, st.lists(st.booleans(), max_size=max_size)) 
Example #23
Source File: hashable_strategies.py    From py-ssz with MIT License 5 votes vote down vote up
def bitvector_value_st(size=None):
    min_size = size or 1
    max_size = size
    return st.builds(
        tuple, st.lists(st.booleans(), min_size=min_size, max_size=max_size)
    ) 
Example #24
Source File: testtools.py    From flocker with Apache License 2.0 5 votes vote down vote up
def node_strategy(
        draw,
        min_number_of_applications=0,
        stateful_applications=False,
        uuid=st.uuids(),
        applications=application_strategy()
):
    """
    A hypothesis strategy to generate a ``Node``

    :param uuid: The strategy to use to generate the Node's uuid.

    :param applications: The strategy to use to generate the applications on
        the Node.
    """
    applications = {
        a.name: a for a in
        draw(
            st.lists(
                application_strategy(stateful=stateful_applications),
                min_size=min_number_of_applications,
                average_size=2,
                max_size=5
            )
        )
    }
    return Node(
        uuid=draw(uuid),
        applications=applications,
        manifestations={
            a.volume.manifestation.dataset_id: a.volume.manifestation
            for a in applications.values()
            if a.volume is not None
        }
    ) 
Example #25
Source File: ChecksumCL_test.py    From pymtl3 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def cksum_func( s, words ):
    return checksum_cl( words )

  # ''' TUTORIAL TASK ''''''''''''''''''''''''''''''''''''''''''''''''''''
  # Use Hypothesis to test Checksum CL
  # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\/
  #; Use Hypothesis to verify that ChecksumCL has the same behavior as
  #; ChecksumFL. Simply uncomment the following test_hypothesis method
  #; and rerun pytest. Make sure that you fix the indentation so that
  #; this new test_hypothesis method is correctly indented with respect
  #; to the class ChecksumCL_Tests
  #;
  #;   @hypothesis.settings( deadline=None )
  #;   @hypothesis.given(
  #;     words=st.lists( pm_st.bits(16), min_size=8, max_size=8 )
  #;   )
  #;   def test_hypothesis( s, words ):
  #;     print( [ int(x) for x in words ] )
  #;     assert s.cksum_func( words ) == checksum( words )
  #;
  #; This new test uses Hypothesis to generate random inputs, then uses
  #; the checksum_cl to run a little simulation and compares the output to
  #; the checksum function from ChecksumFL.
  #;
  #; To really see Hypothesis in action, go back to ChecksumCL and
  #; corrupt one word of the input by forcing it to always be zero. For
  #; example, change the update block in the CL implementation to be
  #; something like this:
  #;
  #;   @update
  #;   def up_checksum_cl():
  #;     if s.pipe.enq.rdy() and s.in_q.deq.rdy():
  #;       bits = s.in_q.deq()
  #;       words = b128_to_words( bits )
  #;       words[5] = b16(0) # <--- INJECT A BUG!
  #;       result = checksum( words )
  #;       s.pipe.enq( result ) !\vspace{0.07in}!
  #;     if s.send.rdy() and s.pipe.deq.rdy():
  #;       s.send( s.pipe.deq() ) 
Example #26
Source File: testutils.py    From strax with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sorted_bounds(disjoint=False,
                  max_value=50,
                  max_len=10,
                  remove_duplicates=False):
    if disjoint:
        # Since we accumulate later:
        max_value /= max_len

    s = strategies.lists(strategies.integers(min_value=0,
                                             max_value=max_value),
                         min_size=0, max_size=20)
    if disjoint:
        s = s.map(accumulate).map(list)

    # Select only cases with even-length lists
    s = s.filter(lambda x: len(x) % 2 == 0)

    # Convert to list of 2-tuples
    s = s.map(lambda x: [tuple(q)
                         for q in iterutils.chunked(sorted(x), size=2)])

    # Remove cases with zero-length intervals
    s = s.filter(lambda x: all([a[0] != a[1] for a in x]))

    if remove_duplicates:
        # (this will always succeed if disjoint=True)
        s = s.filter(lambda x: x == list(set(x)))

    # Sort intervals and result
    return s.map(sorted)


##
# Fake intervals
##

# TODO: isn't this duplicated with bounds_to_records?? 
Example #27
Source File: strategies-and-tactics.py    From escape-from-automanual-testing with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_map_odd_numbers(x):
    assert x[-1] in "13579"


# Takeaway
# --------
# `.map` permits us to extend Hypothesis' core strategies in powerful
# ways. See that it can be used to affect the individual values being
# produced by a strategy (e.g. mapping integers to even-valued
# integers), as well as to cast the values to a different type (e.g.
# mapping an integer to a string.
#
# If it seems like a data-type is missing from Hypothesis'
# strategies, then it is likely that a simple application of `.map`
# will suffice. E.g. suppose you want a strategy that generate deques,
# then
#     `deque_strat = st.lists(...).map(deque)`
# will serve nicely - we don't even need a lambda!


##############################################################################
# Defining recursive data.

# There are a couple of ways to define recursive data with Hypothesis,
# leaning on the fact that strategies are lazily instantiated.
#
# `st.recursive` takes a base strategy, and a function that takes a strategy
# and returns an extended strategy.  All good if we want that structure!
# If you want mutual recursion though, or have a complicated kind of data
# (or just limited time in a tutorial), `st.deferred` is the way to go.
#
# The `Record` exercise in pbt-101.py defined JSON using `st.recursive`,
# if you want to compare them, and has some extension problems that you
# could write as tests here instead.


# JSON values are defined as one of null, false, true, a finite number,
# a string, an array of json values, or a dict of string to json values. 
Example #28
Source File: test-the-untestable.py    From escape-from-automanual-testing with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_mean_properties(data, type_, strat):
    strat = strat or st.from_type(type_)
    values = data.draw(st.lists(strat, min_size=1))
    result = mean(values)  # already testing no exceptions!
    # TODO: property assertions, e.g. bounds on result, etc.
    if type_ is Fraction:
        assert min(values) <= result <= max(values)
    # What constraints make sense for an integer mean?

    # TODO: metamorphic test assertions.  For example, how should result
    # change if you add the mean to values?  a number above or below result?
    # Remove some elements from values? 
Example #29
Source File: hashable_strategies.py    From py-ssz with MIT License 5 votes vote down vote up
def vector_value_st(draw, elements, *, size=None):
    if size is None:
        size = draw(st.integers(0, 10))
    return st.builds(tuple, st.lists(elements, min_size=size, max_size=size)) 
Example #30
Source File: strategies.py    From txacme with MIT License 5 votes vote down vote up
def dns_names():
    """
    Strategy for generating limited charset DNS names.
    """
    return (
        s.lists(dns_labels(), min_size=1, max_size=10)
        .map(u'.'.join))