Example #1
0
def test_puts_elements_of_set_in_database():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))
    verifier.falsify(lambda x: not x, {int})
    assert list(database.storage_for([int]).fetch()) == []
    assert list(database.storage_for({int}).fetch()) == [{0}]
    assert list(database.storage_for(int).fetch()) == [0]
Example #2
0
def test_storage_cleans_up_invalid_data_from_the_db():
    database = ExampleDatabase()
    ints = database.storage_for(integers())
    database.backend.save(ints.key, '[false, false, true]')
    assert list(database.backend.fetch(ints.key)) != []
    assert list(ints.fetch()) == []
    assert list(database.backend.fetch(ints.key)) == []
Example #3
0
def via_database(spec, strat, template):
    db = ExampleDatabase()
    s = db.storage_for(strat, strat)
    s.save(template)
    results = list(s.fetch())
    assert len(results) == 1
    return results[0]
Example #4
0
def test_can_use_values_in_the_database():
    example = 'Hello world'
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    storage = database.storage_for(text_type)
    storage.save(example)
    verifier = Verifier(settings=Settings(database=database))
    assert verifier.falsify(lambda x: x != example, text_type) == (example, )
Example #5
0
def via_database(spec, strat, template):
    db = ExampleDatabase()
    s = db.storage_for(strat, strat)
    s.save(template)
    results = list(s.fetch())
    assert len(results) == 1
    return results[0]
Example #6
0
def test_storage_cleans_up_invalid_data_from_the_db():
    database = ExampleDatabase()
    ints = database.storage_for(integers())
    database.backend.save(ints.key, '[false, false, true]')
    assert list(database.backend.fetch(ints.key)) != []
    assert list(ints.fetch()) == []
    assert list(database.backend.fetch(ints.key)) == []
Example #7
0
def test_deduplicates():
    database = ExampleDatabase()
    storage = database.storage_for(int)
    storage.save(1)
    storage.save(1)
    assert list(storage.fetch()) == [1]
    database.close()
Example #8
0
 def test_can_round_trip_through_the_database(self, template, rnd):
     empty_db = ExampleDatabase(backend=SQLiteBackend(':memory:'), )
     try:
         storage = empty_db.storage_for(specifier)
         storage.save(template)
         values = list(storage.fetch())
         assert len(values) == 1
         assert strat.to_basic(template) == strat.to_basic(values[0])
     finally:
         empty_db.close()
Example #9
0
def run_round_trip(descriptor, value, format=None, backend=None):
    if backend is not None:
        backend = backend()
    else:
        backend = SQLiteBackend()
    db = ExampleDatabase(format=format, backend=backend)
    storage = db.storage_for(descriptor)
    storage.save(value)
    saved = list(storage.fetch())
    assert actually_equal(saved, [value])
Example #10
0
def test_puts_keyword_arguments_in_the_database_from_given():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))

    @given(x=int, verifier=verifier)
    def a_test(x):
        assert False

    with pytest.raises(AssertionError):
        a_test()
    assert list(database.storage_for(int).fetch()) == [0]
Example #11
0
def test_example_source_terminates_if_just_from_db():
    db = ExampleDatabase()
    storage = db.storage_for(int)
    storage.save(1)
    source = ExampleSource(random=random.Random(),
                           storage=storage,
                           strategy=None)
    its = iter(source)
    assert next(its) == 1
    with pytest.raises(StopIteration):
        next(its)
Example #12
0
 def test_can_round_trip_through_the_database(self, template, rnd):
     empty_db = ExampleDatabase(
         backend=SQLiteBackend(':memory:'),
     )
     try:
         storage = empty_db.storage_for(specifier)
         storage.save(template)
         values = list(storage.fetch())
         assert len(values) == 1
         assert strat.to_basic(template) == strat.to_basic(values[0])
     finally:
         empty_db.close()
Example #13
0
def run_round_trip(specifier, value, format=None, backend=None):
    if backend is not None:
        backend = backend()
    else:
        backend = SQLiteBackend()
    db = ExampleDatabase(format=format, backend=backend)
    try:
        storage = db.storage_for(specifier)
        storage.save(value)
        saved = list(storage.fetch())
        assert len(saved) == 1
        strat = strategy(specifier)
        assert strat.to_basic(saved[0]) == strat.to_basic(value)
    finally:
        db.close()
Example #14
0
def run_round_trip(specifier, value, format=None, backend=None):
    if backend is not None:
        backend = backend()
    else:
        backend = SQLiteBackend()
    db = ExampleDatabase(format=format, backend=backend)
    try:
        storage = db.storage_for(specifier)
        storage.save(value)
        saved = list(storage.fetch())
        assert len(saved) == 1
        strat = strategy(specifier)
        assert strat.to_basic(saved[0]) == strat.to_basic(value)
    finally:
        db.close()
Example #15
0
def test_verifier_deduplicates_on_coming_out_of_the_database():
    db = ExampleDatabase()
    storage = db.storage_for((frozenset({int}), ))
    db.backend.save(storage.key, '[1, 2, 3]')
    db.backend.save(storage.key, '[3, 2, 1]')
    counter = Counter()
    calls = []
    good = frozenset({1, 2, 3})

    def count_and_object(x):
        counter[x] += 1
        if not calls:
            calls.append(x)
        return x == good

    verifier = Verifier(settings=hs.Settings(database=db))
    verifier.falsify(count_and_object, frozenset({int}))
    assert calls[0] == good
    assert counter[good] == 1
Example #16
0
def test_storage_does_not_return_things_not_matching_strategy():
    table = StrategyTable()
    strategy = JustStrategy(PickyStrategyLazyFormat())

    strategy.could_have_produced = lambda *args: False
    table.define_specification_for(PickyStrategyLazyFormat,
                                   lambda s, d: strategy)
    converters = ConverterTable(strategy_table=table)
    converters.define_specification_for(
        PickyStrategyLazyFormat,
        lambda s, d: JustConverter(PickyStrategyLazyFormat()))
    database = ExampleDatabase(
        converters=converters,
        backend=SQLiteBackend(),
    )
    stor = database.storage_for(PickyStrategyLazyFormat)
    database.backend.save(stor.key, 'null')
    assert list(database.backend.fetch(stor.key)) != []
    assert list(stor.fetch()) == []
    assert list(database.backend.fetch(stor.key)) == []
Example #17
0
def test_storage_has_specifier_in_repr():
    db = ExampleDatabase()
    d = (int, int)
    s = db.storage_for(d)
    assert repr(d) in repr(s)
Example #18
0
def test_can_save_all_strings(s):
    db = ExampleDatabase()
    storage = db.storage_for(text())
    storage.save(tuple(s))
Example #19
0
def test_does_not_put_binary_substrings_in_database():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))
    verifier.falsify(lambda x: len(x) <= 3, binary_type)
    assert len(list(database.storage_for(binary_type).fetch())) == 1
    assert len(list(database.storage_for(int).fetch())) == 0
Example #20
0
def test_puts_branches_of_one_of_in_database():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))
    verifier.falsify(lambda x: isinstance(x, bool), one_of((int, bool)))
    assert list(database.storage_for(int).fetch()) == [0]
    assert list(database.storage_for(bool).fetch()) == []
Example #21
0
def test_storage_has_specifier_in_repr():
    db = ExampleDatabase()
    d = tuples(integers(), integers())
    s = db.storage_for(d)
    assert repr(d) in repr(s)
Example #22
0
def test_puts_arguments_in_the_database_from_falsify():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))
    verifier.falsify(lambda x, y: False, text_type, int)
    assert list(database.storage_for(text_type).fetch()) == ['']
    assert list(database.storage_for(int).fetch()) == [0]
Example #23
0
def test_a_verifier_saves_any_failing_examples_in_its_database():
    database = ExampleDatabase()
    verifier = Verifier(settings=hs.Settings(database=database))
    counterexample = verifier.falsify(lambda x: x > 0, int)
    saved = list(database.storage_for((int, )).fetch())
    assert saved == [counterexample]
Example #24
0
def test_storage_does_not_error_if_the_database_is_invalid():
    database = ExampleDatabase()
    ints = database.storage_for(integers())
    database.backend.save(ints.key, '["hi", "there"]')
    assert list(ints.fetch()) == []
Example #25
0
def test_storage_has_specifier_in_repr():
    db = ExampleDatabase()
    d = tuples(integers(), integers())
    s = db.storage_for(d)
    assert repr(d) in repr(s)
Example #26
0
def test_storage_does_not_error_if_the_database_is_invalid():
    database = ExampleDatabase()
    ints = database.storage_for(integers())
    database.backend.save(ints.key, '["hi", "there"]')
    assert list(ints.fetch()) == []
Example #27
0
def test_storage_errors_if_given_the_wrong_type():
    database = ExampleDatabase()
    ints = database.storage_for(int)
    with pytest.raises(ValueError):
        ints.save('hi')
Example #28
0
def test_storage_does_not_error_if_the_database_is_invalid():
    database = ExampleDatabase()
    ints = database.storage_for(int)
    database.backend.save(ints.key, '[false, false, true]')
    assert list(ints.fetch()) == []
Example #29
0
def test_can_save_all_strings(s):
    db = ExampleDatabase()
    storage = db.storage_for(text())
    storage.save(tuple(s))
Example #30
0
def test_storage_has_specifier_in_repr():
    db = ExampleDatabase()
    d = (int, int)
    s = db.storage_for(d)
    assert repr(d) in repr(s)