Пример #1
0
def test_selecting_base_nodes(ast):
    query = "SELECT a FROM b"
    antlr_tree = ast.parse_ast(ast.grammar, query, start="sql_script")
    ast_tree = ast.process_tree(antlr_tree)

    base_selector = Selector(ast.AstNode,
                             "Query_block",
                             strict=False,
                             priority=3)
    base_selector.visit(ast_tree)
    found = base_selector.out
    assert len(found) == 1
    assert isinstance(found[0], ast.AstNode)
    assert found[0].__class__.__name__ == "Query_block"

    base_dispatch = Dispatcher(ast.AstNode, ast_mod=ast)
    select_stmt = base_dispatch.find("Query_block", ast_tree, priority=3)[0]
    assert select_stmt == found[0]
Пример #2
0
def test_initial_state():
    State(student_code={'script.py': '1'},
          solution_code={'script.py': '1'},
          reporter=Reporter(),
          pre_exercise_code="",
          student_result="",
          solution_result="",
          student_conn=None,
          solution_conn=None,
          ast_dispatcher=Dispatcher(DUMMY_NODES, ParseHey()))
Пример #3
0
def state():
    return State(
        student_code="",
        solution_code="",
        reporter=Reporter(),
        # args below should be ignored
        pre_exercise_code="NA",
        student_result="",
        solution_result="",
        student_conn=None,
        solution_conn=None,
        ast_dispatcher=Dispatcher(DUMMY_NODES, ParseHey()))
Пример #4
0
def state():
    return State(
        # only Reporter and Dispatcher are used
        student_code="",
        solution_code="",
        reporter=Reporter(),
        pre_exercise_code="",
        student_result="",
        solution_result="",
        student_conn=None,
        solution_conn=None,
        ast_dispatcher=Dispatcher(ast.AST, DUMMY_NODES, ParseHey()),
    )
Пример #5
0
def prepare_state(solution_code, student_code, dialect='postgresql'):
    dispatcher = Dispatcher.from_module(PARSER_MODULES[dialect])
    return State(
        student_code=student_code,
        solution_code=solution_code,
        reporter=Reporter(),
        # args below should be ignored
        pre_exercise_code="NA",
        student_result=[],
        solution_result=[],
        student_conn=None,
        solution_conn=None,
        ast_dispatcher=dispatcher)
Пример #6
0
def test_line_info(sql_cmd, start, pos):
    state = State(sql_cmd,
                  "",
                  "",
                  None,
                  None, {}, {},
                  Reporter(),
                  ast_dispatcher=Dispatcher.from_module(
                      PARSER_MODULES['mssql']))
    try:
        state.do_test("failure message")
    except TF as tf:
        for ii, k in enumerate(pos_names):
            assert tf.payload[k] == pos[ii]
Пример #7
0
    def get_dispatcher(self):
        # MCE doesn't always have connection - fallback on postgresql
        dialect = self.student_conn.dialect.name if self.student_conn else 'postgresql'
        ast_dispatcher = Dispatcher.from_module(PARSER_MODULES[dialect])

        # TODO: the code below monkney patches the mssql ast to use only lowercase
        #       representations. This is because msft server has a setting to be
        #       case sensitive. However, this is often not the case, and probably
        #       detremental to DataCamp courses. Need to move to more sane configuration.
        #        if dialect_name == 'mssql':
        if dialect == 'mssql':
            AstNode = ast_dispatcher.ast.AstNode
            AstNode.__repr__ = lower_case(AstNode.__repr__)

        return ast_dispatcher
Пример #8
0
    def get_dispatcher(self):
        if not self.student_conn:
            return DummyDispatcher()

        dialect = self.student_conn.dialect.name
        ast_dispatcher = Dispatcher.from_module(PARSER_MODULES[dialect])

        # TODO: the code below monkney patches the mssql ast to use only lowercase
        #       representations. This is because msft server has a setting to be
        #       case sensitive. However, this is often not the case, and probably
        #       detremental to DataCamp courses. Need to move to more sane configuration.
        if dialect == "mssql":
            AstNode = ast_dispatcher.ast_mod.AstNode
            AstNode.__repr__ = lower_case(AstNode.__repr__)

        return ast_dispatcher
Пример #9
0
def code_state():
    return State(
        student_code={
            'script1.py': '1 + 1',
            'script2.py': '2 + 2'
        },
        solution_code={
            'script1.py': '3 + 3',
            'script2.py': '4 + 4'
        },
        reporter=Reporter(),
        # args below should be ignored
        pre_exercise_code="NA",
        student_result="",
        solution_result="",
        student_conn=None,
        solution_conn=None,
        ast_dispatcher=Dispatcher(DUMMY_NODES, ParseHey()))
Пример #10
0
def test_state_line_info():
    state = State("\nSELECT x FROM y",
                  "SELECT x FROM y",
                  "",
                  None,
                  None, {}, {},
                  Reporter(),
                  ast_dispatcher=Dispatcher.from_module(
                      PARSER_MODULES['mssql']))

    with pytest.raises(TF):
        state.do_test("failure message")

    payload = state.reporter.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]
Пример #11
0
def test_dispatcher_select(node):
    assert isinstance(Dispatcher(AST).select("value", node), Num)
Пример #12
0
def test_dispatcher_find(node):
    assert isinstance(Dispatcher(AST).find("Num", node)[0], Num)
Пример #13
0
def dispatcher():
    return Dispatcher.from_module(ast())
Пример #14
0
 def get_dispatcher():
     ast_mod = OshParser(ParseError = ParseError)
     return Dispatcher(ast_mod.classes, ast_mod)
Пример #15
0
 def get_dispatcher(self):
     return Dispatcher.from_module(DEFAULT_PARSER)