refactor: Implement dependency injection for handlers and services
- Refactored AuthHandler, UserHandler, TextureHandler, ProfileHandler, CaptchaHandler, and YggdrasilHandler to use dependency injection. - Removed direct instantiation of services and repositories within handlers, replacing them with constructor injection. - Updated the container to initialize service instances and provide them to handlers. - Enhanced code structure for better testability and adherence to Go best practices.
This commit is contained in:
50
internal/service/helpers_test.go
Normal file
50
internal/service/helpers_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestNormalizePagination_Basic 覆盖 NormalizePagination 的边界分支
|
||||
func TestNormalizePagination_Basic(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
page int
|
||||
size int
|
||||
wantPage int
|
||||
wantPageSize int
|
||||
}{
|
||||
{"page 小于 1", 0, 10, 1, 10},
|
||||
{"pageSize 小于 1", 1, 0, 1, 20},
|
||||
{"pageSize 大于 100", 2, 200, 2, 100},
|
||||
{"正常范围", 3, 30, 3, 30},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotPage, gotSize := NormalizePagination(tt.page, tt.size)
|
||||
if gotPage != tt.wantPage || gotSize != tt.wantPageSize {
|
||||
t.Fatalf("NormalizePagination(%d,%d) = (%d,%d), want (%d,%d)",
|
||||
tt.page, tt.size, gotPage, gotSize, tt.wantPage, tt.wantPageSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestWrapError 覆盖 WrapError 的 nil 与非 nil 分支
|
||||
func TestWrapError(t *testing.T) {
|
||||
if err := WrapError(nil, "msg"); err != nil {
|
||||
t.Fatalf("WrapError(nil, ...) 应返回 nil, got=%v", err)
|
||||
}
|
||||
|
||||
orig := errors.New("orig")
|
||||
wrapped := WrapError(orig, "context")
|
||||
if wrapped == nil {
|
||||
t.Fatalf("WrapError 应返回非 nil 错误")
|
||||
}
|
||||
if wrapped.Error() == orig.Error() {
|
||||
t.Fatalf("WrapError 应添加上下文信息, got=%v", wrapped)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user