Debian Codegraph @

hypothesis-python/tests/nocover/test_database_agreement.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import os
12 import shutil
13 import tempfile
14
15 import pytest
16
17 from hypothesis import settings, strategies as st
18 from hypothesis.database import (
19 BackgroundWriteDatabase,
20 DirectoryBasedExampleDatabase,
21 InMemoryExampleDatabase,
22 )
23 from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule
24
25
26 class DatabaseComparison(RuleBasedStateMachine):
27 def __init__(self):
28 super().__init__()
29 self.tempd = tempfile.mkdtemp()
30 exampledir = os.path.join(self.tempd, "examples")
31
32 self.dbs = [
33 InMemoryExampleDatabase(),
34 DirectoryBasedExampleDatabase(exampledir),
35 BackgroundWriteDatabase(InMemoryExampleDatabase()),
36 ]
37
38 keys = Bundle("keys")
39 values = Bundle("values")
40
41 @rule(target=keys, k=st.binary())
42 def k(self, k):
43 return k
44
45 @rule(target=values, v=st.binary())
46 def v(self, v):
47 return v
48
49 @rule(k=keys, v=values)
50 def save(self, k, v):
51 for db in self.dbs:
52 db.save(k, v)
53
54 @rule(k=keys, v=values)
55 def delete(self, k, v):
56 for db in self.dbs:
57 db.delete(k, v)
58
59 @rule(k1=keys, k2=keys, v=values)
60 def move(self, k1, k2, v):
61 for db in self.dbs:
62 db.move(k1, k2, v)
63
64 @rule(k=keys)
65 def values_agree(self, k):
66 last = None
67 last_db = None
68 for db in self.dbs:
69 keys = set(db.fetch(k))
70 if last is not None:
71 assert last == keys, (last_db, db)
72 last = keys
73 last_db = db
74
75 def teardown(self):
76 shutil.rmtree(self.tempd)
77
78
79 @pytest.mark.skipif(
80 settings.get_current_profile_name() == "crosshair", reason="isn't threadsafe"
81 )
82 def test_database_equivalence():
83 DatabaseComparison.TestCase().runTest()