2026-08-19 00:14:42 +08:00
|
|
|
|
// 功能描述:验证气站合同与配送订单展示查询的对象权限范围。
|
2026-08-31 22:04:22 +08:00
|
|
|
|
// 版本:v1.1.0。
|
2026-08-19 00:14:42 +08:00
|
|
|
|
package gas
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
2026-08-31 22:04:22 +08:00
|
|
|
|
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
2026-08-19 00:14:42 +08:00
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// TestScopedOrderDisplayQuery 验证订单用户名称直接按订单主键读取且查询受当前气站约束。
|
|
|
|
|
|
func TestScopedOrderDisplayQuery(t *testing.T) {
|
|
|
|
|
|
connection, _, err := sqlmock.New()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("create SQL mock: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Cleanup(func() { _ = connection.Close() })
|
|
|
|
|
|
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("open GORM database: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
statement := scopedOrderDisplayQuery(databaseService, 42).
|
|
|
|
|
|
Find(&[]gasOrderListDisplay{}).Statement.SQL.String()
|
|
|
|
|
|
for _, fragment := range []string{
|
|
|
|
|
|
"LEFT JOIN user_account AS order_user",
|
|
|
|
|
|
"user_account_display_name",
|
|
|
|
|
|
"gasorder_basic.gas_basic_id =",
|
|
|
|
|
|
} {
|
|
|
|
|
|
if !strings.Contains(statement, fragment) {
|
|
|
|
|
|
t.Fatalf("scoped order display SQL must include %q, got: %s", fragment, statement)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-31 22:04:22 +08:00
|
|
|
|
|
|
|
|
|
|
// TestGasContractProductCandidates 验证气站候选按指定合同用户过滤,而非返回本站全部用户设备。
|
|
|
|
|
|
func TestGasContractProductCandidates(t *testing.T) {
|
|
|
|
|
|
connection, _, err := sqlmock.New()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("创建 SQL 模拟失败:%v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
t.Cleanup(func() { _ = connection.Close() })
|
|
|
|
|
|
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("创建 GORM 数据库失败:%v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
statement := gasContractProductCandidateQuery(databaseService, models.GasorderContract{Entity: models.Entity{ID: 17}, UserAccountID: 23}, 42).
|
|
|
|
|
|
Find(&[]models.ProductInfo{}).Statement.SQL.String()
|
|
|
|
|
|
for _, fragment := range []string{
|
|
|
|
|
|
"product_info.user_account_id =",
|
|
|
|
|
|
"candidate_binding.gasorder_contract_id =",
|
|
|
|
|
|
"candidate_relation.gas_basic_id =",
|
|
|
|
|
|
"candidate_relation.user_account_id = product_info.user_account_id",
|
|
|
|
|
|
} {
|
|
|
|
|
|
if !strings.Contains(statement, fragment) {
|
|
|
|
|
|
t.Fatalf("气站合同气阀候选 SQL 缺少 %q:%s", fragment, statement)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|