package adminuser import ( "strings" "testing" "gorm.io/driver/sqlite" "gorm.io/gorm" ) func TestDepositFreeQuotaAmountCent(t *testing.T) { tests := []struct { name string req DepositFreeQuotaRequest want int64 }{ { name: "使用分字段", req: DepositFreeQuotaRequest{AmountCent: 1234}, want: 1234, }, { name: "负分拒绝", req: DepositFreeQuotaRequest{AmountCent: -1}, want: -1, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := depositFreeQuotaAmountCent(tt.req); got != tt.want { t.Fatalf("depositFreeQuotaAmountCent() = %d, want %d", got, tt.want) } }) } } func TestParseBoolQuery(t *testing.T) { tests := []struct { name string value string want bool }{ {name: "true 开启", value: "true", want: true}, {name: "数字 1 开启", value: "1", want: true}, {name: "on 开启", value: "on", want: true}, {name: "空值关闭", value: "", want: false}, {name: "false 关闭", value: "false", want: false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := parseBoolQuery(tt.value); got != tt.want { t.Fatalf("parseBoolQuery(%q) = %v, want %v", tt.value, got, tt.want) } }) } } func TestApplyUserFilterHasDepositFreeQuota(t *testing.T) { db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{DryRun: true}) if err != nil { t.Fatalf("打开测试数据库失败:%v", err) } stmt := applyUserFilter(db.Table("users AS u"), ListQuery{ Status: "active", HasDepositFreeQuota: true, }).Find(&[]struct{}{}).Statement sql := stmt.SQL.String() if !strings.Contains(sql, "u.deposit_free_quota_cent > 0") { t.Fatalf("SQL 未包含免押额度过滤:%s", sql) } if len(stmt.Vars) != 1 || stmt.Vars[0] != "active" { t.Fatalf("筛选变量不正确:%v", stmt.Vars) } }