重构: 将卡/设备的套餐系列绑定从分配ID改为系列ID
- 数据库: 重命名 series_allocation_id → series_id - Model: IotCard 和 Device 字段重命名 - DTO: 所有请求/响应字段统一为 series_id - Store: 方法重命名,新增 GetByShopAndSeries 查询 - Service: 业务逻辑优化,系列验证和权限验证分离 - 测试: 更新所有测试用例,新增 shop_series_allocation_store_test.go - 文档: 更新 API 文档说明参数变更 BREAKING CHANGE: API 参数从 series_allocation_id 改为 series_id
This commit is contained in:
@@ -106,8 +106,8 @@ func (s *DeviceStore) List(ctx context.Context, opts *store.QueryOptions, filter
|
||||
if createdAtEnd, ok := filters["created_at_end"].(time.Time); ok && !createdAtEnd.IsZero() {
|
||||
query = query.Where("created_at <= ?", createdAtEnd)
|
||||
}
|
||||
if seriesAllocationID, ok := filters["series_allocation_id"].(uint); ok && seriesAllocationID > 0 {
|
||||
query = query.Where("series_allocation_id = ?", seriesAllocationID)
|
||||
if seriesID, ok := filters["series_id"].(uint); ok && seriesID > 0 {
|
||||
query = query.Where("series_id = ?", seriesID)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@@ -185,20 +185,20 @@ func (s *DeviceStore) GetByDeviceNos(ctx context.Context, deviceNos []string) ([
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// BatchUpdateSeriesAllocation 批量更新设备的套餐系列分配
|
||||
func (s *DeviceStore) BatchUpdateSeriesAllocation(ctx context.Context, deviceIDs []uint, seriesAllocationID *uint) error {
|
||||
// BatchUpdateSeriesID 批量更新设备的套餐系列ID
|
||||
func (s *DeviceStore) BatchUpdateSeriesID(ctx context.Context, deviceIDs []uint, seriesID *uint) error {
|
||||
if len(deviceIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.db.WithContext(ctx).Model(&model.Device{}).
|
||||
Where("id IN ?", deviceIDs).
|
||||
Update("series_allocation_id", seriesAllocationID).Error
|
||||
Update("series_id", seriesID).Error
|
||||
}
|
||||
|
||||
// ListBySeriesAllocationID 根据套餐系列分配ID查询设备列表
|
||||
func (s *DeviceStore) ListBySeriesAllocationID(ctx context.Context, seriesAllocationID uint) ([]*model.Device, error) {
|
||||
// ListBySeriesID 根据套餐系列ID查询设备列表
|
||||
func (s *DeviceStore) ListBySeriesID(ctx context.Context, seriesID uint) ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
if err := s.db.WithContext(ctx).Where("series_allocation_id = ?", seriesAllocationID).Find(&devices).Error; err != nil {
|
||||
if err := s.db.WithContext(ctx).Where("series_id = ?", seriesID).Find(&devices).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return devices, nil
|
||||
|
||||
@@ -16,7 +16,7 @@ func uniqueDeviceNoPrefix() string {
|
||||
return fmt.Sprintf("D%d", time.Now().UnixNano()%1000000000)
|
||||
}
|
||||
|
||||
func TestDeviceStore_BatchUpdateSeriesAllocation(t *testing.T) {
|
||||
func TestDeviceStore_BatchUpdateSeriesID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
@@ -31,39 +31,39 @@ func TestDeviceStore_BatchUpdateSeriesAllocation(t *testing.T) {
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, devices))
|
||||
|
||||
t.Run("设置系列分配ID", func(t *testing.T) {
|
||||
seriesAllocationID := uint(100)
|
||||
t.Run("设置系列ID", func(t *testing.T) {
|
||||
seriesID := uint(100)
|
||||
deviceIDs := []uint{devices[0].ID, devices[1].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, deviceIDs, &seriesAllocationID)
|
||||
err := s.BatchUpdateSeriesID(ctx, deviceIDs, &seriesID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var updatedDevices []*model.Device
|
||||
require.NoError(t, tx.Where("id IN ?", deviceIDs).Find(&updatedDevices).Error)
|
||||
for _, device := range updatedDevices {
|
||||
require.NotNil(t, device.SeriesAllocationID)
|
||||
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
|
||||
require.NotNil(t, device.SeriesID)
|
||||
assert.Equal(t, seriesID, *device.SeriesID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("清除系列分配ID", func(t *testing.T) {
|
||||
t.Run("清除系列ID", func(t *testing.T) {
|
||||
deviceIDs := []uint{devices[0].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, deviceIDs, nil)
|
||||
err := s.BatchUpdateSeriesID(ctx, deviceIDs, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
var updatedDevice model.Device
|
||||
require.NoError(t, tx.First(&updatedDevice, devices[0].ID).Error)
|
||||
assert.Nil(t, updatedDevice.SeriesAllocationID)
|
||||
assert.Nil(t, updatedDevice.SeriesID)
|
||||
})
|
||||
|
||||
t.Run("空列表不报错", func(t *testing.T) {
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, []uint{}, nil)
|
||||
err := s.BatchUpdateSeriesID(ctx, []uint{}, nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeviceStore_ListBySeriesAllocationID(t *testing.T) {
|
||||
func TestDeviceStore_ListBySeriesID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
@@ -72,23 +72,23 @@ func TestDeviceStore_ListBySeriesAllocationID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueDeviceNoPrefix()
|
||||
seriesAllocationID := uint(200)
|
||||
seriesID := uint(200)
|
||||
devices := []*model.Device{
|
||||
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesAllocationID: nil},
|
||||
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesID: &seriesID},
|
||||
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesID: &seriesID},
|
||||
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesID: nil},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, devices))
|
||||
|
||||
result, err := s.ListBySeriesAllocationID(ctx, seriesAllocationID)
|
||||
result, err := s.ListBySeriesID(ctx, seriesID)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, result, 2)
|
||||
for _, device := range result {
|
||||
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
|
||||
assert.Equal(t, seriesID, *device.SeriesID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceStore_List_SeriesAllocationFilter(t *testing.T) {
|
||||
func TestDeviceStore_List_SeriesIDFilter(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
@@ -97,23 +97,23 @@ func TestDeviceStore_List_SeriesAllocationFilter(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueDeviceNoPrefix()
|
||||
seriesAllocationID := uint(300)
|
||||
seriesID := uint(300)
|
||||
devices := []*model.Device{
|
||||
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesAllocationID: nil},
|
||||
{DeviceNo: prefix + "001", DeviceName: "测试设备1", Status: 1, SeriesID: &seriesID},
|
||||
{DeviceNo: prefix + "002", DeviceName: "测试设备2", Status: 1, SeriesID: &seriesID},
|
||||
{DeviceNo: prefix + "003", DeviceName: "测试设备3", Status: 1, SeriesID: nil},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, devices))
|
||||
|
||||
filters := map[string]interface{}{
|
||||
"series_allocation_id": seriesAllocationID,
|
||||
"device_no": prefix,
|
||||
"series_id": seriesID,
|
||||
"device_no": prefix,
|
||||
}
|
||||
result, total, err := s.List(ctx, nil, filters)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(2), total)
|
||||
assert.Len(t, result, 2)
|
||||
for _, device := range result {
|
||||
assert.Equal(t, seriesAllocationID, *device.SeriesAllocationID)
|
||||
assert.Equal(t, seriesID, *device.SeriesID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,8 +147,8 @@ func (s *IotCardStore) List(ctx context.Context, opts *store.QueryOptions, filte
|
||||
if iccidEnd, ok := filters["iccid_end"].(string); ok && iccidEnd != "" {
|
||||
query = query.Where("iccid <= ?", iccidEnd)
|
||||
}
|
||||
if seriesAllocationID, ok := filters["series_allocation_id"].(uint); ok && seriesAllocationID > 0 {
|
||||
query = query.Where("series_allocation_id = ?", seriesAllocationID)
|
||||
if seriesID, ok := filters["series_id"].(uint); ok && seriesID > 0 {
|
||||
query = query.Where("series_id = ?", seriesID)
|
||||
}
|
||||
|
||||
// 统计总数
|
||||
@@ -242,8 +242,8 @@ func (s *IotCardStore) ListStandalone(ctx context.Context, opts *store.QueryOpti
|
||||
Where("deleted_at IS NULL"))
|
||||
}
|
||||
}
|
||||
if seriesAllocationID, ok := filters["series_allocation_id"].(uint); ok && seriesAllocationID > 0 {
|
||||
query = query.Where("series_allocation_id = ?", seriesAllocationID)
|
||||
if seriesID, ok := filters["series_id"].(uint); ok && seriesID > 0 {
|
||||
query = query.Where("series_id = ?", seriesID)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@@ -381,22 +381,22 @@ func (s *IotCardStore) GetByIDsWithEnterpriseFilter(ctx context.Context, cardIDs
|
||||
return cards, nil
|
||||
}
|
||||
|
||||
// BatchUpdateSeriesAllocation 批量更新卡的套餐系列分配
|
||||
// BatchUpdateSeriesID 批量更新卡的套餐系列ID
|
||||
// 用于批量设置或清除卡与套餐系列的关联关系
|
||||
func (s *IotCardStore) BatchUpdateSeriesAllocation(ctx context.Context, cardIDs []uint, seriesAllocationID *uint) error {
|
||||
func (s *IotCardStore) BatchUpdateSeriesID(ctx context.Context, cardIDs []uint, seriesID *uint) error {
|
||||
if len(cardIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.db.WithContext(ctx).Model(&model.IotCard{}).
|
||||
Where("id IN ?", cardIDs).
|
||||
Update("series_allocation_id", seriesAllocationID).Error
|
||||
Update("series_id", seriesID).Error
|
||||
}
|
||||
|
||||
// ListBySeriesAllocationID 根据套餐系列分配ID查询卡列表
|
||||
// 用于查询某个套餐系列分配下的所有卡
|
||||
func (s *IotCardStore) ListBySeriesAllocationID(ctx context.Context, seriesAllocationID uint) ([]*model.IotCard, error) {
|
||||
// ListBySeriesID 根据套餐系列ID查询卡列表
|
||||
// 用于查询某个套餐系列下的所有卡
|
||||
func (s *IotCardStore) ListBySeriesID(ctx context.Context, seriesID uint) ([]*model.IotCard, error) {
|
||||
var cards []*model.IotCard
|
||||
if err := s.db.WithContext(ctx).Where("series_allocation_id = ?", seriesAllocationID).Find(&cards).Error; err != nil {
|
||||
if err := s.db.WithContext(ctx).Where("series_id = ?", seriesID).Find(&cards).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cards, nil
|
||||
|
||||
@@ -426,7 +426,7 @@ func TestIotCardStore_GetBoundCardIDs(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIotCardStore_BatchUpdateSeriesAllocation(t *testing.T) {
|
||||
func TestIotCardStore_BatchUpdateSeriesID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
@@ -440,39 +440,39 @@ func TestIotCardStore_BatchUpdateSeriesAllocation(t *testing.T) {
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
t.Run("设置系列分配ID", func(t *testing.T) {
|
||||
seriesAllocationID := uint(100)
|
||||
t.Run("设置系列ID", func(t *testing.T) {
|
||||
seriesID := uint(100)
|
||||
cardIDs := []uint{cards[0].ID, cards[1].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, cardIDs, &seriesAllocationID)
|
||||
err := s.BatchUpdateSeriesID(ctx, cardIDs, &seriesID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var updatedCards []*model.IotCard
|
||||
require.NoError(t, tx.Where("id IN ?", cardIDs).Find(&updatedCards).Error)
|
||||
for _, card := range updatedCards {
|
||||
require.NotNil(t, card.SeriesAllocationID)
|
||||
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
|
||||
require.NotNil(t, card.SeriesID)
|
||||
assert.Equal(t, seriesID, *card.SeriesID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("清除系列分配ID", func(t *testing.T) {
|
||||
t.Run("清除系列ID", func(t *testing.T) {
|
||||
cardIDs := []uint{cards[0].ID}
|
||||
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, cardIDs, nil)
|
||||
err := s.BatchUpdateSeriesID(ctx, cardIDs, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
var updatedCard model.IotCard
|
||||
require.NoError(t, tx.First(&updatedCard, cards[0].ID).Error)
|
||||
assert.Nil(t, updatedCard.SeriesAllocationID)
|
||||
assert.Nil(t, updatedCard.SeriesID)
|
||||
})
|
||||
|
||||
t.Run("空列表不报错", func(t *testing.T) {
|
||||
err := s.BatchUpdateSeriesAllocation(ctx, []uint{}, nil)
|
||||
err := s.BatchUpdateSeriesID(ctx, []uint{}, nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIotCardStore_ListBySeriesAllocationID(t *testing.T) {
|
||||
func TestIotCardStore_ListBySeriesID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
@@ -480,23 +480,23 @@ func TestIotCardStore_ListBySeriesAllocationID(t *testing.T) {
|
||||
s := NewIotCardStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
seriesAllocationID := uint(200)
|
||||
seriesID := uint(200)
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: "89860012345678911001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{ICCID: "89860012345678911002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{ICCID: "89860012345678911003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: nil},
|
||||
{ICCID: "89860012345678911001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: "89860012345678911002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: "89860012345678911003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: nil},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
result, err := s.ListBySeriesAllocationID(ctx, seriesAllocationID)
|
||||
result, err := s.ListBySeriesID(ctx, seriesID)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, result, 2)
|
||||
for _, card := range result {
|
||||
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
|
||||
assert.Equal(t, seriesID, *card.SeriesID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIotCardStore_ListStandalone_SeriesAllocationFilter(t *testing.T) {
|
||||
func TestIotCardStore_ListStandalone_SeriesIDFilter(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
rdb := testutils.GetTestRedis(t)
|
||||
testutils.CleanTestRedisKeys(t, rdb)
|
||||
@@ -505,23 +505,23 @@ func TestIotCardStore_ListStandalone_SeriesAllocationFilter(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
prefix := uniqueICCIDPrefix()
|
||||
seriesAllocationID := uint(300)
|
||||
seriesID := uint(300)
|
||||
cards := []*model.IotCard{
|
||||
{ICCID: prefix + "S001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{ICCID: prefix + "S002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: &seriesAllocationID},
|
||||
{ICCID: prefix + "S003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesAllocationID: nil},
|
||||
{ICCID: prefix + "S001", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: prefix + "S002", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: &seriesID},
|
||||
{ICCID: prefix + "S003", CardType: "data_card", CarrierID: 1, Status: 1, SeriesID: nil},
|
||||
}
|
||||
require.NoError(t, s.CreateBatch(ctx, cards))
|
||||
|
||||
filters := map[string]interface{}{
|
||||
"series_allocation_id": seriesAllocationID,
|
||||
"iccid": prefix,
|
||||
"series_id": seriesID,
|
||||
"iccid": prefix,
|
||||
}
|
||||
result, total, err := s.ListStandalone(ctx, nil, filters)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(2), total)
|
||||
assert.Len(t, result, 2)
|
||||
for _, card := range result {
|
||||
assert.Equal(t, seriesAllocationID, *card.SeriesAllocationID)
|
||||
assert.Equal(t, seriesID, *card.SeriesID)
|
||||
}
|
||||
}
|
||||
|
||||
114
internal/store/postgres/shop_series_allocation_store_test.go
Normal file
114
internal/store/postgres/shop_series_allocation_store_test.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/tests/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestShopSeriesAllocationStore_GetByShopAndSeries(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
s := NewShopSeriesAllocationStore(tx)
|
||||
|
||||
// 创建测试数据
|
||||
allocation := &model.ShopSeriesAllocation{
|
||||
ShopID: 1,
|
||||
SeriesID: 100,
|
||||
AllocatorShopID: 0,
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, s.Create(ctx, allocation))
|
||||
|
||||
t.Run("查询存在的分配", func(t *testing.T) {
|
||||
result, err := s.GetByShopAndSeries(ctx, 1, 100)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, uint(1), result.ShopID)
|
||||
assert.Equal(t, uint(100), result.SeriesID)
|
||||
})
|
||||
|
||||
t.Run("查询不存在的分配", func(t *testing.T) {
|
||||
result, err := s.GetByShopAndSeries(ctx, 999, 999)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, gorm.ErrRecordNotFound, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShopSeriesAllocationStore_Create(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
s := NewShopSeriesAllocationStore(tx)
|
||||
|
||||
allocation := &model.ShopSeriesAllocation{
|
||||
ShopID: 1,
|
||||
SeriesID: 100,
|
||||
AllocatorShopID: 0,
|
||||
Status: 1,
|
||||
}
|
||||
|
||||
err := s.Create(ctx, allocation)
|
||||
require.NoError(t, err)
|
||||
assert.NotZero(t, allocation.ID)
|
||||
}
|
||||
|
||||
func TestShopSeriesAllocationStore_GetByID(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
s := NewShopSeriesAllocationStore(tx)
|
||||
|
||||
allocation := &model.ShopSeriesAllocation{
|
||||
ShopID: 1,
|
||||
SeriesID: 100,
|
||||
AllocatorShopID: 0,
|
||||
Status: 1,
|
||||
}
|
||||
require.NoError(t, s.Create(ctx, allocation))
|
||||
|
||||
result, err := s.GetByID(ctx, allocation.ID)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, allocation.ID, result.ID)
|
||||
}
|
||||
|
||||
func TestShopSeriesAllocationStore_List(t *testing.T) {
|
||||
tx := testutils.NewTestTransaction(t)
|
||||
ctx := context.Background()
|
||||
|
||||
s := NewShopSeriesAllocationStore(tx)
|
||||
|
||||
// 创建测试数据
|
||||
allocations := []*model.ShopSeriesAllocation{
|
||||
{ShopID: 1, SeriesID: 100, AllocatorShopID: 0, Status: 1},
|
||||
{ShopID: 1, SeriesID: 101, AllocatorShopID: 0, Status: 1},
|
||||
{ShopID: 2, SeriesID: 100, AllocatorShopID: 0, Status: 1},
|
||||
}
|
||||
for _, a := range allocations {
|
||||
require.NoError(t, s.Create(ctx, a))
|
||||
}
|
||||
|
||||
t.Run("按店铺ID过滤", func(t *testing.T) {
|
||||
filters := map[string]interface{}{"shop_id": uint(1)}
|
||||
result, total, err := s.List(ctx, nil, filters)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(2), total)
|
||||
assert.Len(t, result, 2)
|
||||
})
|
||||
|
||||
t.Run("按系列ID过滤", func(t *testing.T) {
|
||||
filters := map[string]interface{}{"series_id": uint(100)}
|
||||
result, total, err := s.List(ctx, nil, filters)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(2), total)
|
||||
assert.Len(t, result, 2)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user