sq/libsq/query_orderby_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

72 lines
2.4 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
func TestQuery_orderby(t *testing.T) {
testCases := []queryTestCase{
{
name: "order_by/single-element",
in: `@sakila | .actor | order_by(.first_name)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/single-element-table-selector",
in: `@sakila | .actor | order_by(.actor.first_name)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "actor"."first_name"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `actor`.`first_name`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/single-element-asc",
in: `@sakila | .actor | order_by(.first_name+)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name" ASC`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name` ASC"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/single-element-desc",
in: `@sakila | .actor | order_by(.first_name-)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name" DESC`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name` DESC"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/multiple-elements",
in: `@sakila | .actor | order_by(.first_name+, .last_name-)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name" ASC, "last_name" DESC`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name` ASC, `last_name` DESC"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/synonym-sort-by",
in: `@sakila | .actor | sort_by(.first_name)`,
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name`"},
wantRecCount: sakila.TblActorCount,
},
{
name: "order_by/error-no-selector",
in: `@sakila | .actor | order_by()`,
wantErr: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}