sq/libsq/ast/selector_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

55 lines
1006 B
Go

package ast
import (
"testing"
"github.com/neilotoole/sq/testh/tutil"
"github.com/stretchr/testify/require"
"github.com/neilotoole/lg/testlg"
)
func TestColumnAlias(t *testing.T) {
t.Parallel()
testCases := []struct {
in string
wantErr bool
wantColName string
wantAlias string
}{
{
in: `@sakila | .actor | .first_name:given_name`,
wantColName: "first_name",
wantAlias: "given_name",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tutil.Name(tc.in), func(t *testing.T) {
t.Parallel()
log := testlg.New(t)
ast, err := Parse(log, tc.in)
if tc.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
insp := NewInspector(log, ast)
nodes := insp.FindNodes(typeColSelectorNode)
require.Equal(t, 1, len(nodes))
colSel, ok := nodes[0].(*ColSelectorNode)
require.True(t, ok)
require.Equal(t, tc.wantColName, colSel.ColName())
require.Equal(t, tc.wantAlias, colSel.Alias())
})
}
}