优化测试数据库连接管理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s
- 创建全局单例连接池,性能提升 6-7 倍 - 实现 NewTestTransaction/GetTestRedis/CleanTestRedisKeys - 移除旧的 SetupTestDB/TeardownTestDB API - 迁移所有测试文件到新方案(47 个文件) - 添加测试连接管理规范文档 - 更新 AGENTS.md 和 README.md 性能对比: - 旧方案:~71 秒(204 测试) - 新方案:~10.5 秒(首次初始化 + 后续复用) - 内存占用降低约 80% - 网络连接数从 204 降至 1
This commit is contained in:
@@ -15,10 +15,11 @@ import (
|
||||
|
||||
// TestAccountModel_Create 测试创建账号
|
||||
func TestAccountModel_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("创建 root 账号", func(t *testing.T) {
|
||||
@@ -59,10 +60,11 @@ func TestAccountModel_Create(t *testing.T) {
|
||||
|
||||
// TestAccountModel_GetByID 测试根据 ID 查询账号
|
||||
func TestAccountModel_GetByID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -92,10 +94,11 @@ func TestAccountModel_GetByID(t *testing.T) {
|
||||
|
||||
// TestAccountModel_GetByUsername 测试根据用户名查询账号
|
||||
func TestAccountModel_GetByUsername(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -123,10 +126,11 @@ func TestAccountModel_GetByUsername(t *testing.T) {
|
||||
|
||||
// TestAccountModel_GetByPhone 测试根据手机号查询账号
|
||||
func TestAccountModel_GetByPhone(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -154,10 +158,11 @@ func TestAccountModel_GetByPhone(t *testing.T) {
|
||||
|
||||
// TestAccountModel_Update 测试更新账号
|
||||
func TestAccountModel_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -187,10 +192,11 @@ func TestAccountModel_Update(t *testing.T) {
|
||||
|
||||
// TestAccountModel_List 测试查询账号列表
|
||||
func TestAccountModel_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个测试账号
|
||||
@@ -227,10 +233,11 @@ func TestAccountModel_List(t *testing.T) {
|
||||
|
||||
// TestAccountModel_UniqueConstraints 测试唯一约束
|
||||
func TestAccountModel_UniqueConstraints(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
|
||||
@@ -22,16 +22,17 @@ func createWithdrawalTestContext(userID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_ListWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
service := commission_withdrawal.New(tx, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("查询提现申请列表-空结果", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
@@ -79,16 +80,17 @@ func TestCommissionWithdrawalService_ListWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_Approve(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
service := commission_withdrawal.New(tx, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("审批不存在的提现申请应失败", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
@@ -103,16 +105,17 @@ func TestCommissionWithdrawalService_Approve(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_Reject(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
service := commission_withdrawal.New(tx, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("拒绝不存在的提现申请应失败", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
@@ -22,13 +22,14 @@ func createWithdrawalSettingTestContext(userID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
service := commission_withdrawal_setting.New(tx, accountStore, settingStore)
|
||||
|
||||
t.Run("新增提现配置", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
@@ -63,13 +64,14 @@ func TestCommissionWithdrawalSettingService_Create(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_ConfigSwitch(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
service := commission_withdrawal_setting.New(tx, accountStore, settingStore)
|
||||
|
||||
t.Run("配置切换-旧配置自动失效", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
@@ -103,13 +105,14 @@ func TestCommissionWithdrawalSettingService_ConfigSwitch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
service := commission_withdrawal_setting.New(tx, accountStore, settingStore)
|
||||
|
||||
t.Run("查询配置列表-空结果", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
@@ -152,13 +155,14 @@ func TestCommissionWithdrawalSettingService_List(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_GetCurrent(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
service := commission_withdrawal_setting.New(tx, accountStore, settingStore)
|
||||
|
||||
t.Run("获取当前配置-无配置时应返回错误", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
@@ -23,14 +23,15 @@ func createCustomerAccountTestContext(userID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("查询账号列表-空结果", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -59,7 +60,7 @@ func TestCustomerAccountService_List(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -96,7 +97,7 @@ func TestCustomerAccountService_List(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -122,14 +123,15 @@ func TestCustomerAccountService_List(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("新增代理商账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -144,7 +146,7 @@ func TestCustomerAccountService_Create(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.CreateCustomerAccountReq{
|
||||
@@ -176,7 +178,7 @@ func TestCustomerAccountService_Create(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req1 := &dto.CreateCustomerAccountReq{
|
||||
@@ -228,14 +230,15 @@ func TestCustomerAccountService_Create(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("编辑账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -250,7 +253,7 @@ func TestCustomerAccountService_Update(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -286,14 +289,15 @@ func TestCustomerAccountService_Update(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("修改密码", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -308,7 +312,7 @@ func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -324,7 +328,7 @@ func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
@@ -339,14 +343,15 @@ func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("禁用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -361,7 +366,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -377,7 +382,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
@@ -395,7 +400,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -414,7 +419,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
@@ -24,13 +24,14 @@ func createEnterpriseCardTestContext(userID uint, shopID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权预检-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -64,7 +65,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsPreviewReq{
|
||||
@@ -87,7 +88,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsPreviewReq{
|
||||
@@ -112,7 +113,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -122,7 +123,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsPreviewReq{
|
||||
@@ -138,13 +139,14 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权卡-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -167,7 +169,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -177,7 +179,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsReq{
|
||||
@@ -200,7 +202,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -210,7 +212,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsReq{
|
||||
@@ -224,7 +226,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.EnterpriseCardAuthorization{}).
|
||||
tx.Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND iot_card_id = ?", ent.ID, card.ID).
|
||||
Count(&count)
|
||||
assert.Equal(t, int64(1), count)
|
||||
@@ -232,13 +234,14 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("回收授权-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -261,7 +264,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -271,7 +274,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.RecallCardsReq{
|
||||
@@ -294,7 +297,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -304,7 +307,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -324,13 +327,14 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("查询企业卡列表-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -354,7 +358,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.EnterpriseCardListReq{
|
||||
@@ -378,7 +382,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -388,7 +392,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -420,7 +424,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -430,7 +434,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -453,13 +457,14 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("停机-未授权的卡应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -471,7 +476,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -481,7 +486,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.SuspendCard(ctx, ent.ID, card.ID)
|
||||
@@ -498,7 +503,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -509,7 +514,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
NetworkStatus: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -522,14 +527,14 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var suspendedCard model.IotCard
|
||||
db.First(&suspendedCard, card.ID)
|
||||
tx.First(&suspendedCard, card.ID)
|
||||
assert.Equal(t, 0, suspendedCard.NetworkStatus)
|
||||
|
||||
err = service.ResumeCard(ctx, ent.ID, card.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var resumedCard model.IotCard
|
||||
db.First(&resumedCard, card.ID)
|
||||
tx.First(&resumedCard, card.ID)
|
||||
assert.Equal(t, 1, resumedCard.NetworkStatus)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -23,14 +23,15 @@ func createEnterpriseTestContext(userID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestEnterpriseService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
service := enterprise.New(tx, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("创建企业-含账号创建", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
@@ -123,14 +124,15 @@ func TestEnterpriseService_Create(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
service := enterprise.New(tx, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("编辑企业", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
@@ -173,14 +175,15 @@ func TestEnterpriseService_Update(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
service := enterprise.New(tx, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("禁用企业-账号同步禁用", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
@@ -204,7 +207,7 @@ func TestEnterpriseService_UpdateStatus(t *testing.T) {
|
||||
assert.Equal(t, constants.StatusDisabled, ent.Status)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
err = tx.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
@@ -234,7 +237,7 @@ func TestEnterpriseService_UpdateStatus(t *testing.T) {
|
||||
assert.Equal(t, constants.StatusEnabled, ent.Status)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
err = tx.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
@@ -248,14 +251,15 @@ func TestEnterpriseService_UpdateStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
service := enterprise.New(tx, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("修改企业账号密码", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
@@ -275,7 +279,7 @@ func TestEnterpriseService_UpdatePassword(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
err = tx.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
@@ -290,14 +294,15 @@ func TestEnterpriseService_UpdatePassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
service := enterprise.New(tx, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("查询企业列表-空结果", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
@@ -15,10 +15,11 @@ import (
|
||||
|
||||
// TestEnterpriseStore_Create 测试创建企业
|
||||
func TestEnterpriseStore_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
@@ -79,10 +80,11 @@ func TestEnterpriseStore_Create(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_GetByID 测试根据 ID 查询企业
|
||||
func TestEnterpriseStore_GetByID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试企业
|
||||
@@ -118,10 +120,11 @@ func TestEnterpriseStore_GetByID(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_GetByCode 测试根据企业编号查询
|
||||
func TestEnterpriseStore_GetByCode(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试企业
|
||||
@@ -156,10 +159,11 @@ func TestEnterpriseStore_GetByCode(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_Update 测试更新企业
|
||||
func TestEnterpriseStore_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试企业
|
||||
@@ -212,10 +216,11 @@ func TestEnterpriseStore_Update(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_Delete 测试软删除企业
|
||||
func TestEnterpriseStore_Delete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试企业
|
||||
@@ -247,10 +252,11 @@ func TestEnterpriseStore_Delete(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_List 测试查询企业列表
|
||||
func TestEnterpriseStore_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个测试企业
|
||||
@@ -293,10 +299,11 @@ func TestEnterpriseStore_List(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_GetByOwnerShopID 测试根据归属店铺查询企业
|
||||
func TestEnterpriseStore_GetByOwnerShopID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
shopID1 := uint(100)
|
||||
@@ -364,10 +371,11 @@ func TestEnterpriseStore_GetByOwnerShopID(t *testing.T) {
|
||||
|
||||
// TestEnterpriseStore_UniqueConstraints 测试唯一约束
|
||||
func TestEnterpriseStore_UniqueConstraints(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewEnterpriseStore(db, redisClient)
|
||||
store := postgres.NewEnterpriseStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试企业
|
||||
|
||||
@@ -24,18 +24,19 @@ func createMyCommissionTestContext(userID uint, shopID uint, userType int) conte
|
||||
}
|
||||
|
||||
func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -51,7 +52,7 @@ func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -79,18 +80,19 @@ func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -106,7 +108,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -133,7 +135,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
setting := &model.CommissionWithdrawalSetting{
|
||||
@@ -144,7 +146,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
setting.Creator = 1
|
||||
setting.Updater = 1
|
||||
err = db.Create(setting).Error
|
||||
err = tx.Create(setting).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -171,7 +173,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
wallet := &model.Wallet{
|
||||
@@ -180,7 +182,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
WalletType: constants.WalletTypeCommission,
|
||||
Balance: 5000,
|
||||
}
|
||||
err = db.Create(wallet).Error
|
||||
err = tx.Create(wallet).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -212,18 +214,19 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -239,7 +242,7 @@ func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -266,7 +269,7 @@ func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -297,18 +300,19 @@ func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -324,7 +328,7 @@ func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -351,7 +355,7 @@ func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
@@ -17,16 +17,17 @@ import (
|
||||
)
|
||||
|
||||
func TestPermissionCache_FirstCallMissSecondHit(t *testing.T) {
|
||||
db, rdb := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, rdb)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, rdb)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
|
||||
permSvc := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
@@ -99,16 +100,17 @@ func TestPermissionCache_FirstCallMissSecondHit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPermissionCache_ExpiredAfter30Minutes(t *testing.T) {
|
||||
db, rdb := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, rdb)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, rdb)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
|
||||
permSvc := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
|
||||
@@ -26,13 +26,14 @@ func createContextWithUserType(userID uint, userType int) context.Context {
|
||||
}
|
||||
|
||||
func TestPermissionService_CheckPermission_SuperAdmin(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
t.Run("超级管理员自动拥有所有权限", func(t *testing.T) {
|
||||
ctx := createContextWithUserType(1, constants.UserTypeSuperAdmin)
|
||||
@@ -44,14 +45,15 @@ func TestPermissionService_CheckPermission_SuperAdmin(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPermissionService_CheckPermission_NormalUser(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
ctx := createContextWithUserType(100, constants.UserTypePlatform)
|
||||
|
||||
@@ -164,13 +166,14 @@ func TestPermissionService_CheckPermission_NormalUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPermissionService_CheckPermission_NoRole(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
t.Run("用户无角色应返回false", func(t *testing.T) {
|
||||
ctx := createContextWithUserType(200, constants.UserTypePlatform)
|
||||
@@ -182,14 +185,15 @@ func TestPermissionService_CheckPermission_NoRole(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPermissionService_CheckPermission_RoleNoPermission(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
ctx := createContextWithUserType(300, constants.UserTypePlatform)
|
||||
|
||||
|
||||
@@ -18,13 +18,14 @@ import (
|
||||
|
||||
// TestPermissionPlatformFilter_List 测试权限列表按 platform 过滤
|
||||
func TestPermissionPlatformFilter_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
|
||||
@@ -38,7 +39,7 @@ func TestPermissionPlatformFilter_List(t *testing.T) {
|
||||
{PermName: "H5按钮", PermCode: "button:h5", PermType: constants.PermissionTypeButton, Platform: constants.PlatformH5, Status: constants.StatusEnabled},
|
||||
}
|
||||
for _, perm := range permissions {
|
||||
require.NoError(t, db.Create(perm).Error)
|
||||
require.NoError(t, tx.Create(perm).Error)
|
||||
}
|
||||
|
||||
// 测试查询全部权限(不过滤)
|
||||
@@ -104,13 +105,14 @@ func TestPermissionPlatformFilter_List(t *testing.T) {
|
||||
|
||||
// TestPermissionPlatformFilter_CreateWithDefaultPlatform 测试创建权限时默认 platform 为 all
|
||||
func TestPermissionPlatformFilter_CreateWithDefaultPlatform(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
|
||||
@@ -130,13 +132,14 @@ func TestPermissionPlatformFilter_CreateWithDefaultPlatform(t *testing.T) {
|
||||
|
||||
// TestPermissionPlatformFilter_CreateWithSpecificPlatform 测试创建权限时指定 platform
|
||||
func TestPermissionPlatformFilter_CreateWithSpecificPlatform(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
|
||||
@@ -169,13 +172,14 @@ func TestPermissionPlatformFilter_CreateWithSpecificPlatform(t *testing.T) {
|
||||
|
||||
// TestPermissionPlatformFilter_Tree 测试权限树包含 platform 字段
|
||||
func TestPermissionPlatformFilter_Tree(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, redisClient)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := permission.New(permissionStore, accountRoleStore, rolePermStore, rdb)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
|
||||
@@ -188,7 +192,7 @@ func TestPermissionPlatformFilter_Tree(t *testing.T) {
|
||||
Platform: constants.PlatformWeb,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(parent).Error)
|
||||
require.NoError(t, tx.Create(parent).Error)
|
||||
|
||||
child := &model.Permission{
|
||||
PermName: "用户管理",
|
||||
@@ -198,7 +202,7 @@ func TestPermissionPlatformFilter_Tree(t *testing.T) {
|
||||
ParentID: &parent.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(child).Error)
|
||||
require.NoError(t, tx.Create(child).Error)
|
||||
|
||||
// 获取权限树
|
||||
tree, err := service.GetTree(ctx, nil)
|
||||
|
||||
@@ -14,10 +14,11 @@ import (
|
||||
)
|
||||
|
||||
func TestPermissionStore_List_AvailableForRoleTypes(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPermissionStore(db)
|
||||
store := postgres.NewPermissionStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
platformPerm := &model.Permission{
|
||||
@@ -112,10 +113,11 @@ func TestPermissionStore_List_AvailableForRoleTypes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPermissionStore_GetAll_AvailableForRoleType(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPermissionStore(db)
|
||||
store := postgres.NewPermissionStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
platformPerm := &model.Permission{
|
||||
@@ -188,10 +190,11 @@ func TestPermissionStore_GetAll_AvailableForRoleType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPermissionStore_GetByPlatform_AvailableForRoleType(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPermissionStore(db)
|
||||
store := postgres.NewPermissionStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
webPlatformPerm := &model.Permission{
|
||||
|
||||
@@ -15,10 +15,11 @@ import (
|
||||
|
||||
// TestPersonalCustomerStore_Create 测试创建个人客户
|
||||
func TestPersonalCustomerStore_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
@@ -66,10 +67,11 @@ func TestPersonalCustomerStore_Create(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_GetByID 测试根据 ID 查询个人客户
|
||||
func TestPersonalCustomerStore_GetByID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -97,10 +99,11 @@ func TestPersonalCustomerStore_GetByID(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_GetByPhone 测试根据手机号查询
|
||||
func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -120,7 +123,7 @@ func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
|
||||
IsPrimary: true,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
err = db.Create(customerPhone).Error
|
||||
err = tx.Create(customerPhone).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("根据手机号查询", func(t *testing.T) {
|
||||
@@ -138,10 +141,11 @@ func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_GetByWxOpenID 测试根据微信 OpenID 查询
|
||||
func TestPersonalCustomerStore_GetByWxOpenID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -169,10 +173,11 @@ func TestPersonalCustomerStore_GetByWxOpenID(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_Update 测试更新个人客户
|
||||
func TestPersonalCustomerStore_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -224,10 +229,11 @@ func TestPersonalCustomerStore_Update(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_Delete 测试软删除个人客户
|
||||
func TestPersonalCustomerStore_Delete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -252,10 +258,11 @@ func TestPersonalCustomerStore_Delete(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_List 测试查询客户列表
|
||||
func TestPersonalCustomerStore_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个测试客户
|
||||
@@ -291,10 +298,11 @@ func TestPersonalCustomerStore_List(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_UniqueConstraints 测试唯一约束
|
||||
func TestPersonalCustomerStore_UniqueConstraints(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
|
||||
@@ -16,13 +16,13 @@ import (
|
||||
|
||||
// TestQueueClientEnqueue 测试任务入队
|
||||
func TestQueueClientEnqueue(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -47,13 +47,13 @@ func TestQueueClientEnqueue(t *testing.T) {
|
||||
|
||||
// TestQueueClientEnqueueWithOptions 测试带选项的任务入队
|
||||
func TestQueueClientEnqueueWithOptions(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -136,13 +136,13 @@ func TestQueueClientEnqueueWithOptions(t *testing.T) {
|
||||
|
||||
// TestQueueClientTaskUniqueness 测试任务唯一性
|
||||
func TestQueueClientTaskUniqueness(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -224,13 +224,13 @@ func TestTaskPayloadSizeLimit(t *testing.T) {
|
||||
// Redis 默认支持最大 512MB,但实际应用中不建议超过 1MB
|
||||
}
|
||||
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -268,13 +268,13 @@ func TestTaskPayloadSizeLimit(t *testing.T) {
|
||||
|
||||
// TestTaskScheduling 测试任务调度
|
||||
func TestTaskScheduling(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -320,13 +320,13 @@ func TestTaskScheduling(t *testing.T) {
|
||||
|
||||
// TestQueueInspectorStats 测试队列统计
|
||||
func TestQueueInspectorStats(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -363,13 +363,13 @@ func TestQueueInspectorStats(t *testing.T) {
|
||||
|
||||
// TestTaskRetention 测试任务保留策略
|
||||
func TestTaskRetention(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -395,13 +395,13 @@ func TestTaskRetention(t *testing.T) {
|
||||
|
||||
// TestQueueDraining 测试队列暂停和恢复
|
||||
func TestQueueDraining(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
inspector := asynq.NewInspector(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -429,13 +429,13 @@ func TestQueueDraining(t *testing.T) {
|
||||
|
||||
// TestTaskCancellation 测试任务取消
|
||||
func TestTaskCancellation(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -471,13 +471,13 @@ func TestTaskCancellation(t *testing.T) {
|
||||
|
||||
// TestBatchTaskEnqueue 测试批量任务入队
|
||||
func TestBatchTaskEnqueue(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
@@ -512,13 +512,13 @@ func TestBatchTaskEnqueue(t *testing.T) {
|
||||
|
||||
// TestTaskGrouping 测试任务分组
|
||||
func TestTaskGrouping(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer func() { _ = redisClient.Close() }()
|
||||
defer func() { _ = rdb.Close() }()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
client := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: "localhost:6379",
|
||||
|
||||
@@ -17,12 +17,13 @@ import (
|
||||
|
||||
// TestRoleAssignmentLimit_PlatformUser 测试平台用户可以分配多个角色(无限制)
|
||||
func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
service := account.New(accountStore, roleStore, accountRoleStore)
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -36,7 +37,7 @@ func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(platformUser).Error)
|
||||
require.NoError(t, tx.Create(platformUser).Error)
|
||||
|
||||
// 创建 3 个平台角色
|
||||
roles := []*model.Role{
|
||||
@@ -45,7 +46,7 @@ func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
|
||||
{RoleName: "财务", RoleType: constants.RoleTypePlatform, Status: constants.StatusEnabled},
|
||||
}
|
||||
for _, role := range roles {
|
||||
require.NoError(t, db.Create(role).Error)
|
||||
require.NoError(t, tx.Create(role).Error)
|
||||
}
|
||||
|
||||
// 为平台用户分配 3 个角色(应该成功,因为平台用户无限制)
|
||||
@@ -57,12 +58,13 @@ func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
|
||||
|
||||
// TestRoleAssignmentLimit_AgentUser 测试代理账号只能分配一个角色
|
||||
func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
service := account.New(accountStore, roleStore, accountRoleStore)
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -76,7 +78,7 @@ func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
|
||||
UserType: constants.UserTypeAgent,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(agentAccount).Error)
|
||||
require.NoError(t, tx.Create(agentAccount).Error)
|
||||
|
||||
// 创建 2 个客户角色
|
||||
roles := []*model.Role{
|
||||
@@ -84,7 +86,7 @@ func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
|
||||
{RoleName: "二级代理", RoleType: constants.RoleTypeCustomer, Status: constants.StatusEnabled},
|
||||
}
|
||||
for _, role := range roles {
|
||||
require.NoError(t, db.Create(role).Error)
|
||||
require.NoError(t, tx.Create(role).Error)
|
||||
}
|
||||
|
||||
// 先分配第一个角色(应该成功)
|
||||
@@ -100,12 +102,13 @@ func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
|
||||
|
||||
// TestRoleAssignmentLimit_EnterpriseUser 测试企业账号只能分配一个角色
|
||||
func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
service := account.New(accountStore, roleStore, accountRoleStore)
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -119,7 +122,7 @@ func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
|
||||
UserType: constants.UserTypeEnterprise,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(enterpriseAccount).Error)
|
||||
require.NoError(t, tx.Create(enterpriseAccount).Error)
|
||||
|
||||
// 创建 2 个客户角色
|
||||
roles := []*model.Role{
|
||||
@@ -127,7 +130,7 @@ func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
|
||||
{RoleName: "企业高级", RoleType: constants.RoleTypeCustomer, Status: constants.StatusEnabled},
|
||||
}
|
||||
for _, role := range roles {
|
||||
require.NoError(t, db.Create(role).Error)
|
||||
require.NoError(t, tx.Create(role).Error)
|
||||
}
|
||||
|
||||
// 先分配第一个角色(应该成功)
|
||||
@@ -143,12 +146,13 @@ func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
|
||||
|
||||
// TestRoleAssignmentLimit_SuperAdmin 测试超级管理员不允许分配角色
|
||||
func TestRoleAssignmentLimit_SuperAdmin(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
service := account.New(accountStore, roleStore, accountRoleStore)
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -162,7 +166,7 @@ func TestRoleAssignmentLimit_SuperAdmin(t *testing.T) {
|
||||
UserType: constants.UserTypeSuperAdmin,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(superAdmin).Error)
|
||||
require.NoError(t, tx.Create(superAdmin).Error)
|
||||
|
||||
// 创建一个平台角色
|
||||
role := &model.Role{
|
||||
@@ -170,7 +174,7 @@ func TestRoleAssignmentLimit_SuperAdmin(t *testing.T) {
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
require.NoError(t, db.Create(role).Error)
|
||||
require.NoError(t, tx.Create(role).Error)
|
||||
|
||||
// 尝试为超级管理员分配角色(应该失败)
|
||||
_, err := service.AssignRoles(ctx, superAdmin.ID, []uint{role.ID})
|
||||
|
||||
@@ -14,12 +14,13 @@ import (
|
||||
)
|
||||
|
||||
func TestRoleService_AssignPermissions_ValidateAvailableForRoleTypes(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := role.New(roleStore, permStore, rolePermStore)
|
||||
|
||||
ctx := createContextWithUserID(1)
|
||||
@@ -133,12 +134,13 @@ func TestRoleService_AssignPermissions_ValidateAvailableForRoleTypes(t *testing.
|
||||
}
|
||||
|
||||
func TestRoleService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
permStore := postgres.NewPermissionStore(db)
|
||||
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
permStore := postgres.NewPermissionStore(tx)
|
||||
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
service := role.New(roleStore, permStore, rolePermStore)
|
||||
|
||||
ctx := createContextWithUserID(1)
|
||||
|
||||
@@ -18,11 +18,12 @@ import (
|
||||
|
||||
// TestShopAccountService_Create 测试创建商户账号
|
||||
func TestShopAccountService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("创建商户账号成功", func(t *testing.T) {
|
||||
@@ -130,11 +131,12 @@ func TestShopAccountService_Create(t *testing.T) {
|
||||
|
||||
// TestShopAccountService_Update 测试更新商户账号
|
||||
func TestShopAccountService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("更新商户账号成功", func(t *testing.T) {
|
||||
@@ -204,11 +206,12 @@ func TestShopAccountService_Update(t *testing.T) {
|
||||
|
||||
// TestShopAccountService_UpdatePassword 测试更新密码
|
||||
func TestShopAccountService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("更新密码成功", func(t *testing.T) {
|
||||
@@ -276,11 +279,12 @@ func TestShopAccountService_UpdatePassword(t *testing.T) {
|
||||
|
||||
// TestShopAccountService_UpdateStatus 测试更新状态
|
||||
func TestShopAccountService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("更新状态成功", func(t *testing.T) {
|
||||
@@ -348,11 +352,12 @@ func TestShopAccountService_UpdateStatus(t *testing.T) {
|
||||
|
||||
// TestShopAccountService_List 测试查询商户账号列表
|
||||
func TestShopAccountService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
service := shop_account.New(accountStore, shopStore)
|
||||
|
||||
t.Run("查询商户账号列表", func(t *testing.T) {
|
||||
|
||||
@@ -23,14 +23,15 @@ func createCommissionTestContext(userID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
@@ -94,14 +95,15 @@ func TestShopCommissionService_ListShopCommissionSummary(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
@@ -148,14 +150,15 @@ func TestShopCommissionService_ListShopWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
|
||||
@@ -18,11 +18,12 @@ import (
|
||||
|
||||
// TestShopService_Create 测试创建店铺
|
||||
func TestShopService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("创建一级店铺成功", func(t *testing.T) {
|
||||
@@ -236,11 +237,12 @@ func TestShopService_Create(t *testing.T) {
|
||||
|
||||
// TestShopService_Update 测试更新店铺
|
||||
func TestShopService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("更新店铺信息成功", func(t *testing.T) {
|
||||
@@ -365,11 +367,12 @@ func TestShopService_Update(t *testing.T) {
|
||||
|
||||
// TestShopService_Disable 测试禁用店铺
|
||||
func TestShopService_Disable(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("禁用店铺成功", func(t *testing.T) {
|
||||
@@ -428,11 +431,12 @@ func TestShopService_Disable(t *testing.T) {
|
||||
|
||||
// TestShopService_Enable 测试启用店铺
|
||||
func TestShopService_Enable(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("启用店铺成功", func(t *testing.T) {
|
||||
@@ -500,11 +504,12 @@ func TestShopService_Enable(t *testing.T) {
|
||||
|
||||
// TestShopService_GetByID 测试获取店铺详情
|
||||
func TestShopService_GetByID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("获取存在的店铺", func(t *testing.T) {
|
||||
@@ -548,11 +553,12 @@ func TestShopService_GetByID(t *testing.T) {
|
||||
|
||||
// TestShopService_List 测试查询店铺列表
|
||||
func TestShopService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("查询店铺列表", func(t *testing.T) {
|
||||
@@ -584,11 +590,12 @@ func TestShopService_List(t *testing.T) {
|
||||
|
||||
// TestShopService_GetSubordinateShopIDs 测试获取下级店铺 ID 列表
|
||||
func TestShopService_GetSubordinateShopIDs(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("获取下级店铺 ID 列表", func(t *testing.T) {
|
||||
@@ -648,11 +655,12 @@ func TestShopService_GetSubordinateShopIDs(t *testing.T) {
|
||||
|
||||
// TestShopService_Delete 测试删除店铺
|
||||
func TestShopService_Delete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
service := shop.New(shopStore, accountStore)
|
||||
|
||||
t.Run("删除店铺成功", func(t *testing.T) {
|
||||
|
||||
@@ -15,10 +15,11 @@ import (
|
||||
|
||||
// TestShopStore_Create 测试创建店铺
|
||||
func TestShopStore_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
@@ -77,10 +78,11 @@ func TestShopStore_Create(t *testing.T) {
|
||||
|
||||
// TestShopStore_GetByID 测试根据 ID 查询店铺
|
||||
func TestShopStore_GetByID(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试店铺
|
||||
@@ -115,10 +117,11 @@ func TestShopStore_GetByID(t *testing.T) {
|
||||
|
||||
// TestShopStore_GetByCode 测试根据店铺编号查询
|
||||
func TestShopStore_GetByCode(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试店铺
|
||||
@@ -152,10 +155,11 @@ func TestShopStore_GetByCode(t *testing.T) {
|
||||
|
||||
// TestShopStore_Update 测试更新店铺
|
||||
func TestShopStore_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试店铺
|
||||
@@ -205,10 +209,11 @@ func TestShopStore_Update(t *testing.T) {
|
||||
|
||||
// TestShopStore_Delete 测试软删除店铺
|
||||
func TestShopStore_Delete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试店铺
|
||||
@@ -239,10 +244,11 @@ func TestShopStore_Delete(t *testing.T) {
|
||||
|
||||
// TestShopStore_List 测试查询店铺列表
|
||||
func TestShopStore_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个测试店铺
|
||||
@@ -284,10 +290,11 @@ func TestShopStore_List(t *testing.T) {
|
||||
|
||||
// TestShopStore_GetSubordinateShopIDs 测试递归查询下级店铺 ID
|
||||
func TestShopStore_GetSubordinateShopIDs(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建店铺层级结构
|
||||
@@ -403,10 +410,11 @@ func TestShopStore_GetSubordinateShopIDs(t *testing.T) {
|
||||
|
||||
// TestShopStore_UniqueConstraints 测试唯一约束
|
||||
func TestShopStore_UniqueConstraints(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewShopStore(db, redisClient)
|
||||
store := postgres.NewShopStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试店铺
|
||||
|
||||
@@ -16,10 +16,11 @@ import (
|
||||
|
||||
// TestAccountSoftDelete 测试账号软删除功能
|
||||
func TestAccountSoftDelete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -45,7 +46,7 @@ func TestAccountSoftDelete(t *testing.T) {
|
||||
|
||||
t.Run("使用 Unscoped 可以查到已删除账号", func(t *testing.T) {
|
||||
var found model.Account
|
||||
err := db.Unscoped().First(&found, account.ID).Error
|
||||
err := tx.Unscoped().First(&found, account.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, account.Username, found.Username)
|
||||
assert.NotNil(t, found.DeletedAt)
|
||||
@@ -68,10 +69,11 @@ func TestAccountSoftDelete(t *testing.T) {
|
||||
|
||||
// TestRoleSoftDelete 测试角色软删除功能
|
||||
func TestRoleSoftDelete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试角色
|
||||
@@ -96,7 +98,7 @@ func TestRoleSoftDelete(t *testing.T) {
|
||||
|
||||
t.Run("使用 Unscoped 可以查到已删除角色", func(t *testing.T) {
|
||||
var found model.Role
|
||||
err := db.Unscoped().First(&found, role.ID).Error
|
||||
err := tx.Unscoped().First(&found, role.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, role.RoleName, found.RoleName)
|
||||
assert.NotNil(t, found.DeletedAt)
|
||||
@@ -105,10 +107,11 @@ func TestRoleSoftDelete(t *testing.T) {
|
||||
|
||||
// TestPermissionSoftDelete 测试权限软删除功能
|
||||
func TestPermissionSoftDelete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试权限
|
||||
@@ -146,12 +149,13 @@ func TestPermissionSoftDelete(t *testing.T) {
|
||||
|
||||
// TestAccountRoleSoftDelete 测试账号-角色关联软删除功能
|
||||
func TestAccountRoleSoftDelete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -216,12 +220,13 @@ func TestAccountRoleSoftDelete(t *testing.T) {
|
||||
|
||||
// TestRolePermissionSoftDelete 测试角色-权限关联软删除功能
|
||||
func TestRolePermissionSoftDelete(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
rolePermissionStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
rolePermissionStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试角色
|
||||
|
||||
@@ -25,33 +25,33 @@ type MockEmailPayload struct {
|
||||
|
||||
// TestHandlerIdempotency 测试处理器幂等性逻辑
|
||||
func TestHandlerIdempotency(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer redisClient.Close()
|
||||
defer rdb.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
requestID := "test-req-001"
|
||||
lockKey := constants.RedisTaskLockKey(requestID)
|
||||
|
||||
// 测试场景1: 第一次执行(未加锁)
|
||||
t.Run("First Execution - Should Acquire Lock", func(t *testing.T) {
|
||||
result, err := redisClient.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
result, err := rdb.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result, "第一次执行应该成功获取锁")
|
||||
})
|
||||
|
||||
// 测试场景2: 重复执行(已加锁)
|
||||
t.Run("Duplicate Execution - Should Skip", func(t *testing.T) {
|
||||
result, err := redisClient.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
result, err := rdb.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
require.NoError(t, err)
|
||||
assert.False(t, result, "重复执行应该跳过(锁已存在)")
|
||||
})
|
||||
|
||||
// 清理
|
||||
redisClient.Del(ctx, lockKey)
|
||||
rdb.Del(ctx, lockKey)
|
||||
}
|
||||
|
||||
// TestHandlerErrorHandling 测试处理器错误处理
|
||||
@@ -218,13 +218,13 @@ func TestPayloadDeserialization(t *testing.T) {
|
||||
|
||||
// TestTaskStatusTransition 测试任务状态转换
|
||||
func TestTaskStatusTransition(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer redisClient.Close()
|
||||
defer rdb.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
taskID := "task-transition-001"
|
||||
statusKey := constants.RedisTaskStatusKey(taskID)
|
||||
@@ -245,7 +245,7 @@ func TestTaskStatusTransition(t *testing.T) {
|
||||
t.Run("Transition to "+tr.status, func(t *testing.T) {
|
||||
// 检查状态转换是否合法
|
||||
if isValidTransition(currentStatus, tr.status) == tr.valid {
|
||||
err := redisClient.Set(ctx, statusKey, tr.status, 7*24*time.Hour).Err()
|
||||
err := rdb.Set(ctx, statusKey, tr.status, 7*24*time.Hour).Err()
|
||||
require.NoError(t, err)
|
||||
currentStatus = tr.status
|
||||
} else {
|
||||
@@ -281,13 +281,13 @@ func isValidTransition(from, to string) bool {
|
||||
|
||||
// TestConcurrentTaskExecution 测试并发任务执行
|
||||
func TestConcurrentTaskExecution(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer redisClient.Close()
|
||||
defer rdb.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
// 模拟多个并发任务尝试获取同一个锁
|
||||
requestID := "concurrent-test-001"
|
||||
@@ -301,7 +301,7 @@ func TestConcurrentTaskExecution(t *testing.T) {
|
||||
// 并发执行
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func() {
|
||||
result, err := redisClient.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
result, err := rdb.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
if err == nil && result {
|
||||
successCount++
|
||||
}
|
||||
@@ -364,19 +364,19 @@ func TestTaskTimeout(t *testing.T) {
|
||||
|
||||
// TestLockExpiration 测试锁过期机制
|
||||
func TestLockExpiration(t *testing.T) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
})
|
||||
defer redisClient.Close()
|
||||
defer rdb.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
redisClient.FlushDB(ctx)
|
||||
rdb.FlushDB(ctx)
|
||||
|
||||
requestID := "expiration-test-001"
|
||||
lockKey := constants.RedisTaskLockKey(requestID)
|
||||
|
||||
// 设置短 TTL 的锁
|
||||
result, err := redisClient.SetNX(ctx, lockKey, "1", 100*time.Millisecond).Result()
|
||||
result, err := rdb.SetNX(ctx, lockKey, "1", 100*time.Millisecond).Result()
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result)
|
||||
|
||||
@@ -384,7 +384,7 @@ func TestLockExpiration(t *testing.T) {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
// 验证锁已过期,可以重新获取
|
||||
result, err = redisClient.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
result, err = rdb.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result, "锁过期后应该可以重新获取")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user