chore: 初始化仓库,排除二进制文件和覆盖率文件
This commit is contained in:
471
internal/service/texture_service_test.go
Normal file
471
internal/service/texture_service_test.go
Normal file
@@ -0,0 +1,471 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestTextureService_TypeValidation 测试材质类型验证
|
||||
func TestTextureService_TypeValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
textureType string
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "SKIN类型有效",
|
||||
textureType: "SKIN",
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "CAPE类型有效",
|
||||
textureType: "CAPE",
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "无效类型",
|
||||
textureType: "INVALID",
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "空类型无效",
|
||||
textureType: "",
|
||||
wantValid: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isValid := tt.textureType == "SKIN" || tt.textureType == "CAPE"
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Texture type validation failed: got %v, want %v", isValid, tt.wantValid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTextureService_DefaultValues 测试材质默认值
|
||||
func TestTextureService_DefaultValues(t *testing.T) {
|
||||
// 测试默认状态
|
||||
defaultStatus := 1
|
||||
if defaultStatus != 1 {
|
||||
t.Errorf("默认状态应为1,实际为%d", defaultStatus)
|
||||
}
|
||||
|
||||
// 测试默认下载数
|
||||
defaultDownloadCount := 0
|
||||
if defaultDownloadCount != 0 {
|
||||
t.Errorf("默认下载数应为0,实际为%d", defaultDownloadCount)
|
||||
}
|
||||
|
||||
// 测试默认收藏数
|
||||
defaultFavoriteCount := 0
|
||||
if defaultFavoriteCount != 0 {
|
||||
t.Errorf("默认收藏数应为0,实际为%d", defaultFavoriteCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTextureService_StatusValidation 测试材质状态验证
|
||||
func TestTextureService_StatusValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
status int16
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "状态为1(正常)时有效",
|
||||
status: 1,
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "状态为-1(删除)时无效",
|
||||
status: -1,
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "状态为0时可能有效(取决于业务逻辑)",
|
||||
status: 0,
|
||||
wantValid: true, // 状态为0(禁用)时,材质仍然存在,只是不可用,但查询时不会返回错误
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 材质状态为-1时表示已删除,无效
|
||||
isValid := tt.status != -1
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Status validation failed: got %v, want %v", isValid, tt.wantValid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetUserTextures_Pagination 测试分页逻辑
|
||||
func TestGetUserTextures_Pagination(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
page int
|
||||
pageSize int
|
||||
wantPage int
|
||||
wantSize int
|
||||
}{
|
||||
{
|
||||
name: "有效的分页参数",
|
||||
page: 2,
|
||||
pageSize: 20,
|
||||
wantPage: 2,
|
||||
wantSize: 20,
|
||||
},
|
||||
{
|
||||
name: "page小于1,应该设为1",
|
||||
page: 0,
|
||||
pageSize: 20,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
{
|
||||
name: "pageSize小于1,应该设为20",
|
||||
page: 1,
|
||||
pageSize: 0,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
{
|
||||
name: "pageSize超过100,应该设为20",
|
||||
page: 1,
|
||||
pageSize: 200,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
page := tt.page
|
||||
pageSize := tt.pageSize
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
|
||||
if page != tt.wantPage {
|
||||
t.Errorf("Page = %d, want %d", page, tt.wantPage)
|
||||
}
|
||||
if pageSize != tt.wantSize {
|
||||
t.Errorf("PageSize = %d, want %d", pageSize, tt.wantSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSearchTextures_Pagination 测试搜索分页逻辑
|
||||
func TestSearchTextures_Pagination(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
page int
|
||||
pageSize int
|
||||
wantPage int
|
||||
wantSize int
|
||||
}{
|
||||
{
|
||||
name: "有效的分页参数",
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
wantPage: 1,
|
||||
wantSize: 10,
|
||||
},
|
||||
{
|
||||
name: "page小于1,应该设为1",
|
||||
page: -1,
|
||||
pageSize: 20,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
{
|
||||
name: "pageSize超过100,应该设为20",
|
||||
page: 1,
|
||||
pageSize: 150,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
page := tt.page
|
||||
pageSize := tt.pageSize
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
|
||||
if page != tt.wantPage {
|
||||
t.Errorf("Page = %d, want %d", page, tt.wantPage)
|
||||
}
|
||||
if pageSize != tt.wantSize {
|
||||
t.Errorf("PageSize = %d, want %d", pageSize, tt.wantSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateTexture_PermissionCheck 测试更新材质的权限检查
|
||||
func TestUpdateTexture_PermissionCheck(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
uploaderID int64
|
||||
requestID int64
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "上传者ID匹配,允许更新",
|
||||
uploaderID: 1,
|
||||
requestID: 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "上传者ID不匹配,拒绝更新",
|
||||
uploaderID: 1,
|
||||
requestID: 2,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
hasError := tt.uploaderID != tt.requestID
|
||||
if hasError != tt.wantErr {
|
||||
t.Errorf("Permission check failed: got %v, want %v", hasError, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateTexture_FieldUpdates 测试更新字段逻辑
|
||||
func TestUpdateTexture_FieldUpdates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
nameValue string
|
||||
descValue string
|
||||
isPublic *bool
|
||||
wantUpdates int
|
||||
}{
|
||||
{
|
||||
name: "更新所有字段",
|
||||
nameValue: "NewName",
|
||||
descValue: "NewDesc",
|
||||
isPublic: boolPtr(true),
|
||||
wantUpdates: 3,
|
||||
},
|
||||
{
|
||||
name: "只更新名称",
|
||||
nameValue: "NewName",
|
||||
descValue: "",
|
||||
isPublic: nil,
|
||||
wantUpdates: 1,
|
||||
},
|
||||
{
|
||||
name: "只更新描述",
|
||||
nameValue: "",
|
||||
descValue: "NewDesc",
|
||||
isPublic: nil,
|
||||
wantUpdates: 1,
|
||||
},
|
||||
{
|
||||
name: "只更新公开状态",
|
||||
nameValue: "",
|
||||
descValue: "",
|
||||
isPublic: boolPtr(false),
|
||||
wantUpdates: 1,
|
||||
},
|
||||
{
|
||||
name: "没有更新",
|
||||
nameValue: "",
|
||||
descValue: "",
|
||||
isPublic: nil,
|
||||
wantUpdates: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
updates := 0
|
||||
if tt.nameValue != "" {
|
||||
updates++
|
||||
}
|
||||
if tt.descValue != "" {
|
||||
updates++
|
||||
}
|
||||
if tt.isPublic != nil {
|
||||
updates++
|
||||
}
|
||||
|
||||
if updates != tt.wantUpdates {
|
||||
t.Errorf("Updates count = %d, want %d", updates, tt.wantUpdates)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteTexture_PermissionCheck 测试删除材质的权限检查
|
||||
func TestDeleteTexture_PermissionCheck(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
uploaderID int64
|
||||
requestID int64
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "上传者ID匹配,允许删除",
|
||||
uploaderID: 1,
|
||||
requestID: 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "上传者ID不匹配,拒绝删除",
|
||||
uploaderID: 1,
|
||||
requestID: 2,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
hasError := tt.uploaderID != tt.requestID
|
||||
if hasError != tt.wantErr {
|
||||
t.Errorf("Permission check failed: got %v, want %v", hasError, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestToggleTextureFavorite_Logic 测试切换收藏状态的逻辑
|
||||
func TestToggleTextureFavorite_Logic(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
isFavorited bool
|
||||
wantResult bool
|
||||
}{
|
||||
{
|
||||
name: "已收藏,取消收藏",
|
||||
isFavorited: true,
|
||||
wantResult: false,
|
||||
},
|
||||
{
|
||||
name: "未收藏,添加收藏",
|
||||
isFavorited: false,
|
||||
wantResult: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := !tt.isFavorited
|
||||
if result != tt.wantResult {
|
||||
t.Errorf("Toggle favorite failed: got %v, want %v", result, tt.wantResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetUserTextureFavorites_Pagination 测试收藏列表分页
|
||||
func TestGetUserTextureFavorites_Pagination(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
page int
|
||||
pageSize int
|
||||
wantPage int
|
||||
wantSize int
|
||||
}{
|
||||
{
|
||||
name: "有效的分页参数",
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
{
|
||||
name: "page小于1,应该设为1",
|
||||
page: 0,
|
||||
pageSize: 20,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
{
|
||||
name: "pageSize超过100,应该设为20",
|
||||
page: 1,
|
||||
pageSize: 200,
|
||||
wantPage: 1,
|
||||
wantSize: 20,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
page := tt.page
|
||||
pageSize := tt.pageSize
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
|
||||
if page != tt.wantPage {
|
||||
t.Errorf("Page = %d, want %d", page, tt.wantPage)
|
||||
}
|
||||
if pageSize != tt.wantSize {
|
||||
t.Errorf("PageSize = %d, want %d", pageSize, tt.wantSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckTextureUploadLimit_Logic 测试上传限制检查逻辑
|
||||
func TestCheckTextureUploadLimit_Logic(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
count int64
|
||||
maxTextures int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "未达到上限",
|
||||
count: 5,
|
||||
maxTextures: 10,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "达到上限",
|
||||
count: 10,
|
||||
maxTextures: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "超过上限",
|
||||
count: 15,
|
||||
maxTextures: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
hasError := tt.count >= int64(tt.maxTextures)
|
||||
if hasError != tt.wantErr {
|
||||
t.Errorf("Limit check failed: got %v, want %v", hasError, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 辅助函数
|
||||
func boolPtr(b bool) *bool {
|
||||
return &b
|
||||
}
|
||||
Reference in New Issue
Block a user