chore: 初始化仓库,排除二进制文件和覆盖率文件
This commit is contained in:
174
internal/service/captcha_service_test.go
Normal file
174
internal/service/captcha_service_test.go
Normal file
@@ -0,0 +1,174 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestCaptchaService_Constants 测试验证码服务常量
|
||||
func TestCaptchaService_Constants(t *testing.T) {
|
||||
if redisKeyPrefix != "captcha:" {
|
||||
t.Errorf("redisKeyPrefix = %s, want 'captcha:'", redisKeyPrefix)
|
||||
}
|
||||
|
||||
if paddingValue != 3 {
|
||||
t.Errorf("paddingValue = %d, want 3", paddingValue)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRedisData_Structure 测试RedisData结构
|
||||
func TestRedisData_Structure(t *testing.T) {
|
||||
data := RedisData{
|
||||
Tx: 100,
|
||||
Ty: 200,
|
||||
}
|
||||
|
||||
if data.Tx != 100 {
|
||||
t.Errorf("RedisData.Tx = %d, want 100", data.Tx)
|
||||
}
|
||||
|
||||
if data.Ty != 200 {
|
||||
t.Errorf("RedisData.Ty = %d, want 200", data.Ty)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGenerateCaptchaData_Logic 测试生成验证码的逻辑部分
|
||||
func TestGenerateCaptchaData_Logic(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
captchaID string
|
||||
wantErr bool
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "有效的captchaID",
|
||||
captchaID: "test-uuid-123",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "空的captchaID应该失败",
|
||||
captchaID: "",
|
||||
wantErr: true,
|
||||
errContains: "生成验证码唯一标识失败",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 测试UUID验证逻辑
|
||||
if tt.captchaID == "" {
|
||||
if !tt.wantErr {
|
||||
t.Error("空captchaID应该返回错误")
|
||||
}
|
||||
} else {
|
||||
if tt.wantErr {
|
||||
t.Error("非空captchaID不应该返回错误")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestVerifyCaptchaData_Logic 测试验证验证码的逻辑部分
|
||||
func TestVerifyCaptchaData_Logic(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dx int
|
||||
tx int
|
||||
ty int
|
||||
padding int
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "精确匹配",
|
||||
dx: 100,
|
||||
tx: 100,
|
||||
ty: 200,
|
||||
padding: 3,
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "在误差范围内(+3)",
|
||||
dx: 103,
|
||||
tx: 100,
|
||||
ty: 200,
|
||||
padding: 3,
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "在误差范围内(-3)",
|
||||
dx: 97,
|
||||
tx: 100,
|
||||
ty: 200,
|
||||
padding: 3,
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "超出误差范围(+4)",
|
||||
dx: 104,
|
||||
tx: 100,
|
||||
ty: 200,
|
||||
padding: 3,
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "超出误差范围(-4)",
|
||||
dx: 96,
|
||||
tx: 100,
|
||||
ty: 200,
|
||||
padding: 3,
|
||||
wantValid: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 验证逻辑:dx应该在[tx-padding, tx+padding]范围内
|
||||
diff := tt.dx - tt.tx
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
isValid := diff <= tt.padding
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Validation failed: got %v, want %v (dx=%d, tx=%d, padding=%d)", isValid, tt.wantValid, tt.dx, tt.tx, tt.padding)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestVerifyCaptchaData_RedisKey 测试Redis键生成逻辑
|
||||
func TestVerifyCaptchaData_RedisKey(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
id string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "生成正确的Redis键",
|
||||
id: "test-id-123",
|
||||
expected: "captcha:test-id-123",
|
||||
},
|
||||
{
|
||||
name: "空ID",
|
||||
id: "",
|
||||
expected: "captcha:",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
redisKey := redisKeyPrefix + tt.id
|
||||
if redisKey != tt.expected {
|
||||
t.Errorf("Redis key = %s, want %s", redisKey, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGenerateCaptchaData_ExpireTime 测试过期时间
|
||||
func TestGenerateCaptchaData_ExpireTime(t *testing.T) {
|
||||
expectedExpireTime := 300 * time.Second
|
||||
if expectedExpireTime != 5*time.Minute {
|
||||
t.Errorf("Expire time should be 5 minutes")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user