54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
)
|
|
|
|
func financeTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func TestFinancePaymentStatsAreScopedByTenant(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
now := time.Now()
|
|
payments := []model.PaymentOrder{
|
|
{
|
|
TenantID: 101, PaymentNo: "PAY-FIN-A", OrderID: 1, OrderNo: "RO-FIN-A",
|
|
UserID: 1, PaymentConfigID: 1, Provider: "mock", MerchantID: "A",
|
|
ThirdOrderID: "PAY-FIN-A", PayWay: "ZFBZF", AmountCent: 1000,
|
|
BizType: "order_pay", Status: "paid", PaidAt: &now,
|
|
},
|
|
{
|
|
TenantID: 202, PaymentNo: "PAY-FIN-B", OrderID: 2, OrderNo: "RO-FIN-B",
|
|
UserID: 2, PaymentConfigID: 2, Provider: "mock", MerchantID: "B",
|
|
ThirdOrderID: "PAY-FIN-B", PayWay: "ZFBZF", AmountCent: 2000,
|
|
BizType: "order_pay", Status: "paid", PaidAt: &now,
|
|
},
|
|
}
|
|
if err := db.Create(&payments).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewRepository(db)
|
|
query := DashboardQuery{StartDate: now.Add(-time.Hour), EndDate: now.Add(time.Hour)}
|
|
rows, err := repo.dailyPayments(financeTenantContext(101), query)
|
|
if err != nil {
|
|
t.Fatalf("daily payments: %v", err)
|
|
}
|
|
var flow int64
|
|
for _, row := range rows {
|
|
flow += row.TotalFlowAmountCent
|
|
}
|
|
if flow != 1000 {
|
|
t.Fatalf("dachui payment flow = %d, want 1000", flow)
|
|
}
|
|
if repo.dashboardCacheKey(financeTenantContext(101), query) == repo.dashboardCacheKey(financeTenantContext(202), query) {
|
|
t.Fatal("dashboard cache key must include tenant")
|
|
}
|
|
}
|