Ejemplo n.º 1
0
def test_list_node_str_should_return_formatted():
    # Test a node with PairNodes as items
    node = ListNode(
        type=SyntaxToken("filters"),
        left_bracket=LeftBracket(),
        items=(
            PairNode(SyntaxToken("created_date"), QuotedSyntaxToken("7 days")),
            PairNode(SyntaxToken("user.status", prefix=" "),
                     QuotedSyntaxToken("-disabled")),
        ),
        right_bracket=RightBracket(),
    )
    assert str(
        node) == 'filters: [created_date: "7 days", user.status: "-disabled"]'

    # Test a node with SyntaxTokens as items
    node = ListNode(
        type=SyntaxToken("fields"),
        left_bracket=LeftBracket(),
        items=(
            SyntaxToken("user.user_id", prefix="\n  "),
            SyntaxToken("user.age", prefix="\n  ", suffix="\n"),
        ),
        right_bracket=RightBracket(),
    )
    assert str(node) == "fields: [\n  user.user_id,\n  user.age\n]"

    # Test a node with zero items
    node = ListNode(
        type=SyntaxToken("fields"),
        left_bracket=LeftBracket(),
        items=tuple(),
        right_bracket=RightBracket(),
    )
    assert str(node) == "fields: []"
Ejemplo n.º 2
0
def test_parse_value_quoted_literal():
    quoted_literal = "This is a quoted literal."
    stream = (tokens.QuotedLiteralToken(quoted_literal,
                                        1), tokens.StreamEndToken(1))
    parser = lkml.parser.Parser(stream)
    result = parser.parse_value()
    assert result == QuotedSyntaxToken(quoted_literal, 1)
Ejemplo n.º 3
0
    def parse_token(
        key: str,
        value: str,
        force_quote: bool = False,
        prefix: str = "",
        suffix: str = "",
    ) -> SyntaxToken:
        """Parses a value into a token, quoting it if required by the key or forced.

        Args:
            key: A LookML field type (e.g. "hidden")
            value: The value string (e.g. "yes")
            force_quote: True if value should always be quoted

        Returns:
            A generator of serialized string chunks

        """
        if force_quote or key in QUOTED_LITERAL_KEYS:
            return QuotedSyntaxToken(value, prefix=prefix, suffix=suffix)
        elif key in EXPR_BLOCK_KEYS:
            return ExpressionSyntaxToken(value.strip(),
                                         prefix=prefix,
                                         suffix=suffix)
        else:
            return SyntaxToken(value, prefix=prefix, suffix=suffix)
Ejemplo n.º 4
0
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"),
        left_bracket=LeftBracket(),
        items=(
            PairNode(SyntaxToken("orders.customer_id"), SyntaxToken("asc")),
            PairNode(SyntaxToken("orders.order_id"), SyntaxToken("desc")),
        ),
        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"),
        left_bracket=LeftBracket(),
        items=(
            PairNode(
                SyntaxToken("view_name.field_one", prefix="\n  "),
                QuotedSyntaxToken("-0,-1,-8,-9,-99,-NULL,-EMPTY"),
            ),
        ),
        right_bracket=RightBracket(prefix="\n"),
    )
Ejemplo n.º 5
0
def test_parse_value_quoted_literal_with_leftovers():
    quoted_literal = "This is a quoted literal."
    literal = "Some other tokens following."
    stream = (
        tokens.QuotedLiteralToken(quoted_literal, 1),
        tokens.LiteralToken(literal, 1),
        tokens.ValueToken(1),
        tokens.StreamEndToken(1),
    )
    parser = lkml.parser.Parser(stream)
    result = parser.parse_value()
    assert result == QuotedSyntaxToken(quoted_literal, 1)
    assert parser.index == 1
Ejemplo n.º 6
0
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"), value=QuotedSyntaxToken("The View")
    )
    with pytest.raises(AttributeError):
        result.prefix
Ejemplo n.º 7
0
def test_quoted_syntax_token_quotes_double_quotes():
    text = 'This is the "best" dimension'
    token = QuotedSyntaxToken(text)
    assert token.format_value() == r'"This is the \"best\" dimension"'