test: 补充单元测试覆盖率 29.9% → 36.6%
测试构建 / 代码检查 (push) Has been cancelled
测试构建 / 单元测试和构建 (push) Has been cancelled
测试构建 / 构建验证 (push) Has been cancelled

新建 18 个测试文件,追加 30 个已有测试文件,覆盖协议解析、
错误分类、CEL 表达式求值、YAML 反序列化、字节编码等纯函数。
This commit is contained in:
ZacharyZcR
2026-06-15 19:11:15 +08:00
parent d7dbccab76
commit 353d525642
48 changed files with 7556 additions and 1 deletions
+49
View File
@@ -3,6 +3,7 @@
package services
import (
"errors"
"io"
"net"
"strings"
@@ -85,3 +86,51 @@ func TestMySQLConnStringRejectsUnsupportedUsernameDelimiters(t *testing.T) {
t.Fatal("mySQLConnString() error = nil, want unsupported delimiter error")
}
}
func TestMySQLConnStringRejectsAtSign(t *testing.T) {
info := &common.HostInfo{Host: "127.0.0.1", Port: 3306}
if _, err := mySQLConnString("user@host", "pass", info, time.Second); err == nil {
t.Fatal("mySQLConnString() error = nil, want unsupported delimiter error for @")
}
}
func TestMySQLConnStringRejectsSlash(t *testing.T) {
info := &common.HostInfo{Host: "127.0.0.1", Port: 3306}
if _, err := mySQLConnString("user/name", "pass", info, time.Second); err == nil {
t.Fatal("mySQLConnString() error = nil, want unsupported delimiter error for /")
}
}
func TestMySQLConnStringValidUser(t *testing.T) {
info := &common.HostInfo{Host: "127.0.0.1", Port: 3306}
dsn, err := mySQLConnString("root", "password", info, 3*time.Second)
if err != nil {
t.Fatalf("mySQLConnString() error = %v", err)
}
if dsn == "" {
t.Fatal("mySQLConnString() returned empty DSN")
}
}
func TestClassifyMySQLErrorType(t *testing.T) {
tests := []struct {
name string
err error
want ErrorType
}{
{"nil error", nil, ErrorTypeUnknown},
{"access denied for user", errors.New("access denied for user"), ErrorTypeAuth},
{"host is not allowed", errors.New("host is not allowed"), ErrorTypeAuth},
{"too many connections", errors.New("too many connections"), ErrorTypeNetwork},
{"can't connect to mysql server", errors.New("can't connect to mysql server"), ErrorTypeNetwork},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := classifyMySQLErrorType(tt.err)
if got != tt.want {
t.Errorf("classifyMySQLErrorType(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}