feat: Enhance dependency injection and service integration
- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
This commit is contained in:
@@ -7,6 +7,9 @@ import (
|
||||
|
||||
// TestGenerateVerificationCode 测试生成验证码函数
|
||||
func TestGenerateVerificationCode(t *testing.T) {
|
||||
// 创建服务实例(使用 nil,因为这个测试不需要依赖)
|
||||
svc := &verificationService{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
wantLen int
|
||||
@@ -21,18 +24,18 @@ func TestGenerateVerificationCode(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
code, err := GenerateVerificationCode()
|
||||
code, err := svc.generateCode()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GenerateVerificationCode() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("generateCode() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && len(code) != tt.wantLen {
|
||||
t.Errorf("GenerateVerificationCode() code length = %v, want %v", len(code), tt.wantLen)
|
||||
t.Errorf("generateCode() code length = %v, want %v", len(code), tt.wantLen)
|
||||
}
|
||||
// 验证验证码只包含数字
|
||||
for _, c := range code {
|
||||
if c < '0' || c > '9' {
|
||||
t.Errorf("GenerateVerificationCode() code contains non-digit: %c", c)
|
||||
t.Errorf("generateCode() code contains non-digit: %c", c)
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -41,9 +44,9 @@ func TestGenerateVerificationCode(t *testing.T) {
|
||||
// 测试多次生成,验证码应该不同(概率上)
|
||||
codes := make(map[string]bool)
|
||||
for i := 0; i < 100; i++ {
|
||||
code, err := GenerateVerificationCode()
|
||||
code, err := svc.generateCode()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateVerificationCode() failed: %v", err)
|
||||
t.Fatalf("generateCode() failed: %v", err)
|
||||
}
|
||||
if codes[code] {
|
||||
t.Logf("发现重复验证码(这是正常的,因为只有6位数字): %s", code)
|
||||
@@ -82,9 +85,10 @@ func TestVerificationConstants(t *testing.T) {
|
||||
|
||||
// TestVerificationCodeFormat 测试验证码格式
|
||||
func TestVerificationCodeFormat(t *testing.T) {
|
||||
code, err := GenerateVerificationCode()
|
||||
svc := &verificationService{}
|
||||
code, err := svc.generateCode()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateVerificationCode() failed: %v", err)
|
||||
t.Fatalf("generateCode() failed: %v", err)
|
||||
}
|
||||
|
||||
// 验证长度
|
||||
|
||||
Reference in New Issue
Block a user