2016-10-17 07:14:01 +03:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/slogt"
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-17 07:14:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInspector_findSelectableSegments(t *testing.T) {
|
2023-04-02 22:49:45 +03:00
|
|
|
log := slogt.New(t)
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
// `@mydb1 | .user | .uid, .username`
|
2020-08-06 20:58:47 +03:00
|
|
|
ast, err := buildInitialAST(t, fixtSelect1)
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Nil(t, err)
|
2023-03-22 09:17:34 +03:00
|
|
|
err = NewWalker(log, ast).AddVisitor(typeSelectorNode, narrowTblSel).Walk()
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
insp := NewInspector(log, ast)
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
segs := ast.Segments()
|
|
|
|
require.Equal(t, 3, len(segs))
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
selSegs := insp.FindTablerSegments()
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Equal(t, 1, len(selSegs), "should be 1 selectable segment: the tbl sel segment")
|
2023-03-28 09:48:24 +03:00
|
|
|
finalSelSeg, err := insp.FindFinalTablerSegment()
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, selSegs[0], finalSelSeg)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country`
|
|
|
|
ast, err = buildInitialAST(t, fixtJoinQuery1)
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Nil(t, err)
|
2023-03-22 09:17:34 +03:00
|
|
|
err = NewWalker(log, ast).AddVisitor(typeSelectorNode, narrowTblSel).Walk()
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Nil(t, err)
|
2020-08-06 20:58:47 +03:00
|
|
|
insp = NewInspector(log, ast)
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
segs = ast.Segments()
|
|
|
|
require.Equal(t, 4, len(segs))
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
selSegs = insp.FindTablerSegments()
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Equal(t, 2, len(selSegs), "should be 2 selectable segments: the tbl selector segment, and the join segment")
|
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
finalSelSeg, err = insp.FindFinalTablerSegment()
|
2016-10-17 07:14:01 +03:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, selSegs[1], finalSelSeg)
|
|
|
|
}
|