sq/libsq/ast/node_test.go

42 lines
1.1 KiB
Go
Raw Normal View History

2016-10-17 07:14:01 +03:00
package ast
import (
"testing"
2020-08-06 20:58:47 +03:00
"github.com/stretchr/testify/require"
"github.com/neilotoole/lg/testlg"
2016-10-17 07:14:01 +03:00
)
func TestChildIndex(t *testing.T) {
2020-08-06 20:58:47 +03:00
log := testlg.New(t).Strict(true)
2016-10-17 07:14:01 +03:00
// `@mydb1 | .user, .address | join(.uid == .uid) | .uid, .username, .country`
2020-08-06 20:58:47 +03:00
p := getSLQParser(fixtJoinQuery1)
2016-10-17 07:14:01 +03:00
query := p.Query()
2020-08-06 20:58:47 +03:00
ast, err := buildAST(log, query)
require.Nil(t, err)
require.NotNil(t, ast)
require.Equal(t, 4, len(ast.Segments()))
2016-10-17 07:14:01 +03:00
for i, seg := range ast.Segments() {
2020-08-06 20:58:47 +03:00
index := childIndex(ast, seg)
require.Equal(t, i, index)
2016-10-17 07:14:01 +03:00
}
2020-08-06 20:58:47 +03:00
}
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)))
}
2016-10-17 07:14:01 +03:00
2020-08-06 20:58:47 +03:00
func TestAvg(t *testing.T) {
const input = `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country | .[0:2] | avg(.uid)` //nolint:lll
2020-08-06 20:58:47 +03:00
ast := mustBuildAST(t, input)
require.NotNil(t, ast)
2016-10-17 07:14:01 +03:00
}