2023-04-01 12:48:24 +03:00
package libsq_test
2023-04-01 11:38:32 +03:00
import (
"testing"
2023-11-20 04:06:36 +03:00
_ "github.com/mattn/go-sqlite3"
2023-04-01 11:38:32 +03:00
2023-11-21 00:42:38 +03:00
"github.com/neilotoole/sq/libsq/source/drivertype"
2023-04-01 11:38:32 +03:00
)
//nolint:exhaustive,lll
func TestQuery_groupby ( t * testing . T ) {
testCases := [ ] queryTestCase {
{
2023-06-17 07:54:25 +03:00
name : "group_by/single-term" ,
in : ` @sakila | .payment | .customer_id, sum(.amount) | group_by(.customer_id) ` ,
wantSQL : ` SELECT "customer_id", sum("amount") AS "sum(.amount)" FROM "payment" GROUP BY "customer_id" ` ,
2024-01-25 09:29:55 +03:00
override : driverMap { drivertype . MySQL : "SELECT `customer_id`, sum(`amount`) AS `sum(.amount)` FROM `payment` GROUP BY `customer_id`" } ,
2023-06-17 07:54:25 +03:00
wantRecCount : 599 ,
2023-04-01 11:38:32 +03:00
} ,
2023-11-22 21:36:34 +03:00
{
name : "alias-gb" ,
in : ` @sakila | .payment | .customer_id, sum(.amount) | gb(.customer_id) ` ,
wantSQL : ` SELECT "customer_id", sum("amount") AS "sum(.amount)" FROM "payment" GROUP BY "customer_id" ` ,
2024-01-25 09:29:55 +03:00
override : driverMap { drivertype . MySQL : "SELECT `customer_id`, sum(`amount`) AS `sum(.amount)` FROM `payment` GROUP BY `customer_id`" } ,
2023-11-22 21:36:34 +03:00
wantRecCount : 599 ,
} ,
2023-04-01 11:38:32 +03:00
{
2023-06-17 07:54:25 +03:00
name : "group_by/multiple_terms" ,
in : ` @sakila | .payment | .customer_id, .staff_id, sum(.amount) | group_by(.customer_id, .staff_id) ` ,
wantSQL : ` SELECT "customer_id", "staff_id", sum("amount") AS "sum(.amount)" FROM "payment" GROUP BY "customer_id", "staff_id" ` ,
2024-01-25 09:29:55 +03:00
override : driverMap { drivertype . MySQL : "SELECT `customer_id`, `staff_id`, sum(`amount`) AS `sum(.amount)` FROM `payment` GROUP BY `customer_id`, `staff_id`" } ,
2023-06-17 07:54:25 +03:00
wantRecCount : 1198 ,
2023-04-01 11:38:32 +03:00
} ,
{
2023-06-17 07:54:25 +03:00
name : "group_by/with_func/sqlite" ,
in : ` @sakila | .payment | _date("month", .payment_date):month, count(.payment_id):count | group_by(_date("month", .payment_date)) ` ,
wantSQL : ` SELECT date('month', "payment_date") AS "month", count("payment_id") AS "count" FROM "payment" GROUP BY date('month', "payment_date") ` ,
2024-01-25 09:29:55 +03:00
onlyFor : [ ] drivertype . Type { drivertype . SQLite } ,
2023-06-17 07:54:25 +03:00
wantRecCount : 1 ,
2023-04-01 11:38:32 +03:00
} ,
2023-11-22 20:56:19 +03:00
{
name : "group_by/having" ,
in : ` @sakila | .payment | .customer_id, sum(.amount) | group_by(.customer_id) | having(sum(.amount) > 100) ` ,
wantSQL : ` SELECT "customer_id", sum("amount") AS "sum(.amount)" FROM "payment" GROUP BY "customer_id" HAVING sum("amount") > 100 ` ,
2024-01-25 09:29:55 +03:00
override : driverMap { drivertype . MySQL : "SELECT `customer_id`, sum(`amount`) AS `sum(.amount)` FROM `payment` GROUP BY `customer_id` HAVING sum(`amount`) > 100" } ,
2023-11-22 20:56:19 +03:00
wantRecCount : 395 ,
} ,
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 )
} )
}
}