sq/libsq/ast/node_test.go
Neil O'Toole c778c8ced5
Linting of _test.go files (#121)
* test linting

* test linting

* test linting

* test linting
2022-12-18 00:18:35 -07:00

42 lines
1.1 KiB
Go

package ast
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/lg/testlg"
)
func TestChildIndex(t *testing.T) {
log := testlg.New(t).Strict(true)
// `@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 := childIndex(ast, seg)
require.Equal(t, i, index)
}
}
func TestNodesWithType(t *testing.T) {
nodes := []Node{&ColSelector{}, &ColSelector{}, &TblSelector{}, &RowRange{}}
require.Equal(t, 2, len(nodesWithType(nodes, typeColSelector)))
require.Equal(t, 1, len(nodesWithType(nodes, typeTblSelector)))
require.Equal(t, 1, len(nodesWithType(nodes, typeRowRange)))
require.Equal(t, 0, len(nodesWithType(nodes, typeJoin)))
}
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 := mustBuildAST(t, input)
require.NotNil(t, ast)
}