2023-04-07 11:00:49 +03:00
|
|
|
package libsq_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-06-17 07:54:25 +03:00
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
"github.com/neilotoole/sq/drivers/mysql"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
2023-04-07 11:00:49 +03:00
|
|
|
)
|
|
|
|
|
2023-06-18 09:05:09 +03:00
|
|
|
//nolint:exhaustive
|
2023-06-17 07:54:25 +03:00
|
|
|
func TestQuery_expr_where(t *testing.T) {
|
2023-04-07 11:00:49 +03:00
|
|
|
testCases := []queryTestCase{
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "literal/string",
|
|
|
|
in: `@sakila | .actor | where(.first_name == "TOM")`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" WHERE "first_name" = 'TOM'`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` WHERE `first_name` = 'TOM'"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: 2,
|
2023-04-07 11:00:49 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "literal/two-strings",
|
|
|
|
in: `@sakila | .actor | where(.first_name == "TOM" && .last_name == "MIRANDA")`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" WHERE "first_name" = 'TOM' AND "last_name" = 'MIRANDA'`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` WHERE `first_name` = 'TOM' AND `last_name` = 'MIRANDA'"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: 1,
|
2023-04-07 11:00:49 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "literal/integer",
|
|
|
|
in: `@sakila | .actor | where(.actor_id == 1)`,
|
|
|
|
wantSQL: `SELECT * FROM "actor" WHERE "actor_id" = 1`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `actor` WHERE `actor_id` = 1"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: 1,
|
2023-04-07 11:00:49 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "is_null",
|
|
|
|
in: `@sakila | .address | where(.postal_code == null)`,
|
|
|
|
wantSQL: `SELECT * FROM "address" WHERE "postal_code" IS NULL`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `address` WHERE `postal_code` IS NULL"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: 4,
|
2023-04-07 11:00:49 +03:00
|
|
|
// skipExec because mysql sakila db doesn't have the same null values.
|
|
|
|
// This is a bug in the dataset.
|
|
|
|
skipExec: true,
|
|
|
|
},
|
|
|
|
{
|
2023-06-17 07:54:25 +03:00
|
|
|
name: "is_not_null",
|
|
|
|
in: `@sakila | .address | where(.postal_code != null)`,
|
|
|
|
wantSQL: `SELECT * FROM "address" WHERE "postal_code" IS NOT NULL`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT * FROM `address` WHERE `postal_code` IS NOT NULL"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: 599,
|
2023-04-07 11:00:49 +03:00
|
|
|
// skipExec because mysql sakila db doesn't have the same null values.
|
|
|
|
// This is a bug in the dataset.
|
|
|
|
skipExec: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
execQueryTestCase(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 07:54:25 +03:00
|
|
|
|
|
|
|
//nolint:exhaustive
|
|
|
|
func TestQuery_expr_literal(t *testing.T) {
|
|
|
|
testCases := []queryTestCase{
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/col_and_literal",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | .first_name, 1`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT "first_name", 1 AS "1" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT `first_name`, 1 AS `1` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(1, int64(1))},
|
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/literal",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | 1`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT 1 AS "1" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT 1 AS `1` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(1))},
|
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "no-table/literal",
|
|
|
|
in: `@sakila | 1`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT 1 AS "1"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT 1 AS `1`"},
|
2023-06-18 04:28:11 +03:00
|
|
|
wantRecCount: 1,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(1))},
|
|
|
|
},
|
2023-06-18 09:05:09 +03:00
|
|
|
{
|
|
|
|
name: "no-table/literal_addition",
|
|
|
|
in: `@sakila | 1+1`,
|
|
|
|
wantSQL: `SELECT 1+1 AS "1+1"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT 1+1 AS `1+1`"},
|
|
|
|
wantRecCount: 1,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(2))},
|
|
|
|
},
|
2023-06-18 04:28:11 +03:00
|
|
|
{
|
|
|
|
name: "table/literal_parens",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | (1)`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT (1) AS "(1)" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT (1) AS `(1)` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(1))},
|
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/addition",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | 1+2`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT 1+2 AS "1+2" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT 1+2 AS `1+2` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(3))},
|
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/addition_whitespace",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | 1+ 2 + 3`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT 1+2+3 AS "1+2+3" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT 1+2+3 AS `1+2+3` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(6))},
|
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/math_parens",
|
2023-06-18 09:05:09 +03:00
|
|
|
in: `@sakila | .actor | ((2+2) * 3)`,
|
|
|
|
wantSQL: `SELECT ((2+2)*3) AS "((2+2)*3)" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT ((2+2)*3) AS `((2+2)*3)` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
2023-06-18 09:05:09 +03:00
|
|
|
sinkFns: []SinkTestFunc{assertSinkColValue(0, int64(12))},
|
2023-06-17 07:54:25 +03:00
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/literal_alias",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | 1:total`,
|
|
|
|
wantSQL: `SELECT 1 AS "total" FROM "actor"`,
|
2023-06-18 09:05:09 +03:00
|
|
|
override: driverMap{mysql.Type: "SELECT 1 AS `total` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
sinkFns: []SinkTestFunc{
|
|
|
|
assertSinkColName(0, "total"),
|
|
|
|
assertSinkColValue(0, int64(1)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-06-18 04:28:11 +03:00
|
|
|
name: "table/addition_alias",
|
2023-06-17 07:54:25 +03:00
|
|
|
in: `@sakila | .actor | (1+2):total`,
|
2023-06-18 09:05:09 +03:00
|
|
|
wantSQL: `SELECT (1+2) AS "total" FROM "actor"`,
|
|
|
|
override: driverMap{mysql.Type: "SELECT (1+2) AS `total` FROM `actor`"},
|
2023-06-17 07:54:25 +03:00
|
|
|
sinkFns: []SinkTestFunc{
|
|
|
|
assertSinkColValue(0, int64(3)),
|
|
|
|
assertSinkColName(0, "total"),
|
|
|
|
},
|
|
|
|
wantRecCount: sakila.TblActorCount,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-06-18 09:05:09 +03:00
|
|
|
for i, tc := range testCases {
|
2023-06-17 07:54:25 +03:00
|
|
|
tc := tc
|
2023-06-18 09:05:09 +03:00
|
|
|
t.Run(tutil.Name(i, tc.name), func(t *testing.T) {
|
2023-06-17 07:54:25 +03:00
|
|
|
execQueryTestCase(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|