108 lines
4.2 KiB
Go
108 lines
4.2 KiB
Go
|
|
// 功能:验证气站与配送点支付记录的数据范围 SQL 和失败关闭行为。
|
|||
|
|
// 版本:v1.0。
|
|||
|
|
package common
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
|
|||
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
|||
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|||
|
|
"gorm.io/driver/postgres"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// paymentScopeSQL 生成 PostgreSQL 方言下的支付范围查询,供各边界用例断言。
|
|||
|
|
func paymentScopeSQL(t *testing.T, ownerType string, ownerID uint64, ownerIdentity string, paymentIdentity string) string {
|
|||
|
|
t.Helper()
|
|||
|
|
connection, _, err := sqlmock.New()
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatal(err)
|
|||
|
|
}
|
|||
|
|
t.Cleanup(func() { _ = connection.Close() })
|
|||
|
|
database, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{})
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatal(err)
|
|||
|
|
}
|
|||
|
|
return database.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
|||
|
|
query := ScopePaymentOrdersByOwner(ActiveRecords(tx.Model(&models.PaymentOrder{})), ownerType, ownerID, ownerIdentity)
|
|||
|
|
if paymentIdentity != "" {
|
|||
|
|
query = query.Where("payment_order.identity = ?", paymentIdentity)
|
|||
|
|
}
|
|||
|
|
return query.Find(&[]models.PaymentOrder{})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// assertSQLContains 校验查询必须包含所有数据范围片段。
|
|||
|
|
func assertSQLContains(t *testing.T, statement string, fragments ...string) {
|
|||
|
|
t.Helper()
|
|||
|
|
for _, fragment := range fragments {
|
|||
|
|
if !strings.Contains(statement, fragment) {
|
|||
|
|
t.Fatalf("支付范围查询缺少 %q:%s", fragment, statement)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestScopePaymentOrdersByGas 验证三类支付只能通过当前气站业务对象进入结果集。
|
|||
|
|
func TestScopePaymentOrdersByGas(t *testing.T) {
|
|||
|
|
statement := paymentScopeSQL(t, "gas", 11, "gas-identity", "")
|
|||
|
|
assertSQLContains(t, statement,
|
|||
|
|
`payment_order.business_type = 'gasorder'`,
|
|||
|
|
`gasorder_basic.gas_basic_id = 11`,
|
|||
|
|
`payment_order.business_type = 'ec_order'`,
|
|||
|
|
`ec_order.gas_station_id = 11`,
|
|||
|
|
`payment_order.business_type = 'recharge'`,
|
|||
|
|
`wallet_recharge_order.owner_type = 'gas'`,
|
|||
|
|
`wallet_recharge_order.owner_identity = 'gas-identity'`,
|
|||
|
|
`wallet_basic.owner_type = 'gas'`,
|
|||
|
|
`wallet_basic.owner_id = 11`,
|
|||
|
|
`wallet_basic.owner_identity = 'gas-identity'`,
|
|||
|
|
)
|
|||
|
|
if strings.Contains(statement, "payment_order.wallet_basic_id") {
|
|||
|
|
t.Fatalf("统一支付单仍错误依赖钱包主键:%s", statement)
|
|||
|
|
}
|
|||
|
|
if strings.Contains(statement, "delivery_basic_id") || strings.Contains(statement, "delivery_point_id") {
|
|||
|
|
t.Fatalf("气站范围查询混入配送点归属字段:%s", statement)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestScopePaymentOrdersByDelivery 验证三类支付只能通过当前配送点业务对象进入结果集。
|
|||
|
|
func TestScopePaymentOrdersByDelivery(t *testing.T) {
|
|||
|
|
statement := paymentScopeSQL(t, "delivery", 22, "delivery-identity", "")
|
|||
|
|
assertSQLContains(t, statement,
|
|||
|
|
`payment_order.business_type = 'gasorder'`,
|
|||
|
|
`gasorder_basic.delivery_basic_id = 22`,
|
|||
|
|
`payment_order.business_type = 'ec_order'`,
|
|||
|
|
`ec_order.delivery_point_id = 22`,
|
|||
|
|
`payment_order.business_type = 'recharge'`,
|
|||
|
|
`wallet_recharge_order.owner_type = 'delivery'`,
|
|||
|
|
`wallet_recharge_order.owner_identity = 'delivery-identity'`,
|
|||
|
|
`wallet_basic.owner_type = 'delivery'`,
|
|||
|
|
`wallet_basic.owner_id = 22`,
|
|||
|
|
`wallet_basic.owner_identity = 'delivery-identity'`,
|
|||
|
|
)
|
|||
|
|
if strings.Contains(statement, "payment_order.wallet_basic_id") {
|
|||
|
|
t.Fatalf("统一支付单仍错误依赖钱包主键:%s", statement)
|
|||
|
|
}
|
|||
|
|
if strings.Contains(statement, "gasorder_basic.gas_basic_id") || strings.Contains(statement, "ec_order.gas_station_id") {
|
|||
|
|
t.Fatalf("配送点范围查询混入气站归属字段:%s", statement)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestScopePaymentOrdersByOwnerFailsClosed 验证未知组织类型不会放行任何支付记录。
|
|||
|
|
func TestScopePaymentOrdersByOwnerFailsClosed(t *testing.T) {
|
|||
|
|
statement := paymentScopeSQL(t, "unknown", 33, "unknown-identity", "")
|
|||
|
|
assertSQLContains(t, statement, "1 = 0")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestScopePaymentOrderDetailKeepsOwnerBoundary 验证详情定位不会绕过与列表相同的组织范围。
|
|||
|
|
func TestScopePaymentOrderDetailKeepsOwnerBoundary(t *testing.T) {
|
|||
|
|
statement := paymentScopeSQL(t, "delivery", 44, "delivery-detail", "payment-detail")
|
|||
|
|
assertSQLContains(t, statement,
|
|||
|
|
`gasorder_basic.delivery_basic_id = 44`,
|
|||
|
|
`ec_order.delivery_point_id = 44`,
|
|||
|
|
`wallet_basic.owner_identity = 'delivery-detail'`,
|
|||
|
|
`payment_order.identity = 'payment-detail'`,
|
|||
|
|
)
|
|||
|
|
}
|