sq/libsq/ast/walker_test.go
Neil O'Toole a1a89ee9dd
Support table and column names with spaces. (#156)
* sakila: initial test data

* sakila: more test data

* sakila: yet more test data setup

* whitespace cols: now working for sqlite

* grammar cleanup

* whitespace cols: now working inside count() func for sqlite

* whitespace cols: tests mostly passing; begining refactoring

* grammar: refactor handle

* grammar: more refactoring

* grammar: rename selElement to selector

* wip

* all tests passing

* all tests passing

* linting

* driver: implement CurrentSchema for all driver.SQLDriver impls

* driver: tests for AlterTableRename and AlterTableRenameColumn

* undo reformat of SQL

* undo reformat of SQL

* undo reformat of SQL

* undo reformat of SQL
2023-03-22 00:17:34 -06:00

56 lines
1.2 KiB
Go

package ast
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/neilotoole/lg"
"github.com/neilotoole/lg/testlg"
)
func TestWalker(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)
assert.Nil(t, err)
assert.NotNil(t, ast)
walker := NewWalker(log, ast)
count := 0
visitor := func(log lg.Log, w *Walker, node Node) error {
count++
return w.visitChildren(node)
}
walker.AddVisitor(typeJoinNode, visitor)
err = walker.Walk()
assert.Nil(t, err)
assert.Equal(t, 1, count)
// test multiple visitors on the same node type
walker = NewWalker(log, ast)
countA := 0
visitorA := func(log lg.Log, w *Walker, node Node) error {
countA++
return w.visitChildren(node)
}
countB := 0
visitorB := func(log lg.Log, w *Walker, node Node) error {
countB++
return w.visitChildren(node)
}
walker.AddVisitor(typeTblSelectorNode, visitorA)
walker.AddVisitor(typeColSelectorNode, visitorB)
err = walker.Walk()
assert.Nil(t, err)
assert.Equal(t, 2, countA)
assert.Equal(t, 3, countB)
}