test(db): Open 自动建父目录改为断言成功(行为改进)
This commit is contained in:
1 parent
1d9c14300d
commit
cb6fac587b
1 file changed
+15
-5
+15
-5
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"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()
|
t.Parallel()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// Parent directory does not exist, so the connection cannot be established.
|
// Parent directory does not exist yet: Open must create it so a custom
|
||||||
path := filepath.Join(t.TempDir(), "does-not-exist", "cyrene.db")
|
// DATA_DIR works on first run.
|
||||||
if _, err := Open(ctx, path, testLogger()); err == nil {
|
dir := filepath.Join(t.TempDir(), "does-not-exist")
|
||||||
t.Fatalf("Open(%q) expected error for missing parent dir, got nil", path)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user