Ejemplo n.º 1
0
    def test_should_query_specification_visitor(self):
        EvaParserVisitor.visit = MagicMock()
        mock_visit = EvaParserVisitor.visit
        mock_visit.side_effect = [
            "target", {
                "from": ["from"],
                "where": "where"
            }
        ]

        visitor = EvaParserVisitor()
        ctx = MagicMock()
        child_1 = MagicMock()
        child_1.getRuleIndex.return_value = frameQLParser.RULE_selectElements

        child_2 = MagicMock()
        child_2.getRuleIndex.return_value = frameQLParser.RULE_fromClause
        ctx.children = [None, child_1, child_2]

        expected = visitor.visitQuerySpecification(ctx)

        mock_visit.assert_has_calls([call(child_1), call(child_2)])

        self.assertEqual(expected.from_table, "from")
        self.assertEqual(expected.where_clause, "where")
        self.assertEqual(expected.target_list, "target")
Ejemplo n.º 2
0
 def test_visit_full_column_name(self):
     ctx = MagicMock()
     visitor = EvaParserVisitor()
     EvaParserVisitor.visit = MagicMock()
     EvaParserVisitor.visit.return_value = None
     with self.assertWarns(SyntaxWarning, msg='Column Name Missing'):
         visitor.visitFullColumnName(ctx)
Ejemplo n.º 3
0
    def test_visit_nested_expression_atom(self):
        ctx = MagicMock()
        visitor = EvaParserVisitor()
        EvaParserVisitor.visit = MagicMock()
        mock_visit = EvaParserVisitor.visit
        expected = visitor.visitNestedExpressionAtom(ctx)

        mock_visit.assert_has_calls([call(ctx.expression(0))])
Ejemplo n.º 4
0
 def test_visit_full_column_name_none(self):
     ''' Testing for getting a Warning when column name is None
         Function: visitFullColumnName
     '''
     ctx = MagicMock()
     visitor = EvaParserVisitor()
     EvaParserVisitor.visit = MagicMock()
     EvaParserVisitor.visit.return_value = None
     with self.assertWarns(SyntaxWarning, msg='Column Name Missing'):
         visitor.visitFullColumnName(ctx)
Ejemplo n.º 5
0
 def test_visit_constant(self):
     ''' Testing for value of returned constant
         when real literal is not None
         Function: visitConstant
     '''
     ctx = MagicMock()
     visitor = EvaParserVisitor()
     ctx.REAL_LITERAL.return_value = '5'
     expected = visitor.visitConstant(ctx)
     self.assertEqual(expected.evaluate(), float(ctx.getText()))
Ejemplo n.º 6
0
 def test_visit_table_name_none(self):
     ''' Testing for getting a Warning when table name is None
         Function: visitTableName
     '''
     ctx = MagicMock()
     visitor = EvaParserVisitor()
     EvaParserVisitor.visit = MagicMock()
     EvaParserVisitor.visit.return_value = None
     with self.assertWarns(SyntaxWarning, msg='Invalid from table'):
         visitor.visitTableName(ctx)
Ejemplo n.º 7
0
    def test_visit_string_literal_none(self):
        ''' Testing when string literal is None
            Function: visitStringLiteral
        '''
        visitor = EvaParserVisitor()
        ctx = MagicMock()
        ctx.STRING_LITERAL.return_value = None

        EvaParserVisitor.visitChildren = MagicMock()
        mock_visit = EvaParserVisitor.visitChildren

        expected = visitor.visitStringLiteral(ctx)
        mock_visit.assert_has_calls([call(ctx)])
Ejemplo n.º 8
0
    def test_logical_operator(self):
        ctx = MagicMock()
        visitor = EvaParserVisitor()

        self.assertEqual(visitor.visitLogicalOperator(ctx),
                         ExpressionType.INVALID)

        ctx.getText.return_value = 'OR'
        self.assertEqual(visitor.visitLogicalOperator(ctx),
                         ExpressionType.LOGICAL_OR)

        ctx.getText.return_value = 'AND'
        self.assertEqual(visitor.visitLogicalOperator(ctx),
                         ExpressionType.LOGICAL_AND)
Ejemplo n.º 9
0
    def test_from_clause_visitor(self, mock_visit):
        mock_visit.side_effect = ["from", "where"]

        ctx = MagicMock()
        tableSources = MagicMock()
        ctx.tableSources.return_value = tableSources
        whereExpr = MagicMock()
        ctx.whereExpr = whereExpr

        visitor = EvaParserVisitor()
        expected = visitor.visitFromClause(ctx)
        mock_visit.assert_has_calls([call(tableSources), call(whereExpr)])

        self.assertEqual(expected.get('where'), 'where')
        self.assertEqual(expected.get('from'), 'from')
Ejemplo n.º 10
0
    def test_visit_query_specification_base_exception(self):
        ''' Testing Base Exception error handling
            Function: visitQuerySpecification
        '''
        EvaParserVisitor.visit = MagicMock()
        mock_visit = EvaParserVisitor.visit

        visitor = EvaParserVisitor()
        ctx = MagicMock()
        child_1 = MagicMock()
        child_2 = MagicMock()
        ctx.children = [None, child_1, child_2]
        child_1.getRuleIndex.side_effect = BaseException()

        expected = visitor.visitQuerySpecification(ctx)

        self.assertEqual(expected, None)
Ejemplo n.º 11
0
class EvaFrameQLParser():
    """
    Parser for eva; based on frameQL grammar
    """
    def __init__(self):
        self._visitor = EvaParserVisitor()

    def parse(self, query_string: str) -> list:
        lexer = frameQLLexer(InputStream(query_string))
        stream = CommonTokenStream(lexer)
        parser = frameQLParser(stream)
        tree = parser.root()
        return self._visitor.visit(tree)
Ejemplo n.º 12
0
    def test_logical_expression(self):
        ''' Testing for break in code if len(children) < 3
            Function : visitLogicalExpression
        '''
        ctx = MagicMock()
        visitor = EvaParserVisitor()

        # Test for no children
        ctx.children = []
        expected = visitor.visitLogicalExpression(ctx)
        self.assertEqual(expected, None)

        # Test for one children
        child_1 = MagicMock()
        ctx.children = [child_1]
        expected = visitor.visitLogicalExpression(ctx)
        self.assertEqual(expected, None)

        # Test for two children
        child_1 = MagicMock()
        child_2 = MagicMock()
        ctx.children = [child_1, child_2]
        expected = visitor.visitLogicalExpression(ctx)
        self.assertEqual(expected, None)
Ejemplo n.º 13
0
    def test_comparison_operator(self):
        ctx = MagicMock()
        visitor = EvaParserVisitor()

        self.assertEqual(visitor.visitComparisonOperator(ctx),
                         ExpressionType.INVALID)

        ctx.getText.return_value = '='
        self.assertEqual(visitor.visitComparisonOperator(ctx),
                         ExpressionType.COMPARE_EQUAL)

        ctx.getText.return_value = '<'
        self.assertEqual(visitor.visitComparisonOperator(ctx),
                         ExpressionType.COMPARE_LESSER)

        ctx.getText.return_value = '>'
        self.assertEqual(visitor.visitComparisonOperator(ctx),
                         ExpressionType.COMPARE_GREATER)
Ejemplo n.º 14
0
 def __init__(self):
     self._visitor = EvaParserVisitor()