本文整理汇总了Python中hypothesis.database.ExampleDatabase类的典型用法代码示例。如果您正苦于以下问题:Python ExampleDatabase类的具体用法?Python ExampleDatabase怎么用?Python ExampleDatabase使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExampleDatabase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_will_handle_a_really_weird_failure
def test_will_handle_a_really_weird_failure(self, rnd):
db = ExampleDatabase()
@given(
specifier,
settings=Settings(
database=db,
max_examples=max_examples,
min_satisfying_examples=2,
average_list_length=2.0,
), random=rnd
)
def nope(x):
s = hashlib.sha1(repr(x).encode(u'utf-8')).digest()
assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
if Random(s).randint(0, 1):
raise Rejected(u'%r with digest %r' % (
x, s
))
try:
try:
nope()
except Rejected:
pass
try:
nope()
except Rejected:
pass
finally:
db.close()
示例2: test_garbage_collects_the_database
def test_garbage_collects_the_database():
key = b'hi there'
n = 200
db = ExampleDatabase(':memory:')
assert list(db.fetch(key)) == []
seen = set()
go = True
def f(data):
x = hbytes(data.draw_bytes(512))
if not go:
return
if sum(x) >= 5000 and len(seen) < n:
seen.add(x)
if x in seen:
data.mark_interesting()
runner = TestRunner(
f, settings=settings(database=db, max_shrinks=2 * n), database_key=key)
runner.run()
assert runner.last_data.status == Status.INTERESTING
assert len(seen) == n
assert set(db.fetch(key)) == seen
go = False
runner = TestRunner(
f, settings=settings(database=db, max_shrinks=2 * n), database_key=key)
runner.run()
assert 0 < len(set(db.fetch(key))) < n
示例3: test_saves_data_while_shrinking
def test_saves_data_while_shrinking():
key = b'hi there'
n = 5
db = ExampleDatabase(':memory:')
assert list(db.fetch(key)) == []
seen = set()
def f(data):
x = data.draw_bytes(512)
if sum(x) >= 5000 and len(seen) < n:
seen.add(hbytes(x))
if hbytes(x) in seen:
data.mark_interesting()
runner = ConjectureRunner(
f, settings=settings(database=db), database_key=key)
runner.run()
assert runner.last_data.status == Status.INTERESTING
assert len(seen) == n
in_db = set(
v
for vs in db.data.values()
for v in vs
)
assert in_db.issubset(seen)
assert in_db == seen
示例4: test_storage_cleans_up_invalid_data_from_the_db
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)) == []
示例5: test_will_handle_a_really_weird_failure
def test_will_handle_a_really_weird_failure(self, s):
db = ExampleDatabase()
@given(specifier)
@Settings(
settings,
database=db,
max_examples=max_examples,
min_satisfying_examples=2,
)
@seed(s)
def nope(x):
s = hashlib.sha1(repr(x).encode('utf-8')).digest()
assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
if Random(s).randint(0, 1):
raise Rejected('%r with digest %r' % (
x, s
))
try:
try:
nope()
except Rejected:
pass
try:
nope()
except Rejected:
pass
finally:
db.close()
示例6: via_database
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]
示例7: test_storage_does_not_error_if_the_database_is_invalid
def test_storage_does_not_error_if_the_database_is_invalid():
database = ExampleDatabase()
strat = integers()
key = u'wtf'
ints = database.storage(key)
database.backend.save(key, u'["hi", "there"]')
assert list(ints.fetch(strat)) == []
示例8: test_will_handle_a_really_weird_failure
def test_will_handle_a_really_weird_failure(self):
db = ExampleDatabase()
@given(
specifier,
settings=Settings(
database=db,
max_examples=max_examples,
min_satisfying_examples=2,
average_list_length=2.0,
)
)
def nope(x):
s = hashlib.sha1(show(x).encode('utf-8')).digest()
if Random(s).randint(0, 1):
raise Rejected()
try:
try:
nope()
except Rejected:
pass
try:
nope()
except Rejected:
pass
finally:
db.close()
示例9: test_can_round_trip_through_the_database
def test_can_round_trip_through_the_database(self, template, rnd):
empty_db = ExampleDatabase(backend=SQLiteBackend(":memory:"))
try:
storage = empty_db.storage("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()
示例10: via_database
def via_database(spec, strat, template):
db = ExampleDatabase()
key = u'via_database'
try:
s = db.storage(key)
s.save(template, strat)
results = list(s.fetch(strat))
assert len(results) == 1
return results[0]
finally:
db.close()
示例11: test_will_find_a_failure_from_the_database
def test_will_find_a_failure_from_the_database(self):
db = ExampleDatabase()
@given(specifier, settings=Settings(max_examples=10, database=db))
def nope(x):
raise Rejected()
try:
for i in hrange(3):
self.assertRaises(Rejected, nope) # pragma: no cover
finally:
db.close()
示例12: test_can_round_trip_through_the_database
def test_can_round_trip_through_the_database(self, template):
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()
示例13: run_round_trip
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()
示例14: test_can_load_data_from_a_corpus
def test_can_load_data_from_a_corpus():
key = b'hi there'
db = ExampleDatabase()
value = b'=\xc3\xe4l\x81\xe1\xc2H\xc9\xfb\x1a\xb6bM\xa8\x7f'
db.save(key, value)
def f(data):
if data.draw_bytes(len(value)) == value:
data.mark_interesting()
runner = ConjectureRunner(
f, settings=settings(database=db), database_key=key)
runner.run()
last_data, = runner.interesting_examples.values()
assert last_data.buffer == value
assert len(list(db.fetch(key))) == 1
示例15: test_will_find_a_failure_from_the_database
def test_will_find_a_failure_from_the_database():
db = ExampleDatabase()
class Rejected(Exception):
pass
@given(dav_strategy, settings=Settings(max_examples=10, database=db))
def nope(x):
raise Rejected()
try:
with pytest.raises(Rejected):
nope() # pragma: no branch
finally:
db.close()