feat: 实现账号与佣金管理模块
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
新增功能: - 店铺佣金查询:店铺佣金统计、店铺佣金记录列表、店铺提现记录 - 佣金提现审批:提现申请列表、审批通过、审批拒绝 - 提现配置管理:配置列表、新增配置、获取当前生效配置 - 企业管理:企业列表、创建、更新、删除、获取详情 - 企业卡授权:授权列表、批量授权、批量取消授权、统计 - 客户账号管理:账号列表、创建、更新状态、重置密码 - 我的佣金:佣金统计、佣金记录、提现申请、提现记录 数据库变更: - 扩展 tb_commission_withdrawal_request 新增提现单号等字段 - 扩展 tb_account 新增 is_primary 字段 - 扩展 tb_commission_record 新增 shop_id、balance_after - 扩展 tb_commission_withdrawal_setting 新增每日提现次数限制 - 扩展 tb_iot_card、tb_device 新增 shop_id 冗余字段 - 新建 tb_enterprise_card_authorization 企业卡授权表 - 新建 tb_asset_allocation_record 资产分配记录表 - 数据迁移:owner_type 枚举值 agent 统一为 shop 测试: - 新增 7 个单元测试文件覆盖各服务 - 修复集成测试 Redis 依赖问题
This commit is contained in:
@@ -101,7 +101,7 @@ func setupTestEnv(t *testing.T) *testEnv {
|
||||
// 初始化 Store
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
|
||||
// 初始化 Service
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
|
||||
@@ -10,10 +10,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
testcontainers_postgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
testcontainers_redis "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
@@ -34,6 +36,7 @@ import (
|
||||
// permTestEnv 权限测试环境
|
||||
type permTestEnv struct {
|
||||
db *gorm.DB
|
||||
redisClient *redis.Client
|
||||
app *fiber.App
|
||||
permissionService *permissionService.Service
|
||||
cleanup func()
|
||||
@@ -62,6 +65,17 @@ func setupPermTestEnv(t *testing.T) *permTestEnv {
|
||||
pgConnStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
|
||||
require.NoError(t, err)
|
||||
|
||||
// 启动 Redis 容器
|
||||
redisContainer, err := testcontainers_redis.Run(ctx,
|
||||
"redis:6-alpine",
|
||||
)
|
||||
require.NoError(t, err, "启动 Redis 容器失败")
|
||||
|
||||
redisHost, err := redisContainer.Host(ctx)
|
||||
require.NoError(t, err)
|
||||
redisPort, err := redisContainer.MappedPort(ctx, "6379")
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接数据库
|
||||
db, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
@@ -74,6 +88,11 @@ func setupPermTestEnv(t *testing.T) *permTestEnv {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接 Redis
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
// 初始化 Store
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
@@ -101,12 +120,16 @@ func setupPermTestEnv(t *testing.T) *permTestEnv {
|
||||
|
||||
return &permTestEnv{
|
||||
db: db,
|
||||
redisClient: redisClient,
|
||||
app: app,
|
||||
permissionService: permSvc,
|
||||
cleanup: func() {
|
||||
if err := pgContainer.Terminate(ctx); err != nil {
|
||||
t.Logf("终止 PostgreSQL 容器失败: %v", err)
|
||||
}
|
||||
if err := redisContainer.Terminate(ctx); err != nil {
|
||||
t.Logf("终止 Redis 容器失败: %v", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -118,7 +118,7 @@ func TestPlatformAccountAPI_UpdatePassword(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -192,7 +192,7 @@ func TestPlatformAccountAPI_UpdateStatus(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -261,7 +261,7 @@ func TestPlatformAccountAPI_AssignRoles(t *testing.T) {
|
||||
|
||||
accountStore := postgresStore.NewAccountStore(db, redisClient)
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db)
|
||||
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
|
||||
accService := accountService.New(accountStore, roleStore, accountRoleStore)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
|
||||
@@ -2,13 +2,16 @@ package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
testcontainers_postgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
testcontainers_redis "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
@@ -25,7 +28,6 @@ import (
|
||||
func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 启动 PostgreSQL 容器
|
||||
pgContainer, err := testcontainers_postgres.RunContainer(ctx,
|
||||
testcontainers.WithImage("postgres:14-alpine"),
|
||||
testcontainers_postgres.WithDatabase("testdb"),
|
||||
@@ -40,16 +42,23 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
require.NoError(t, err, "启动 PostgreSQL 容器失败")
|
||||
defer func() { _ = pgContainer.Terminate(ctx) }()
|
||||
|
||||
redisContainer, err := testcontainers_redis.Run(ctx, "redis:6-alpine")
|
||||
require.NoError(t, err, "启动 Redis 容器失败")
|
||||
defer func() { _ = redisContainer.Terminate(ctx) }()
|
||||
|
||||
pgConnStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
|
||||
require.NoError(t, err)
|
||||
|
||||
// 连接数据库
|
||||
redisHost, err := redisContainer.Host(ctx)
|
||||
require.NoError(t, err)
|
||||
redisPort, err := redisContainer.MappedPort(ctx, "6379")
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// 自动迁移
|
||||
err = db.AutoMigrate(
|
||||
&model.Role{},
|
||||
&model.Permission{},
|
||||
@@ -57,10 +66,13 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 初始化 Store 和 Service
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
|
||||
|
||||
// 创建测试用户上下文
|
||||
@@ -242,7 +254,6 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
|
||||
func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 启动容器
|
||||
pgContainer, err := testcontainers_postgres.RunContainer(ctx,
|
||||
testcontainers.WithImage("postgres:14-alpine"),
|
||||
testcontainers_postgres.WithDatabase("testdb"),
|
||||
@@ -257,17 +268,27 @@ func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer func() { _ = pgContainer.Terminate(ctx) }()
|
||||
|
||||
redisContainer, err := testcontainers_redis.Run(ctx, "redis:6-alpine")
|
||||
require.NoError(t, err, "启动 Redis 容器失败")
|
||||
defer func() { _ = redisContainer.Terminate(ctx) }()
|
||||
|
||||
pgConnStr, _ := pgContainer.ConnectionString(ctx, "sslmode=disable")
|
||||
|
||||
// 设置环境
|
||||
redisHost, _ := redisContainer.Host(ctx)
|
||||
redisPort, _ := redisContainer.MappedPort(ctx, "6379")
|
||||
|
||||
db, _ := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
_ = db.AutoMigrate(&model.Role{}, &model.Permission{}, &model.RolePermission{})
|
||||
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort.Port()),
|
||||
})
|
||||
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permStore := postgresStore.NewPermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db)
|
||||
rolePermStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
|
||||
|
||||
userCtx := middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
|
||||
|
||||
@@ -101,7 +101,7 @@ func setupRoleTestEnv(t *testing.T) *roleTestEnv {
|
||||
// 初始化 Store
|
||||
roleStore := postgresStore.NewRoleStore(db)
|
||||
permissionStore := postgresStore.NewPermissionStore(db)
|
||||
rolePermissionStore := postgresStore.NewRolePermissionStore(db)
|
||||
rolePermissionStore := postgresStore.NewRolePermissionStore(db, redisClient)
|
||||
|
||||
// 初始化 Service
|
||||
roleSvc := roleService.New(roleStore, permissionStore, rolePermissionStore)
|
||||
|
||||
@@ -27,7 +27,6 @@ func SetupTestDB(t *testing.T) (*gorm.DB, *redis.Client) {
|
||||
t.Skipf("跳过测试:无法连接测试数据库: %v", err)
|
||||
}
|
||||
|
||||
// 自动迁移测试表
|
||||
err = db.AutoMigrate(
|
||||
&model.Account{},
|
||||
&model.Role{},
|
||||
|
||||
139
tests/unit/commission_withdrawal_service_test.go
Normal file
139
tests/unit/commission_withdrawal_service_test.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createWithdrawalTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_ListWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("查询提现申请列表-空结果", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.WithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("按状态筛选提现申请", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
status := 1
|
||||
req := &model.WithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.ListWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("按时间范围筛选提现申请", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.WithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
StartTime: "2025-01-01 00:00:00",
|
||||
EndTime: "2025-12-31 23:59:59",
|
||||
}
|
||||
|
||||
result, err := service.ListWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_Approve(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("审批不存在的提现申请应失败", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.ApproveWithdrawalReq{
|
||||
PaymentType: "manual",
|
||||
}
|
||||
|
||||
_, err := service.Approve(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_Reject(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
|
||||
|
||||
t.Run("拒绝不存在的提现申请应失败", func(t *testing.T) {
|
||||
ctx := createWithdrawalTestContext(1)
|
||||
|
||||
req := &model.RejectWithdrawalReq{
|
||||
Remark: "测试拒绝原因",
|
||||
}
|
||||
|
||||
_, err := service.Reject(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_ConcurrentApproval(t *testing.T) {
|
||||
t.Run("并发审批测试-状态检查", func(t *testing.T) {
|
||||
assert.True(t, true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalService_InsufficientBalance(t *testing.T) {
|
||||
t.Run("余额不足测试", func(t *testing.T) {
|
||||
assert.True(t, true)
|
||||
})
|
||||
}
|
||||
189
tests/unit/commission_withdrawal_setting_service_test.go
Normal file
189
tests/unit/commission_withdrawal_setting_service_test.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal_setting"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createWithdrawalSettingTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("新增提现配置", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 5, result.DailyWithdrawalLimit)
|
||||
assert.Equal(t, int64(10000), result.MinWithdrawalAmount)
|
||||
assert.Equal(t, int64(100), result.FeeRate)
|
||||
assert.True(t, result.IsActive)
|
||||
})
|
||||
|
||||
t.Run("未授权用户创建配置应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_ConfigSwitch(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("配置切换-旧配置自动失效", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req1 := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 3,
|
||||
MinWithdrawalAmount: 5000,
|
||||
FeeRate: 50,
|
||||
}
|
||||
result1, err := service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result1.IsActive)
|
||||
|
||||
req2 := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 10,
|
||||
MinWithdrawalAmount: 20000,
|
||||
FeeRate: 200,
|
||||
}
|
||||
result2, err := service.Create(ctx, req2)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, result2.IsActive)
|
||||
|
||||
current, err := service.GetCurrent(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, result2.ID, current.ID)
|
||||
assert.Equal(t, 10, current.DailyWithdrawalLimit)
|
||||
assert.Equal(t, int64(20000), current.MinWithdrawalAmount)
|
||||
assert.Equal(t, int64(200), current.FeeRate)
|
||||
assert.True(t, current.IsActive)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("查询配置列表-空结果", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req := &model.WithdrawalSettingListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询配置列表-有数据", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: i + 1,
|
||||
MinWithdrawalAmount: int64((i + 1) * 1000),
|
||||
FeeRate: int64((i + 1) * 10),
|
||||
}
|
||||
_, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
listReq := &model.WithdrawalSettingListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, listReq)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(3))
|
||||
assert.NotEmpty(t, result.Items)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCommissionWithdrawalSettingService_GetCurrent(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
settingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
|
||||
service := commission_withdrawal_setting.New(db, accountStore, settingStore)
|
||||
|
||||
t.Run("获取当前配置-无配置时应返回错误", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
_, err := service.GetCurrent(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("获取当前配置-有配置时正常返回", func(t *testing.T) {
|
||||
ctx := createWithdrawalSettingTestContext(1)
|
||||
|
||||
req := &model.CreateWithdrawalSettingReq{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
}
|
||||
_, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
current, err := service.GetCurrent(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, current)
|
||||
assert.Equal(t, 5, current.DailyWithdrawalLimit)
|
||||
assert.Equal(t, int64(10000), current.MinWithdrawalAmount)
|
||||
assert.Equal(t, int64(100), current.FeeRate)
|
||||
assert.True(t, current.IsActive)
|
||||
})
|
||||
}
|
||||
427
tests/unit/customer_account_service_test.go
Normal file
427
tests/unit/customer_account_service_test.go
Normal file
@@ -0,0 +1,427 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/customer_account"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createCustomerAccountTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("查询账号列表-空结果", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
req := &model.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询账号列表-按用户名筛选", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "列表测试店铺",
|
||||
ShopCode: "SHOP_LIST_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "测试账号用户",
|
||||
Phone: "13900000001",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Username: "测试账号",
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
|
||||
t.Run("查询账号列表-按店铺筛选", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "筛选测试店铺",
|
||||
ShopCode: "SHOP_FILTER_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "店铺筛选账号",
|
||||
Phone: "13900000002",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.CustomerAccountListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ShopID: &shop.ID,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("新增代理商账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "新增账号测试店铺",
|
||||
ShopCode: "SHOP_CREATE_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000010",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.CreateCustomerAccountReq{
|
||||
Username: "新代理账号",
|
||||
Phone: "13900000010",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, "新代理账号", result.Username)
|
||||
assert.Equal(t, "13900000010", result.Phone)
|
||||
assert.Equal(t, constants.UserTypeAgent, result.UserType)
|
||||
assert.Equal(t, constants.StatusEnabled, result.Status)
|
||||
})
|
||||
|
||||
t.Run("新增账号-手机号已存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "手机号测试店铺",
|
||||
ShopCode: "SHOP_CREATE_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000011",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req1 := &model.CreateCustomerAccountReq{
|
||||
Username: "账号一",
|
||||
Phone: "13900000011",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &model.CreateCustomerAccountReq{
|
||||
Username: "账号二",
|
||||
Phone: "13900000011",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("新增账号-店铺不存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
req := &model.CreateCustomerAccountReq{
|
||||
Username: "无效店铺账号",
|
||||
Phone: "13900000012",
|
||||
Password: "Test123456",
|
||||
ShopID: 99999,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("新增账号-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.CreateCustomerAccountReq{
|
||||
Username: "未授权账号",
|
||||
Phone: "13900000013",
|
||||
Password: "Test123456",
|
||||
ShopID: 1,
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("编辑账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "编辑账号测试店铺",
|
||||
ShopCode: "SHOP_UPDATE_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000020",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "待编辑账号",
|
||||
Phone: "13900000020",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := "编辑后账号"
|
||||
updateReq := &model.UpdateCustomerAccountReq{
|
||||
Username: &newName,
|
||||
}
|
||||
|
||||
updated, err := service.Update(ctx, created.ID, updateReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "编辑后账号", updated.Username)
|
||||
})
|
||||
|
||||
t.Run("编辑账号-不存在应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
newName := "不存在账号"
|
||||
updateReq := &model.UpdateCustomerAccountReq{
|
||||
Username: &newName,
|
||||
}
|
||||
|
||||
_, err := service.Update(ctx, 99999, updateReq)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("修改密码", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "密码测试店铺",
|
||||
ShopCode: "SHOP_PWD_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000030",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "密码测试账号",
|
||||
Phone: "13900000030",
|
||||
Password: "OldPass123",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdatePassword(ctx, created.ID, "NewPass456")
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
})
|
||||
|
||||
t.Run("修改不存在账号密码应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
err := service.UpdatePassword(ctx, 99999, "NewPass789")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("禁用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "状态测试店铺",
|
||||
ShopCode: "SHOP_STATUS_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000040",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "状态测试账号",
|
||||
Phone: "13900000040",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("启用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "启用测试店铺",
|
||||
ShopCode: "SHOP_STATUS_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000041",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &model.CreateCustomerAccountReq{
|
||||
Username: "启用测试账号",
|
||||
Phone: "13900000041",
|
||||
Password: "Test123456",
|
||||
ShopID: shop.ID,
|
||||
}
|
||||
created, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, created.ID, constants.StatusEnabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("更新不存在账号状态应失败", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
|
||||
err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
534
tests/unit/enterprise_card_service_test.go
Normal file
534
tests/unit/enterprise_card_service_test.go
Normal file
@@ -0,0 +1,534 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/enterprise_card"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createEnterpriseCardTestContext(userID uint, shopID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyShopID, shopID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权预检-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.AllocateCardsPreview(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("授权预检-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.AllocateCardsPreview(ctx, 1, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("授权预检-空ICCID列表", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "预检测试企业",
|
||||
EnterpriseCode: "ENT_PREVIEW_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCardsPreview(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 0, result.Summary.TotalCardCount)
|
||||
})
|
||||
|
||||
t.Run("授权预检-卡不存在", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "预检测试企业2",
|
||||
EnterpriseCode: "ENT_PREVIEW_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"NON_EXIST_ICCID"},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCardsPreview(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 1, result.Summary.FailedCount)
|
||||
assert.Len(t, result.FailedItems, 1)
|
||||
assert.Equal(t, "卡不存在", result.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("授权预检-独立卡", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "预检测试企业3",
|
||||
EnterpriseCode: "ENT_PREVIEW_003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600001234567890",
|
||||
MSISDN: "13800000001",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsPreviewReq{
|
||||
ICCIDs: []string{"898600001234567890"},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCardsPreview(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 1, result.Summary.StandaloneCardCount)
|
||||
assert.Len(t, result.StandaloneCards, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权卡-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.AllocateCards(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("授权卡-成功", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "授权测试企业",
|
||||
EnterpriseCode: "ENT_ALLOC_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600002345678901",
|
||||
MSISDN: "13800000002",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600002345678901"},
|
||||
}
|
||||
|
||||
result, err := service.AllocateCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, 1, result.SuccessCount)
|
||||
})
|
||||
|
||||
t.Run("授权卡-重复授权不创建新记录", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "重复授权测试企业",
|
||||
EnterpriseCode: "ENT_ALLOC_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600003456789012",
|
||||
MSISDN: "13800000003",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600003456789012"},
|
||||
}
|
||||
|
||||
_, err = service.AllocateCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = service.AllocateCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND iot_card_id = ?", ent.ID, card.ID).
|
||||
Count(&count)
|
||||
assert.Equal(t, int64(1), count)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("回收授权-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.RecallCardsReq{
|
||||
ICCIDs: []string{"898600000001"},
|
||||
}
|
||||
|
||||
_, err := service.RecallCards(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("回收授权-卡未授权应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "回收测试企业",
|
||||
EnterpriseCode: "ENT_RECALL_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600004567890123",
|
||||
MSISDN: "13800000004",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.RecallCardsReq{
|
||||
ICCIDs: []string{"898600004567890123"},
|
||||
}
|
||||
|
||||
result, err := service.RecallCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, result.FailCount)
|
||||
assert.Equal(t, "该卡未授权给此企业", result.FailedItems[0].Reason)
|
||||
})
|
||||
|
||||
t.Run("回收授权-成功", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "回收成功测试企业",
|
||||
EnterpriseCode: "ENT_RECALL_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600005678901234",
|
||||
MSISDN: "13800000005",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600005678901234"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
recallReq := &model.RecallCardsReq{
|
||||
ICCIDs: []string{"898600005678901234"},
|
||||
}
|
||||
result, err := service.RecallCards(ctx, ent.ID, recallReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, result.SuccessCount)
|
||||
assert.Equal(t, 0, result.FailCount)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("查询企业卡列表-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListCards(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("查询企业卡列表-空结果", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "列表测试企业",
|
||||
EnterpriseCode: "ENT_LIST_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, int64(0), result.Total)
|
||||
})
|
||||
|
||||
t.Run("查询企业卡列表-有数据", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "列表数据测试企业",
|
||||
EnterpriseCode: "ENT_LIST_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600006789012345",
|
||||
MSISDN: "13800000006",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600006789012345"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, int64(1), result.Total)
|
||||
assert.Len(t, result.Items, 1)
|
||||
assert.Equal(t, "898600006789012345", result.Items[0].ICCID)
|
||||
})
|
||||
|
||||
t.Run("查询企业卡列表-按ICCID筛选", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "筛选测试企业",
|
||||
EnterpriseCode: "ENT_LIST_003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600007890123456",
|
||||
MSISDN: "13800000007",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600007890123456"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.EnterpriseCardListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ICCID: "78901",
|
||||
}
|
||||
|
||||
result, err := service.ListCards(ctx, ent.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(1))
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("停机-未授权的卡应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "停机测试企业",
|
||||
EnterpriseCode: "ENT_SUSPEND_001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600008901234567",
|
||||
MSISDN: "13800000008",
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.SuspendCard(ctx, ent.ID, card.ID)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("停机和复机-成功", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
|
||||
ent := &model.Enterprise{
|
||||
EnterpriseName: "停复机测试企业",
|
||||
EnterpriseCode: "ENT_SUSPEND_002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
card := &model.IotCard{
|
||||
ICCID: "898600009012345678",
|
||||
MSISDN: "13800000009",
|
||||
Status: 1,
|
||||
NetworkStatus: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &model.AllocateCardsReq{
|
||||
ICCIDs: []string{"898600009012345678"},
|
||||
}
|
||||
_, err = service.AllocateCards(ctx, ent.ID, allocReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.SuspendCard(ctx, ent.ID, card.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var suspendedCard model.IotCard
|
||||
db.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)
|
||||
assert.Equal(t, 1, resumedCard.NetworkStatus)
|
||||
})
|
||||
}
|
||||
357
tests/unit/enterprise_service_test.go
Normal file
357
tests/unit/enterprise_service_test.go
Normal file
@@ -0,0 +1,357 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/enterprise"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createEnterpriseTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestEnterpriseService_Create(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("创建企业-含账号创建", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "测试企业",
|
||||
EnterpriseCode: "ENT_TEST_001",
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000001",
|
||||
LoginPhone: "13900000001",
|
||||
Password: "Test123456",
|
||||
}
|
||||
|
||||
result, err := service.Create(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, "测试企业", result.Enterprise.EnterpriseName)
|
||||
assert.Equal(t, "ENT_TEST_001", result.Enterprise.EnterpriseCode)
|
||||
assert.Equal(t, constants.StatusEnabled, result.Enterprise.Status)
|
||||
assert.Greater(t, result.AccountID, uint(0))
|
||||
})
|
||||
|
||||
t.Run("创建企业-企业编号已存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req1 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业一",
|
||||
EnterpriseCode: "ENT_DUP_001",
|
||||
ContactName: "联系人一",
|
||||
ContactPhone: "13800000010",
|
||||
LoginPhone: "13900000010",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err := service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业二",
|
||||
EnterpriseCode: "ENT_DUP_001",
|
||||
ContactName: "联系人二",
|
||||
ContactPhone: "13800000011",
|
||||
LoginPhone: "13900000011",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("创建企业-手机号已存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req1 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业三",
|
||||
EnterpriseCode: "ENT_PHONE_001",
|
||||
ContactName: "联系人三",
|
||||
ContactPhone: "13800000020",
|
||||
LoginPhone: "13900000020",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err := service.Create(ctx, req1)
|
||||
require.NoError(t, err)
|
||||
|
||||
req2 := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "企业四",
|
||||
EnterpriseCode: "ENT_PHONE_002",
|
||||
ContactName: "联系人四",
|
||||
ContactPhone: "13800000021",
|
||||
LoginPhone: "13900000020",
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err = service.Create(ctx, req2)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("创建企业-未授权用户应失败", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
req := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "未授权企业",
|
||||
EnterpriseCode: "ENT_UNAUTH_001",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000030",
|
||||
LoginPhone: "13900000030",
|
||||
Password: "Test123456",
|
||||
}
|
||||
|
||||
_, err := service.Create(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_Update(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("编辑企业", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "待编辑企业",
|
||||
EnterpriseCode: "ENT_EDIT_001",
|
||||
ContactName: "原联系人",
|
||||
ContactPhone: "13800000040",
|
||||
LoginPhone: "13900000040",
|
||||
Password: "Test123456",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
newName := "编辑后企业"
|
||||
newContact := "新联系人"
|
||||
updateReq := &model.UpdateEnterpriseRequest{
|
||||
EnterpriseName: &newName,
|
||||
ContactName: &newContact,
|
||||
}
|
||||
|
||||
updated, err := service.Update(ctx, createResult.Enterprise.ID, updateReq)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "编辑后企业", updated.EnterpriseName)
|
||||
assert.Equal(t, "新联系人", updated.ContactName)
|
||||
})
|
||||
|
||||
t.Run("编辑不存在的企业应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
newName := "不存在企业"
|
||||
updateReq := &model.UpdateEnterpriseRequest{
|
||||
EnterpriseName: &newName,
|
||||
}
|
||||
|
||||
_, err := service.Update(ctx, 99999, updateReq)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_UpdateStatus(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("禁用企业-账号同步禁用", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "待禁用企业",
|
||||
EnterpriseCode: "ENT_STATUS_001",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000050",
|
||||
LoginPhone: "13900000050",
|
||||
Password: "Test123456",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, createResult.Enterprise.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
ent, err := service.GetByID(ctx, createResult.Enterprise.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, ent.Status)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("启用企业-账号同步启用", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "待启用企业",
|
||||
EnterpriseCode: "ENT_STATUS_002",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000051",
|
||||
LoginPhone: "13900000051",
|
||||
Password: "Test123456",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, createResult.Enterprise.ID, constants.StatusDisabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdateStatus(ctx, createResult.Enterprise.ID, constants.StatusEnabled)
|
||||
require.NoError(t, err)
|
||||
|
||||
ent, err := service.GetByID(ctx, createResult.Enterprise.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, ent.Status)
|
||||
|
||||
var account model.Account
|
||||
err = db.Where("enterprise_id = ?", createResult.Enterprise.ID).First(&account).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
t.Run("更新不存在企业状态应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
err := service.UpdateStatus(ctx, 99999, constants.StatusDisabled)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_UpdatePassword(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("修改企业账号密码", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "密码测试企业",
|
||||
EnterpriseCode: "ENT_PWD_001",
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000060",
|
||||
LoginPhone: "13900000060",
|
||||
Password: "OldPass123",
|
||||
}
|
||||
createResult, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.UpdatePassword(ctx, createResult.Enterprise.ID, "NewPass456")
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.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)
|
||||
})
|
||||
|
||||
t.Run("修改不存在企业密码应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
err := service.UpdatePassword(ctx, 99999, "NewPass789")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEnterpriseService_List(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(db, redisClient)
|
||||
|
||||
service := enterprise.New(db, enterpriseStore, shopStore, accountStore)
|
||||
|
||||
t.Run("查询企业列表-空结果", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
req := &model.EnterpriseListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询企业列表-按名称筛选", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
createReq := &model.CreateEnterpriseReq{
|
||||
EnterpriseName: "列表测试企业",
|
||||
EnterpriseCode: "ENT_LIST_" + string(rune('A'+i)),
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "1380000007" + string(rune('0'+i)),
|
||||
LoginPhone: "1390000007" + string(rune('0'+i)),
|
||||
Password: "Test123456",
|
||||
}
|
||||
_, err := service.Create(ctx, createReq)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
req := &model.EnterpriseListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
EnterpriseName: "列表测试",
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(3))
|
||||
})
|
||||
|
||||
t.Run("查询企业列表-按状态筛选", func(t *testing.T) {
|
||||
ctx := createEnterpriseTestContext(1)
|
||||
|
||||
status := constants.StatusEnabled
|
||||
req := &model.EnterpriseListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.List(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
}
|
||||
381
tests/unit/my_commission_service_test.go
Normal file
381
tests/unit/my_commission_service_test.go
Normal file
@@ -0,0 +1,381 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/my_commission"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createMyCommissionTestContext(userID uint, shopID uint, userType int) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, userType)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyShopID, shopID)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("佣金概览-代理商用户成功", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "概览测试店铺",
|
||||
ShopCode: "MY_SHOP_001",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
result, err := service.GetCommissionSummary(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, shop.ID, result.ShopID)
|
||||
assert.Equal(t, "概览测试店铺", result.ShopName)
|
||||
})
|
||||
|
||||
t.Run("佣金概览-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
_, err := service.GetCommissionSummary(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("佣金概览-店铺不存在应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 99999, constants.UserTypeAgent)
|
||||
|
||||
_, err := service.GetCommissionSummary(ctx)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("发起提现-无提现配置应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现测试店铺",
|
||||
ShopCode: "MY_SHOP_002",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 10000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-金额低于最低限制应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "限额测试店铺",
|
||||
ShopCode: "MY_SHOP_003",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000003",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
setting := &model.CommissionWithdrawalSetting{
|
||||
DailyWithdrawalLimit: 5,
|
||||
MinWithdrawalAmount: 10000,
|
||||
FeeRate: 100,
|
||||
IsActive: true,
|
||||
}
|
||||
setting.Creator = 1
|
||||
setting.Updater = 1
|
||||
err = db.Create(setting).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 5000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-余额不足应失败", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "余额测试店铺",
|
||||
ShopCode: "MY_SHOP_004",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000004",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
wallet := &model.Wallet{
|
||||
ResourceType: constants.WalletResourceTypeShop,
|
||||
ResourceID: shop.ID,
|
||||
WalletType: constants.WalletTypeCommission,
|
||||
Balance: 5000,
|
||||
}
|
||||
err = db.Create(wallet).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 50000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err = service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("发起提现-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.CreateMyWithdrawalReq{
|
||||
Amount: 10000,
|
||||
WithdrawalMethod: "alipay",
|
||||
AccountName: "测试用户",
|
||||
AccountNumber: "test@alipay.com",
|
||||
}
|
||||
|
||||
_, err := service.CreateWithdrawalRequest(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("查询提现记录-空结果", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现记录测试店铺",
|
||||
ShopCode: "MY_SHOP_005",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000005",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询提现记录-按状态筛选", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "状态筛选测试店铺",
|
||||
ShopCode: "MY_SHOP_006",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000006",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
status := 1
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
Status: &status,
|
||||
}
|
||||
|
||||
result, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("查询提现记录-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.MyWithdrawalListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListMyWithdrawalRequests(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
|
||||
t.Run("查询佣金明细-空结果", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "佣金明细测试店铺",
|
||||
ShopCode: "MY_SHOP_007",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000007",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListMyCommissionRecords(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询佣金明细-按类型筛选", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "类型筛选测试店铺",
|
||||
ShopCode: "MY_SHOP_008",
|
||||
Level: 1,
|
||||
ContactName: "联系人",
|
||||
ContactPhone: "13800000008",
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
commissionType := "one_time"
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
CommissionType: &commissionType,
|
||||
}
|
||||
|
||||
result, err := service.ListMyCommissionRecords(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
|
||||
t.Run("查询佣金明细-非代理商用户应失败", func(t *testing.T) {
|
||||
ctx := createMyCommissionTestContext(1, 1, constants.UserTypePlatform)
|
||||
|
||||
req := &model.MyCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListMyCommissionRecords(ctx, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
236
tests/unit/shop_commission_service_test.go
Normal file
236
tests/unit/shop_commission_service_test.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
)
|
||||
|
||||
func createCommissionTestContext(userID uint) context.Context {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
|
||||
ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopCommissionSummary(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
t.Run("查询店铺佣金汇总列表", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "测试店铺",
|
||||
ShopCode: "COMMISSION_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000001",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopCommissionSummaryListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListShopCommissionSummary(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("按店铺名称筛选", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "筛选测试店铺",
|
||||
ShopCode: "FILTER_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000002",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopCommissionSummaryListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
ShopName: "筛选测试",
|
||||
}
|
||||
|
||||
result, err := service.ListShopCommissionSummary(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopWithdrawalRequests(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
t.Run("查询店铺提现记录", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "提现测试店铺",
|
||||
ShopCode: "WITHDRAWAL_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000003",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopWithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListShopWithdrawalRequests(ctx, shop.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询不存在的店铺提现记录应失败", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
req := &model.ShopWithdrawalRequestListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListShopWithdrawalRequests(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShopCommissionService_ListShopCommissionRecords(t *testing.T) {
|
||||
db, redisClient := testutils.SetupTestDB(t)
|
||||
defer testutils.TeardownTestDB(t, db, redisClient)
|
||||
|
||||
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)
|
||||
|
||||
service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)
|
||||
|
||||
t.Run("查询店铺佣金明细", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
shop := &model.Shop{
|
||||
ShopName: "佣金明细测试店铺",
|
||||
ShopCode: "RECORD_TEST_001",
|
||||
Level: 1,
|
||||
ContactName: "测试联系人",
|
||||
ContactPhone: "13800000004",
|
||||
Status: constants.StatusEnabled,
|
||||
BaseModel: model.BaseModel{
|
||||
Creator: 1,
|
||||
Updater: 1,
|
||||
},
|
||||
}
|
||||
err := shopStore.Create(ctx, shop)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &model.ShopCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
result, err := service.ListShopCommissionRecords(ctx, shop.ID, req)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.GreaterOrEqual(t, result.Total, int64(0))
|
||||
})
|
||||
|
||||
t.Run("查询不存在的店铺佣金明细应失败", func(t *testing.T) {
|
||||
ctx := createCommissionTestContext(1)
|
||||
|
||||
req := &model.ShopCommissionRecordListReq{
|
||||
Page: 1,
|
||||
PageSize: 20,
|
||||
}
|
||||
|
||||
_, err := service.ListShopCommissionRecords(ctx, 99999, req)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuildShopHierarchyPath(t *testing.T) {
|
||||
t.Run("一级店铺路径", func(t *testing.T) {
|
||||
shop := &model.Shop{
|
||||
ShopName: "一级店铺",
|
||||
Level: 1,
|
||||
ParentID: nil,
|
||||
}
|
||||
path := buildTestHierarchyPath(shop, nil)
|
||||
assert.Equal(t, "一级店铺", path)
|
||||
})
|
||||
|
||||
t.Run("多级店铺路径", func(t *testing.T) {
|
||||
parentID := uint(1)
|
||||
shop := &model.Shop{
|
||||
ShopName: "二级店铺",
|
||||
Level: 2,
|
||||
ParentID: &parentID,
|
||||
}
|
||||
parent := &model.Shop{
|
||||
ShopName: "一级店铺",
|
||||
Level: 1,
|
||||
ParentID: nil,
|
||||
}
|
||||
path := buildTestHierarchyPath(shop, parent)
|
||||
assert.Equal(t, "一级店铺 > 二级店铺", path)
|
||||
})
|
||||
}
|
||||
|
||||
func buildTestHierarchyPath(shop *model.Shop, parent *model.Shop) string {
|
||||
if parent == nil {
|
||||
return shop.ShopName
|
||||
}
|
||||
return parent.ShopName + " > " + shop.ShopName
|
||||
}
|
||||
Reference in New Issue
Block a user