def test_clears_out_its_database_on_shrinking( initial_attempt, skip_target, monkeypatch ): def generate_new_examples(self): self.test_function( ConjectureData.for_buffer(hbytes([initial_attempt]))) monkeypatch.setattr( ConjectureRunner, 'generate_new_examples', generate_new_examples) key = b'key' db = InMemoryExampleDatabase() def f(data): if data.draw_bits(8) >= 127: data.mark_interesting() runner = ConjectureRunner( f, settings=settings(database=db, max_examples=256), database_key=key, random=Random(0), ) for n in hrange(256): if n != 127 or not skip_target: db.save(runner.secondary_key, hbytes([n])) runner.run() assert len(runner.interesting_examples) == 1 for b in db.fetch(runner.secondary_key): assert b[0] >= 127 assert len(list(db.fetch(runner.database_key))) == 1
def test_database_clears_secondary_key(): key = b"key" database = InMemoryExampleDatabase() def f(data): if data.draw_bits(8) == 10: data.mark_interesting() else: data.mark_invalid() runner = ConjectureRunner( f, settings=settings( max_examples=1, buffer_size=1024, database=database, suppress_health_check=HealthCheck.all(), ), database_key=key, ) for i in range(10): database.save(runner.secondary_key, hbytes([i])) runner.cached_test_function([10]) assert runner.interesting_examples assert len(set(database.fetch(key))) == 1 assert len(set(database.fetch(runner.secondary_key))) == 10 runner.clear_secondary_key() assert len(set(database.fetch(key))) == 1 assert len(set(database.fetch(runner.secondary_key))) == 0
def test_database_uses_values_from_secondary_key(): key = b'key' database = InMemoryExampleDatabase() def f(data): if data.draw_bits(8) >= 5: data.mark_interesting() else: data.mark_invalid() runner = ConjectureRunner(f, settings=settings( max_examples=1, buffer_size=1024, database=database, suppress_health_check=HealthCheck.all(), ), database_key=key) for i in range(10): database.save(runner.secondary_key, hbytes([i])) runner.test_function(ConjectureData.for_buffer(hbytes([10]))) assert runner.interesting_examples assert len(set(database.fetch(key))) == 1 assert len(set(database.fetch(runner.secondary_key))) == 10 runner.clear_secondary_key() assert len(set(database.fetch(key))) == 1 assert set( map(int_from_bytes, database.fetch(runner.secondary_key)) ) == set(range(6, 11)) v, = runner.interesting_examples.values() assert list(v.buffer) == [5]
def test_stops_loading_pareto_front_if_interesting(): with deterministic_PRNG(): def test(data): data.draw_bits(8) data.draw_bits(8) data.mark_interesting() db = InMemoryExampleDatabase() runner = ConjectureRunner( test, settings=settings( max_examples=1000, database=db, suppress_health_check=HealthCheck.all(), phases=[Phase.reuse], ), database_key=b"stuff", ) for i in range(10000): db.save(runner.pareto_key, int_to_bytes(i, 2)) runner.reuse_existing_examples() assert runner.call_count == 1
def test_clears_defunct_pareto_front(): with deterministic_PRNG(): def test(data): data.draw_bits(8) data.draw_bits(8) db = InMemoryExampleDatabase() runner = ConjectureRunner( test, settings=settings( max_examples=10000, database=db, suppress_health_check=HealthCheck.all(), phases=[Phase.reuse], ), database_key=b"stuff", ) for i in range(256): db.save(runner.pareto_key, bytes([i, 0])) runner.run() assert len(list(db.fetch(runner.pareto_key))) == 1
def test_down_samples_the_pareto_front(): with deterministic_PRNG(): def test(data): data.draw_bits(8) data.draw_bits(8) db = InMemoryExampleDatabase() runner = ConjectureRunner( test, settings=settings( max_examples=1000, database=db, suppress_health_check=HealthCheck.all(), phases=[Phase.reuse], ), database_key=b"stuff", ) for i in range(10000): db.save(runner.pareto_key, int_to_bytes(i, 2)) runner.reuse_existing_examples() assert 0 < runner.valid_examples <= 100
def test_database_clears_secondary_key(): key = b"key" database = InMemoryExampleDatabase() def f(data): if data.draw_bits(8) == 10: data.mark_interesting() else: data.mark_invalid() runner = ConjectureRunner( f, settings=settings( max_examples=1, buffer_size=1024, database=database, suppress_health_check=HealthCheck.all(), ), database_key=key, ) for i in range(10): database.save(runner.secondary_key, hbytes([i])) runner.cached_test_function([10]) assert runner.interesting_examples assert len(set(database.fetch(key))) == 1 assert len(set(database.fetch(runner.secondary_key))) == 10 runner.clear_secondary_key() assert len(set(database.fetch(key))) == 1 assert len(set(database.fetch(runner.secondary_key))) == 0
def test_readonly_db_is_not_writable(): inner = InMemoryExampleDatabase() wrapped = ReadOnlyDatabase(inner) inner.save(b"key", b"value") inner.save(b"key", b"value2") wrapped.delete(b"key", b"value") wrapped.move(b"key", b"key2", b"value2") wrapped.save(b"key", b"value3") assert set(wrapped.fetch(b"key")) == {b"value", b"value2"} assert set(wrapped.fetch(b"key2")) == set()
def test_backend_returns_what_you_put_in(xs): backend = InMemoryExampleDatabase() mapping = {} for key, value in xs: mapping.setdefault(key, set()).add(value) backend.save(key, value) for key, values in mapping.items(): backend_contents = list(backend.fetch(key)) distinct_backend_contents = set(backend_contents) assert len(backend_contents) == len(distinct_backend_contents) assert distinct_backend_contents == set(values)
def test_backend_returns_what_you_put_in(xs): backend = InMemoryExampleDatabase() mapping = {} for key, value in xs: mapping.setdefault(key, set()).add(value) backend.save(key, value) for key, values in mapping.items(): backend_contents = list(backend.fetch(key)) distinct_backend_contents = set(backend_contents) assert len(backend_contents) == len(distinct_backend_contents) assert distinct_backend_contents == set(values)
def test_multiplexed_dbs_read_and_write_all(): a = InMemoryExampleDatabase() b = InMemoryExampleDatabase() multi = MultiplexedDatabase(a, b) a.save(b"a", b"aa") b.save(b"b", b"bb") multi.save(b"c", b"cc") multi.move(b"a", b"b", b"aa") for db in (a, b, multi): assert set(db.fetch(b"a")) == set() assert set(db.fetch(b"c")) == {b"cc"} got = list(multi.fetch(b"b")) assert len(got) == 2 assert set(got) == {b"aa", b"bb"} multi.delete(b"c", b"cc") for db in (a, b, multi): assert set(db.fetch(b"c")) == set()
def test_reuse_phase_runs_for_max_examples_if_generation_is_disabled(): with deterministic_PRNG(): db = InMemoryExampleDatabase() for i in range(256): db.save(b"key", bytes([i])) seen = set() def test(data): seen.add(data.draw_bits(8)) ConjectureRunner( test, settings=settings(max_examples=100, database=db, phases=[Phase.reuse]), database_key=b"key", ).run() assert len(seen) == 100
def test_can_delete_keys(): backend = InMemoryExampleDatabase() backend.save(b"foo", b"bar") backend.save(b"foo", b"baz") backend.delete(b"foo", b"bar") assert list(backend.fetch(b"foo")) == [b"baz"]
def test_can_delete_keys(): backend = InMemoryExampleDatabase() backend.save(b'foo', b'bar') backend.save(b'foo', b'baz') backend.delete(b'foo', b'bar') assert list(backend.fetch(b'foo')) == [b'baz']
def test_can_delete_keys(): backend = InMemoryExampleDatabase() backend.save(b'foo', b'bar') backend.save(b'foo', b'baz') backend.delete(b'foo', b'bar') assert list(backend.fetch(b'foo')) == [b'baz']
def test_can_delete_keys(): backend = InMemoryExampleDatabase() backend.save(b"foo", b"bar") backend.save(b"foo", b"baz") backend.delete(b"foo", b"bar") assert list(backend.fetch(b"foo")) == [b"baz"]