test(db): Open 自动建父目录改为断言成功(行为改进)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

This commit is contained in:
Aska committed 2026-09-06 10:40:06 +08:00
1 parent 1d9c14300d
commit cb6fac587b
1 file changed
+15 -5
+15 -5
View File
@@ -5,6 +5,7 @@ import (
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"testing"
)
@@ -124,13 +125,22 @@ func TestOpen_BlankPath_异常(t *testing.T) {
}
}
func TestOpen_MissingParentDir_异常(t *testing.T) {
func TestOpen_CreatesParentDir_正常(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Parent directory does not exist, so the connection cannot be established.
path := filepath.Join(t.TempDir(), "does-not-exist", "cyrene.db")
if _, err := Open(ctx, path, testLogger()); err == nil {
t.Fatalf("Open(%q) expected error for missing parent dir, got nil", path)
// Parent directory does not exist yet: Open must create it so a custom
// DATA_DIR works on first run.
dir := filepath.Join(t.TempDir(), "does-not-exist")
path := filepath.Join(dir, "cyrene.db")
db, err := Open(ctx, path, testLogger())
if err != nil {
t.Fatalf("Open(%q) error creating parent dir: %v", path, err)
}
defer db.Close()
if fi, err := os.Stat(dir); err != nil {
t.Fatalf("parent dir not created: %v", err)
} else if !fi.IsDir() {
t.Fatalf("parent path is not a dir: %v", fi)
}
}