172 lines
4.4 KiB
Markdown
172 lines
4.4 KiB
Markdown
# Asset Service Repository 测试文档
|
||
|
||
## 测试文件说明
|
||
|
||
本目录包含两个测试文件:
|
||
|
||
1. **asset_repository_test.go** - 资产 Repository 测试
|
||
2. **mint_order_repository_test.go** - 铸造订单 Repository 测试
|
||
|
||
## 运行测试前的准备
|
||
|
||
### 1. 安装依赖
|
||
|
||
由于网络问题,需要手动添加 testify 依赖到 `go.mod`:
|
||
|
||
```bash
|
||
cd services/assetService
|
||
```
|
||
|
||
在 `go.mod` 文件中添加:
|
||
```
|
||
require (
|
||
github.com/stretchr/testify v1.8.4
|
||
)
|
||
```
|
||
|
||
然后运行:
|
||
```bash
|
||
go mod tidy
|
||
```
|
||
|
||
### 2. 数据库准备
|
||
|
||
确保 PostgreSQL 数据库已启动,并且配置正确:
|
||
|
||
- Host: localhost
|
||
- Port: 5432
|
||
- User: haihuizhu
|
||
- Password: admin
|
||
- DBName: top-fans
|
||
|
||
### 3. 数据库迁移
|
||
|
||
测试会自动迁移所需的表(assets, mint_orders, asset_likes),但需要确保基础表(users, stars, fan_profiles)已存在。
|
||
|
||
## 运行测试
|
||
|
||
### 运行所有测试
|
||
|
||
```bash
|
||
cd services/assetService
|
||
go test -v ./repository/...
|
||
```
|
||
|
||
### 运行特定测试
|
||
|
||
```bash
|
||
# 只测试 AssetRepository
|
||
go test -v ./repository/... -run TestAssetRepository
|
||
|
||
# 只测试 MintOrderRepository
|
||
go test -v ./repository/... -run TestMintOrderRepository
|
||
|
||
# 运行特定的测试用例
|
||
go test -v ./repository/... -run TestAssetRepository_Create
|
||
```
|
||
|
||
## 测试覆盖的功能
|
||
|
||
### AssetRepository 测试
|
||
|
||
1. ✅ **Create** - 创建资产
|
||
2. ✅ **GetByID** - 根据ID查询资产
|
||
3. ✅ **GetByIDAndOwner** - 根据ID和所有者查询(权限验证)
|
||
4. ✅ **GetByOwner** - 查询用户的资产列表(分页)
|
||
5. ✅ **CountByOwner** - 统计用户的资产数量
|
||
6. ✅ **UpdateStatus** - 更新资产状态
|
||
7. ✅ **UpdateBlockchainInfo** - 更新上链信息(tx_hash, block_number, minted_at)
|
||
8. ✅ **IncrementLikeCount** - 原子增加点赞数
|
||
9. ✅ **DecrementLikeCount** - 原子减少点赞数
|
||
|
||
### MintOrderRepository 测试
|
||
|
||
1. ✅ **Create** - 创建铸造订单
|
||
2. ✅ **GetByOrderID** - 根据订单ID查询
|
||
3. ✅ **GetByOrderIDAndUser** - 根据订单ID和用户ID查询(权限验证)
|
||
4. ✅ **UpdateStatus** - 更新订单状态
|
||
5. ✅ **UpdateAssetID** - 更新关联的资产ID
|
||
6. ✅ **UpdateError** - 更新错误信息
|
||
7. ✅ **UpdateMintedAt** - 更新上链成功时间
|
||
8. ✅ **IncrementRetryCount** - 增加重试次数
|
||
9. ✅ **CompleteFlow** - 完整的铸造流程测试
|
||
|
||
## 测试数据说明
|
||
|
||
### 测试数据前缀
|
||
|
||
- 用户手机号:`199%` (如 19900000001, 19900100001)
|
||
- 明星 IdentityID:`test_asset_%` (如 test_asset_create, test_mint_create)
|
||
|
||
### 数据清理
|
||
|
||
每个测试结束后会自动清理测试数据,清理顺序:
|
||
1. asset_likes(点赞记录)
|
||
2. assets(资产)
|
||
3. mint_orders(铸造订单)
|
||
4. fan_profiles(粉丝档案)
|
||
5. users(用户)
|
||
6. stars(明星)
|
||
|
||
## 注意事项
|
||
|
||
### 1. 网络问题
|
||
|
||
如果遇到网络证书错误(x509: OSStatus -26276),可以:
|
||
|
||
```bash
|
||
# 设置 Go 代理
|
||
export GOPROXY=https://goproxy.io,direct
|
||
# 或
|
||
export GOPROXY=https://goproxy.cn,direct
|
||
```
|
||
|
||
### 2. 数据库连接
|
||
|
||
如果测试跳过(Skipping test),说明数据库连接失败,请检查:
|
||
- PostgreSQL 是否启动
|
||
- 数据库配置是否正确
|
||
- 用户权限是否足够
|
||
|
||
### 3. 测试隔离
|
||
|
||
每个测试使用不同的测试数据(不同的手机号和 IdentityID),确保测试之间不会互相影响。
|
||
|
||
### 4. 时间相关测试
|
||
|
||
某些测试涉及时间排序,使用 `time.Sleep(10 * time.Millisecond)` 确保创建时间不同。
|
||
|
||
## 未实现的测试方法
|
||
|
||
以下方法在当前 Repository 接口中不存在,如需要请先在接口中添加:
|
||
|
||
- `GetByUserAndStar` - 获取用户的订单列表
|
||
- `CountByUserAndStar` - 统计用户的订单数量
|
||
- `CountTodayByUserAndStar` - 统计用户今天的订单数量
|
||
- `GetPendingOrders` - 获取待处理订单
|
||
|
||
## 测试结果示例
|
||
|
||
成功运行测试后,应该看到类似输出:
|
||
|
||
```
|
||
=== RUN TestAssetRepository_Create
|
||
--- PASS: TestAssetRepository_Create (0.05s)
|
||
=== RUN TestAssetRepository_GetByID
|
||
--- PASS: TestAssetRepository_GetByID (0.03s)
|
||
...
|
||
PASS
|
||
ok github.com/topfans/backend/services/assetService/repository 2.456s
|
||
```
|
||
|
||
## 下一步
|
||
|
||
1. 添加 Service 层的单元测试
|
||
2. 添加集成测试(测试服务间RPC调用)
|
||
3. 添加 HTTP 接口测试
|
||
|
||
## 参考
|
||
|
||
- Social Service Repository 测试:`services/socialService/repository/social_repository_test.go`
|
||
- User Service Repository 测试:`services/userService/repository/*_test.go`
|