def test_reporter_line_info_1_based_cols(): rep = Reporter() rep.failed_test = True rep.feedback = Feedback("MESSAGE", ast.parse("\nSELECT x FROM y")) payload = rep.build_payload() pos = [2, 1, 2, 15] pos_names = ['line_start', 'column_start', 'line_end', 'column_end'] for ii, k in enumerate(pos_names): assert payload[k] == pos[ii]
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_examples_parse(fname): # just make sure they don't throw error for now.. 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 ast_examples_parse(fname): # just make sure they don't throw error for now.. 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_ast_parse_strict(): with pytest.raises(ast.AntlrException): ast.parse("SELECT x FROM ____!", strict = True) # ____! is ungrammatical
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_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_dump(start, cmd, res): assert repr(ast.parse(cmd, start, strict=True)) == res
def test_feedback_init(sql_cmd, start, pos): tree = ast.parse(sql_cmd, start) fb = Feedback("MESSAGE", tree, strict=True) pos_names = ['line_start', 'column_start', 'line_end', 'column_end'] for ii, k in enumerate(pos_names): assert fb.line_info[k] == pos[ii]