Example #1
0
def test_backend_returns_what_you_put_in(xs):
    backend = SQLiteBackend(':memory:')
    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 = SQLiteBackend(u':memory:')
    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_does_not_commit_in_error_state():
    backend = SQLiteBackend(u':memory:')
    backend.create_db_if_needed()
    try:
        with backend.cursor() as cursor:
            cursor.execute("""
                insert into hypothesis_data_mapping(key, value)
                values("a", "b")
            """)
            raise ValueError()
    except ValueError:
        pass

    assert backend.fetch(u'a') == []
    def database(self):
        """An ExampleDatabase instance to use for storage of examples. May be
        None.

        If this was explicitly set at settings instantiation then that
        value will be used (even if it was None). If not and the
        database_file setting is not None this will be lazily loaded as
        an SQLite backed ExampleDatabase using that file the first time
        this property is accessed on a particular thread.

        """
        try:
            if self._database is not_set and self.database_file is not None:
                from hypothesis.database import ExampleDatabase
                from hypothesis.database.backend import SQLiteBackend
                if self.database_file not in _db_cache:
                    _db_cache[self.database_file] = (ExampleDatabase(
                        backend=SQLiteBackend(self.database_file)))
                return _db_cache[self.database_file]
            if self._database is not_set:
                self._database = None
            return self._database
        except AttributeError:
            import traceback
            traceback.print_exc()
            assert False
Example #5
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 #6
0
 def test_can_round_trip_through_the_database(self, template, rnd):
     empty_db = ExampleDatabase(backend=SQLiteBackend(u':memory:'), )
     try:
         storage = empty_db.storage(u'round trip')
         storage.save(template, strat)
         values = list(storage.fetch(strat))
         assert len(values) == 1
         assert strat.to_basic(template) == strat.to_basic(values[0])
     finally:
         empty_db.close()
Example #7
0
 def __init__(
     self,
     backend=None,
     format=None,
 ):
     self.backend = backend or SQLiteBackend()
     self.format = format or JSONFormat()
     if self.format.data_type() != self.backend.data_type():
         raise ValueError(
             ('Inconsistent data types: format provides data of type %s '
              'but backend expects data of type %s' %
              (self.format.data_type(), self.backend.data_type())))
Example #8
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(u'round trip')
        storage.save(value, specifier)
        saved = list(storage.fetch(specifier))
        assert len(saved) == 1
        assert specifier.to_basic(saved[0]) == specifier.to_basic(value)
    finally:
        db.close()
Example #9
0
 def __init__(
     self,
     converters=None,
     backend=None,
     format=None,
 ):
     self.converters = converters or ConverterTable.default()
     self.strategies = self.converters.strategy_table
     self.backend = backend or SQLiteBackend()
     self.format = format or JSONFormat()
     if self.format.data_type() != self.backend.data_type():
         raise ValueError(
             ('Inconsistent data types: format provides data of type %s '
              'but backend expects data of type %s' %
              (self.format.data_type(), self.backend.data_type())))
     self.storage_cache = {}
    def database(self):
        """An ExampleDatabase instance to use for storage of examples. May be
        None.

        If this was explicitly set at Settings instantiation then that
        value will be used (even if it was None). If not and the
        database_file setting is not None this will be lazily loaded as
        an SQLite backed ExampleDatabase using that file the first time
        this property is accessed.

        """
        if self._database is not_set and self.database_file is not None:
            from hypothesis.database import ExampleDatabase
            from hypothesis.database.backend import SQLiteBackend
            self._database = databases.get(self.database_file) or (
                ExampleDatabase(backend=SQLiteBackend(self.database_file)))
            databases[self.database_file] = self._database
        return self._database
Example #11
0
def test_does_not_commit_in_error_state():
    backend = SQLiteBackend(':memory:')
    backend.create_db_if_needed()
    try:
        with backend.cursor() as cursor:
            cursor.execute("""
                insert into hypothesis_data_mapping(key, value)
                values("a", "b")
            """)
            raise ValueError()
    except ValueError:
        pass

    assert backend.fetch('a') == []
Example #12
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 #13
0
def test_can_delete_keys():
    backend = SQLiteBackend(':memory:')
    backend.save('foo', 'bar')
    backend.save('foo', 'baz')
    backend.delete('foo', 'bar')
    assert list(backend.fetch('foo')) == ['baz']
Example #14
0
def test_db_has_path_in_repr():
    backend = SQLiteBackend(u':memory:')
    db = ExampleDatabase(backend=backend)
    assert u':memory:' in repr(db)
Example #15
0
        min_satisfying_examples=USE_DEFAULT,
        max_examples=USE_DEFAULT,
        max_skipped_examples=USE_DEFAULT,
        timeout=USE_DEFAULT,
        derandomize=USE_DEFAULT,
        database=USE_DEFAULT,
    ):
        set_or_default(self, 'min_satisfying_examples',
                       min_satisfying_examples)
        set_or_default(self, 'max_examples', max_examples)
        set_or_default(self, 'max_skipped_examples', max_skipped_examples)
        set_or_default(self, 'timeout', timeout)
        set_or_default(self, 'derandomize', derandomize)
        set_or_default(self, 'database', database)


default = Settings(min_satisfying_examples=5,
                   max_examples=200,
                   timeout=60,
                   max_skipped_examples=50,
                   derandomize=False,
                   database=None)

DATABASE_OVERRIDE = os.getenv('HYPOTHESIS_DATABASE_FILE')

if DATABASE_OVERRIDE:
    from hypothesis.database import ExampleDatabase
    from hypothesis.database.backend import SQLiteBackend
    default.database = ExampleDatabase(
        backend=SQLiteBackend(DATABASE_OVERRIDE))
Example #16
0
def test_can_fetch_all_keys():
    backend = SQLiteBackend(u':memory:')
    backend.save(u'foo', u'bar')
    backend.save(u'foo', u'baz')
    backend.save(u'boib', u'baz')
    assert len(list(backend.keys())) == 2
Example #17
0
def test_can_delete_keys():
    backend = SQLiteBackend(u':memory:')
    backend.save(u'foo', u'bar')
    backend.save(u'foo', u'baz')
    backend.delete(u'foo', u'bar')
    assert list(backend.fetch(u'foo')) == [u'baz']
Example #18
0
def test_can_double_close():
    backend = SQLiteBackend(u':memory:')
    backend.create_db_if_needed()
    backend.close()
    backend.close()
def test_can_fetch_all_keys():
    backend = SQLiteBackend(u':memory:')
    backend.save(u'foo', u'bar')
    backend.save(u'foo', u'baz')
    backend.save(u'boib', u'baz')
    assert len(list(backend.keys())) == 2
Example #20
0
def test_can_double_close():
    backend = SQLiteBackend(':memory:')
    backend.create_db_if_needed()
    backend.close()
    backend.close()