2016-10-17 07:14:01 +03:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSegment(t *testing.T) {
|
|
|
|
// `@mydb1 | .user, .address | join(.uid == .uid) | .uid, .username, .country`
|
2023-03-19 07:58:00 +03:00
|
|
|
ast := mustParse(t, fixtJoinQuery1)
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
segs := ast.Segments()
|
|
|
|
assert.Equal(t, 4, len(segs))
|
|
|
|
|
|
|
|
assert.Nil(t, ast.Segments()[0].Prev(), "first segment should not have a parent")
|
|
|
|
assert.Equal(t, ast.Segments()[0], ast.Segments()[1].Prev())
|
|
|
|
assert.Equal(t, ast.Segments()[1], ast.Segments()[2].Prev())
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
ok, err := ast.Segments()[0].uniformChildren()
|
2016-10-17 07:14:01 +03:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
typ, err := ast.Segments()[0].ChildType()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, typ)
|
2023-03-22 09:17:34 +03:00
|
|
|
assert.Equal(t, typeHandleNode.String(), typ.String())
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
typ, err = ast.Segments()[1].ChildType()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, typ)
|
2023-03-22 09:17:34 +03:00
|
|
|
assert.Equal(t, typeTblSelectorNode.String(), typ.String())
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
typ, err = ast.Segments()[2].ChildType()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, typ)
|
2023-03-22 09:17:34 +03:00
|
|
|
assert.Equal(t, typeJoinNode.String(), typ.String())
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
typ, err = ast.Segments()[3].ChildType()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, typ)
|
2023-03-22 09:17:34 +03:00
|
|
|
assert.Equal(t, typeColSelectorNode.String(), typ.String())
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|