58 lines
2.3 KiB
Go
58 lines
2.3 KiB
Go
package gas
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// TestScopedUserListIncludesDeliveryRelation 验证用户列表带出当前气站服务关系的配送点主键。
|
|
func TestScopedUserListIncludesDeliveryRelation(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 := scopedUserList(databaseService, 42).
|
|
Find(&[]gasUserListItem{}).Statement.SQL.String()
|
|
if !strings.Contains(statement, "user_service_relation.delivery_basic_id AS delivery_basic_id") {
|
|
t.Fatalf("user list must select delivery relation, got: %s", statement)
|
|
}
|
|
if !strings.Contains(statement, "user_service_relation.gas_basic_id =") {
|
|
t.Fatalf("user list must keep gas scope, got: %s", statement)
|
|
}
|
|
}
|
|
|
|
// TestContractScopedUserRequiresCurrentGasContract 验证只读用户详情同时受用户标识与当前气站合同约束。
|
|
func TestContractScopedUserRequiresCurrentGasContract(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 := contractScopedUserQuery(databaseService, "user-identity", 42, "contract-identity").
|
|
Find(&[]contractScopedUserRecord{}).Statement.SQL.String()
|
|
if !strings.Contains(statement, "JOIN gasorder_contract ON gasorder_contract.user_account_id = user_account.id") {
|
|
t.Fatalf("contract-scoped detail must join contract, got: %s", statement)
|
|
}
|
|
if !strings.Contains(statement, "user_account.identity =") || !strings.Contains(statement, "gasorder_contract.gas_basic_id =") {
|
|
t.Fatalf("contract-scoped detail must keep user and gas scope, got: %s", statement)
|
|
}
|
|
if !strings.Contains(statement, "gasorder_contract.identity =") || !strings.Contains(statement, "contract_delivery_basic_id") {
|
|
t.Fatalf("contract-scoped detail must keep contract context and delivery point, got: %s", statement)
|
|
}
|
|
}
|