sq/libsq/query_count_test.go

116 lines
3.9 KiB
Go
Raw Normal View History

package libsq_test
2023-04-01 11:38:32 +03:00
import (
"testing"
"github.com/neilotoole/sq/drivers/mysql"
"github.com/neilotoole/sq/libsq/source"
_ "github.com/mattn/go-sqlite3"
)
//nolint:exhaustive
func TestQuery_count(t *testing.T) {
testCases := []queryTestCase{
{
name: "alias",
in: `@sakila | .actor | count:quantity`,
wantSQL: `SELECT count(*) AS "quantity" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `quantity` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "count-same-alias",
in: `@sakila | .actor | count:count`,
wantSQL: `SELECT count(*) AS "count" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `count` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "whitespace-col",
in: `@sakila | .actor | count(."first name")`,
wantSQL: `SELECT count("first name") FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(`first name`) FROM `actor`"},
2023-04-01 11:38:32 +03:00
skipExec: true,
},
{
name: "select-handle-table",
in: `@sakila.actor | count`,
wantSQL: `SELECT count(*) AS "count" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `count` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "select-handle-table-ws-selector",
in: `@sakila.actor | count(."first name")`,
wantSQL: `SELECT count("first name") FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(`first name`) FROM `actor`"},
2023-04-01 11:38:32 +03:00
skipExec: true,
},
{
name: "no-parens-no-args-with-alias-unique",
// Test that the count:ALIAS form can handle the alias
// being a reserved word (unique).
in: `@sakila | .actor | count:unique`,
wantSQL: `SELECT count(*) AS "unique" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `unique` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "no-parens-no-args-with-alias-arbitrary",
in: `@sakila | .actor | count:something_123`,
wantSQL: `SELECT count(*) AS "something_123" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `something_123` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "parens-no-args",
in: `@sakila | .actor | count()`,
wantSQL: `SELECT count(*) AS "count" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `count` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "error-star",
in: `@sakila | .actor | count(*)`,
wantErr: true, // Star version is not supported
},
{
name: "single-selector",
in: `@sakila | .actor | count(.first_name)`,
wantSQL: `SELECT count("first_name") FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(`first_name`) FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "error-multiple-selector",
in: `@sakila | .actor | count(.first_name, .last_name)`,
wantErr: true, // Only a single selector is permitted
},
{
name: "count/no-parens-no-args",
in: `@sakila | .actor | count`,
wantSQL: `SELECT count(*) AS "count" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `count` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
{
name: "count/no-parens-no-args-with-alias-count",
// Test that the count:ALIAS form can handle the alias
// being a reserved word (count).
in: `@sakila | .actor | count:count`,
wantSQL: `SELECT count(*) AS "count" FROM "actor"`,
override: map[source.DriverType]string{mysql.Type: "SELECT count(*) AS `count` FROM `actor`"},
wantRecCount: 1,
2023-04-01 11:38:32 +03:00
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}