def test_check_node_priority_fail(): state = prepare_state("SELECT id + 1, name FROM Trips", "INSERT INTO Trips VALUES (1)") with pytest.raises(TF): check_node(state, "SelectStmt", 0, priority=0) with pytest.raises(TF): check_node(state, "BinaryExpr", 0, priority=99)
def test_check_node_antlr_exception_skips(dialect_name): state = prepare_state("SELECT x FROM ___!", "SELECT x FROM ___!", dialect_name) assert isinstance(state.student_ast, state.ast_dispatcher.ast_mod.ParseError) select = check_node(state, "SelectStmt", 2) # should be skipped assert select is state
def test_has_equal_ast_not_exact_fail(): query = "SELECT id, name FROM Trips WHERE id < 100 AND name = 'greg'" state = prepare_state(query, query) child = check_node(state, "SelectStmt") with pytest.raises(TF) as exc_info: has_equal_ast(child, sql="id < 999", start="expression", exact=False) print_message(exc_info)
def test_has_equal_ast_field_fail(): state = prepare_state("SELECT id, name FROM Trips", "SELECT name FROM Trips") sel = check_node(state, "SelectStmt", 0) tl = check_edge(sel, "target_list", 0) with pytest.raises(TF): has_equal_ast(tl)
def test_osh_selector_high_priority(self, state): target = state.ast_dispatcher.nodes.get("BracedVarSub") child = check_node(state, "BracedVarSub", priority=99) assert isinstance(child.student_ast, target) assert child.student_ast.token.val == "c" with pytest.raises(TF): child.report("test")
def test_has_equal_ast_field_fail(): state = prepare_state('SELECT id, name FROM Trips', "SELECT name FROM Trips") sel = cf.check_node(state, "SelectStmt", 0) tl = cf.check_field(sel, "target_list", 0) with pytest.raises(TF) as exc_info: has_equal_ast(tl) print_message(exc_info)
def test_where_brackets_with_has_code(): state = prepare_state("SELECT a FROM b WHERE c AND d", "SELECT a FROM b WHERE (d AND c)") sel = check_node(state, "SelectStmt") wc = check_edge(sel, "where_clause") has_code(wc, "d") with pytest.raises(TF): has_code(wc, "not_there")
def test_osh_selector_var_sub(state): target = state.ast_dispatcher.nodes.get('SimpleVarSub') cl = check_node(state, "SimpleCommand") word = check_field(cl, 'words', 2) varsub = check_field(word, 'parts', 0) assert isinstance(varsub.student_ast, target) assert varsub.student_ast.token == "$b"
def test_check_file_no_parse(state, temp_file_sum): child = cf.check_file(state, temp_file_sum.name, parse=False) has_code(child, "1 + 1", fixed=True) assert child.student_code == "1 + 1" assert child.student_ast is False assert child.solution_ast is None # no solution code is provided with pytest.raises(TypeError): assert check_node(child, "Expr", 0)
def test_check_file_use_fs(state, tf): state.solution_code = {tf.name: '3 + 3'} child = cf.check_file(state, tf.name, use_fs=True) assert child.student_code == '1 + 1' assert_equal_ast(child.student_ast, ast.parse(child.student_code)) assert child.solution_code == '3 + 3' assert_equal_ast(child.solution_ast, ast.parse(child.solution_code)) assert check_node(child, 'Expr', 0)
def test_check_file_use_fs_no_parse(state, tf): state.solution_code = {tf.name: '3 + 3'} child = cf.check_file(state, tf.name, parse=False, use_fs=True) assert child.student_code == '1 + 1' assert child.student_ast is None assert child.solution_ast is None with pytest.raises(TypeError): assert check_node(child, 'Expr', 0)
def test_check_file(state, temp_file_sum): child = cf.check_file(state, temp_file_sum.name, solution_code="3 + 3") assert child.student_code == "1 + 1" assert_equal_ast(child.student_ast, ast.parse(child.student_code)) assert child.solution_code == "3 + 3" assert_equal_ast(child.solution_ast, ast.parse(child.solution_code)) assert child.path == Path(temp_file_sum.name) assert child.path.parent == Path(temp_file_sum.name).parent assert child.path.name == Path(temp_file_sum.name).name grandchild = check_node(child, "Expr", 0) assert grandchild.path == child.path
def test_has_code_fixed_subset_fail2(state_tst): select = check_node(state_tst, "SelectStmt", 0) where = check_edge(select, "where_clause") with pytest.raises(TF): has_code(where, "WHERE id > 4", fixed=True)
def test_has_equal_ast_not_exact_pass(): query = "SELECT id, name FROM Trips WHERE id < 100 AND name = 'greg'" state = prepare_state(query, query) child = check_node(state, "SelectStmt") has_equal_ast(child, sql="id < 100", start="expression", exact=False)
def test_check_node_priority_pass(ast_mod): state = prepare_state("SELECT id, name FROM Trips", "SELECT id FROM Trips") child = check_node(state, "Identifier", 0, priority=99) assert isinstance(child.student_ast, ast_mod.Identifier) assert isinstance(child.solution_ast, ast_mod.Identifier)
def test_has_equal_ast_manual_fail(): query = "SELECT id, name FROM Trips" state = prepare_state(query, query) with pytest.raises(TF): child = check_node(state, "SelectStmt") has_equal_ast(child, sql="SELECT * FROM Trips", start="subquery")
def test_has_equal_ast_manual_pass(): query = "SELECT id, name FROM Trips" state = prepare_state(query, query) child = check_node(state, "SelectStmt") has_equal_ast(child, sql=query, start="subquery")
def test_has_equal_ast_field_success(): state = prepare_state("SELECT name FROM Trips", "SELECT name FROM Trips") sel = check_node(state, "SelectStmt", 0) tl = check_edge(sel, "target_list", 0) has_equal_ast(tl)
def test_check_node_pass(ast_mod): state = prepare_state("SELECT id, name FROM Trips", "SELECT id FROM Trips") child = check_node(state, "SelectStmt", 0) assert isinstance(child.student_ast, ast_mod.SelectStmt) assert isinstance(child.solution_ast, ast_mod.SelectStmt)
def test_check_node_from_list(): state = prepare_state("SELECT a, b, c FROM x", "SELECT a, b, c FROM x") sel = check_node(state, "SelectStmt", 0) tl = check_edge(sel, "target_list", None) check_node(tl, "Identifier")
def test_has_code_subset_re_pass2(state_tst): select = check_node(state_tst, "SelectStmt", 0) where = check_edge(select, "where_clause") with pytest.raises(TF): has_code(where, "id > [a-z]")
def test_has_code_fixed_subset_pass(state_tst): select = check_node(state_tst, "SelectStmt", 0) where = check_edge(select, "where_clause") has_code(where, "id > 4", fixed=True)
def test_check_edge_fail(): state = prepare_state("SELECT id FROM Trips WHERE id > 3", "SELECT id FROM Trips WHERE id>4") select = check_node(state, "SelectStmt", 0) check_edge(select, "where_clause")
def test_has_code_fixed_subset_fail(state_tst): select = check_node(state_tst, "SelectStmt", 0) # should fail because the select statement does not include ';' with pytest.raises(TF): has_code(select, state_tst.student_code, fixed=True)
def test_check_node_fail(): state = prepare_state("SELECT id, name FROM Trips", "INSERT INTO Trips VALUES (1)") with pytest.raises(TF): check_node(state, "SelectStmt", 0)
def test_check_edge_index_none_fail(): state = prepare_state("SELECT a, b FROM b WHERE a < 10", "SELECT a FROM b") sel = check_node(state, "SelectStmt") with pytest.raises(TF): check_edge(sel, "where_clause")
def test_check_edge_index_fail(): state = prepare_state("SELECT id, name FROM Trips", "SELECT id FROM Trips") select = check_node(state, "SelectStmt", 0) with pytest.raises(TF): check_edge(select, "target_list", 1)
def test_check_edge_index_pass(): state = prepare_state("SELECT id, name FROM Trips", "SELECT id, name FROM Trips") select = check_node(state, "SelectStmt", 0) check_edge(select, "target_list", 1)
def test_has_code_subset_re_pass(state_tst): select = check_node(state_tst, "SelectStmt", 0) where = check_edge(select, "where_clause") has_code(where, "id > [0-9]")
def test_check_node_back_to_back(): state = prepare_state("SELECT 1 + 2 + 3 FROM x", "SELECT 1 + 2 + 3 FROM x") sel = check_node(state, "SelectStmt", 0) bin1 = check_node(sel, "BinaryExpr", 0) bin2 = check_node(bin1, "BinaryExpr", 0) assert bin2.student_ast.left == "1"