76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// =============================================================================
|
||
|
|
// Auth Runtime Helper Functions Tests
|
||
|
|
// =============================================================================
|
||
|
|
|
||
|
|
func TestIsUserNotFoundError(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
err error
|
||
|
|
expected bool
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "nil error",
|
||
|
|
err: nil,
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "gorm record not found",
|
||
|
|
err: gorm.ErrRecordNotFound,
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "wrapped gorm record not found",
|
||
|
|
err: errors.Join(gorm.ErrRecordNotFound, errors.New("additional context")),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "other error",
|
||
|
|
err: errors.New("some other error"),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "generic error",
|
||
|
|
err: errors.New("something went wrong"),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "error containing user not found",
|
||
|
|
err: errors.New("user not found"),
|
||
|
|
expected: true, // contains "user not found" in lowercase
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "error containing record not found",
|
||
|
|
err: errors.New("record not found"),
|
||
|
|
expected: true, // contains "record not found"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "error containing not found",
|
||
|
|
err: errors.New("entity not found"),
|
||
|
|
expected: true, // contains "not found"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "error containing 用户不存在",
|
||
|
|
err: errors.New("用户不存在"),
|
||
|
|
expected: true, // contains Chinese "用户不存在"
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := isUserNotFoundError(tt.err)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("isUserNotFoundError(%v) = %v, want %v", tt.err, result, tt.expected)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|