MySQL select statement tests.
This commit is contained in:
parent
449cd9fd8f
commit
6af43f0c46
22 changed files with 65812 additions and 47193 deletions
|
|
@ -95,7 +95,7 @@ LIMIT $5;
|
|||
|
||||
//testutils.JsonPrint(dest)
|
||||
|
||||
testutils.AssertJSON(t, dest, `
|
||||
testutils.AssertJSON(t, `
|
||||
[
|
||||
{
|
||||
"IsNull": false,
|
||||
|
|
@ -114,7 +114,7 @@ LIMIT $5;
|
|||
"NotInSelect": null
|
||||
}
|
||||
]
|
||||
`)
|
||||
`, dest)
|
||||
}
|
||||
|
||||
func TestExpressionCast(t *testing.T) {
|
||||
|
|
@ -299,7 +299,7 @@ LIMIT $5;
|
|||
|
||||
assert.NilError(t, err)
|
||||
|
||||
testutils.AssertJSONFile(t, "./testdata/common/bool_operators.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./testdata/common/bool_operators.json")
|
||||
}
|
||||
|
||||
func TestFloatOperators(t *testing.T) {
|
||||
|
|
@ -397,7 +397,7 @@ LIMIT $35;
|
|||
|
||||
//testutils.JsonPrint(dest)
|
||||
|
||||
testutils.AssertJSONFile(t, "./testdata/common/float_operators.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./testdata/common/float_operators.json")
|
||||
}
|
||||
|
||||
func TestIntegerOperators(t *testing.T) {
|
||||
|
|
@ -536,7 +536,7 @@ LIMIT $22;
|
|||
|
||||
//testutils.JsonSave("./testdata/common/int_operators.json", dest)
|
||||
//testutils.JsonPrint(dest)
|
||||
testutils.AssertJSONFile(t, "./testdata/common/int_operators.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./testdata/common/int_operators.json")
|
||||
}
|
||||
|
||||
func TestTimeOperators(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ func TestJoinEverything(t *testing.T) {
|
|||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(dest), 275)
|
||||
testutils.AssertJSONFile(t, "./postgres/testdata/joined_everything.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./postgres/testdata/joined_everything.json")
|
||||
}
|
||||
|
||||
func TestSelfJoin(t *testing.T) {
|
||||
|
|
@ -142,7 +142,7 @@ ORDER BY "Employee"."EmployeeId";
|
|||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(dest), 8)
|
||||
testutils.AssertJSON(t, dest[0:2], `
|
||||
testutils.AssertJSON(t, `
|
||||
[
|
||||
{
|
||||
"EmployeeId": 1,
|
||||
|
|
@ -197,7 +197,7 @@ ORDER BY "Employee"."EmployeeId";
|
|||
}
|
||||
}
|
||||
]
|
||||
`)
|
||||
`, dest[0:2])
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -62,5 +62,5 @@ func TestNorthwindJoinEverything(t *testing.T) {
|
|||
assert.NilError(t, err)
|
||||
|
||||
//jsonSave("./testdata/northwind-all.json", dest)
|
||||
testutils.AssertJSONFile(t, "./postgres/testdata/northwind-all.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./postgres/testdata/northwind-all.json")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ FROM test_sample.person;
|
|||
err := query.Query(db, &result)
|
||||
|
||||
assert.NilError(t, err)
|
||||
testutils.AssertJSON(t, result, `
|
||||
testutils.AssertJSON(t, `
|
||||
[
|
||||
{
|
||||
"PersonID": "b68dbff4-a87d-11e9-a7f2-98ded00c39c6",
|
||||
|
|
@ -68,7 +68,7 @@ FROM test_sample.person;
|
|||
"Mood": "ok"
|
||||
}
|
||||
]
|
||||
`)
|
||||
`, result)
|
||||
}
|
||||
|
||||
func TestSelecSelfJoin1(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -534,7 +534,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(dest), 2)
|
||||
testutils.AssertJSON(t, dest, `
|
||||
testutils.AssertJSON(t, `
|
||||
[
|
||||
{
|
||||
"CityID": 312,
|
||||
|
|
@ -573,7 +573,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
]
|
||||
}
|
||||
]
|
||||
`)
|
||||
`, dest)
|
||||
}
|
||||
|
||||
func TestJoinQuerySliceWithPtrs(t *testing.T) {
|
||||
|
|
@ -992,60 +992,72 @@ ORDER BY film.film_id ASC;
|
|||
|
||||
func TestSelectGroupByHaving(t *testing.T) {
|
||||
expectedSQL := `
|
||||
SELECT payment.customer_id AS "customer_payment_sum.customer_id",
|
||||
SUM(payment.amount) AS "customer_payment_sum.amount_sum",
|
||||
AVG(payment.amount) AS "customer_payment_sum.amount_avg",
|
||||
MAX(payment.amount) AS "customer_payment_sum.amount_max",
|
||||
MIN(payment.amount) AS "customer_payment_sum.amount_min",
|
||||
COUNT(payment.amount) AS "customer_payment_sum.amount_count"
|
||||
SELECT customer.customer_id AS "customer.customer_id",
|
||||
customer.store_id AS "customer.store_id",
|
||||
customer.first_name AS "customer.first_name",
|
||||
customer.last_name AS "customer.last_name",
|
||||
customer.email AS "customer.email",
|
||||
customer.address_id AS "customer.address_id",
|
||||
customer.activebool AS "customer.activebool",
|
||||
customer.create_date AS "customer.create_date",
|
||||
customer.last_update AS "customer.last_update",
|
||||
customer.active AS "customer.active",
|
||||
SUM(payment.amount) AS "amount.sum",
|
||||
AVG(payment.amount) AS "amount.avg",
|
||||
MAX(payment.amount) AS "amount.max",
|
||||
MIN(payment.amount) AS "amount.min",
|
||||
COUNT(payment.amount) AS "amount.count"
|
||||
FROM dvds.payment
|
||||
GROUP BY payment.customer_id
|
||||
HAVING SUM(payment.amount) > 100
|
||||
ORDER BY SUM(payment.amount) ASC;
|
||||
INNER JOIN dvds.customer ON (customer.customer_id = payment.customer_id)
|
||||
GROUP BY customer.customer_id
|
||||
HAVING SUM(payment.amount) > 125.6
|
||||
ORDER BY customer.customer_id, SUM(payment.amount) ASC;
|
||||
`
|
||||
customersPaymentQuery := Payment.
|
||||
query := Payment.
|
||||
INNER_JOIN(Customer, Customer.CustomerID.EQ(Payment.CustomerID)).
|
||||
SELECT(
|
||||
Payment.CustomerID.AS("customer_payment_sum.customer_id"),
|
||||
SUMf(Payment.Amount).AS("customer_payment_sum.amount_sum"),
|
||||
AVG(Payment.Amount).AS("customer_payment_sum.amount_avg"),
|
||||
MAXf(Payment.Amount).AS("customer_payment_sum.amount_max"),
|
||||
MINf(Payment.Amount).AS("customer_payment_sum.amount_min"),
|
||||
COUNT(Payment.Amount).AS("customer_payment_sum.amount_count"),
|
||||
).
|
||||
GROUP_BY(Payment.CustomerID).
|
||||
ORDER_BY(
|
||||
SUMf(Payment.Amount).ASC(),
|
||||
Customer.AllColumns,
|
||||
|
||||
SUMf(Payment.Amount).AS("amount.sum"),
|
||||
AVG(Payment.Amount).AS("amount.avg"),
|
||||
MAXf(Payment.Amount).AS("amount.max"),
|
||||
MINf(Payment.Amount).AS("amount.min"),
|
||||
COUNT(Payment.Amount).AS("amount.count"),
|
||||
).
|
||||
GROUP_BY(Customer.CustomerID).
|
||||
HAVING(
|
||||
SUMf(Payment.Amount).GT(Float(100)),
|
||||
SUMf(Payment.Amount).GT(Float(125.6)),
|
||||
).
|
||||
ORDER_BY(
|
||||
Customer.CustomerID, SUMf(Payment.Amount).ASC(),
|
||||
)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, customersPaymentQuery, expectedSQL, float64(100))
|
||||
//fmt.Println(query.DebugSql())
|
||||
|
||||
type CustomerPaymentSum struct {
|
||||
CustomerID int16
|
||||
AmountSum float64
|
||||
AmountAvg float64
|
||||
AmountMax float64
|
||||
AmountMin float64
|
||||
AmountCount int64
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, float64(125.6))
|
||||
|
||||
var dest []struct {
|
||||
model.Customer
|
||||
|
||||
Amount struct {
|
||||
Sum float64
|
||||
Avg float64
|
||||
Max float64
|
||||
Min float64
|
||||
Count int64
|
||||
} `alias:"amount"`
|
||||
}
|
||||
|
||||
customerPaymentSum := []CustomerPaymentSum{}
|
||||
|
||||
err := customersPaymentQuery.Query(db, &customerPaymentSum)
|
||||
err := query.Query(db, &dest)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(customerPaymentSum), 296)
|
||||
assert.DeepEqual(t, customerPaymentSum[0], CustomerPaymentSum{
|
||||
CustomerID: 135,
|
||||
AmountSum: 100.72,
|
||||
AmountAvg: 3.597142857142857,
|
||||
AmountMax: 7.99,
|
||||
AmountMin: 0.99,
|
||||
AmountCount: 28,
|
||||
})
|
||||
//testutils.JsonPrint(dest)
|
||||
|
||||
assert.Equal(t, len(dest), 104)
|
||||
|
||||
//testutils.JsonSave(dest, "postgres/testdata/customer_payment_sum.json")
|
||||
testutils.AssertJSONFile(t, dest, "postgres/testdata/customer_payment_sum.json")
|
||||
}
|
||||
|
||||
func TestSelectGroupBy2(t *testing.T) {
|
||||
|
|
@ -1125,7 +1137,7 @@ func TestSelectStaff(t *testing.T) {
|
|||
|
||||
assert.NilError(t, err)
|
||||
|
||||
testutils.AssertJSON(t, staffs, `
|
||||
testutils.AssertJSON(t, `
|
||||
[
|
||||
{
|
||||
"StaffID": 1,
|
||||
|
|
@ -1154,7 +1166,7 @@ func TestSelectStaff(t *testing.T) {
|
|||
"Picture": null
|
||||
}
|
||||
]
|
||||
`)
|
||||
`, staffs)
|
||||
}
|
||||
|
||||
func TestSelectTimeColumns(t *testing.T) {
|
||||
|
|
@ -1460,7 +1472,7 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
|
|||
assert.NilError(t, err)
|
||||
|
||||
//jsonSave("./testdata/quick-start-dest.json", dest)
|
||||
testutils.AssertJSONFile(t, "./postgres/testdata/quick-start-dest.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./postgres/testdata/quick-start-dest.json")
|
||||
|
||||
var dest2 []struct {
|
||||
model.Category
|
||||
|
|
@ -1473,7 +1485,7 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
|
|||
assert.NilError(t, err)
|
||||
|
||||
//jsonSave("./testdata/quick-start-dest2.json", dest2)
|
||||
testutils.AssertJSONFile(t, "./postgres/testdata/quick-start-dest2.json", dest2)
|
||||
testutils.AssertJSONFile(t, dest2, "./postgres/testdata/quick-start-dest2.json")
|
||||
}
|
||||
|
||||
func TestQuickStartWithSubQueries(t *testing.T) {
|
||||
|
|
@ -1525,7 +1537,7 @@ func TestQuickStartWithSubQueries(t *testing.T) {
|
|||
assert.NilError(t, err)
|
||||
|
||||
//jsonSave("./testdata/quick-start-dest.json", dest)
|
||||
testutils.AssertJSONFile(t, "./postgres/testdata/quick-start-dest.json", dest)
|
||||
testutils.AssertJSONFile(t, dest, "./postgres/testdata/quick-start-dest.json")
|
||||
|
||||
var dest2 []struct {
|
||||
model.Category
|
||||
|
|
@ -1538,5 +1550,5 @@ func TestQuickStartWithSubQueries(t *testing.T) {
|
|||
assert.NilError(t, err)
|
||||
|
||||
//jsonSave("./testdata/quick-start-dest2.json", dest2)
|
||||
testutils.AssertJSONFile(t, "./postgres/testdata/quick-start-dest2.json", dest2)
|
||||
testutils.AssertJSONFile(t, dest2, "./postgres/testdata/quick-start-dest2.json")
|
||||
}
|
||||
|
|
|
|||
1978
tests/postgres/testdata/customer_payment_sum.json
vendored
Normal file
1978
tests/postgres/testdata/customer_payment_sum.json
vendored
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue