def test_parse_list_with_inner_comment(): stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.WhitespaceToken("\n ", 1), tokens.LiteralToken("view_name.field_one", 2), tokens.CommaToken(2), tokens.WhitespaceToken("\n ", 2), tokens.LiteralToken("view_name.field_two", 3), tokens.WhitespaceToken(" ", 3), tokens.CommentToken("# This is a comment", 3), tokens.WhitespaceToken("\n", 3), tokens.ListEndToken(4), tokens.StreamEndToken(4), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=( SyntaxToken("view_name.field_one", 2, prefix="\n "), SyntaxToken("view_name.field_two", 3, prefix="\n ", suffix=" # This is a comment\n"), ), right_bracket=RightBracket(), )
def test_parse_list_with_trailing_comma(): stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.LiteralToken("view_name.field_one", 1), tokens.CommaToken(1), tokens.ListEndToken(1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=(SyntaxToken("view_name.field_one", 1), ), trailing_comma=Comma(), right_bracket=RightBracket(), ) # Test when the list items are separated by newlines stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.WhitespaceToken("\n ", 1), tokens.LiteralToken("view_name.field_one", 2), tokens.CommaToken(2), tokens.WhitespaceToken("\n", 2), tokens.ListEndToken(3), tokens.StreamEndToken(3), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=(SyntaxToken("view_name.field_one", 2, prefix="\n "), ), trailing_comma=Comma(), right_bracket=RightBracket(prefix="\n"), )
def test_parse_list_with_no_contents(): stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.ListEndToken(1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=tuple(), right_bracket=RightBracket(), ) # Add whitespace between brackets stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListEndToken(1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=tuple(), right_bracket=RightBracket(prefix=" "), )
def test_parse_pair_with_literal(): stream = ( tokens.LiteralToken("hidden", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.LiteralToken("yes", 1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_pair() assert result == PairNode( type=SyntaxToken("hidden", 1), colon=Colon(line_number=1, suffix=" "), value=SyntaxToken("yes", 1), )
def test_parse_pair_with_sql_block(): sql = " SELECT * FROM schema.table " stream = ( tokens.LiteralToken("sql", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ExpressionBlockToken(sql, 1), tokens.ExpressionBlockEndToken(1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_pair() assert result == PairNode( type=SyntaxToken("sql", 1), colon=Colon(line_number=1, suffix=" "), value=ExpressionSyntaxToken(sql.strip(), 1), )
def test_parse_pair_with_quoted_literal(): stream = ( tokens.LiteralToken("view_label", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.QuotedLiteralToken("The View", 1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_pair() assert result == PairNode( type=SyntaxToken("view_label", 1), colon=Colon(line_number=1, suffix=" "), value=QuotedSyntaxToken("The View", 1), ) with pytest.raises(AttributeError): result.prefix
def test_parse_list_with_only_comment(): stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.WhitespaceToken("\n ", 1), tokens.CommentToken("# Put some fields here", 2), tokens.WhitespaceToken("\n", 2), tokens.ListEndToken(3), tokens.StreamEndToken(3), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=tuple(), right_bracket=RightBracket(prefix="\n # Put some fields here\n"), )
def test_parse_list_with_leading_comma(): stream = ( tokens.LiteralToken("drill_fields", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.CommaToken(1), tokens.LiteralToken("view_name.field_one", 1), tokens.ListEndToken(1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("drill_fields", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=(SyntaxToken("view_name.field_one", 1), ), right_bracket=RightBracket(), leading_comma=Comma(), )
def test_parse_block_with_no_expression(): stream = ( tokens.LiteralToken("dimension", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.LiteralToken("dimension_name", 1), tokens.WhitespaceToken(" ", 1), tokens.BlockStartToken(1), tokens.BlockEndToken(1), tokens.WhitespaceToken(" ", 1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_block() assert result == BlockNode( type=SyntaxToken("dimension", 1), name=SyntaxToken("dimension_name", 1), colon=Colon(line_number=1, suffix=" "), left_brace=LeftCurlyBrace(prefix=" "), container=ContainerNode(items=tuple()), right_brace=RightCurlyBrace(suffix=" "), )
def test_parse_list_with_pairs(): stream = ( tokens.LiteralToken("sorts", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.LiteralToken("orders.customer_id", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.LiteralToken("asc", 1), tokens.CommaToken(1), tokens.LiteralToken("orders.order_id", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.LiteralToken("desc", 1), tokens.ListEndToken(1), tokens.StreamEndToken(1), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("sorts", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=( PairNode( type=SyntaxToken("orders.customer_id", 1), colon=Colon(line_number=1, suffix=" "), value=SyntaxToken("asc", 1), ), PairNode( type=SyntaxToken("orders.order_id", 1), colon=Colon(line_number=1, suffix=" "), value=SyntaxToken("desc", 1), ), ), right_bracket=RightBracket(), ) stream = ( tokens.LiteralToken("filters", 1), tokens.ValueToken(1), tokens.WhitespaceToken(" ", 1), tokens.ListStartToken(1), tokens.WhitespaceToken("\n ", 1), tokens.LiteralToken("view_name.field_one", 2), tokens.ValueToken(2), tokens.WhitespaceToken(" ", 2), tokens.QuotedLiteralToken("-0,-1,-8,-9,-99,-NULL,-EMPTY", 2), tokens.WhitespaceToken("\n", 2), tokens.ListEndToken(3), tokens.StreamEndToken(3), ) parser = lkml.parser.Parser(stream) result = parser.parse_list() assert result == ListNode( type=SyntaxToken("filters", 1), colon=Colon(line_number=1, suffix=" "), left_bracket=LeftBracket(), items=(PairNode( type=SyntaxToken("view_name.field_one", 2, prefix="\n "), colon=Colon(line_number=2, suffix=" "), value=QuotedSyntaxToken("-0,-1,-8,-9,-99,-NULL,-EMPTY", 2), ), ), right_bracket=RightBracket(prefix="\n"), )
def test_parse_key_normal_returns_token_value(): stream = (tokens.LiteralToken("label", 1), tokens.ValueToken(1)) parser = lkml.parser.Parser(stream) result = parser.parse_key() assert result == (SyntaxToken("label", 1), Colon(line_number=1))