本文整理汇总了Python中binding.Context.bind_variable方法的典型用法代码示例。如果您正苦于以下问题:Python Context.bind_variable方法的具体用法?Python Context.bind_variable怎么用?Python Context.bind_variable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类binding.Context
的用法示例。
在下文中一共展示了Context.bind_variable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_content_file_template
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_content_file_template(self):
""" Test file read and templating of read files in this directory """
variables = {'id':1, 'login':'thewizard'}
context = Context()
file_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(file_path, 'person_body_template.json')
file_content = None
with open(file_path, 'r') as f:
file_content = f.read()
# Test basic read
handler = ContentHandler()
handler.setup(file_path, is_file=True)
self.assertEqual(file_content, handler.get_content())
# Test templating of read content
handler.setup(file_path, is_file=True, is_template_content=True)
self.assertEqual(file_content, handler.get_content())
self.assertEqual(file_content, handler.get_content(context)) # No substitution
substituted = string.Template(file_content).safe_substitute(variables)
context.bind_variables(variables)
self.assertEqual(substituted, handler.get_content(context))
# Test path templating
templated_file_path = '$filepath'
context.bind_variable('filepath', file_path)
handler.setup(file_path, is_file=True, is_template_path=True)
self.assertEqual(file_content, handler.get_content(context))
# Test double templating with files
handler.setup(file_path, is_file=True, is_template_path=True, is_template_content=True)
self.assertEqual(substituted, handler.get_content(context=context))
示例2: test_abstract_extractor_readableconfig
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_abstract_extractor_readableconfig(self):
""" Test human-readable extractor config string output """
config = 'key.val'
extractor = validators.parse_extractor('jsonpath_mini', config)
expected_string = 'Extractor Type: jsonpath_mini, Query: "key.val", Templated?: False'
self.assertEqual(expected_string, extractor.get_readable_config())
# Check empty context & args uses okay
context = Context()
self.assertEqual(expected_string, extractor.get_readable_config(context=context))
context.bind_variable('foo', 'bar')
self.assertEqual(expected_string, extractor.get_readable_config(context=context))
extractor.args = dict()
self.assertEqual(expected_string, extractor.get_readable_config(context=context))
# Check args output is handled correctly
extractor.args = {'caseSensitive': True}
self.assertEqual(expected_string+", Args: "+str(extractor.args), extractor.get_readable_config(context=context))
# Check template handling is okay
config = {'template': 'key.$templated'}
context.bind_variable('templated', 'val')
extractor = validators.parse_extractor('jsonpath_mini', config)
expected_string = 'Extractor Type: jsonpath_mini, Query: "key.val", Templated?: True'
self.assertEqual(expected_string, extractor.get_readable_config(context=context))
示例3: test_update_context_variables
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_update_context_variables(self):
test = Test()
context = Context()
context.bind_variable('foo', 'broken')
test.variable_binds = {'foo': 'correct', 'test': 'value'}
test.update_context_before(context)
self.assertEqual('correct', context.get_value('foo'))
self.assertEqual('value', context.get_value('test'))
示例4: test_mixing_binds
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_mixing_binds(self):
""" Ensure that variables are set correctly when mixing explicit declaration and variables """
context = Context()
context.add_generator('gen', count_gen())
context.bind_variable('foo', '100')
self.assertEqual(1, context.mod_count)
context.bind_generator_next('foo', 'gen')
self.assertEqual(1, context.get_value('foo'))
self.assertEqual(2, context.mod_count)
示例5: test_abstract_extractor_templating
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_abstract_extractor_templating(self):
""" Test that abstract extractors template the query """
ext = validators.AbstractExtractor()
ext.query = '$val.vee'
ext.is_templated = True
context = Context()
context.bind_variable('val', 'foo')
self.assertEqual('$val.vee', ext.templated_query())
self.assertEqual('foo.vee', ext.templated_query(context=context))
ext.is_templated = False
self.assertEqual('$val.vee', ext.templated_query(context=context))
示例6: test_parse_content_templated
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_parse_content_templated(self):
""" Test parsing of templated content """
node = {'template':'myval $var'}
handler = ContentHandler.parse_content(node)
context = Context()
context.bind_variable('var','cheese')
self.assertEqual(node['template'], handler.content)
self.assertEqual('myval cheese', handler.get_content(context))
self.assertTrue(handler.is_dynamic())
self.assertFalse(handler.is_file)
self.assertFalse(handler.is_template_path)
self.assertTrue(handler.is_template_content)
示例7: test_update_context_generators
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_update_context_generators(self):
""" Test updating context variables using generator """
test = Test()
context = Context()
context.bind_variable('foo', 'broken')
test.variable_binds = {'foo': 'initial_value'}
test.generator_binds = {'foo': 'gen'}
context.add_generator('gen', generators.generator_basic_ids())
test.update_context_before(context)
self.assertEqual(1, context.get_value('foo'))
test.update_context_before(context)
self.assertEqual(2, context.get_value('foo'))
示例8: test_test_url_templating
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_test_url_templating(self):
test = Test()
test.set_url('$cheese', isTemplate=True)
self.assertTrue(test.is_dynamic())
self.assertEqual('$cheese', test.get_url())
self.assertTrue(test.templates['url'])
context = Context()
context.bind_variable('cheese', 'stilton')
self.assertEqual('stilton', test.get_url(context=context))
realized = test.realize(context)
self.assertEqual('stilton', realized.url)
示例9: test_parse_validator
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_parse_validator(self):
""" Test basic parsing using registry """
config = {"jsonpath_mini": "key.val", "comparator": "eq", "expected": 3}
validator = validators.parse_validator("comparator", config)
myjson = '{"key": {"val": 3}}'
comp = validator.validate(body=myjson)
# Try it with templating
config["jsonpath_mini"] = {"template": "key.$node"}
validator = validators.parse_validator("comparator", config)
context = Context()
context.bind_variable("node", "val")
comp = validator.validate(myjson, context=context)
示例10: test_content_templating
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_content_templating(self):
""" Test content and templating of it """
handler = ContentHandler()
body = '$variable value'
context = Context()
context.bind_variable('variable', 'bar')
# No templating
handler.setup(body, is_template_content=False)
self.assertEqual(body, handler.get_content())
self.assertEqual(body, handler.get_content(context))
# Templating
handler.setup(body, is_template_content=True)
self.assertEqual(body, handler.get_content())
示例11: test_variables
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_variables(self):
""" Test bind/return of variables """
context = Context()
self.assertTrue(context.get_value('foo') is None)
self.assertEqual(0, context.mod_count)
context.bind_variable('foo','bar')
self.assertEqual('bar', context.get_value('foo'))
self.assertEqual('bar', context.get_values()['foo'])
self.assertEqual(1, context.mod_count)
context.bind_variable('foo','bar2')
self.assertEqual('bar2', context.get_value('foo'))
self.assertEqual(2, context.mod_count)
示例12: test_parse_validator
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_parse_validator(self):
""" Test basic parsing using registry """
config = {
'jsonpath_mini': 'key.val',
'comparator': 'eq',
'expected': 3
}
validator = validators.parse_validator('comparator', config)
myjson = '{"key": {"val": 3}}'
comp = validator.validate(body=myjson)
# Try it with templating
config['jsonpath_mini']={'template':'key.$node'}
validator = validators.parse_validator('comparator', config)
context = Context()
context.bind_variable('node','val')
comp = validator.validate(myjson, context=context)
示例13: test_validator_comparator_templating
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_validator_comparator_templating(self):
""" Try templating comparator validator """
config = {"jsonpath_mini": {"template": "key.$node"}, "comparator": "eq", "expected": 3}
context = Context()
context.bind_variable("node", "val")
myjson_pass = '{"id": 3, "key": {"val": 3}}'
myjson_fail = '{"id": 3, "key": {"val": 4}}'
comp = validators.ComparatorValidator.parse(config)
self.assertTrue(comp.validate(body=myjson_pass, context=context))
self.assertFalse(comp.validate(body=myjson_fail, context=context))
# Template expected
config["expected"] = {"template": "$id"}
context.bind_variable("id", 3)
self.assertTrue(comp.validate(body=myjson_pass, context=context))
self.assertFalse(comp.validate(body=myjson_fail, context=context))
示例14: test_parse_extractor_minijson
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_parse_extractor_minijson(self):
config = 'key.val'
extractor = validators.MiniJsonExtractor.parse(config)
myjson = '{"key": {"val": 3}}'
context = Context()
context.bind_variable('node', 'val')
extracted = extractor.extract(body=myjson)
self.assertEqual(3, extracted)
self.assertEqual(extracted, extractor.extract(body=myjson, context=context))
try:
val = extractor.extract(body='[31{]')
self.fail("Should throw exception on invalid JSON")
except ValueError:
pass
# Templating
config = {'template': 'key.$node'}
extract = validators.MiniJsonExtractor.parse(config)
self.assertEqual(3, extract.extract(myjson, context=context))
示例15: test_validator_comparator_templating
# 需要导入模块: from binding import Context [as 别名]
# 或者: from binding.Context import bind_variable [as 别名]
def test_validator_comparator_templating(self):
""" Try templating comparator validator """
config = {
'jsonpath_mini': {'template': 'key.$node'},
'comparator': 'eq',
'expected': 3
}
context = Context()
context.bind_variable('node', 'val')
myjson_pass = '{"id": 3, "key": {"val": 3}}'
myjson_fail = '{"id": 3, "key": {"val": 4}}'
comp = validators.ComparatorValidator.parse(config)
self.assertTrue(comp.validate(body=myjson_pass, context=context))
self.assertFalse(comp.validate(body=myjson_fail, context=context))
# Template expected
config['expected'] = {'template' : '$id'}
context.bind_variable('id', 3)
self.assertTrue(comp.validate(body=myjson_pass, context=context))
self.assertFalse(comp.validate(body=myjson_fail, context=context))