Skip to content

Commit

Permalink
Parser and Select statement Unit TestCases
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav274 committed Nov 22, 2019
1 parent 71209e6 commit 9ffb00d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
7 changes: 1 addition & 6 deletions src/query_parser/eva_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ def build_eva_parse_tree(self, query_string : str) -> EvaStatementList:
parser = frameQLParser(stream)
tree = parser.root()
visitor = EvaParserVisitor()
visitor.visit(tree)
return visitor.visit(tree)
# print(tree.toStringTree(recog=parser))


if __name__ == "__main__":
eva = EvaFrameQLParser()
query = 'SELECT CLASS , REDNESS FROM TAIPAI WHERE ( CLASS = "VAN" AND REDNESS = 200 ) OR REDNESS > 300'
eva.build_eva_parse_tree(query)



5 changes: 3 additions & 2 deletions src/query_parser/eva_ql_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
class EvaParserVisitor(frameQLParserVisitor):
# Visit a parse tree produced by frameQLParser#root.
def visitRoot(self, ctx:frameQLParser.RootContext):
return self.visitChildren(ctx)
for child in ctx.children:
if child is not TerminalNode:
return self.visit(child)


# Visit a parse tree produced by frameQLParser#sqlStatements.
Expand Down Expand Up @@ -987,7 +989,6 @@ def visitReplaceStatement(self, ctx:frameQLParser.ReplaceStatementContext):

# Visit a parse tree produced by frameQLParser#simpleSelect.
def visitSimpleSelect(self, ctx:frameQLParser.SimpleSelectContext):
print(len(ctx.children))
select_stm = self.visitChildren(ctx)
return select_stm

Expand Down
2 changes: 1 addition & 1 deletion src/query_parser/eva_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, stmt_type : StatementType):
self._stmt_type = stmt_type

@property
def get_type(self):
def stmt_type(self):
return self._stmt_type

class EvaStatementList():
Expand Down
59 changes: 55 additions & 4 deletions test/query_parser/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
import unittest
from src.query_parser.eva_parser import EvaFrameQLParser

from src.query_parser.eva_statement import EvaStatementList, EvaStatement
from src.query_parser.eva_statement import StatementType
from src.query_parser.select_statement import SelectStatement
from src.expression.abstract_expression import AbstractExpression, ExpressionType
class ParserTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def test_basic_visitor(self):
def test_eva_parser(self):
eva = EvaFrameQLParser()
single_queries = []
single_queries.append("SELECT CLASS FROM TAIPAI;")
single_queries.append("SELECT CLASS FROM TAIPAI WHERE CLASS = 'VAN';")
single_queries.append("SELECT CLASS,REDNESS FROM TAIPAI WHERE CLASS = 'VAN' AND REDNESS > 20;")
single_queries.append("SELECT CLASS FROM TAIPAI WHERE (CLASS = 'VAN' AND REDNESS < 300 ) OR REDNESS > 500;")
single_queries.append("SELECT CLASS FROM TAIPAI WHERE (CLASS = 'VAN' AND REDNESS < 300 ) OR REDNESS > 500;")
for query in single_queries:
eva_statement_list = eva.build_eva_parse_tree(query)
self.assertIsInstance(eva_statement_list, EvaStatementList)
self.assertEqual(len(eva_statement_list), 1)
self.assertIsInstance(eva_statement_list.get_statement(0), EvaStatement)

multiple_queries = []
multiple_queries.append("SELECT CLASS FROM TAIPAI WHERE CLASS = 'VAN' AND REDNESS < 300 OR REDNESS > 500; \
SELECT REDNESS FROM TAIPAI WHERE (CLASS = 'VAN' AND REDNESS = 300)")

for query in multiple_queries:
eva_statement_list = eva.build_eva_parse_tree(query)
self.assertIsInstance(eva_statement_list, EvaStatementList)
self.assertEqual(len(eva_statement_list), 2)
self.assertIsInstance(eva_statement_list.get_statement(0), EvaStatement)
self.assertIsInstance(eva_statement_list.get_statement(1), EvaStatement)


def test_select_parser(self):
eva = EvaFrameQLParser()
query = "SELECT CLASS , REDNESS FROM TAIPAI WHERE CLASS = 'VAN' AND REDNESS = 3 OR REDNESS > 300.23; GROUP BY CLASS HAVING REDNESS > 20 ORDER BY REDNESS;"
eva.build_eva_parse_tree(query)
select_query = "SELECT CLASS, REDNESS FROM TAIPAI WHERE (CLASS = 'VAN' AND REDNESS < 300 ) OR REDNESS > 500;"
eva_statement_list = eva.build_eva_parse_tree(select_query)
self.assertIsInstance(eva_statement_list, EvaStatementList)
self.assertEqual(len(eva_statement_list), 1)
self.assertEqual(eva_statement_list.get_statement(0).stmt_type, \
StatementType.SELECT)

select_stmt = eva_statement_list.get_statement(0)

#target List
self.assertIsNotNone(select_stmt.target_list)
self.assertEqual(len(select_stmt.target_list), 2)
self.assertEqual(select_stmt.target_list[0].etype, ExpressionType.TUPLE_VALUE)
self.assertEqual(select_stmt.target_list[1].etype, ExpressionType.TUPLE_VALUE)

#from_table
#Todo change this to table ref, discuss with data loader team before making any change
self.assertIsNotNone(select_stmt.from_table)
#self.assertEqual(select_stmt.from_table.etype, ExpressionType.TUPLE_VALUE)

#where_clause
self.assertIsNotNone(select_stmt.where_clause)
#other tests should go in expression testing


if __name__ == '__main__':
unittest.main()

0 comments on commit 9ffb00d

Please sign in to comment.