Exemplo n.º 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
Exemplo n.º 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"
Exemplo n.º 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"]
Exemplo n.º 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']
Exemplo n.º 5
0
def test_ast_dumps_unary():
    tree = ast.parse("-1", "unary_expression")
    assert ast.dump_node(tree) == {
        "type": "UnaryExpr",
        "data": {
            "expr": "1",
            "op": "-"
        },
    }
Exemplo n.º 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
Exemplo n.º 7
0
def test_ast_dumps_unary():
    tree = ast.parse("-1", "unary_expression")
    assert tree._dump() == {
        'type': 'UnaryExpr',
        'data': {
            'expr': '1',
            'op': '-'
        }
    }
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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}"))
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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}")
Exemplo n.º 14
0
def test_select_statement(speaker):
    select = ast.parse("SELECT x FROM y", start="subquery")
    assert speaker.describe(select, "{node_name}") == "SELECT statement"
Exemplo n.º 15
0
def test_dump(start, cmd, res):
    assert repr(ast.parse(cmd, start, strict=True)) == res
Exemplo n.º 16
0
def test_examples(name, query):
    ast.parse(query)
Exemplo n.º 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))
Exemplo n.º 18
0
def test_inner_join(sql_text):
    tree = ast.parse(sql_text)
    assert tree.body[0].from_clause[0].join_type == "inner"
Exemplo n.º 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
Exemplo n.º 20
0
def test_ast_dump():
    sql_txt = "SELECT a, b FROM x WHERE a < 10"
    tree = ast.parse(sql_txt)
    tree._dump()
Exemplo n.º 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))
Exemplo n.º 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)
Exemplo n.º 23
0
def test_ast_dumps_noerr(sql_text, start):
    tree = ast.parse(sql_text, start)
    d = tree._dumps()
Exemplo n.º 24
0
def test_ast_parse_strict():
    with pytest.raises(AntlrException):
        ast.parse("SELECT x FROM ____", strict=True)  # ____ is ungrammatical
Exemplo n.º 25
0
def test_call_name(speaker):
    call = ast.parse("COUNT(*)", start="standard_function")
    assert speaker.describe(call, fmt="{node_name}") == "function call `COUNT`"
Exemplo n.º 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))
Exemplo n.º 27
0
def test_ast_select_paren():
    node = ast.parse("(SELECT a FROM b)", "subquery")
    assert isinstance(node, ast.SelectStmt)