Esempio n. 1
0
class DatabaseComparison(RuleBasedStateMachine):
    def __init__(self):
        super().__init__()
        self.tempd = tempfile.mkdtemp()
        self.database = DirectoryBasedExampleDatabase(self.tempd)
        self.model = defaultdict(set)

    keys = Bundle("keys")
    values = Bundle("values")

    @rule(target=keys, k=st.binary())
    def add_key(self, k):
        return k

    @rule(target=values, v=st.binary())
    def add_value(self, v):
        return v

    @rule(k=keys, v=values)
    def save(self, k, v):
        self.model[k].add(v)
        self.database.save(k, v)

    @rule(k=keys, v=values)
    def delete(self, k, v):
        self.model[k].discard(v)
        self.database.delete(k, v)

    @rule(k=keys)
    def values_agree(self, k):
        assert set(self.database.fetch(k)) == self.model[k]

    def teardown(self):
        shutil.rmtree(self.tempd)
Esempio n. 2
0
def test_can_handle_disappearing_files(tmpdir, monkeypatch):
    path = str(tmpdir)
    db = DirectoryBasedExampleDatabase(path)
    db.save(b"foo", b"bar")
    base_listdir = os.listdir
    monkeypatch.setattr(os, "listdir",
                        lambda d: base_listdir(d) + ["this-does-not-exist"])
    assert list(db.fetch(b"foo")) == [b"bar"]
def test_can_handle_disappearing_files(tmpdir, monkeypatch):
    path = str(tmpdir)
    db = DirectoryBasedExampleDatabase(path)
    db.save(b'foo', b'bar')
    base_listdir = os.listdir
    monkeypatch.setattr(os, 'listdir',
                        lambda d: base_listdir(d) + ['this-does-not-exist'])
    assert list(db.fetch(b'foo')) == [b'bar']
def test_can_handle_disappearing_files(tmpdir, monkeypatch):
    path = str(tmpdir)
    db = DirectoryBasedExampleDatabase(path)
    db.save(b'foo', b'bar')
    base_listdir = os.listdir
    monkeypatch.setattr(os, 'listdir',
                        lambda d: base_listdir(d) + ['this-does-not-exist'])
    assert list(db.fetch(b'foo')) == [b'bar']
def test_two_directory_databases_can_interact(tmpdir):
    path = str(tmpdir)
    db1 = DirectoryBasedExampleDatabase(path)
    db2 = DirectoryBasedExampleDatabase(path)
    db1.save(b'foo', b'bar')
    assert list(db2.fetch(b'foo')) == [b'bar']
    db2.save(b'foo', b'bar')
    db2.save(b'foo', b'baz')
    assert sorted(db1.fetch(b'foo')) == [b'bar', b'baz']
def test_can_handle_disappearing_files(tmpdir, monkeypatch):
    path = str(tmpdir)
    db = DirectoryBasedExampleDatabase(path)
    db.save(b"foo", b"bar")
    base_listdir = os.listdir
    monkeypatch.setattr(
        os, "listdir", lambda d: base_listdir(d) + ["this-does-not-exist"]
    )
    assert list(db.fetch(b"foo")) == [b"bar"]
    def __init__(self):
        super().__init__()
        # Define the system under test
        self.tempd = tempfile.mkdtemp()
        self.database = DirectoryBasedExampleDatabase(self.tempd)

        # State of the model.
        # Transitions are defined in the rules itself.
        self.model = defaultdict(set)
Esempio n. 8
0
def test_two_directory_databases_can_interact(tmpdir):
    path = str(tmpdir)
    db1 = DirectoryBasedExampleDatabase(path)
    db2 = DirectoryBasedExampleDatabase(path)
    db1.save(b"foo", b"bar")
    assert list(db2.fetch(b"foo")) == [b"bar"]
    db2.save(b"foo", b"bar")
    db2.save(b"foo", b"baz")
    assert sorted(db1.fetch(b"foo")) == [b"bar", b"baz"]
    def __init__(self):
        super().__init__()
        self.tempd = tempfile.mkdtemp()
        exampledir = os.path.join(self.tempd, "examples")

        self.dbs = [
            DirectoryBasedExampleDatabase(exampledir),
            InMemoryExampleDatabase(),
            DirectoryBasedExampleDatabase(exampledir),
        ]
    def __init__(self):
        super(DatabaseComparison, self).__init__()
        self.tempd = tempfile.mkdtemp()
        exampledir = os.path.join(self.tempd, 'examples')

        self.dbs = [
            DirectoryBasedExampleDatabase(exampledir),
            InMemoryExampleDatabase(), SQLiteExampleDatabase(':memory:'),
            DirectoryBasedExampleDatabase(exampledir),
        ]
Esempio n. 11
0
    def __init__(self):
        super(DatabaseComparison, self).__init__()
        self.tempd = tempfile.mkdtemp()
        exampledir = os.path.join(self.tempd, 'examples')

        self.dbs = [
            DirectoryBasedExampleDatabase(exampledir),
            InMemoryExampleDatabase(),
            DirectoryBasedExampleDatabase(exampledir),
        ]

        with validate_deprecation():
            self.dbs.append(SQLiteExampleDatabase(':memory:'))
class DatabaseComparison(RuleBasedStateMachine):
    def __init__(self):
        super().__init__()
        # Define the system under test
        self.tempd = tempfile.mkdtemp()
        self.database = DirectoryBasedExampleDatabase(self.tempd)

        # State of the model.
        # Transitions are defined in the rules itself.
        self.model = defaultdict(set)

    # Bundles are used for storing "draws" during the stateful test.
    # One could store, for example, randomly drawn users here.
    keys = Bundle("keys")
    values = Bundle("values")

    @rule(target=keys, k=st.binary())
    def add_key(self, k):
        return k

    @rule(target=values, v=st.binary())
    def add_value(self, v):
        return v

    @rule(k=keys, v=values)
    def save(self, k, v):
        self.model[k].add(v)
        self.database.save(k, v)

    @rule(k=keys, v=values)
    def delete(self, k, v):
        # What does model do when this operation is run?
        # This is a "`next_state` transition"
        self.model[k].discard(v)

        # What does actual system do when this operation is run?
        self.database.delete(k, v)
        # Should post conditions be here in the operations?

    @rule(k=keys)
    def values_agree(self, k):
        assert set(self.database.fetch(k)) == self.model[k]

    @invariant()
    def always_true(self):
        # Somehow compare model state and actual response here?
        return True

    def teardown(self):
        shutil.rmtree(self.tempd)
def test_two_directory_databases_can_interact(tmpdir):
    path = str(tmpdir)
    db1 = DirectoryBasedExampleDatabase(path)
    db2 = DirectoryBasedExampleDatabase(path)
    db1.save(b'foo', b'bar')
    assert list(db2.fetch(b'foo')) == [b'bar']
    db2.save(b'foo', b'bar')
    db2.save(b'foo', b'baz')
    assert sorted(db1.fetch(b'foo')) == [b'bar', b'baz']
def test_two_directory_databases_can_interact(tmpdir):
    path = str(tmpdir)
    db1 = DirectoryBasedExampleDatabase(path)
    db2 = DirectoryBasedExampleDatabase(path)
    db1.save(b"foo", b"bar")
    assert list(db2.fetch(b"foo")) == [b"bar"]
    db2.save(b"foo", b"bar")
    db2.save(b"foo", b"baz")
    assert sorted(db1.fetch(b"foo")) == [b"bar", b"baz"]
def exampledatabase(request, tmpdir):
    if request.param == 'memory':
        return ExampleDatabase()
    if request.param == 'sql':
        return SQLiteExampleDatabase(str(tmpdir.join('example.db')))
    if request.param == 'directory':
        return DirectoryBasedExampleDatabase(str(tmpdir.join('examples')))
    assert False
Esempio n. 16
0
def _modify_hypothesis_settings(settings, name, parent):
    hp_settings.register_profile(
        name,
        parent=hp_settings.get_profile(parent),
        database=DirectoryBasedExampleDatabase(
            _get_data_folder().joinpath("hypothesis")),
        **settings,
    )
    hp_settings.load_profile(name)
Esempio n. 17
0
class DatabaseComparison(RuleBasedStateMachine):
    def __init__(self):
        super(DatabaseComparison, self).__init__()
        writer.writerow(['init_state'])
        self.tempd = tempfile.mkdtemp()
        self.database = DirectoryBasedExampleDatabase(self.tempd)
        self.model = defaultdict(set)

    keys = Bundle("keys")
    values = Bundle("values")

    @rule(target=keys, k=st.binary())
    def add_key(self, k):
        writer.writerow(['add_key'])
        return k

    @rule(target=values, v=st.binary())
    def add_value(self, v):
        writer.writerow(['add_value'])
        return v

    @rule(k=keys, v=values)
    def save(self, k, v):
        writer.writerow(['save'])
        self.model[k].add(v)
        self.database.save(k, v)

    @rule(k=keys, v=values)
    def delete(self, k, v):
        writer.writerow(['delete'])
        self.model[k].discard(v)  # outcomment this to see it working
        self.database.delete(k, v)

    @rule(k=keys)
    def values_agree(self, k):
        assert set(self.database.fetch(k)) == self.model[k]

    def teardown(self):
        writer.writerow(['teardown'])
        shutil.rmtree(self.tempd)
Esempio n. 18
0
def _modify_hypothesis_settings(settings, name, parent=None):
    settings = settings.copy()
    if parent is None:
        parent = hp_settings._current_profile

    if "phases" in settings:
        try:
            settings["phases"] = [getattr(Phase, k) for k, v in settings["phases"].items() if v]
        except AttributeError as exc:
            raise ValueError(f"'{exc.args[0]}' is not a valid hypothesis phase setting")

    hp_settings.register_profile(
        name,
        parent=hp_settings.get_profile(parent),
        database=DirectoryBasedExampleDatabase(_get_data_folder().joinpath("hypothesis")),
        **settings,
    )
    hp_settings.load_profile(name)
Esempio n. 19
0
 def __init__(self):
     super().__init__()
     self.tempd = tempfile.mkdtemp()
     self.database = DirectoryBasedExampleDatabase(self.tempd)
     self.model = defaultdict(set)
def test_does_not_error_when_fetching_when_not_exist(tmpdir):
    db = DirectoryBasedExampleDatabase(tmpdir.join('examples'))
    db.fetch(b'foo')
def test_selects_directory_based_if_already_directory(tmpdir):
    path = str(tmpdir.join('hi.sqlite3'))
    DirectoryBasedExampleDatabase(path).save(b"foo", b"bar")
    assert isinstance(ExampleDatabase(path), DirectoryBasedExampleDatabase)
Esempio n. 22
0
def exampledatabase(request, tmpdir):
    if request.param == "memory":
        return ExampleDatabase()
    assert request.param == "directory"
    return DirectoryBasedExampleDatabase(str(tmpdir.join("examples")))
def test_does_not_error_when_fetching_when_not_exist(tmpdir):
    db = DirectoryBasedExampleDatabase(tmpdir.join('examples'))
    db.fetch(b'foo')
Esempio n. 24
0
 def __init__(self):
     super(DatabaseComparison, self).__init__()
     writer.writerow(['init_state'])
     self.tempd = tempfile.mkdtemp()
     self.database = DirectoryBasedExampleDatabase(self.tempd)
     self.model = defaultdict(set)