sq/libsq/query_cols_test.go
Neil O'Toole 2ba633fc2a
#258: Alias can be an arbitrary string. (#259)
* Fixed space issues with expressions

* Alias can now be an arbitrary string

* Alias can now be an arbitrary string (fixed)

* Alias now automatically applied to expressions

* Ignore .run

* Fixed issue with TestRun not logging correctly to testing.T

* Fiddling with sqlite3 temp file closing

* Re-enable tests
2023-06-18 00:05:09 -06:00

90 lines
3.2 KiB
Go

package libsq_test
import (
"testing"
"github.com/neilotoole/sq/testh/sakila"
"github.com/neilotoole/sq/drivers/mysql"
_ "github.com/mattn/go-sqlite3"
)
//nolint:exhaustive,lll
func TestQuery_cols(t *testing.T) {
testCases := []queryTestCase{
{
name: "cols",
in: `@sakila | .actor | .first_name, .last_name`,
wantSQL: `SELECT "first_name", "last_name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first_name`, `last_name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "cols-whitespace-single-col",
in: `@sakila | .actor | ."first name"`,
wantSQL: `SELECT "first name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
skipExec: true,
},
{
name: "cols-whitespace-multiple-cols",
in: `@sakila | .actor | .actor_id, ."first name", ."last name"`,
wantSQL: `SELECT "actor_id", "first name", "last name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `actor_id`, `first name`, `last name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
skipExec: true,
},
{
name: "table-whitespace",
in: `@sakila | ."film actor"`,
wantSQL: `SELECT * FROM "film actor"`,
override: driverMap{mysql.Type: "SELECT * FROM `film actor`"},
skipExec: true,
},
{
name: "cols-aliases",
in: `@sakila | .actor | .first_name:given_name, .last_name:family_name`,
wantSQL: `SELECT "first_name" AS "given_name", "last_name" AS "family_name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first_name` AS `given_name`, `last_name` AS `family_name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "cols-aliases-whitespace",
in: `@sakila | .actor | .first_name:"Given Name", .last_name:family_name`,
wantSQL: `SELECT "first_name" AS "Given Name", "last_name" AS "family_name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first_name` AS `Given Name`, `last_name` AS `family_name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "handle-table/cols",
in: `@sakila.actor | .first_name, .last_name`,
wantSQL: `SELECT "first_name", "last_name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first_name`, `last_name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "cols-select-literal-value",
in: `@sakila.actor | .first_name, "xxx", .last_name`,
wantSQL: `SELECT "first_name", 'xxx' AS "xxx", "last_name" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first_name`, 'xxx' AS `xxx`, `last_name` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "select/literal",
in: `@sakila.actor | .first_name, 5`,
wantSQL: `SELECT "first_name", 5 AS "5" FROM "actor"`,
override: driverMap{mysql.Type: "SELECT `first_name`, 5 AS `5` FROM `actor`"},
wantRecCount: sakila.TblActorCount,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}