2023-04-01 12:48:24 +03:00
|
|
|
package libsq_test
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/drivers/mysql"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2023-06-18 09:05:09 +03:00
|
|
|
//nolint:exhaustive
|
2023-04-01 11:38:32 +03:00
|
|
|
func TestQuery_orderby(t *testing.T) {
|
|
|
|
testCases := []queryTestCase{
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "order_by/single-element",
|
|
|
|
in: `@sakila | .actor | order_by(.first_name)`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name"`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-04-01 11:38:32 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "order_by/single-element-table-selector",
|
|
|
|
in: `@sakila | .actor | order_by(.actor.first_name)`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" ORDER BY "actor"."first_name"`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `actor`.`first_name`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-04-01 11:38:32 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "order_by/single-element-asc",
|
|
|
|
in: `@sakila | .actor | order_by(.first_name+)`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name" ASC`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name` ASC"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-04-01 11:38:32 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "order_by/single-element-desc",
|
|
|
|
in: `@sakila | .actor | order_by(.first_name-)`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name" DESC`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name` DESC"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-04-01 11:38:32 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
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`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name` ASC, `last_name` DESC"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-04-01 11:38:32 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "order_by/synonym-sort-by",
|
|
|
|
in: `@sakila | .actor | sort_by(.first_name)`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" ORDER BY "first_name"`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` ORDER BY `first_name`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-04-01 11:38:32 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|