def test_block_node_str_should_return_formatted(): # Test a regular block node = BlockNode( type=SyntaxToken("set"), name=SyntaxToken("user_dimensions"), left_brace=LeftCurlyBrace(prefix=" ", suffix=" "), container=ContainerNode((ListNode( type=SyntaxToken("fields"), left_bracket=LeftBracket(), items=( SyntaxToken("user.user_id"), SyntaxToken("user.age", prefix=" "), ), right_bracket=RightBracket(), ), )), right_brace=RightCurlyBrace(prefix=" "), ) assert str( node) == "set: user_dimensions { fields: [user.user_id, user.age] }" # Test a block with no expression node = BlockNode( type=SyntaxToken("set"), name=SyntaxToken("foo"), left_brace=LeftCurlyBrace(prefix=" "), container=tuple(), right_brace=RightCurlyBrace(), ) assert str(node) == "set: foo {}"
def test_container_node_str_should_return_formatted(): node = ContainerNode(( PairNode(SyntaxToken("hidden"), SyntaxToken("true")), BlockNode( type=SyntaxToken("set", prefix=" "), name=SyntaxToken("foo"), left_brace=LeftCurlyBrace(prefix=" "), container=tuple(), right_brace=RightCurlyBrace(), ), ListNode( type=SyntaxToken("fields", prefix=" "), left_bracket=LeftBracket(), items=tuple(), right_bracket=RightBracket(), ), )) assert str(node) == "hidden: true set: foo {} fields: []"
def parse_block( self, key: str, items: Dict[str, Any], name: Optional[str] = None ) -> BlockNode: """Serializes a dictionary to a LookML block. Args: key: A LookML field type (e.g. "dimension") fields: A dictionary to serialize (e.g. {"sql": "${TABLE}.order_id"}) name: An optional name of the block (e.g. "order_id") Returns: A generator of serialized string chunks """ prev_parent_key = self.parent_key self.parent_key = key latest_node_at_this_level = self.latest_node self.increase_level() nodes = [self.parse_any(key, value) for key, value in items.items()] self.decrease_level() self.latest_node = latest_node_at_this_level self.parent_key = prev_parent_key container = ContainerNode(items=tuple(flatten(nodes))) if self.latest_node and self.latest_node != DocumentNode: prefix = "\n" + self.newline_indent else: prefix = self.prefix node = BlockNode( type=SyntaxToken(key, prefix=prefix), left_brace=LeftCurlyBrace(prefix=" " if name else ""), right_brace=RightCurlyBrace( prefix=self.newline_indent if container.items else "" ), name=SyntaxToken(name) if name else None, container=container, ) self.latest_node = BlockNode return node
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"), name=SyntaxToken("dimension_name"), left_brace=LeftCurlyBrace(prefix=" "), container=ContainerNode(items=tuple()), right_brace=RightCurlyBrace(suffix=" "), )