From a41e3f34360b6e26766a60e64a347a7ce959d2c2 Mon Sep 17 00:00:00 2001 From: Amos Rimon Date: Sun, 11 Aug 2019 16:12:22 +0300 Subject: [PATCH] Issue #11 - fix dynamic rendering --- pykusto/utils.py | 13 ++++++------- test/test_expressions.py | 4 ++-- test/test_utils.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 test/test_utils.py diff --git a/pykusto/utils.py b/pykusto/utils.py index 0252b2f..521900d 100644 --- a/pykusto/utils.py +++ b/pykusto/utils.py @@ -32,21 +32,20 @@ def dynamic_to_kql(d: Union[Mapping, List, Tuple]) -> KQL: # Issue #11 counter = 0 prev = "" - for i in range(len(query)): + for i, c in enumerate(query): if counter == 0: - if query[i] == "[": + if c == "[": query[i] = "(" - elif query[i] == "]": + elif c == "]": query[i] = ")" - elif query[i] == '"' and prev != "\\": + elif c in ['"', '\''] and prev != "\\": counter += 1 elif counter > 0: - if query[i] == '"' and prev != "\\": + if c in ['"', '\''] and prev != "\\": counter -= 1 prev = query[i] - i += 1 assert counter == 0 - return KQL("".join([c for c in query])) + return KQL("".join(query)) def bool_to_kql(b: bool) -> KQL: diff --git a/test/test_expressions.py b/test/test_expressions.py index 28f47a1..77a6282 100644 --- a/test/test_expressions.py +++ b/test/test_expressions.py @@ -79,5 +79,5 @@ class TestExpressions(TestBase): ) self.assertEqual( " | where foo in (\"[\", \"[[\", \"]\")", - Query().where(col.foo.is_in(["[", "[[", "]"])).render() - ) + Query().where(col.foo.is_in(['[', "[[", "]"])).render() + ) \ No newline at end of file diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 0000000..c3614d8 --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,16 @@ +from pykusto import utils +from test.test_base import TestBase + + +class TestUtils(TestBase): + def test_dynamic_to_kql(self): + dict ={ + "name": "Alan", + "age": 21, + "address": ("NY", 36), + "pets": ["Libby", "Panda", "]", "["] + } + self.assertEqual( + "{\"name\": \"Alan\", \"age\": 21, \"address\": (\"NY\", 36), \"pets\": (\"Libby\", \"Panda\", \"]\", \"[\")}", + utils.to_kql(dict) + )