本文整理汇总了Python中hypothesis.internal.conjecture.data.TestData.for_buffer方法的典型用法代码示例。如果您正苦于以下问题:Python TestData.for_buffer方法的具体用法?Python TestData.for_buffer怎么用?Python TestData.for_buffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hypothesis.internal.conjecture.data.TestData
的用法示例。
在下文中一共展示了TestData.for_buffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_draw_past_end_sets_overflow
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_draw_past_end_sets_overflow():
x = TestData.for_buffer(bytes(5))
with pytest.raises(StopTest) as e:
x.draw_bytes(6)
assert e.value.testcounter == x.testcounter
assert x.frozen
assert x.status == Status.OVERRUN
示例2: incorporate_new_buffer
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def incorporate_new_buffer(self, buffer):
if buffer in self.seen:
return False
assert self.last_data.status == Status.INTERESTING
if (
self.settings.timeout > 0 and
time.time() >= self.start_time + self.settings.timeout
):
raise RunIsComplete()
self.examples_considered += 1
buffer = buffer[:self.last_data.index]
if sort_key(buffer) >= sort_key(self.last_data.buffer):
return False
assert sort_key(buffer) <= sort_key(self.last_data.buffer)
data = TestData.for_buffer(buffer)
self.test_function(data)
data.freeze()
self.note_for_corpus(data)
if self.consider_new_test_data(data):
self.shrinks += 1
self.last_data = data
if self.shrinks >= self.settings.max_shrinks:
raise RunIsComplete()
self.last_data = data
self.changed += 1
return True
return False
示例3: test_variadic_draw
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_variadic_draw():
def draw_list(data):
result = []
while True:
data.start_example()
d = data.draw_bytes(1)[0] & 7
if d:
result.append(data.draw_bytes(d))
data.stop_example()
if not d:
break
return result
@run_to_buffer
def b(data):
if any(all(d) for d in draw_list(data)):
data.mark_interesting()
l = draw_list(TestData.for_buffer(b))
assert len(l) == 1
assert len(l[0]) == 1
示例4: test_notes_repr
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_notes_repr():
x = TestData.for_buffer(b'')
x.note(b'hi')
assert repr(b'hi') in x.output
示例5: test_can_draw_zero_bytes
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_can_draw_zero_bytes():
x = TestData.for_buffer(b'')
for _ in range(10):
assert x.draw_bytes(0) == b''
示例6: test_can_double_freeze
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_can_double_freeze():
x = TestData.for_buffer(b'hi')
x.freeze()
assert x.frozen
x.freeze()
assert x.frozen
示例7: test_cannot_draw_after_freeze
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_cannot_draw_after_freeze():
x = TestData.for_buffer(b'hi')
x.draw_bytes(1)
x.freeze()
with pytest.raises(Frozen):
x.draw_bytes(1)
示例8: test_closes_interval_on_error_in_strategy
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_closes_interval_on_error_in_strategy():
x = TestData.for_buffer(b'hi')
with pytest.raises(ValueError):
x.draw(BoomStrategy())
x.freeze()
assert len(x.intervals) == 1
示例9: find
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def find(specifier, condition, settings=None, random=None, database_key=None):
settings = settings or Settings(max_examples=2000, min_satisfying_examples=0, max_shrinks=2000)
if database_key is None and settings.database is not None:
database_key = function_digest(condition)
if not isinstance(specifier, SearchStrategy):
raise InvalidArgument("Expected SearchStrategy but got %r of type %s" % (specifier, type(specifier).__name__))
search = specifier
random = random or new_random()
successful_examples = [0]
last_data = [None]
def template_condition(data):
with BuildContext():
try:
data.is_find = True
result = data.draw(search)
data.note(result)
success = condition(result)
except UnsatisfiedAssumption:
data.mark_invalid()
if success:
successful_examples[0] += 1
if settings.verbosity == Verbosity.verbose:
if not successful_examples[0]:
report(lambda: u"Trying example %s" % (nicerepr(result),))
elif success:
if successful_examples[0] == 1:
report(lambda: u"Found satisfying example %s" % (nicerepr(result),))
else:
report(lambda: u"Shrunk example to %s" % (nicerepr(result),))
last_data[0] = data
if success and not data.frozen:
data.mark_interesting()
from hypothesis.internal.conjecture.engine import TestRunner
from hypothesis.internal.conjecture.data import TestData, Status
start = time.time()
runner = TestRunner(template_condition, settings=settings, random=random, database_key=database_key)
runner.run()
run_time = time.time() - start
if runner.last_data.status == Status.INTERESTING:
with BuildContext():
return TestData.for_buffer(runner.last_data.buffer).draw(search)
if runner.valid_examples <= settings.min_satisfying_examples:
if settings.timeout > 0 and run_time > settings.timeout:
raise Timeout(
(
"Ran out of time before finding enough valid examples for "
"%s. Only %d valid examples found in %.2f seconds."
)
% (get_pretty_function_description(condition), runner.valid_examples, run_time)
)
else:
raise Unsatisfiable(
("Unable to satisfy assumptions of " "%s. Only %d examples considered satisfied assumptions")
% (get_pretty_function_description(condition), runner.valid_examples)
)
raise NoSuchExample(get_pretty_function_description(condition))
示例10: wrapped_test
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
#.........这里部分代码省略.........
)
% (count, runtime, filtered_draws, overruns),
HealthCheck.too_slow,
)
last_exception = [None]
repr_for_last_exception = [None]
def evaluate_test_data(data):
try:
result = test_runner(data, reify_and_execute(search_strategy, test))
if result is not None and settings.perform_health_check:
fail_health_check(
("Tests run under @given should return None, but " "%s returned %r instead.")
% (test.__name__, result),
HealthCheck.return_value,
)
return False
except UnsatisfiedAssumption:
data.mark_invalid()
except (HypothesisDeprecationWarning, FailedHealthCheck, StopTest):
raise
except Exception:
last_exception[0] = traceback.format_exc()
verbose_report(last_exception[0])
data.mark_interesting()
from hypothesis.internal.conjecture.engine import TestRunner
falsifying_example = None
database_key = str_to_bytes(fully_qualified_name(test))
start_time = time.time()
runner = TestRunner(evaluate_test_data, settings=settings, random=random, database_key=database_key)
runner.run()
run_time = time.time() - start_time
timed_out = settings.timeout > 0 and run_time >= settings.timeout
if runner.last_data is None:
return
if runner.last_data.status == Status.INTERESTING:
falsifying_example = runner.last_data.buffer
if settings.database is not None:
settings.database.save(database_key, falsifying_example)
else:
if runner.valid_examples < min(settings.min_satisfying_examples, settings.max_examples):
if timed_out:
raise Timeout(
(
"Ran out of time before finding a satisfying "
"example for "
"%s. Only found %d examples in " + "%.2fs."
)
% (get_pretty_function_description(test), runner.valid_examples, run_time)
)
else:
raise Unsatisfiable(
(
"Unable to satisfy assumptions of hypothesis "
"%s. Only %d examples considered "
"satisfied assumptions"
)
% (get_pretty_function_description(test), runner.valid_examples)
)
return
assert last_exception[0] is not None
try:
with settings:
test_runner(
TestData.for_buffer(falsifying_example),
reify_and_execute(search_strategy, test, print_example=True, is_final=True),
)
except (UnsatisfiedAssumption, StopTest):
report(traceback.format_exc())
raise Flaky(
"Unreliable assumption: An example which satisfied " "assumptions on the first run now fails it."
)
report("Failed to reproduce exception. Expected: \n" + last_exception[0])
filter_message = (
"Unreliable test data: Failed to reproduce a failure "
"and then when it came to recreating the example in "
"order to print the test data with a flaky result "
"the example was filtered out (by e.g. a "
"call to filter in your strategy) when we didn't "
"expect it to be."
)
try:
test_runner(
TestData.for_buffer(falsifying_example),
reify_and_execute(
search_strategy,
test_is_flaky(test, repr_for_last_exception[0]),
print_example=True,
is_final=True,
),
)
except (UnsatisfiedAssumption, StopTest):
raise Flaky(filter_message)
示例11: bc
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def bc():
return BuildContext(TD.for_buffer(b''))
示例12: _run
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def _run(self):
self.last_data = None
mutations = 0
start_time = time.time()
if (
self.settings.database is not None and
self.database_key is not None
):
corpus = sorted(
self.settings.database.fetch(self.database_key),
key=lambda d: (len(d), d)
)
for existing in corpus:
if self.valid_examples >= self.settings.max_examples:
return
if self.iterations >= max(
self.settings.max_iterations, self.settings.max_examples
):
return
data = TestData.for_buffer(existing)
self.test_function(data)
data.freeze()
self.last_data = data
if data.status < Status.VALID:
self.settings.database.delete(
self.database_key, existing)
elif data.status == Status.VALID:
# Incremental garbage collection! we store a lot of
# examples in the DB as we shrink: Those that stay
# interesting get kept, those that become invalid get
# dropped, but those that are merely valid gradually go
# away over time.
if self.random.randint(0, 2) == 0:
self.settings.database.delete(
self.database_key, existing)
else:
assert data.status == Status.INTERESTING
self.last_data = data
break
if Phase.generate in self.settings.phases:
if (
self.last_data is None or
self.last_data.status < Status.INTERESTING
):
self.new_buffer()
mutator = self._new_mutator()
while self.last_data.status != Status.INTERESTING:
if self.valid_examples >= self.settings.max_examples:
return
if self.iterations >= max(
self.settings.max_iterations, self.settings.max_examples
):
return
if (
self.settings.timeout > 0 and
time.time() >= start_time + self.settings.timeout
):
return
if mutations >= self.settings.max_mutations:
mutations = 0
self.new_buffer()
mutator = self._new_mutator()
else:
data = TestData(
draw_bytes=mutator,
max_length=self.settings.buffer_size
)
self.test_function(data)
data.freeze()
self.note_for_corpus(data)
prev_data = self.last_data
if self.consider_new_test_data(data):
self.last_data = data
if data.status > prev_data.status:
mutations = 0
else:
mutator = self._new_mutator()
mutations += 1
data = self.last_data
if data is None:
return
assert isinstance(data.output, text_type)
if self.settings.max_shrinks <= 0:
return
if Phase.shrink not in self.settings.phases:
return
if not self.last_data.buffer:
return
data = TestData.for_buffer(self.last_data.buffer)
self.test_function(data)
if data.status != Status.INTERESTING:
#.........这里部分代码省略.........
示例13: wrapped_test
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
#.........这里部分代码省略.........
if (
initial_state is not None and
getglobalrandomstate() != initial_state
):
fail_health_check(
'Your test used the global random module. '
'This is unlikely to work correctly. You should '
'consider using the randoms() strategy from '
'hypothesis.strategies instead. Alternatively, '
'you can use the random_module() strategy to '
'explicitly seed the random module.')
from hypothesis.internal.conjecture.engine import TestRunner
falsifying_example = None
database_key = str_to_bytes(fully_qualified_name(test))
start_time = time.time()
runner = TestRunner(
evaluate_test_data,
settings=settings, random=random,
database_key=database_key,
)
runner.run()
run_time = time.time() - start_time
timed_out = (
settings.timeout > 0 and
run_time >= settings.timeout
)
if runner.last_data.status == Status.INTERESTING:
falsifying_example = runner.last_data.buffer
if settings.database is not None:
settings.database.save(
database_key, falsifying_example
)
else:
if runner.valid_examples < min(
settings.min_satisfying_examples,
settings.max_examples,
):
if timed_out:
raise Timeout((
'Ran out of time before finding a satisfying '
'example for '
'%s. Only found %d examples in ' +
'%.2fs.'
) % (
get_pretty_function_description(test),
runner.valid_examples, run_time
))
else:
raise Unsatisfiable((
'Unable to satisfy assumptions of hypothesis '
'%s. Only %d examples considered '
'satisfied assumptions'
) % (
get_pretty_function_description(test),
runner.valid_examples,))
return
assert last_exception[0] is not None
try:
with settings:
test_runner(
TestData.for_buffer(falsifying_example),
reify_and_execute(
search_strategy, test,
print_example=True, is_final=True
))
except (UnsatisfiedAssumption, StopTest):
report(traceback.format_exc())
raise Flaky(
'Unreliable assumption: An example which satisfied '
'assumptions on the first run now fails it.'
)
report(
'Failed to reproduce exception. Expected: \n' +
last_exception[0],
)
filter_message = (
'Unreliable test data: Failed to reproduce a failure '
'and then when it came to recreating the example in '
'order to print the test data with a flaky result '
'the example was filtered out (by e.g. a '
'call to filter in your strategy) when we didn\'t '
'expect it to be.'
)
try:
test_runner(
TestData.for_buffer(falsifying_example),
reify_and_execute(
search_strategy,
test_is_flaky(test, repr_for_last_exception[0]),
print_example=True, is_final=True
))
except (UnsatisfiedAssumption, StopTest):
raise Flaky(filter_message)
示例14: test_can_mark_interesting
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_can_mark_interesting():
x = TestData.for_buffer(bytes())
with pytest.raises(StopTest):
x.mark_interesting()
assert x.frozen
assert x.status == Status.INTERESTING
示例15: test_does_not_double_freeze_in_interval_close
# 需要导入模块: from hypothesis.internal.conjecture.data import TestData [as 别名]
# 或者: from hypothesis.internal.conjecture.data.TestData import for_buffer [as 别名]
def test_does_not_double_freeze_in_interval_close():
x = TestData.for_buffer(b'hi')
with pytest.raises(StopTest):
x.draw(BigStrategy())
assert x.frozen
assert len(x.intervals) == 0