2023-04-01 12:48:24 +03:00
|
|
|
package libsq_test
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-27 06:11:38 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/mysql"
|
2023-04-01 11:38:32 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/sqlserver"
|
|
|
|
|
2023-05-27 06:11:38 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/postgres"
|
2023-04-01 11:38:32 +03:00
|
|
|
|
2023-05-27 06:11:38 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/sqlite3"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
2023-04-01 11:38:32 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/source"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQuery_datetime(t *testing.T) {
|
|
|
|
testCases := []queryTestCase{
|
|
|
|
{
|
|
|
|
name: "datetime/strftime/sqlite",
|
2023-05-27 06:11:38 +03:00
|
|
|
in: `@sakila | .payment | _strftime("%m", .payment_date)`,
|
2023-04-01 11:38:32 +03:00
|
|
|
wantSQL: `SELECT strftime('%m', "payment_date") AS "strftime(""%m"",.payment_date)" FROM "payment"`,
|
2023-04-22 06:36:32 +03:00
|
|
|
onlyFor: []source.DriverType{sqlite3.Type},
|
2023-04-01 11:38:32 +03:00
|
|
|
wantRecs: sakila.TblPaymentCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "datetime/date_trunc/postgres",
|
2023-05-27 06:11:38 +03:00
|
|
|
in: `@sakila | .payment | _date_trunc("month", .payment_date)`,
|
2023-04-01 11:38:32 +03:00
|
|
|
wantSQL: `SELECT date_trunc('month', "payment_date") AS "date_trunc(""month"",.payment_date)" FROM "payment"`,
|
2023-04-22 06:36:32 +03:00
|
|
|
onlyFor: []source.DriverType{postgres.Type},
|
2023-04-01 11:38:32 +03:00
|
|
|
wantRecs: sakila.TblPaymentCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "datetime/month/sqlserver",
|
2023-05-27 06:11:38 +03:00
|
|
|
in: `@sakila | .payment | _month(.payment_date)`,
|
2023-04-01 11:38:32 +03:00
|
|
|
wantSQL: `SELECT month("payment_date") AS "month(.payment_date)" FROM "payment"`,
|
2023-04-22 06:36:32 +03:00
|
|
|
onlyFor: []source.DriverType{sqlserver.Type},
|
2023-04-01 11:38:32 +03:00
|
|
|
wantRecs: sakila.TblPaymentCount,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "datetime/date_format/mysql",
|
2023-05-27 06:11:38 +03:00
|
|
|
in: `@sakila | .payment | _date_format(.payment_date, "%m")`,
|
2023-04-01 11:38:32 +03:00
|
|
|
wantSQL: "SELECT date_format(`payment_date`, '%m') AS `date_format(.payment_date,\"%m\")` FROM `payment`",
|
2023-04-22 06:36:32 +03:00
|
|
|
onlyFor: []source.DriverType{mysql.Type},
|
2023-04-01 11:38:32 +03:00
|
|
|
wantRecs: sakila.TblPaymentCount,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
execQueryTestCase(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|