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