def test_reports_differently_for_single_shrink(): with capture_verbosity(Verbosity.verbose) as o: @fails @given(basic(SillyStrategy), settings=Settings(database=None)) def test_foo(x): assert False test_foo() assert 'shrunk example once' in o.getvalue()
def test_two_incompatible_unreified_templates(): r = Random(1) strat = basic(Bitfields).flatmap(lambda x: integers(0, x)) x = some_template(strat, r) y = some_template(strat, r) assert x.source_template != y.source_template assert not strat.strictly_simpler(x, y) assert not strat.strictly_simpler(y, x)
def test_reports_differently_for_single_shrink(): with capture_verbosity(Verbosity.verbose) as o: @fails @given(basic(SillyStrategy), settings=Settings(database=None)) def test_foo(x): assert False test_foo() assert u'shrunk example once' in o.getvalue()
def test_can_recalculate_shrinks_without_reify_cache(): random = Random('test_can_recalculate_shrinks_without_reify_cache') strat = basic(Bitfields) template = some_template(strat, random) for shrunk_template in strat.full_simplify(random, template): strat.reify_cache.pop(shrunk_template, None) strat.reify_cache.pop(template, None) assert not (~strat.reify(template) & strat.reify(shrunk_template)) new_template = strat.from_basic(strat.to_basic(template)) assert strat.reify(template) == strat.reify(new_template)
def test_can_recalculate_shrinks_without_reify_cache(): random = Random(u'test_can_recalculate_shrinks_without_reify_cache') strat = basic(Bitfields) template = some_template(strat, random) for shrunk_template in strat.full_simplify(random, template): strat.wrapped_strategy.reify_cache.pop(shrunk_template, None) strat.wrapped_strategy.reify_cache.pop(template, None) assert not (~strat.reify(template) & strat.reify(shrunk_template)) new_template = strat.from_basic(strat.to_basic(template)) assert strat.reify(template) == strat.reify(new_template)
def test_can_find_adjacent_one_bits(): class Nope(Exception): pass @given(basic(Bitfields)) def has_no_adjacent_one_bits(x): if has_adjacent_one_bits(x): raise Nope() for _ in range(5): with pytest.raises(Nope): has_no_adjacent_one_bits()
def test_can_find_adjacent_one_bits(): class Nope(Exception): pass @given(basic(Bitfields)) def has_no_adjacent_one_bits(x): if has_adjacent_one_bits(x): raise Nope() for _ in range(5): with pytest.raises(Nope): has_no_adjacent_one_bits()
def test_simplifying_results_in_strictly_simpler(): random = Random(u'test_simplifying_results_in_strictly_simpler') strat = basic(Bitfields) template = some_template(strat, random) for shrunk_template in strat.full_simplify(random, template): assert strat.strictly_simpler(shrunk_template, template)
def test_all_bad(x): assert False try: test_all_bad() except AssertionError: pass gc_clear() assert all(isinstance(v, integer_types) for v in st.reify_cache.values()) assert len(st.reify_cache) == 0, len(st.reify_cache) @fails @given(basic(BoringBitfields)) def test_boring_failure(x): assert x & 1 def test_does_not_get_stuck_in_a_loop(): bad_strategy = basic_strategy(generate=lambda r, p: 1, simplify=lambda r, v: [v]) @timeout(2) @given(bad_strategy) def oh_noes(x): assert x != 1 with pytest.raises(AssertionError): oh_noes()
def constant_list(strat): return strat.flatmap(lambda v: lists(just(v))) EvalledIntStream = streaming(integers()).map(lambda x: list(x[:5]) and x) ABC = namedtuple(u"ABC", (u"a", u"b", u"c")) def abc(x, y, z): return builds(ABC, x, y, z) with Settings(average_list_length=10.0): standard_types = [ basic(Bitfields), EvalledIntStream, lists(max_size=0), tuples(), sets(max_size=0), frozensets(max_size=0), fixed_dictionaries({}), n_ary_tree(booleans(), booleans(), booleans()), n_ary_tree(integers(), integers(), integers()), abc(booleans(), booleans(), booleans()), abc(booleans(), booleans(), integers()), templates_for(one_of(*map(just, hrange(10)))), fixed_dictionaries({u"a": integers(), u"b": booleans()}), dictionaries(booleans(), integers()), dictionaries(text(), booleans()), one_of(integers(), tuples(booleans())),
max_size=10 ) # A basic MIME message with plaintext body plus From/To/Cc/Bcc/Subject headers mime_message = s.builds( build_mime_message, addresslist, addresslist, addresslist, addresslist, basic_text, basic_text ) randint = s.basic(generate=lambda random, _: random.getrandbits(63)) uid_data = s.builds( build_uid_data, datetimes(timezones=[]), s.sampled_from([(), ('\\Seen',)]), mime_message, s.sampled_from([(), ('\\Inbox',)]), randint, randint) uids = s.dictionaries( s.integers(min_value=22), uid_data, min_size=24)
# We don't want to worry about whacky encodings or pathologically long data # here, so just generate some basic, sane ASCII text. basic_text = s.text(string.ascii_letters, min_size=1, max_size=64) # An email address of the form 'foo@bar'. address = s.builds(lambda localpart, domain: '{}@{}'.format(localpart, domain), basic_text, basic_text) # A list of tuples ('displayname', 'addr@domain') addresslist = s.lists(s.tuples(basic_text, address), min_size=1, max_size=10) # A basic MIME message with plaintext body plus From/To/Cc/Bcc/Subject headers mime_message = s.builds(build_mime_message, addresslist, addresslist, addresslist, addresslist, basic_text, basic_text) randint = s.basic(generate=lambda random, _: random.getrandbits(63)) uid_data = s.builds(build_uid_data, datetimes(timezones=[]), s.sampled_from([(), ('\\Seen', )]), mime_message, s.sampled_from([(), ('\\Inbox', )]), randint, randint) uids = s.dictionaries(s.integers(min_value=22), uid_data, min_size=24) class MockIMAPClient(object): """A bare-bones stand-in for an IMAPClient instance, used to test sync logic without requiring a real IMAP account and server.""" def __init__(self): self._data = {} self.selected_folder = None self.uidvalidity = 1
def test_can_find_distinct_bitfield_representatives(n): find(lists(basic(Bitfields), min_size=n, unique=True), lambda x: True, settings=Settings(max_shrinks=1))
def test_can_override_simplify_in_basic_strategies(): assert find(ds.basic(BoringBitfields, simplify=Bitfields().simplify), lambda x: True) == 0
def test_can_use_basic_strategies(): assert find(ds.basic(Bitfields), lambda x: True) == 0 assert find(ds.basic(Bitfields()), lambda x: True) == 0 assert find(ds.basic(BoringBitfields), lambda x: True) != 0
def test_can_use_basic_strategies(): with Settings(strict=False): assert find(ds.basic(Bitfields), lambda x: True) == 0 assert find(ds.basic(Bitfields()), lambda x: True) == 0 assert find(ds.basic(BoringBitfields), lambda x: True) != 0
def test_can_provide_just_param_and_generate(): bf = basic( generate_parameter=lambda r: r.getrandbits(128), generate=lambda r, p: r.getrandbits(128) & p, ) assert minimal(bf)
def test_can_provide_just_param_and_generate(): bf = basic( generate_parameter=lambda r: r.getrandbits(128), generate=lambda r, p: r.getrandbits(128) & p, ) assert minimal(bf)
def test_all_bad(x): assert False try: test_all_bad() except AssertionError: pass gc_clear() assert all(isinstance(v, integer_types) for v in st.reify_cache.values()) assert len(st.reify_cache) == 0, len(st.reify_cache) @fails @given(basic(BoringBitfields)) def test_boring_failure(x): assert x & 1 def test_does_not_get_stuck_in_a_loop(): bad_strategy = basic_strategy( generate=lambda r, p: 1, simplify=lambda r, v: [v] ) @timeout(2) @given(bad_strategy) def oh_noes(x): assert x != 1 with pytest.raises(AssertionError):
return integers(min_value=x) TestManyFlatmaps = strategy_test_suite( integers() .flatmap(integers_from) .flatmap(integers_from) .flatmap(integers_from) .flatmap(integers_from) ) TestIntStreams = strategy_test_suite(streaming(integers())) TestStreamLists = strategy_test_suite(streaming(integers())) TestIntStreamStreams = strategy_test_suite( streaming(streaming(integers()))) TestBoringBitfieldsClass = strategy_test_suite(basic(BoringBitfields)) TestBitfieldsClass = strategy_test_suite(basic(Bitfields)) TestBitfieldsInstance = strategy_test_suite(basic(Bitfields())) TestBitfields = strategy_test_suite(lists( basic( generate=lambda r, p: r.getrandbits(128), simplify=simplify_bitfield, copy=lambda x: x, ) )) TestBitfieldsSet = strategy_test_suite(sets( basic( generate=lambda r, p: r.getrandbits(128), simplify=simplify_bitfield,
def test_simplifying_results_in_strictly_simpler(): random = Random('test_simplifying_results_in_strictly_simpler') strat = basic(Bitfields) template = some_template(strat, random) for shrunk_template in strat.full_simplify(random, template): assert strat.strictly_simpler(shrunk_template, template)
def test_can_use_basic_strategies(): assert find(ds.basic(Bitfields), lambda x: True) == 0 assert find(ds.basic(Bitfields()), lambda x: True) == 0 assert find(ds.basic(BoringBitfields), lambda x: True) != 0
return strat.flatmap( lambda v: lists(just(v)), ) EvalledIntStream = streaming(integers()).map(lambda x: list(x[:5]) and x) ABC = namedtuple(u'ABC', (u'a', u'b', u'c')) def abc(x, y, z): return builds(ABC, x, y, z) with Settings(average_list_length=10.0): standard_types = [ basic(Bitfields), EvalledIntStream, lists(max_size=0), tuples(), sets(max_size=0), frozensets(max_size=0), fixed_dictionaries({}), n_ary_tree(booleans(), booleans(), booleans()), n_ary_tree(integers(), integers(), integers()), abc(booleans(), booleans(), booleans()), abc(booleans(), booleans(), integers()), templates_for(one_of(*map(just, hrange(10)))), fixed_dictionaries({u'a': integers(), u'b': booleans()}), dictionaries(booleans(), integers()), dictionaries(text(), booleans()), one_of(integers(), tuples(booleans())), sampled_from(range(10)), one_of(just(u'a'), just(u'b'), just(u'c')), sampled_from((u'a', u'b', u'c')),
def test_can_use_basic_strategies_with_only_kwargs(): assert find( ds.basic(generate=BoringBitfields().generate), lambda x: True) != 0
def test_can_use_basic_strategies_with_only_kwargs(): assert find(ds.basic(generate=BoringBitfields().generate), lambda x: True) != 0
def test_can_override_simplify_in_basic_strategies(): assert find( ds.basic(BoringBitfields, simplify=Bitfields().simplify), lambda x: True) == 0
TestMappedSampling = strategy_test_suite( lists(integers(), min_size=1).flatmap(sampled_from)) def integers_from(x): return integers(min_value=x) TestManyFlatmaps = strategy_test_suite( integers().flatmap(integers_from).flatmap(integers_from).flatmap( integers_from).flatmap(integers_from)) TestIntStreams = strategy_test_suite(streaming(integers())) TestStreamLists = strategy_test_suite(streaming(integers())) TestIntStreamStreams = strategy_test_suite(streaming(streaming( integers()))) TestBoringBitfieldsClass = strategy_test_suite(basic(BoringBitfields)) TestBitfieldsClass = strategy_test_suite(basic(Bitfields)) TestBitfieldsInstance = strategy_test_suite(basic(Bitfields())) TestBitfields = strategy_test_suite( lists( basic( generate=lambda r, p: r.getrandbits(128), simplify=simplify_bitfield, copy=lambda x: x, ))) TestBitfieldsSet = strategy_test_suite( sets( basic( generate=lambda r, p: r.getrandbits(128),
def test_can_find_distinct_bitfield_representatives(n): find( lists(basic(Bitfields), min_size=n, unique_by=lambda x: x), lambda x: True, settings=Settings(max_shrinks=1) )