133 lines
4.7 KiB
Go
133 lines
4.7 KiB
Go
package welfare_activity
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"bindbox-game/internal/repository/mysql"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestJoin_UsesConfiguredActivityTimeRangeForSpend(t *testing.T) {
|
|
svc, _, db := newWelfareTestService(t)
|
|
ctx := context.Background()
|
|
now := time.Now().Truncate(time.Second)
|
|
activity := Activity{
|
|
ID: 1,
|
|
Title: "自定义福利时间",
|
|
Type: TypeMonthly,
|
|
ThresholdAmount: 1000,
|
|
StartTime: now.Add(-time.Hour),
|
|
EndTime: now.Add(time.Hour),
|
|
DrawTime: now.Add(2 * time.Hour),
|
|
Status: StatusActive,
|
|
}
|
|
mustInsertWelfareActivity(t, db, activity)
|
|
mustExecWelfare(t, db, `INSERT INTO orders (user_id, order_no, source_type, actual_amount, total_amount, status, paid_at, created_at, updated_at, cancelled_at) VALUES (10, 'WELFARE_BEFORE', 1, 5000, 5000, 2, ?, ?, ?, ?)`, activity.StartTime.Add(-time.Second), activity.StartTime.Add(-time.Second), activity.StartTime.Add(-time.Second), time.Unix(0, 0))
|
|
mustExecWelfare(t, db, `INSERT INTO orders (user_id, order_no, source_type, actual_amount, total_amount, status, paid_at, created_at, updated_at, cancelled_at) VALUES (10, 'WELFARE_INSIDE', 1, 1000, 1000, 2, ?, ?, ?, ?)`, now, now, now, time.Unix(0, 0))
|
|
mustExecWelfare(t, db, `INSERT INTO orders (user_id, order_no, source_type, actual_amount, total_amount, status, paid_at, created_at, updated_at, cancelled_at) VALUES (10, 'WELFARE_AFTER', 1, 7000, 7000, 2, ?, ?, ?, ?)`, activity.EndTime.Add(2*time.Second), activity.EndTime.Add(2*time.Second), activity.EndTime.Add(2*time.Second), time.Unix(0, 0))
|
|
|
|
if err := svc.Join(ctx, activity.ID, 10); err != nil {
|
|
t.Fatalf("Join failed: %v", err)
|
|
}
|
|
var participant Participant
|
|
if err := db.Where("activity_id = ? AND user_id = ?", activity.ID, 10).First(&participant).Error; err != nil {
|
|
t.Fatalf("query participant failed: %v", err)
|
|
}
|
|
if participant.PaidAmountSnapshot != 1000 {
|
|
t.Fatalf("expected only configured-range spend 1000, got %d", participant.PaidAmountSnapshot)
|
|
}
|
|
}
|
|
|
|
func newWelfareTestService(t *testing.T) (*service, mysql.Repo, *gorm.DB) {
|
|
t.Helper()
|
|
repo, err := mysql.NewSQLiteRepoForTest()
|
|
if err != nil {
|
|
t.Fatalf("create sqlite repo failed: %v", err)
|
|
}
|
|
db := repo.GetDbW()
|
|
initWelfareTestTables(t, db)
|
|
return &service{repo: repo}, repo, db
|
|
}
|
|
|
|
func initWelfareTestTables(t *testing.T, db *gorm.DB) {
|
|
t.Helper()
|
|
stmts := []string{
|
|
`CREATE TABLE welfare_activities (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
title TEXT NOT NULL,
|
|
type TEXT NOT NULL,
|
|
threshold_amount INTEGER NOT NULL DEFAULT 0,
|
|
start_time DATETIME NOT NULL,
|
|
end_time DATETIME NOT NULL,
|
|
draw_time DATETIME NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'active',
|
|
description TEXT,
|
|
cover_image TEXT,
|
|
draw_batch TEXT NOT NULL DEFAULT '',
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
deleted_at DATETIME
|
|
);`,
|
|
`CREATE TABLE welfare_activity_participants (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
activity_id INTEGER NOT NULL,
|
|
user_id INTEGER NOT NULL,
|
|
period_key TEXT NOT NULL,
|
|
paid_amount_snapshot INTEGER NOT NULL DEFAULT 0,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE(activity_id, user_id, period_key)
|
|
);`,
|
|
`CREATE TABLE orders (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
user_id INTEGER NOT NULL,
|
|
order_no TEXT NOT NULL,
|
|
source_type INTEGER NOT NULL DEFAULT 1,
|
|
total_amount INTEGER NOT NULL DEFAULT 0,
|
|
discount_amount INTEGER NOT NULL DEFAULT 0,
|
|
points_amount INTEGER NOT NULL DEFAULT 0,
|
|
actual_amount INTEGER NOT NULL DEFAULT 0,
|
|
status INTEGER NOT NULL DEFAULT 1,
|
|
pay_preorder_id INTEGER NOT NULL DEFAULT 0,
|
|
paid_at DATETIME,
|
|
cancelled_at DATETIME,
|
|
user_address_id INTEGER NOT NULL DEFAULT 0,
|
|
is_consumed INTEGER NOT NULL DEFAULT 0,
|
|
points_ledger_id INTEGER NOT NULL DEFAULT 0,
|
|
coupon_id INTEGER NOT NULL DEFAULT 0,
|
|
item_card_id INTEGER NOT NULL DEFAULT 0,
|
|
remark TEXT,
|
|
ext_order_id TEXT NOT NULL DEFAULT ''
|
|
);`,
|
|
}
|
|
for _, stmt := range stmts {
|
|
if err := db.Exec(stmt).Error; err != nil {
|
|
t.Fatalf("create table failed: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func mustExecWelfare(t *testing.T, db *gorm.DB, sql string, args ...any) {
|
|
t.Helper()
|
|
if err := db.Exec(sql, args...).Error; err != nil {
|
|
t.Fatalf("exec failed: %v, sql=%s", err, sql)
|
|
}
|
|
}
|
|
|
|
func mustInsertWelfareActivity(t *testing.T, db *gorm.DB, activity Activity) {
|
|
t.Helper()
|
|
if activity.CreatedAt.IsZero() {
|
|
activity.CreatedAt = time.Now()
|
|
}
|
|
if activity.UpdatedAt.IsZero() {
|
|
activity.UpdatedAt = activity.CreatedAt
|
|
}
|
|
if err := db.Create(&activity).Error; err != nil {
|
|
t.Fatalf("insert activity failed: %v", err)
|
|
}
|
|
}
|