def test_serialize_iterator_of_frozen_dicts(self): data = iter( [ yaql_utils.FrozenDict(a=1, b=2, c=iter([1, 2, 3])), yaql_utils.FrozenDict( a=11, b=yaql_utils.FrozenDict(b='222'), c=iter( [ 1, yaql_utils.FrozenDict( a=iter([4, yaql_utils.FrozenDict(a=99)]) ) ] ) ) ] ) json_str = utils.to_json_str(data) self.assertIsNotNone(json_str) # Checking the first item. self.assertIn('"a": 1', json_str) self.assertIn('"b": 2', json_str) self.assertIn('"c": [1, 2, 3]', json_str) # Checking the first item. self.assertIn('"a": 11', json_str) self.assertIn('"b": {"b": "222"}', json_str) self.assertIn('"c": [1, {"a": [4, {"a": 99}]}]', json_str)
def test_serialize_dict_of_generators(self): def _f(cnt): for i in range(1, cnt + 1): yield i data = {'numbers': _f(3)} self.assertEqual('{"numbers": [1, 2, 3]}', utils.to_json_str(data))
def test_serialize_generator(self): def _list_stream(_list): for i in _list: yield i gen = _list_stream( [1, yaql_utils.FrozenDict(a=1), _list_stream([12, 15])] ) self.assertEqual('[1, {"a": 1}, [12, 15]]', utils.to_json_str(gen))
def test_serialize_frozen_dict(self): data = yaql_utils.FrozenDict(a=1, b=2, c=iter([1, 2, 3])) json_str = utils.to_json_str(data) self.assertIsNotNone(json_str) self.assertIn('"a": 1', json_str) self.assertIn('"b": 2', json_str) self.assertIn('"c": [1, 2, 3]', json_str)
def evaluate(cls, script, ctx): if not _PY_MINI_RACER: raise exc.MistralException( "PyMiniRacer module is not available. Please install " "PyMiniRacer." ) js_ctx = _PY_MINI_RACER.MiniRacer() return js_ctx.eval( '$ = {}; {}'.format(utils.to_json_str(ctx), script) )
def evaluate(cls, script, ctx): if not _V8EVAL: raise exc.MistralException( "v8eval module is not available. Please install v8eval." ) v8 = _V8EVAL.V8() ctx_str = utils.to_json_str(ctx) return v8.eval( ('$ = %s; %s' % (ctx_str, script)).encode(encoding='UTF-8') )
def evaluate(cls, script, ctx): if not _PYV8: raise exc.MistralException( "PyV8 module is not available. Please install PyV8." ) with _PYV8.JSContext() as js_ctx: # Prepare data context and way for interaction with it. js_ctx.eval('$ = %s' % utils.to_json_str(ctx)) result = js_ctx.eval(script) return _PYV8.convert(result)
def test_context_view_as_root_json(self): ctx = data_flow.ContextView( {'k1': 'v1'}, {'k2': 'v2'}, ) json_str = utils.to_json_str(ctx) self.assertIsNotNone(json_str) self.assertNotEqual('{}', json_str) # We can't use regular dict comparison because key order # is not defined. self.assertIn('"k1": "v1"', json_str) self.assertIn('"k2": "v2"', json_str)
def test_context_view_as_nested_json(self): ctx = data_flow.ContextView( {'k1': 'v1'}, {'k2': 'v2'}, ) d = {'root': ctx} json_str = utils.to_json_str(d) self.assertIsNotNone(json_str) self.assertNotEqual('{"root": {}}', json_str) # We can't use regular dict comparison because key order # is not defined. self.assertIn('"k1": "v1"', json_str) self.assertIn('"k1": "v1"', json_str) self.assertIn('"root"', json_str)
def __init__(self, url, method="GET", params=None, body=None, json=None, headers=None, cookies=None, auth=None, timeout=None, allow_redirects=None, proxies=None, verify=None): super(HTTPAction, self).__init__() if auth and len(auth.split(':')) == 2: self.auth = (auth.split(':')[0], auth.split(':')[1]) else: self.auth = auth if isinstance(headers, dict): for key, val in headers.items(): if isinstance(val, (six.integer_types, float)): headers[key] = str(val) if body and json: raise exc.ActionException( "Only one of the parameters 'json' and 'body' can be passed") self.url = url self.method = method self.params = params self.body = utils.to_json_str(body) if isinstance(body, dict) else body self.json = json self.headers = headers self.cookies = cookies self.timeout = timeout self.allow_redirects = allow_redirects self.proxies = proxies self.verify = verify
def test(self, context): return utils.to_json_str(self.params)
def test_serialize_range(self): self.assertEqual("[1, 2, 3, 4]", utils.to_json_str(range(1, 5)))
def process_bind_param(self, value, dialect): return utils.to_json_str(value)