dcfd24b624
v1.0.3 定位为 bug fix release,收口 v1.0.2 引入的破坏性清理并修复 4 个轻量 bug + 依赖复查 + 版本号治理 + 文档对齐。同时包含此前未提交 的 v1.0.2 全部工作。 v1.0.3 变更: Fixed: - generateJTI 忽略 rand.Read 错误(jwt)— 改为 (string, error) 并传播 - QueryBuilder.Page Count 受残留 Limit 截断(repository)— countDB 加 Limit(-1).Offset(-1) - OSS/本地存储文件名冲突(storage)— 新增 uniqueFilename 加 8 字节随机后缀 - 数据库重试策略对不可恢复错误无效(database)— 新增 isTransientDBError Dependencies: - go mod tidy 补全 postgres 方言传递依赖;安全补丁升级 x/crypto v0.49→v0.53、golang-jwt/jwt/v5 v5.2.1→v5.3.1、gorilla/websocket v1.5.1→v1.5.3 Removed: - Breaking: 清理 v1.0.2 兼容别名(InitMySQL* / driverName) - 死代码 database.DBResolver - 代码中 292 行"评分/理由"自夸注释(#26,23 个文件) Changed: - Breaking: 错误码体系重构 CodeSuccess 1→0、CodeFail 0→1,删除 CodeInvalidParams,加编译期防撞码 - database/mysql.go → manager.go;Logger 拆分三独立 core 修复 Tee 重复写入 - 版本号常量化:app.go 新增 const Version 作为唯一来源,CLI/脚手架模板引用之,不再散落字面量 Security: - CORS 中间件按 W3C 规范修复 Allow-Credentials / Vary: Origin / 非白名单不回显 Added: - console 包显式 level 控制(SetLevel/WithLevel/LevelSilent,atomic.Int32 并发安全) - 新增测试 jwt/repository/storage/database/router/middleware/app - 文档:新增 CHANGELOG.md、Version_v1.0.2_report.md;更新 README/GUIDE 顶部更新日志 - 文档对齐:删除对不存在的 config.DefaultManager() getter 的描述(保持 API 与文档一致) 详见 CHANGELOG.md 与 README.md 更新日志。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
170 lines
4.1 KiB
Go
170 lines
4.1 KiB
Go
package storage_test
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/EthanCodeCraft/xlgo-core/config"
|
|
"github.com/EthanCodeCraft/xlgo-core/logger"
|
|
"github.com/EthanCodeCraft/xlgo-core/storage"
|
|
)
|
|
|
|
func init() {
|
|
// 初始化日志(测试需要)
|
|
cfg := &config.Config{
|
|
Log: config.LogConfig{
|
|
Dir: os.TempDir(),
|
|
MaxSize: 10,
|
|
MaxBackups: 1,
|
|
MaxAge: 1,
|
|
Compress: false,
|
|
},
|
|
}
|
|
logger.Init(cfg)
|
|
}
|
|
|
|
func TestStorageNotInitialized(t *testing.T) {
|
|
storage.SetStorage(nil)
|
|
|
|
if _, err := storage.UploadFromBytes([]byte("test"), "test.txt", "docs"); !errors.Is(err, storage.ErrStorageNotInitialized) {
|
|
t.Fatalf("expected ErrStorageNotInitialized, got %v", err)
|
|
}
|
|
if err := storage.Delete("missing"); !errors.Is(err, storage.ErrStorageNotInitialized) {
|
|
t.Fatalf("expected ErrStorageNotInitialized, got %v", err)
|
|
}
|
|
if _, err := storage.Get("missing"); !errors.Is(err, storage.ErrStorageNotInitialized) {
|
|
t.Fatalf("expected ErrStorageNotInitialized, got %v", err)
|
|
}
|
|
if url := storage.GetURL("missing"); url != "" {
|
|
t.Fatalf("expected empty URL, got %q", url)
|
|
}
|
|
if storage.Exists("missing") {
|
|
t.Fatal("expected Exists false without storage")
|
|
}
|
|
}
|
|
|
|
func TestLocalStorage(t *testing.T) {
|
|
cfg := &config.LocalStorageConfig{
|
|
Path: "/tmp/uploads",
|
|
BaseURL: "http://localhost/uploads",
|
|
}
|
|
|
|
local := storage.NewLocalStorage(cfg)
|
|
if local == nil {
|
|
t.Error("NewLocalStorage should not return nil")
|
|
}
|
|
}
|
|
|
|
func TestStorageInterface(t *testing.T) {
|
|
cfg := &config.LocalStorageConfig{
|
|
Path: "/tmp/uploads",
|
|
BaseURL: "http://localhost/uploads",
|
|
}
|
|
|
|
var s storage.Storage = storage.NewLocalStorage(cfg)
|
|
if s == nil {
|
|
t.Error("LocalStorage should implement Storage interface")
|
|
}
|
|
}
|
|
|
|
func TestLocalStorageGetURL(t *testing.T) {
|
|
cfg := &config.LocalStorageConfig{
|
|
Path: "/uploads",
|
|
BaseURL: "http://example.com",
|
|
}
|
|
|
|
local := storage.NewLocalStorage(cfg)
|
|
url := local.GetURL("images/test.jpg")
|
|
|
|
expected := "http://example.com/images/test.jpg"
|
|
if url != expected {
|
|
t.Errorf("GetURL = %s, want %s", url, expected)
|
|
}
|
|
}
|
|
|
|
func TestLocalStorageDeleteGetExists(t *testing.T) {
|
|
// 创建临时目录
|
|
tmpDir := filepath.Join(os.TempDir(), "xlgo_storage_test")
|
|
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
cfg := &config.LocalStorageConfig{
|
|
Path: tmpDir,
|
|
BaseURL: "http://localhost/uploads",
|
|
}
|
|
|
|
local := storage.NewLocalStorage(cfg)
|
|
|
|
// 创建测试文件
|
|
testFile := filepath.Join(tmpDir, "test.txt")
|
|
testData := []byte("hello world")
|
|
if err := os.WriteFile(testFile, testData, 0644); err != nil {
|
|
t.Fatalf("Failed to write test file: %v", err)
|
|
}
|
|
|
|
// 测试 Exists
|
|
if !local.Exists("test.txt") {
|
|
t.Error("Exists should return true for existing file")
|
|
}
|
|
|
|
// 测试 Get
|
|
data, err := local.Get("test.txt")
|
|
if err != nil {
|
|
t.Errorf("Get failed: %v", err)
|
|
}
|
|
if string(data) != "hello world" {
|
|
t.Errorf("Get data = %s, want 'hello world'", string(data))
|
|
}
|
|
|
|
// 测试 Delete
|
|
err = local.Delete("test.txt")
|
|
if err != nil {
|
|
t.Errorf("Delete failed: %v", err)
|
|
}
|
|
|
|
// 验证删除后不存在
|
|
if local.Exists("test.txt") {
|
|
t.Error("Exists should return false after delete")
|
|
}
|
|
|
|
// 删除不存在的文件应该失败
|
|
err = local.Delete("nonexistent.txt")
|
|
if err == nil {
|
|
t.Error("Delete should fail for nonexistent file")
|
|
}
|
|
|
|
// Get 不存在的文件应该失败
|
|
_, err = local.Get("nonexistent.txt")
|
|
if err == nil {
|
|
t.Error("Get should fail for nonexistent file")
|
|
}
|
|
}
|
|
|
|
func TestStorageInitInvalidDriver(t *testing.T) {
|
|
cfg := &config.StorageConfig{
|
|
Driver: "invalid",
|
|
}
|
|
|
|
err := storage.Init(cfg)
|
|
if err == nil {
|
|
t.Error("Init should fail with invalid driver")
|
|
}
|
|
}
|
|
|
|
func TestOSSStorageConfig(t *testing.T) {
|
|
cfg := &config.OSSStorageConfig{
|
|
Endpoint: "oss-cn-hangzhou.aliyuncs.com",
|
|
Bucket: "test-bucket",
|
|
AccessKeyID: "test-id",
|
|
AccessKeySecret: "test-secret",
|
|
BaseURL: "https://test.oss-cn-hangzhou.aliyuncs.com",
|
|
}
|
|
|
|
if cfg.Endpoint != "oss-cn-hangzhou.aliyuncs.com" {
|
|
t.Error("OSSStorageConfig Endpoint failed")
|
|
}
|
|
} |