Debian Codegraph @

hypothesis-python/tests/nocover/test_database_usage.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 pytest
12
13 from hypothesis import assume, core, find, given, settings, strategies as st
14 from hypothesis.database import (
15 DirectoryBasedExampleDatabase,
16 GitHubArtifactDatabase,
17 InMemoryExampleDatabase,
18 ReadOnlyDatabase,
19 )
20 from hypothesis.errors import NoSuchExample, Unsatisfiable
21
22 from tests.common.utils import (
23 Why,
24 all_values,
25 non_covering_examples,
26 skipif_threading,
27 xfail_on_crosshair,
28 )
29
30
31 def has_a_non_zero_byte(x):
32 return any(bytes(x))
33
34
35 def test_saves_incremental_steps_in_database():
36 key = b"a database key"
37 database = InMemoryExampleDatabase()
38 find(
39 st.binary(min_size=10),
40 has_a_non_zero_byte,
41 settings=settings(database=database),
42 database_key=key,
43 )
44 assert len(all_values(database)) > 1
45
46
47 @xfail_on_crosshair(Why.symbolic_outside_context, strict=False)
48 def test_clears_out_database_as_things_get_boring():
49 key = b"a database key"
50 database = InMemoryExampleDatabase()
51 do_we_care = True
52
53 def stuff():
54 try:
55 find(
56 st.binary(min_size=50),
57 lambda x: do_we_care and has_a_non_zero_byte(x),
58 settings=settings(database=database, max_examples=10),
59 database_key=key,
60 )
61 except NoSuchExample:
62 pass
63
64 stuff()
65 assert len(non_covering_examples(database)) > 1
66 do_we_care = False
67 stuff()
68 initial = len(non_covering_examples(database))
69 assert initial > 0
70
71 for _ in range(initial):
72 stuff()
73 keys = len(non_covering_examples(database))
74 if not keys:
75 break
76 else:
77 raise AssertionError
78
79
80 @xfail_on_crosshair(Why.other, strict=False)
81 def test_trashes_invalid_examples():
82 database = InMemoryExampleDatabase()
83
84 invalid = set()
85
86 def stuff():
87 try:
88
89 def condition(x):
90 assume(x not in invalid)
91 return not invalid and has_a_non_zero_byte(x)
92
93 return find(
94 st.binary(min_size=5),
95 condition,
96 settings=settings(database=database),
97 database_key=b"a database key",
98 )
99 except (Unsatisfiable, NoSuchExample):
100 pass
101
102 value = stuff()
103
104 original = len(all_values(database))
105 assert original > 1
106
107 invalid.add(value)
108 stuff()
109 assert len(all_values(database)) < original
110
111
112 @pytest.mark.skipif(
113 settings.get_current_profile_name() == "crosshair",
114 reason="condition is easy for crosshair, stops early",
115 )
116 def test_respects_max_examples_in_database_usage():
117 database = InMemoryExampleDatabase()
118 do_we_care = True
119 counter = 0
120
121 def check(x):
122 nonlocal counter
123 counter += 1
124 return do_we_care and has_a_non_zero_byte(x)
125
126 def stuff():
127 try:
128 find(
129 st.binary(min_size=100),
130 check,
131 settings=settings(database=database, max_examples=10),
132 database_key=b"a database key",
133 )
134 except NoSuchExample:
135 pass
136
137 stuff()
138 assert len(all_values(database)) > 10
139
140 do_we_care = False
141 counter = 0
142 stuff()
143
144 assert counter == 10
145
146
147 def test_does_not_use_database_when_seed_is_forced(monkeypatch):
148 monkeypatch.setattr(core, "global_force_seed", 42)
149 database = InMemoryExampleDatabase()
150 database.fetch = None # type: ignore
151
152 @settings(database=database)
153 @given(st.integers())
154 def test(i):
155 pass
156
157 test()
158
159
160 @skipif_threading # pytest .mktemp is not thread safe
161 @given(st.binary(), st.binary())
162 def test_database_not_created_when_not_used(tmp_path_factory, key, value):
163 path = tmp_path_factory.mktemp("hypothesis") / "examples"
164 assert not path.exists()
165 database = DirectoryBasedExampleDatabase(path)
166 assert not list(database.fetch(key))
167 assert not path.exists()
168 database.save(key, value)
169 assert path.exists()
170 assert list(database.fetch(key)) == [value]
171
172
173 @skipif_threading
174 def test_ga_database_not_created_when_not_used(tmp_path_factory):
175 path = tmp_path_factory.mktemp("hypothesis") / "github-actions"
176 assert not path.exists()
177 ReadOnlyDatabase(GitHubArtifactDatabase("mock", "mock", path=path))
178 assert not path.exists()