sq/libsq/ast/node_test.go
Neil O'Toole 9a1c6a7d09
Feature/173 args (#183)
- Implement --arg feature
- Refactor sqlbuilder package (now called "render").
- Bug fixes, especially around expressions.
2023-04-07 02:00:49 -06:00

61 lines
1.6 KiB
Go

package ast
import (
"testing"
"github.com/neilotoole/slogt"
"github.com/stretchr/testify/require"
)
func TestChildIndex(t *testing.T) {
log := slogt.New(t)
// `@mydb1 | .user, .address | join(.uid == .uid) | .uid, .username, .country`
p := getSLQParser(fixtJoinQuery1)
query := p.Query()
ast, err := buildAST(log, query)
require.Nil(t, err)
require.NotNil(t, ast)
require.Equal(t, 4, len(ast.Segments()))
for i, seg := range ast.Segments() {
index := nodeChildIndex(ast, seg)
require.Equal(t, i, index)
}
}
func TestNodesWithType(t *testing.T) {
nodes := []Node{&ColSelectorNode{}, &ColSelectorNode{}, &TblSelectorNode{}, &RowRangeNode{}}
require.Equal(t, 2, len(nodesWithType(nodes, typeColSelectorNode)))
require.Equal(t, 1, len(nodesWithType(nodes, typeTblSelectorNode)))
require.Equal(t, 1, len(nodesWithType(nodes, typeRowRangeNode)))
require.Equal(t, 0, len(nodesWithType(nodes, typeJoinNode)))
}
func TestAvg(t *testing.T) {
const input = `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country | .[0:2] | avg(.uid)` //nolint:lll
ast := mustParse(t, input)
require.NotNil(t, ast)
}
func TestNodePrevNextSibling(t *testing.T) {
const in = `@sakila | .actor | .actor_id == 2`
log := slogt.New(t)
a, err := Parse(log, in)
require.NoError(t, err)
equalsNode := NodesHavingText(a, "==")[0]
gotPrev := NodePrevSibling(equalsNode)
require.Equal(t, ".actor_id", gotPrev.Text())
require.Nil(t, NodePrevSibling(gotPrev))
gotNext := NodeNextSibling(equalsNode)
require.Equal(t, "2", gotNext.Text())
require.Nil(t, NodeNextSibling(gotNext))
}