chore: 恢复旧布局基线(迁移改动丢失后重建,apps/packages 布局暂存)
This commit is contained in:
1 parent
a9510235bb
commit
456fa6954b
203 files changed
+18787
-48
No files matched your search
@@ -0,0 +1,94 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// newSQLiteFile 建一个 sqlite 测试库文件并执行建表/插数语句,返回 ConnReq。
|
||||
func newSQLiteFile(t *testing.T, stmts ...string) ConnReq {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "test.db")
|
||||
if err := os.WriteFile(path, []byte{}, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, s := range stmts {
|
||||
res := ExecSQL(context.Background(), ConnReq{Driver: "sqlite", Database: path, SQL: s})
|
||||
if res.Error != "" {
|
||||
t.Fatalf("初始化语句失败 %q: %s", s, res.Error)
|
||||
}
|
||||
}
|
||||
return ConnReq{Driver: "sqlite", Database: path}
|
||||
}
|
||||
|
||||
// R2-B5:sqlite 返回 sqlite_master 中的原始建表语句。
|
||||
func TestGetDDLSQLite(t *testing.T) {
|
||||
c := newSQLiteFile(t, `CREATE TABLE t_person (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER)`)
|
||||
c.Table = "t_person"
|
||||
ddl, err := GetDDL(context.Background(), c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, "CREATE TABLE t_person") || !strings.Contains(ddl, "name TEXT") {
|
||||
t.Fatalf("DDL 内容不符: %s", ddl)
|
||||
}
|
||||
|
||||
c.Table = "no_such_table"
|
||||
if _, err := GetDDL(context.Background(), c); err == nil {
|
||||
t.Fatal("不存在的表应报错")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDDLRedis(t *testing.T) {
|
||||
_, err := GetDDL(context.Background(), ConnReq{Driver: "redis", Table: "k"})
|
||||
if err == nil || !strings.Contains(err.Error(), "不支持") {
|
||||
t.Fatalf("redis 应报不支持: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDDLMissingTable(t *testing.T) {
|
||||
_, err := GetDDL(context.Background(), ConnReq{Driver: "sqlite", Table: ""})
|
||||
if err == nil {
|
||||
t.Fatal("空表名应报错")
|
||||
}
|
||||
}
|
||||
|
||||
// R2-B5:pg/mssql/oracle 族用列元数据拼装,各 family 引号不同。
|
||||
func TestBuildDDLFromColumns(t *testing.T) {
|
||||
cols := []ColumnInfo{
|
||||
{Name: "id", Type: "integer", Nullable: false, Key: "PRI"},
|
||||
{Name: "na\"me", Type: "text", Nullable: true},
|
||||
}
|
||||
// pg:双引号加倍
|
||||
ddl, err := buildDDLFromColumns("postgres", "public.t", cols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, `"public"."t"`) || !strings.Contains(ddl, `"na""me" text`) ||
|
||||
!strings.Contains(ddl, `"id" integer NOT NULL,`) || !strings.Contains(ddl, `PRIMARY KEY ("id")`) {
|
||||
t.Fatalf("pg DDL 不符:\n%s", ddl)
|
||||
}
|
||||
// mssql:中括号
|
||||
ddl, err = buildDDLFromColumns("mssql", "dbo.t", cols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, `[id] integer NOT NULL`) || !strings.Contains(ddl, `PRIMARY KEY ([id])`) {
|
||||
t.Fatalf("mssql DDL 不符:\n%s", ddl)
|
||||
}
|
||||
// oracle:原名
|
||||
ddl, err = buildDDLFromColumns("oracle", "T", cols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(ddl, `CREATE TABLE T (`) || !strings.Contains(ddl, `PRIMARY KEY (id)`) {
|
||||
t.Fatalf("oracle DDL 不符:\n%s", ddl)
|
||||
}
|
||||
// 无列报错
|
||||
if _, err := buildDDLFromColumns("postgres", "t", nil); err == nil {
|
||||
t.Fatal("无列应报错")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user