Пример #1
0
def test_double_inner_join_with_aliases(sql_text):
    tree = ast.parse(sql_text)
    frm = tree.body[0].from_clause
    assert frm.join_type == "RIGHT"
    assert frm.right.arr[0].fields == ['i']  # not good
    assert frm.left.join_type == "RIGHT"
    assert frm.left.left.arr[0].fields == ['d']  # not good
    assert frm.left.right.arr[0].fields == ['e']  # not good
Пример #2
0
def test_unparsed_to_text():
    sql_txt = "SELECT CURSOR (SELECT a FROM b) FROM c"
    tree = ast.parse(sql_txt)
    cursor = tree.body[0].target_list[0]

    assert isinstance(cursor, ast.UnaryExpr)
    assert cursor.get_text(sql_txt) == "CURSOR (SELECT a FROM b)"
    assert cursor.expr.get_text(sql_txt) == "SELECT a FROM b"
Пример #3
0
def test_double_inner_join_with_aliases(sql_text):
    tree = ast.parse(sql_text)
    frm = tree.body[0].from_clause[0]
    assert frm.join_type == "right"
    assert frm.right.expr.fields == ["i"]
    assert frm.left.join_type == "right"
    assert frm.left.left.expr.fields == ["d"]
    assert frm.left.right.expr.fields == ["e"]
Пример #4
0
def test_double_inner_join(sql_text):
    tree = ast.parse(sql_text)
    frm = tree.body[0].from_clause
    assert frm.join_type == "RIGHT"
    assert frm.right.fields == ['i']
    assert frm.left.join_type == "RIGHT"
    assert frm.left.left.fields == ['d']
    assert frm.left.right.fields == ['e']
Пример #5
0
def test_ast_dumps_unary():
    tree = ast.parse("-1", "unary_expression")
    assert ast.dump_node(tree) == {
        "type": "UnaryExpr",
        "data": {
            "expr": "1",
            "op": "-"
        },
    }
Пример #6
0
def get_ast(code, start, parser_name):
    if parser_name == "plsql":
        return plsql_ast.parse(code, start)
    elif parser_name == "tsql":
        return tsql_ast.parse(code, start)
    elif parser_name == "python":
        return python_ast.parse(code)

    return None
Пример #7
0
def test_ast_dumps_unary():
    tree = ast.parse("-1", "unary_expression")
    assert tree._dump() == {
        'type': 'UnaryExpr',
        'data': {
            'expr': '1',
            'op': '-'
        }
    }
Пример #8
0
def test_select_fields_shaped():
    select = ast.parse(
        """
    SELECT a,b 
    FROM x,y 
    GROUP BY a, b
    ORDER BY a, b
    
    """, "subquery")
    for field in select._get_field_names():
        assert not isinstance(getattr(select, field), ast.Unshaped)
Пример #9
0
def test_ast_examples_parse(fname):
    import yaml
    dirname = os.path.dirname(__file__)
    data = yaml.load(open(dirname + '/' + fname))
    res = {}
    for start, cmds in data['code'].items():
        res[start] = []
        for cmd in cmds:
            res[start].append([cmd, repr(ast.parse(cmd, start, strict=True))])
    print(res)
    with open(dirname + '/dump_' + fname, 'w') as out_f:
        yaml.dump(res, out_f)
Пример #10
0
def test_print_speaker(code, start):
    speaker = Speaker(**yaml.safe_load(open("antlr_plsql/speaker.yml")))

    tree = ast.parse(code, start=start)

    print("\n\n{} -----------------------\n\n".format(tree.__class__.__name__))
    # node printout
    print(speaker.describe(tree))
    # fields
    for field_name in tree._fields:
        print(
            speaker.describe(tree,
                             field=field_name,
                             fmt="The {field_name} of the {node_name}"))
Пример #11
0
def test_select_fields_shaped():
    select = ast.parse(
        """
    SELECT a,b
    FROM x,y
    GROUP BY a, b
    ORDER BY a, b

    """,
        "subquery",
    )
    for field in select._fields:
        # TODO: update Unshaped test
        pass
Пример #12
0
def ast_examples_parse(fname):
    import yaml

    dirname = os.path.dirname(__file__)
    data = yaml.safe_load(open(dirname + "/" + fname))
    res = {}
    for start, cmds in data["code"].items():
        res[start] = []
        for cmd in cmds:
            res[start].append([cmd, repr(ast.parse(cmd, start, strict=True))])
    print(res)
    filename = "dump_" + fname
    with open(dirname + "/" + filename, "w") as out_f:
        yaml.dump(res, out_f)
    return filename
Пример #13
0
def test_select_target_list(speaker):
    select = ast.parse("SELECT x FROM y", start="subquery")
    assert speaker.describe(select,
                            field="target_list",
                            fmt="The {field_name} of {node_name}")
Пример #14
0
def test_select_statement(speaker):
    select = ast.parse("SELECT x FROM y", start="subquery")
    assert speaker.describe(select, "{node_name}") == "SELECT statement"
Пример #15
0
def test_dump(start, cmd, res):
    assert repr(ast.parse(cmd, start, strict=True)) == res
Пример #16
0
def test_examples(name, query):
    ast.parse(query)
Пример #17
0
def case_sensitivity(stu):
    start = 'sql_script'
    lowercase = "select \"Preserve\" from b where b.name = 'Casing'"
    assert repr(ast.parse(lowercase, start, strict=True)) != repr(ast.parse(stu, start, strict=True))
Пример #18
0
def test_inner_join(sql_text):
    tree = ast.parse(sql_text)
    assert tree.body[0].from_clause[0].join_type == "inner"
Пример #19
0
def test_ast_parse_strict():
    with pytest.raises(AntlrException):
        ast.parse("SELECT x FROM ____", strict=True)  # ____ is ungrammatical
    # Test export of exception class
    with pytest.raises(ast.ParseError):
        ast.parse("SELECT x FROM ____!", strict=True)  # ____! is ungrammatical
Пример #20
0
def test_ast_dump():
    sql_txt = "SELECT a, b FROM x WHERE a < 10"
    tree = ast.parse(sql_txt)
    tree._dump()
Пример #21
0
def test_ast_dumps_noerr(sql_text, start):
    tree = ast.parse(sql_text, start)
    import json

    d = json.dumps(ast.dump_node(tree))
Пример #22
0
def test_ast_dump():
    sql_txt = "SELECT a, b FROM x WHERE a < 10"
    tree = ast.parse(sql_txt)
    ast.dump_node(tree)
Пример #23
0
def test_ast_dumps_noerr(sql_text, start):
    tree = ast.parse(sql_text, start)
    d = tree._dumps()
Пример #24
0
def test_ast_parse_strict():
    with pytest.raises(AntlrException):
        ast.parse("SELECT x FROM ____", strict=True)  # ____ is ungrammatical
Пример #25
0
def test_call_name(speaker):
    call = ast.parse("COUNT(*)", start="standard_function")
    assert speaker.describe(call, fmt="{node_name}") == "function call `COUNT`"
Пример #26
0
def test_case_sensitivity(stu):
    lowercase = "select \"Preserve\" from b where b.name = 'Casing'"
    assert repr(ast.parse(lowercase, strict=True)) != repr(
        ast.parse(stu, strict=True))
Пример #27
0
def test_ast_select_paren():
    node = ast.parse("(SELECT a FROM b)", "subquery")
    assert isinstance(node, ast.SelectStmt)