本文整理汇总了Python中binding.Context类的典型用法代码示例。如果您正苦于以下问题:Python Context类的具体用法?Python Context怎么用?Python Context使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_test_templated_headers
def test_parse_test_templated_headers(self):
""" Test parsing with templated headers """
heads = {"Accept":"Application/json", "$AuthHeader":"$AuthString"}
templated_heads = {"Accept":"Application/json", "apikey":"magic_passWord"}
context = Context()
context.bind_variables({'AuthHeader': 'apikey', 'AuthString':'magic_passWord'})
# If this doesn't throw errors we have silent failures
input_invalid = {"url": "/ping", "method": "DELETE", "NAME":"foo", "group":"bar", "body":"<xml>input</xml>","headers": 'goat'}
try:
test = Test.parse_test('', input_invalid)
test.fail("Expected error not thrown")
except TypeError:
pass
def assert_dict_eq(dict1, dict2):
""" Test dicts are equal """
self.assertEqual(2, len(set(dict1.items()) & set(dict2.items())))
# Before templating is used
input = {"url": "/ping", "method": "DELETE", "NAME":"foo", "group":"bar", "body":"<xml>input</xml>","headers": heads}
test = Test.parse_test('', input)
assert_dict_eq(heads, test.headers)
assert_dict_eq(heads, test.get_headers(context=context))
# After templating applied
input_templated = {"url": "/ping", "method": "DELETE", "NAME":"foo", "group":"bar", "body":"<xml>input</xml>","headers": {'tEmplate': heads}}
test2 = Test.parse_test('', input_templated)
assert_dict_eq(heads, test2.get_headers())
assert_dict_eq(templated_heads, test2.get_headers(context=context))
示例2: test_content_file_template
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))
示例3: test_abstract_extractor_readableconfig
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))
示例4: test_header_templating
def test_header_templating(self):
test = Test()
head_templated = {'$key': "$val"}
context = Context()
context.bind_variables({'key': 'cheese', 'val': 'gouda'})
# No templating applied
test.headers = head_templated
head = test.get_headers()
self.assertEqual(1, len(head))
self.assertEqual('$val', head['$key'])
test.set_headers(head_templated, isTemplate=True)
self.assertTrue(test.templates)
self.assertTrue(test.NAME_HEADERS in test.templates)
# No context, no templating
head = test.headers
self.assertEqual(1, len(head))
self.assertEqual('$val', head['$key'])
# Templated with context
head = test.get_headers(context=context)
self.assertEqual(1, len(head))
self.assertEqual('gouda', head['cheese'])
示例5: test_update_context_variables
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'))
示例6: test_parse_content_templated
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_abstract_extractor_templating
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))
示例8: test_test_content_templating
def test_test_content_templating(self):
test = Test()
handler = ContentHandler()
handler.is_template_content = True
handler.content = '{"first_name": "Gaius","id": "$id","last_name": "Baltar","login": "$login"}'
context = Context()
context.bind_variables({'id': 9, 'login': 'kvothe'})
test.set_body(handler)
templated = test.realize(context=context)
self.assertEqual(string.Template(handler.content).safe_substitute(context.get_values()),
templated.body)
示例9: test_parse_validator
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_test_url_templating
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)
示例11: test_content_templating
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())
示例12: test_variable_binding
def test_variable_binding(self):
""" Test that tests successfully bind variables """
element = 3
input = [{"url": "/ping"},{"name": "cheese"},{"expected_status":["200",204,"202"]}]
input.append({"variable_binds":{'var':'value'}})
test = Test.parse_test('', input)
binds = test.variable_binds
self.assertEqual(1, len(binds))
self.assertEqual('value', binds['var'])
# Test that updates context correctly
context = Context()
test.update_context_before(context)
self.assertEqual('value', context.get_value('var'))
self.assertTrue(test.is_context_modifier())
示例13: test_mixing_binds
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)
示例14: test_parse_validator
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)
示例15: test_validator_comparator_templating
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))