本文整理汇总了Python中hypothesis.given方法的典型用法代码示例。如果您正苦于以下问题:Python hypothesis.given方法的具体用法?Python hypothesis.given怎么用?Python hypothesis.given使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hypothesis
的用法示例。
在下文中一共展示了hypothesis.given方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_single_example
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def get_single_example(strategy: st.SearchStrategy[Case]) -> Case:
@hypothesis.given(strategy) # type: ignore
@hypothesis.settings( # type: ignore
database=None,
max_examples=1,
deadline=None,
verbosity=hypothesis.Verbosity.quiet,
phases=(hypothesis.Phase.generate,),
suppress_health_check=hypothesis.HealthCheck.all(),
)
def example_generating_inner_function(ex: Case) -> None:
examples.append(ex)
examples: List[Case] = []
example_generating_inner_function()
return examples[0]
示例2: test_multiple_hooks_per_spec
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_multiple_hooks_per_spec(schema):
@schema.hooks.register("before_generate_query")
def first_hook(context, strategy):
return strategy.filter(lambda x: x["id"].isdigit())
@schema.hooks.register("before_generate_query")
def second_hook(context, strategy):
return strategy.filter(lambda x: int(x["id"]) % 2 == 0)
assert schema.hooks.get_all_by_name("before_generate_query") == [first_hook, second_hook]
strategy = schema.endpoints["/custom_format"]["GET"].as_strategy()
@given(case=strategy)
@settings(max_examples=3)
def test(case):
assert case.query["id"].isdigit()
assert int(case.query["id"]) % 2 == 0
test()
示例3: test_invalid_body_in_get_disable_validation
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_invalid_body_in_get_disable_validation(simple_schema):
schema = schemathesis.from_dict(simple_schema, validate_schema=False)
endpoint = Endpoint(
path="/foo",
method="GET",
definition=EndpointDefinition({}, {}, "foo"),
schema=schema,
body={"required": ["foo"], "type": "object", "properties": {"foo": {"type": "string"}}},
)
strategy = get_case_strategy(endpoint)
@given(strategy)
@settings(max_examples=1)
def test(case):
assert case.body is not None
test()
示例4: test_valid_headers
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_valid_headers(base_url, swagger_20, definition):
endpoint = Endpoint(
"/api/success",
"GET",
definition=EndpointDefinition({}, {}, "foo"),
schema=swagger_20,
base_url=base_url,
headers={
"properties": {"api_key": definition},
"additionalProperties": False,
"type": "object",
"required": ["api_key"],
},
)
@given(case=get_case_strategy(endpoint))
@settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], deadline=None, max_examples=10)
def inner(case):
case.call()
inner()
示例5: test_subscript_slice
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_subscript_slice():
program = '''
x = List[:]
'''
module, _ = cs._parse_text(program)
assign_node = next(module.nodes_of_class(astroid.Assign))
assert isinstance(assign_node.inf_type, TypeFail)
# TODO: this test needs to be converted, but will also fail
# @given(cs.random_list(min_size=2), cs.random_slice_indices())
# def test_subscript_heterogeneous_list_slice(input_list, slice):
# """Test visitor of Subscript node representing slicing of heterogeneous list."""
# assume(not isinstance(input_list[0], type(input_list[1])))
# input_slice = ':'.join([str(index) if index else '' for index in slice])
# program = f'{input_list}[{input_slice}]'
# module, _ = cs._parse_text(program)
# subscript_node = list(module.nodes_of_class(astroid.Subscript))[0]
# assert subscript_node.inf_type.getValue() == List[Any]
示例6: test_non_annotated_function_call_bad_arguments
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_non_annotated_function_call_bad_arguments():
""" User tries to call a non-annotated function on arguments of the wrong type.
"""
program = f'def add_num(num1, num2):\n' \
f' return num1 + num2\n' \
f'\n' \
f'add_num("bob", 1.0)\n'
try:
module, inferer = cs._parse_text(program)
except:
skip()
call_node = next(module.nodes_of_class(astroid.Call))
# TODO: This error is flawed because the unification error occurs for both arguments due to our current implementation,
# which "chooses" the first valid function type from TypeStore.
# Should we fix this implementation first or save it for later and hard-code the correct error message for now?
expected_msg = f'In the Call node in line 4, there was an error in calling the function "add_num":\n' \
f'in parameter (1), the function was expecting an object of inferred type ' \
f'int but was given an object of type str.\n' \
f'in parameter (2), the function was expecting an object of inferred type ' \
f'int but was given an object of type float.\n'
# TODO: should we use the term inferred?
assert call_node.inf_type.getValue() == expected_msg
示例7: test_user_defined_annotated_call_wrong_arguments_type
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_user_defined_annotated_call_wrong_arguments_type():
""" User tries to call an annotated user-defined function on the wrongly-typed arguments.
"""
program = f'def add_3(num1: int, num2: int, num3: int) -> int:\n' \
f' return num1 + num2 + num3\n' \
f'\n' \
f'add_3(1, "bob", 1.0)\n'
try:
module, inferer = cs._parse_text(program)
except:
skip()
call_node = list(module.nodes_of_class(astroid.Call))[0]
expected_msg = f'In the Call node in line 4, there was an error in calling the annotated function "add_3":\n' \
f'in parameter (2), the annotated type is int but was given an object of type str.\n' \
f'in parameter (3), the annotated type is int but was given an object of type float.\n'
assert call_node.inf_type.getValue() == expected_msg
示例8: test_conflicting_inferred_type_variable
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_conflicting_inferred_type_variable():
""" User calls two functions on an object, which contradicts the inferred type of the variable.
"""
program = '''
def return_num(num: int) -> int:
return num
def return_str(str: str) -> str:
return str
def f(x):
return_num(x)
return_str(x)
'''
try:
module, inferer = cs._parse_text(program)
except:
skip()
call_node = list(module.nodes_of_class(astroid.Call))[1]
expected_msg = f'In the Call node in line 8, there was an error in calling the annotated function "return_str":\n' \
f'in parameter (1), the annotated type is str but was given an object of inferred type int.'
# TODO: test case redundant because recursive..?
assert call_node.inf_type.getValue() == expected_msg
示例9: test_log_result_too_low_level
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_log_result_too_low_level(self):
"""
A LoggedMessageId matching the logged message is returned by logging APIs
even when the given level is low enough that a message wasn't sent to
the MCP.
"""
mdk, tracer = create_mdk_with_faketracer()
session = mdk.session()
session.info("cat", "message")
lmid = session.debug("cat", "another message")
lmid2 = session.info("cat", "message")
# Debug message wasn't set:
self.assertEqual([d["level"] for d in tracer.messages],
["INFO", "INFO"])
# But we still got LoggedMessageId for debug message:
self.assertEqual((lmid.causalLevel, lmid.traceId,
lmid2.causalLevel, lmid2.traceId),
([2], session._context.traceId,
[3], session._context.traceId))
示例10: from_schema
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def from_schema(schema):
"""Returns a strategy for objects that match the given schema."""
check_schema(schema)
# TODO: actually handle constraints on number/string/array schemas
return dict(
null=st.none(),
bool=st.booleans(),
number=st.floats(allow_nan=False),
string=st.text(),
array=st.lists(st.nothing()),
)[schema["type"]]
# `@st.composite` is one way to write this - another would be to define a
# bare function, and `return st.one_of(st.none(), st.booleans(), ...)` so
# each strategy can be defined individually. Use whichever seems more
# natural to you - the important thing in tests is usually readability!
示例11: test_bitslist_nested_user_strategy
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_bitslist_nested_user_strategy():
type_ = [ [Bits10, Bits11, Bits12], Bits13 ]
limit_dict = {
1: pst.bits(13, min_value=0, max_value=10)
}
print("")
@hypothesis.given(
blist = pst.bitslists(type_, limit_dict)
)
@hypothesis.settings( max_examples=16 )
def actual_test( blist ):
assert blist[0][0].nbits == 10
assert blist[0][1].nbits == 11
assert blist[0][2].nbits == 12
assert blist[1].nbits == 13
assert 0 <= blist[1] <= 10
print(blist)
actual_test()
示例12: test_nested_point_limited
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_nested_point_limited():
limit_dict = {
'p1': {
'x': range(0xe0,0xf0),
},
'p2': {
'y': range(0xf0,0x100),
}
}
print("")
@hypothesis.given(
bs = pst.bitstructs(NestedPoint, limit_dict)
)
@hypothesis.settings( max_examples=16 )
def actual_test( bs ):
assert isinstance( bs, NestedPoint )
assert 0xe0 <= bs.p1.x <= 0xef
assert 0xf0 <= bs.p2.y <= 0xff
print( bs )
actual_test()
示例13: test_manifestations_from_applications
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_manifestations_from_applications(self):
"""
One cannot construct a ``Node`` where there are manifestations on the
``applications`` attribute that aren't also in the given
``manifestations``.
"""
m1 = Manifestation(dataset=Dataset(dataset_id=unicode(uuid4())),
primary=True)
self.assertRaises(
InvariantException, Node,
hostname=u'node1.example.com',
applications={a.name: a for a in [
APP1,
Application(name=u'a',
image=DockerImage.from_string(u'x'),
volume=AttachedVolume(
manifestation=m1,
mountpoint=FilePath(b"/xxx"))),
]})
示例14: assert_required_field_set
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def assert_required_field_set(self, **fields):
"""
Assert that if one of the given field names is set on a ``NodeState``,
all of them must be set or this will be consideted an invariant
violation.
:param fields: ``NodeState`` attributes that are all expected to
be settable as a group, but which cannot be missing if one of
the others is set.
"""
# If all are set, no problems:
NodeState(hostname=u"127.0.0.1", uuid=uuid4(), **fields)
# If one is missing, an invariant is raised:
for name in fields:
remaining_fields = fields.copy()
del remaining_fields[name]
self.assertRaises(InvariantException, NodeState,
hostname=u"127.0.0.1", uuid=uuid4(),
**remaining_fields)
示例15: test_update_node_new
# 需要导入模块: import hypothesis [as 别名]
# 或者: from hypothesis import given [as 别名]
def test_update_node_new(self):
"""
When doing ``update_node()``, if the given ``Node`` has hostname not
in existing ``Deployment`` then just add new ``Node`` to new
``Deployment``.
"""
node = Node(
hostname=u"node1.example.com",
applications={
u'postgresql-clusterhq': Application(
name=u'postgresql-clusterhq',
image=DockerImage.from_string(u"image"))})
another_node = Node(
hostname=u"node2.example.com",
applications={
u'site-clusterhq.com': Application(
name=u'site-clusterhq.com',
image=DockerImage.from_string(u"image"))},
)
original = Deployment(nodes=frozenset([node]))
updated = original.update_node(another_node)
self.assertEqual((original, updated),
(Deployment(nodes=frozenset([node])),
Deployment(nodes=frozenset([node, another_node]))))