Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# This strategy tests interactions with `filter()`. It generates the even
# integers {0, 2, 4, 6} in equal measures.
one_of_nested_strategy_with_filter = one_of(
just(0),
just(1),
one_of(
just(2),
just(3),
one_of(
just(4),
just(5),
one_of(
just(6),
just(7),
)
)
)
).filter(lambda x: x % 2 == 0)
for i in range(4):
exec('''test_one_of_flattens_filter_branches_%d = define_test(
one_of_nested_strategy_with_filter, lambda x: x == 2 * %d
)''' % (i, i))
string_create_commands = commands(st.just('set'), keys, values)
string_commands = (
commands(st.just('append'), keys, values)
| commands(st.just('bitcount'), keys)
| commands(st.just('bitcount'), keys, values, values)
| commands(st.sampled_from(['incr', 'decr']), keys)
| commands(st.sampled_from(['incrby', 'decrby']), keys, values)
# Disabled for now because Python can't exactly model the long doubles.
# TODO: make a more targeted test that checks the basics.
# TODO: check how it gets stringified, without relying on hypothesis
# to get generate a get call before it gets overwritten.
# | commands(st.just('incrbyfloat'), keys, st.floats(width=32))
| commands(st.just('get'), keys)
| commands(st.just('getbit'), keys, counts)
| commands(st.just('setbit'), keys, counts,
st.integers(min_value=0, max_value=1) | st.integers())
| commands(st.sampled_from(['substr', 'getrange']), keys, counts, counts)
| commands(st.just('getset'), keys, values)
| commands(st.just('mget'), st.lists(keys))
| commands(st.sampled_from(['mset', 'msetnx']), st.lists(st.tuples(keys, values)))
| commands(st.just('set'), keys, values,
st.none() | st.just('nx'), st.none() | st.just('xx'))
| commands(st.just('setex'), keys, expires_seconds, values)
| commands(st.just('psetex'), keys, expires_ms, values)
| commands(st.just('setnx'), keys, values)
| commands(st.just('setrange'), keys, counts, values)
| commands(st.just('strlen'), keys)
)
# TODO: add a test for hincrbyfloat. See incrbyfloat for why this is
# problematic.
def frozen_sets_of_primitives(draw):
"""A strategy that generates frozen sets of primitives."""
prim_strat, t = draw(primitive_strategies)
set_t = draw(st.just(Set) | st.just(Set[t]))
return frozenset(draw(st.sets(prim_strat))), set_t
domain = draw(domains)
return pe.Var(domain=domain)
constants = lambda: st.floats(min_value=-1e19, max_value=1e19, allow_nan=False, allow_infinity=False)
leaves = lambda: st.one_of(variables(), constants())
unary_function_types = st.one_of(
st.just('cos'),
st.just('sin'),
st.just('tan'),
st.just('acos'),
st.just('asin'),
st.just('atan'),
st.just('exp'),
st.just('log'),
st.just('abs'))
@st.composite
def unary_functions(draw, child):
ch = draw(child)
name = draw(unary_function_types)
if name == 'abs':
return abs(ch)
return getattr(pe, name)(ch)
@st.composite
def bool_strat():
""" einsum's optimize=True option has bugs prior to version 1.14.5
(caught by these very unit tests!), thus we only test `optimize=True`
for more recent versions."""
return st.booleans() if np.__version__ >= "1.14.5" else st.just(False)
def test_just_shapes_tuple_of_arrays(shapes, dtype, unique, data):
elements = from_dtype(np.dtype(dtype))
# test again, but this time pass in strategy to make sure it can handle it
S = gu._tuple_of_arrays(just(shapes), just(dtype), elements=elements, unique=just(unique))
X = data.draw(S)
validate_elements(X, dtype=dtype, unique=unique)
assert len(shapes) == len(X)
for spec, drawn in zip(shapes, X):
assert tuple(spec) == np.shape(drawn)
draw: Callable[[SearchStrategy], Any], charge_type: ChargeType, case: CaseSummary
) -> SearchStrategy[Charge]:
if charge_type == DismissedCharge():
disposition_status = one_of(
just(DispositionStatus.DISMISSED), just(DispositionStatus.NO_COMPLAINT), just(DispositionStatus.DIVERTED)
)
else:
disposition_status = one_of(just(DispositionStatus.CONVICTED), just(DispositionStatus.UNRECOGNIZED))
disposition_date = just(DateWithFuture(date=draw(dates(max_value=date(9000, 12, 31)))))
disposition = builds(Disposition, status=disposition_status, date=disposition_date)
arrest_date = just(DateWithFuture(date=draw(dates(max_value=date(9000, 12, 31)))))
probation_revoked_date = one_of(none(), just(DateWithFuture(date=draw(dates(max_value=date(9000, 12, 31))))))
return draw(
builds(
Charge,
charge_type=just(charge_type),
case_number=just(case.case_number),
disposition=disposition,
date=arrest_date,
probation_revoked=probation_revoked_date,
)
def _build_charge_strategy(
draw: Callable[[SearchStrategy], Any], charge_type: ChargeType, case: CaseSummary
) -> SearchStrategy[Charge]:
if charge_type == DismissedCharge():
disposition_status = one_of(
just(DispositionStatus.DISMISSED), just(DispositionStatus.NO_COMPLAINT), just(DispositionStatus.DIVERTED)
)
else:
disposition_status = one_of(just(DispositionStatus.CONVICTED), just(DispositionStatus.UNRECOGNIZED))
disposition_date = just(DateWithFuture(date=draw(dates(max_value=date(9000, 12, 31)))))
disposition = builds(Disposition, status=disposition_status, date=disposition_date)
arrest_date = just(DateWithFuture(date=draw(dates(max_value=date(9000, 12, 31)))))
probation_revoked_date = one_of(none(), just(DateWithFuture(date=draw(dates(max_value=date(9000, 12, 31))))))
return draw(
builds(
Charge,
charge_type=just(charge_type),
case_number=just(case.case_number),
disposition=disposition,
date=arrest_date,
probation_revoked=probation_revoked_date,
)