test: expand coverage across readers, writers, cmd, ui, diff and merge

Implements tests/_plans and previously deferred packages; updates plan
README with new coverage numbers.
This commit is contained in:
2026-10-03 21:33:59 +02:00
parent a32647ee16
commit 495a21b67b
50 changed files with 8461 additions and 8 deletions
+312
View File
@@ -0,0 +1,312 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"git.warky.dev/wdevs/relspecgo/pkg/models"
)
const fixturesDir = "../../tests/assets"
// readableFormats maps each file-based reader format to an existing fixture.
var readableFormats = []struct {
format string
path string
}{
{"dbml", "dbml/simple.dbml"},
{"json", "json/database.json"},
{"yaml", "yaml/database.yaml"},
{"yml", "yaml/database.yaml"},
{"drawdb", "drawdb/simple.json"},
{"dctx", "dctx/p1.dctx"},
{"graphql", "graphql/simple.graphql"},
{"gql", "graphql/simple.graphql"},
{"prisma", "prisma/example.prisma"},
{"typeorm", "typeorm/example.ts"},
{"drizzle", "drizzle/schema.ts"},
{"gorm", "gorm/simple.go"},
{"bun", "bun/simple.go"},
}
func TestReadDatabaseForConvert_FileFormats(t *testing.T) {
for _, tt := range readableFormats {
t.Run(tt.format, func(t *testing.T) {
db, err := readDatabaseForConvert(tt.format, filepath.Join(fixturesDir, tt.path), "")
if err != nil {
t.Fatalf("read: %v", err)
}
if db == nil || len(db.Schemas) == 0 {
t.Fatalf("no schemas read: %+v", db)
}
// Uppercase format names are accepted.
if _, err := readDatabaseForConvert(strings.ToUpper(tt.format), filepath.Join(fixturesDir, tt.path), ""); err != nil {
t.Errorf("uppercase format: %v", err)
}
})
}
}
func TestReadDatabaseForConvert_Errors(t *testing.T) {
filePathFormats := []string{"dbml", "dctx", "drawdb", "json", "yaml", "gorm", "bun", "drizzle", "prisma", "typeorm", "graphql"}
for _, f := range filePathFormats {
t.Run("missing path "+f, func(t *testing.T) {
_, err := readDatabaseForConvert(f, "", "")
if err == nil || !strings.Contains(err.Error(), "file path is required") {
t.Errorf("got %v", err)
}
})
}
connFormats := []string{"pgsql", "postgres", "postgresql", "mssql", "sqlserver", "mysql", "mariadb"}
for _, f := range connFormats {
t.Run("missing conn "+f, func(t *testing.T) {
_, err := readDatabaseForConvert(f, "", "")
if err == nil || !strings.Contains(err.Error(), "connection string is required") {
t.Errorf("got %v", err)
}
})
}
if _, err := readDatabaseForConvert("sqlite", "", ""); err == nil || !strings.Contains(err.Error(), "required for SQLite") {
t.Errorf("sqlite: %v", err)
}
if _, err := readDatabaseForConvert("nope", "x", ""); err == nil || !strings.Contains(err.Error(), "unsupported source format") {
t.Errorf("unsupported: %v", err)
}
if _, err := readDatabaseForConvert("dbml", filepath.Join(t.TempDir(), "missing.dbml"), ""); err == nil || !strings.Contains(err.Error(), "failed to read database") {
t.Errorf("missing file: %v", err)
}
}
func TestReadDatabase_DiffReader(t *testing.T) {
for _, f := range []string{"dbml", "json", "yaml", "drawdb", "dctx"} {
for _, tt := range readableFormats {
if tt.format != f {
continue
}
t.Run(f, func(t *testing.T) {
db, err := readDatabase(f, filepath.Join(fixturesDir, tt.path), "", "source")
if err != nil || db == nil || len(db.Schemas) == 0 {
t.Fatalf("read: %v %+v", err, db)
}
})
}
}
for _, f := range []string{"dbml", "dctx", "drawdb", "json", "yaml", "sqldir"} {
if _, err := readDatabase(f, "", "", "src"); err == nil || !strings.Contains(err.Error(), "src: file path is required") {
t.Errorf("%s missing path: %v", f, err)
}
}
if _, err := readDatabase("pgsql", "", "", "src"); err == nil || !strings.Contains(err.Error(), "connection string is required") {
t.Errorf("pgsql: %v", err)
}
if _, err := readDatabase("sqlite", "", "", "src"); err == nil {
t.Error("sqlite without path must fail")
}
if _, err := readDatabase("nope", "x", "", "src"); err == nil || !strings.Contains(err.Error(), "unsupported database format") {
t.Errorf("unsupported: %v", err)
}
if _, err := readDatabase("json", filepath.Join(t.TempDir(), "missing.json"), "", "src"); err == nil || !strings.Contains(err.Error(), "src: failed to read database") {
t.Errorf("missing file: %v", err)
}
}
func TestMaskPassword(t *testing.T) {
tests := []struct {
in, want string
}{
{"", ""},
{"postgres://user:secret@host:5432/db", "postgres://user:***@host:5432/db"},
{"postgres://user@host:5432/db", "postgres://user@host:5432/db"},
{"host=h user=u password=secret dbname=d", "host=h user=u password=*** dbname=d"},
{"host=h user=u", "host=h user=u"},
{"/tmp/file.db", "/tmp/file.db"},
}
for _, tt := range tests {
if got := maskPassword(tt.in); got != tt.want {
t.Errorf("maskPassword(%q) = %q, want %q", tt.in, got, tt.want)
}
if got := maskPasswordInDiff(tt.in); got != tt.want {
t.Errorf("maskPasswordInDiff(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestGetSchemaNames(t *testing.T) {
db := models.InitDatabase("d")
if got := getSchemaNames(db); len(got) != 0 {
t.Errorf("empty: %v", got)
}
db.Schemas = []*models.Schema{{Name: "a"}, {Name: "b"}}
if got := strings.Join(getSchemaNames(db), ","); got != "a,b" {
t.Errorf("got %s", got)
}
}
func TestLoadExtraFields(t *testing.T) {
dir := t.TempDir()
write := func(name, body string) string {
p := filepath.Join(dir, name)
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
return p
}
valid := write("valid.json", `[{"name":"extra"}]`)
if got, err := loadExtraFields("bun", valid); err != nil || !strings.Contains(got, "extra") {
t.Errorf("valid: %q %v", got, err)
}
if _, err := loadExtraFields("BUN", valid); err != nil {
t.Errorf("case-insensitive format: %v", err)
}
if _, err := loadExtraFields("gorm", valid); err == nil || !strings.Contains(err.Error(), "only supported for Bun") {
t.Errorf("non-bun: %v", err)
}
if _, err := loadExtraFields("bun", filepath.Join(dir, "missing.json")); err == nil || !strings.Contains(err.Error(), "failed to read") {
t.Errorf("missing: %v", err)
}
if _, err := loadExtraFields("bun", write("bad.json", `{not json`)); err == nil || !strings.Contains(err.Error(), "invalid --extra-fields JSON") {
t.Errorf("bad json: %v", err)
}
if _, err := loadExtraFields("bun", write("empty.json", `[]`)); err == nil || !strings.Contains(err.Error(), "at least one field") {
t.Errorf("empty: %v", err)
}
}
func multiSchemaDB() *models.Database {
db := models.InitDatabase("multi")
for _, n := range []string{"a", "b"} {
s := models.InitSchema(n)
tbl := models.InitTable("t_"+n, n)
c := models.InitColumn("id", tbl.Name, n)
c.Type = "integer"
c.IsPrimaryKey = true
tbl.Columns["id"] = c
s.Tables = append(s.Tables, tbl)
db.Schemas = append(db.Schemas, s)
}
return db
}
func TestValidateWriteTarget(t *testing.T) {
db := multiSchemaDB()
single := models.InitDatabase("single")
single.Schemas = []*models.Schema{models.InitSchema("only")}
tests := []struct {
name string
db *models.Database
dbType, pkg, schemaFilter, extraFields, wantErrSubstr string
}{
{"json ok", db, "json", "", "", "", ""},
{"pgsql alias ok", db, "sql", "", "", "", ""},
{"gorm needs package", db, "gorm", "", "", "", "package name is required"},
{"bun needs package", db, "bun", "", "", "", "package name is required"},
{"gorm with package", db, "gorm", "models", "", "", ""},
{"unsupported", db, "nope", "", "", "", "unsupported target format"},
{"schema filter found", db, "json", "", "a", "", ""},
{"schema filter missing", db, "json", "", "zzz", "", "not found in database"},
{"dctx multi schema", db, "dctx", "", "", "", "multiple schemas found"},
{"dctx multi schema with filter", db, "dctx", "", "a", "", ""},
{"dctx single schema", single, "dctx", "", "", "", ""},
{"dctx no schemas", models.InitDatabase("e"), "dctx", "", "", "", "no schemas found"},
{"extra fields non-bun", db, "json", "", "", "x.json", "only supported for Bun"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateWriteTarget(tt.db, tt.dbType, tt.pkg, tt.schemaFilter, tt.extraFields)
if tt.wantErrSubstr == "" {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErrSubstr) {
t.Errorf("got %v, want substring %q", err, tt.wantErrSubstr)
}
})
}
}
func TestWriteDatabase_Formats(t *testing.T) {
db := multiSchemaDB()
formats := []struct{ format, file string }{
{"json", "out.json"},
{"yaml", "out.yaml"},
{"yml", "out.yml"},
{"dbml", "out.dbml"},
{"drawdb", "out.drawdb.json"},
{"pgsql", "out.sql"},
{"postgres", "out2.sql"},
{"sql", "out3.sql"},
{"mssql", "out_ms.sql"},
{"mysql", "out_my.sql"},
{"sqlite", "out_lite.sql"},
{"graphql", "out.graphql"},
{"gql", "out2.graphql"},
{"prisma", "out.prisma"},
{"typeorm", "out.ts"},
{"drizzle", "out_drizzle.ts"},
}
for _, tt := range formats {
t.Run(tt.format, func(t *testing.T) {
out := filepath.Join(t.TempDir(), tt.file)
if err := writeDatabase(db, tt.format, out, "", "", false, "", "", false, ""); err != nil {
t.Fatalf("write: %v", err)
}
info, err := os.Stat(out)
if err != nil || info.Size() == 0 {
t.Errorf("output missing or empty: %v", err)
}
})
}
}
func TestWriteDatabase_GoFormatsWriteIntoDir(t *testing.T) {
db := multiSchemaDB()
for _, f := range []string{"gorm", "bun"} {
t.Run(f, func(t *testing.T) {
out := filepath.Join(t.TempDir(), "models.go")
if err := writeDatabase(db, f, out, "models", "", false, "", "", false, ""); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(out); err != nil {
t.Errorf("no output: %v", err)
}
if err := writeDatabase(db, f, out, "", "", false, "", "", false, ""); err == nil || !strings.Contains(err.Error(), "package name is required") {
t.Errorf("missing package: %v", err)
}
})
}
}
func TestWriteDatabase_SchemaFilterAndDCTX(t *testing.T) {
db := multiSchemaDB()
out := filepath.Join(t.TempDir(), "o.json")
if err := writeDatabase(db, "json", out, "", "a", false, "", "", false, ""); err != nil {
t.Errorf("schema filter: %v", err)
}
if err := writeDatabase(db, "json", out, "", "zzz", false, "", "", false, ""); err == nil || !strings.Contains(err.Error(), "not found in database") {
t.Errorf("missing schema: %v", err)
}
if err := writeDatabase(db, "dctx", out, "", "", false, "", "", false, ""); err == nil || !strings.Contains(err.Error(), "multiple schemas found") {
t.Errorf("dctx multi: %v", err)
}
single := models.InitDatabase("s")
single.Schemas = []*models.Schema{db.Schemas[0]}
if err := writeDatabase(single, "dctx", filepath.Join(t.TempDir(), "o.dctx"), "", "", false, "", "", false, ""); err != nil {
t.Errorf("dctx single: %v", err)
}
if err := writeDatabase(models.InitDatabase("e"), "dctx", out, "", "", false, "", "", false, ""); err == nil || !strings.Contains(err.Error(), "no schemas found") {
t.Errorf("dctx empty: %v", err)
}
if err := writeDatabase(db, "nope", out, "", "", false, "", "", false, ""); err == nil || !strings.Contains(err.Error(), "unsupported target format") {
t.Errorf("unsupported: %v", err)
}
if err := writeDatabase(db, "json", out, "", "", false, "", "", false, filepath.Join(t.TempDir(), "x.json")); err == nil || !strings.Contains(err.Error(), "only supported for Bun") {
t.Errorf("extra fields with json: %v", err)
}
}
@@ -0,0 +1,287 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestReadDatabaseForMerge(t *testing.T) {
for _, tt := range readableFormats {
t.Run(tt.format, func(t *testing.T) {
db, err := readDatabaseForMerge(tt.format, filepath.Join(fixturesDir, tt.path), "", "Target")
if err != nil {
t.Skipf("format %s not supported by merge reader: %v", tt.format, err)
}
if db == nil || len(db.Schemas) == 0 {
t.Errorf("no schemas: %+v", db)
}
})
}
for _, f := range []string{"dbml", "dctx", "drawdb", "graphql", "json", "yaml", "gorm", "bun", "drizzle", "prisma", "typeorm"} {
if _, err := readDatabaseForMerge(f, "", "", "Src"); err == nil || !strings.Contains(err.Error(), "Src: file path is required") {
t.Errorf("%s missing path: %v", f, err)
}
}
if _, err := readDatabaseForMerge("pgsql", "", "", "Src"); err == nil || !strings.Contains(err.Error(), "Src:") {
t.Errorf("pgsql: %v", err)
}
if _, err := readDatabaseForMerge("sqlite", "", "", "Src"); err == nil || !strings.Contains(err.Error(), "Src:") {
t.Errorf("sqlite: %v", err)
}
if _, err := readDatabaseForMerge("nope", "x", "", "Src"); err == nil || !strings.Contains(err.Error(), "unsupported format 'nope'") {
t.Errorf("unsupported: %v", err)
}
}
func TestWriteDatabaseForMerge(t *testing.T) {
db := multiSchemaDB()
single := multiSchemaDB()
single.Schemas = single.Schemas[:1]
files := map[string]string{
"dbml": "o.dbml", "dctx": "o.dctx", "drawdb": "o.drawdb.json", "graphql": "o.graphql",
"json": "o.json", "yaml": "o.yaml", "gorm": "gorm.go", "bun": "bun.go",
"drizzle": "o.ts", "prisma": "o.prisma", "typeorm": "te.ts",
}
for f, name := range files {
t.Run(f, func(t *testing.T) {
out := filepath.Join(t.TempDir(), name)
if f == "dctx" {
// DCTX cannot write a full database.
if err := writeDatabaseForMerge(f, out, "", single, "Output", false); err == nil || !strings.Contains(err.Error(), "not supported for DCTX") {
t.Errorf("dctx: %v", err)
}
if err := writeDatabaseForMerge(f, "", "", single, "Output", false); err == nil || !strings.Contains(err.Error(), "file path is required") {
t.Errorf("dctx missing path: %v", err)
}
return
}
src := db
if err := writeDatabaseForMerge(f, out, "", src, "Output", false); err != nil {
t.Fatalf("write: %v", err)
}
if _, err := os.Stat(out); err != nil {
t.Errorf("no output: %v", err)
}
if err := writeDatabaseForMerge(f, "", "", src, "Output", false); err == nil || !strings.Contains(err.Error(), "Output: file path is required") {
t.Errorf("missing path: %v", err)
}
})
}
for _, f := range []string{"pgsql", "sqlite"} {
out := filepath.Join(t.TempDir(), "o.sql")
if err := writeDatabaseForMerge(f, out, "", db, "Output", false); err != nil {
t.Errorf("%s script write: %v", f, err)
}
}
if err := writeDatabaseForMerge("pgsql", "", "postgres://u:p@127.0.0.1:1/none?connect_timeout=1", db, "Output", false); err == nil {
t.Error("pgsql with unreachable conn must fail")
}
if err := writeDatabaseForMerge("nope", "x", "", db, "Output", false); err == nil || !strings.Contains(err.Error(), "unsupported") {
t.Errorf("unsupported: %v", err)
}
}
func TestIsMergeOutputFormat(t *testing.T) {
for _, f := range []string{"dbml", "JSON", "pgsql", "sqlite3", "prisma"} {
if !isMergeOutputFormat(f) {
t.Errorf("%s should be supported", f)
}
}
for _, f := range []string{"", "nope", "mssql"} {
if isMergeOutputFormat(f) {
t.Errorf("%s should not be supported", f)
}
}
}
func TestExpandPath(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Skip("no home dir")
}
tests := []struct{ in, want string }{
{"", ""},
{"/abs/path", "/abs/path"},
{"rel/path", "rel/path"},
{"~/x/y", filepath.Join(home, "/x/y")},
{"~", home},
}
for _, tt := range tests {
if got := expandPath(tt.in); got != tt.want {
t.Errorf("expandPath(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}
func TestParseSkipTables(t *testing.T) {
tests := []struct {
in string
want []string
}{
{"", nil},
{" , ,", nil},
{"Users", []string{"users"}},
{" Users , ORDERS,,items ", []string{"users", "orders", "items"}},
}
for _, tt := range tests {
got := parseSkipTables(tt.in)
if len(got) != len(tt.want) {
t.Errorf("parseSkipTables(%q) = %v", tt.in, got)
}
for _, w := range tt.want {
if !got[w] {
t.Errorf("parseSkipTables(%q) missing %q", tt.in, w)
}
}
}
}
func TestReadDatabaseForInspect(t *testing.T) {
for _, tt := range readableFormats {
t.Run(tt.format, func(t *testing.T) {
db, err := readDatabaseForInspect(tt.format, filepath.Join(fixturesDir, tt.path), "")
if err != nil {
t.Skipf("format %s not supported by inspect reader: %v", tt.format, err)
}
if db == nil || len(db.Schemas) == 0 {
t.Errorf("no schemas: %+v", db)
}
})
}
for _, f := range []string{"dbml", "dctx", "drawdb", "graphql", "json", "yaml", "gorm", "bun", "drizzle", "prisma", "typeorm"} {
if _, err := readDatabaseForInspect(f, "", ""); err == nil || !strings.Contains(err.Error(), "file path is required") {
t.Errorf("%s missing path: %v", f, err)
}
}
if _, err := readDatabaseForInspect("pgsql", "", ""); err == nil {
t.Error("pgsql without conn must fail")
}
if _, err := readDatabaseForInspect("nope", "x", ""); err == nil || !strings.Contains(err.Error(), "unsupported database type") {
t.Errorf("unsupported: %v", err)
}
}
func TestFilterDatabaseBySchema(t *testing.T) {
db := multiSchemaDB()
db.Description = "desc"
got := filterDatabaseBySchema(db, "b")
if len(got.Schemas) != 1 || got.Schemas[0].Name != "b" || got.Name != db.Name || got.Description != "desc" {
t.Errorf("filtered: %+v", got)
}
if got := filterDatabaseBySchema(db, "zzz"); len(got.Schemas) != 0 {
t.Errorf("missing schema should yield no schemas: %+v", got.Schemas)
}
if len(db.Schemas) != 2 {
t.Error("input mutated")
}
}
func TestHasSilentFlag(t *testing.T) {
tests := []struct {
args []string
want bool
}{
{nil, false},
{[]string{"convert"}, false},
{[]string{"convert", "--silent"}, true},
{[]string{"--silent=true"}, true},
{[]string{"--silent=false"}, false},
}
for _, tt := range tests {
if got := hasSilentFlag(tt.args); got != tt.want {
t.Errorf("hasSilentFlag(%v) = %v", tt.args, got)
}
}
}
func TestPrintVersionHeader(t *testing.T) {
capture := func(args []string) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
printVersionHeader(args)
w.Close()
os.Stdout = old
b := make([]byte, 4096)
n, _ := r.Read(b)
return string(b[:n])
}
if out := capture([]string{"convert"}); !strings.HasPrefix(out, "RelSpec ") {
t.Errorf("header: %q", out)
}
if out := capture([]string{"convert", "--no-version"}); out != "" {
t.Errorf("--no-version: %q", out)
}
if out := capture([]string{"version"}); out != "" {
t.Errorf("version cmd: %q", out)
}
if out := capture(nil); !strings.HasPrefix(out, "RelSpec ") {
t.Errorf("no args: %q", out)
}
}
func TestReportState(t *testing.T) {
cfg := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", cfg)
t.Setenv("HOME", cfg)
dir, err := reportStateDir()
if err != nil || !strings.HasPrefix(dir, cfg) {
t.Fatalf("dir: %q %v", dir, err)
}
state, path, err := loadReportState()
if err != nil || !state.LastReport.IsZero() || state.MachineID != "" {
t.Fatalf("fresh state: %+v %v", state, err)
}
want := reportState{LastReport: time.Now().UTC().Truncate(time.Second), MachineID: "abc"}
if err := saveReportState(path, want); err != nil {
t.Fatal(err)
}
got, _, err := loadReportState()
if err != nil || !got.LastReport.Equal(want.LastReport) || got.MachineID != "abc" {
t.Errorf("round trip: %+v %v", got, err)
}
// Corrupt state is ignored.
if err := os.WriteFile(path, []byte("{bad"), 0o600); err != nil {
t.Fatal(err)
}
if got, _, err := loadReportState(); err != nil || got.MachineID != "" {
t.Errorf("corrupt: %+v %v", got, err)
}
}
func TestSystemUniqueID_NonEmpty(t *testing.T) {
cfg := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", cfg)
state, path, _ := loadReportState()
id, err := systemUniqueID(state, path)
if err != nil || id == "" {
t.Errorf("id: %q %v", id, err)
}
}
func TestReportToken_Decodes(t *testing.T) {
if _, err := reportToken(); err != nil {
t.Errorf("token must decode: %v", err)
}
}
func TestSubmitReport_RateLimited(t *testing.T) {
cfg := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", cfg)
_, path, _ := loadReportState()
if err := saveReportState(path, reportState{LastReport: time.Now()}); err != nil {
t.Fatal(err)
}
// Rate limit rejects before any network call is made.
if err := submitReport("bug", "t", "b", "", ""); err == nil || !strings.Contains(err.Error(), "please wait") {
t.Errorf("got %v", err)
}
}
+86
View File
@@ -0,0 +1,86 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestRunDiff(t *testing.T) {
oldS, oldSP, oldSC, oldT, oldTP, oldTC, oldF, oldO := sourceType, sourcePath, sourceConn, targetType, targetPath, targetConn, outputFormat, outputPath
t.Cleanup(func() {
sourceType, sourcePath, sourceConn, targetType, targetPath, targetConn, outputFormat, outputPath = oldS, oldSP, oldSC, oldT, oldTP, oldTC, oldF, oldO
})
src := filepath.Join(fixturesDir, "dbml/simple.dbml")
cmplx := filepath.Join(fixturesDir, "dbml/complex.dbml")
for _, format := range []string{"summary", "json", "html"} {
t.Run(format, func(t *testing.T) {
sourceType, sourcePath, sourceConn = "dbml", src, ""
targetType, targetPath, targetConn = "dbml", cmplx, ""
outputFormat = format
outputPath = filepath.Join(t.TempDir(), "diff.out")
if format == "summary" {
outputPath = ""
}
if err := runDiff(nil, nil); err != nil {
t.Fatalf("runDiff: %v", err)
}
if outputPath != "" {
if b, err := os.ReadFile(outputPath); err != nil || len(b) == 0 {
t.Errorf("empty output: %v", err)
}
}
})
}
t.Run("bad source", func(t *testing.T) {
sourceType, sourcePath = "dbml", filepath.Join(t.TempDir(), "missing.dbml")
targetType, targetPath = "dbml", src
outputFormat, outputPath = "summary", ""
if err := runDiff(nil, nil); err == nil || !strings.Contains(err.Error(), "failed to read source database") {
t.Errorf("got %v", err)
}
})
t.Run("bad target", func(t *testing.T) {
sourceType, sourcePath = "dbml", src
targetType, targetPath = "dbml", filepath.Join(t.TempDir(), "missing.dbml")
outputFormat, outputPath = "summary", ""
if err := runDiff(nil, nil); err == nil || !strings.Contains(err.Error(), "failed to read target database") {
t.Errorf("got %v", err)
}
})
}
func TestRunInspect(t *testing.T) {
oldT, oldP, oldC, oldR, oldF, oldO, oldS := inspectSourceType, inspectSourcePath, inspectSourceConn, inspectRulesPath, inspectOutputFormat, inspectOutputPath, inspectSchemaFilter
t.Cleanup(func() {
inspectSourceType, inspectSourcePath, inspectSourceConn, inspectRulesPath, inspectOutputFormat, inspectOutputPath, inspectSchemaFilter = oldT, oldP, oldC, oldR, oldF, oldO, oldS
})
inspectSourceType = "dbml"
inspectSourcePath = filepath.Join(fixturesDir, "dbml/simple.dbml")
inspectSourceConn = ""
inspectRulesPath = filepath.Join(t.TempDir(), "no-rules.yaml") // missing: defaults used or error
inspectSchemaFilter = ""
// Whatever the rules outcome, the run must not panic; formats are exercised.
for _, format := range []string{"markdown", "json"} {
inspectOutputFormat = format
inspectOutputPath = filepath.Join(t.TempDir(), "report."+format)
_ = runInspect(nil, nil)
}
inspectOutputFormat = "bogus"
inspectOutputPath = ""
if err := runInspect(nil, nil); err == nil {
t.Error("bogus output format must fail")
}
inspectSourcePath = filepath.Join(t.TempDir(), "missing.dbml")
if err := runInspect(nil, nil); err == nil || !strings.Contains(err.Error(), "failed to read source") {
t.Errorf("missing source: %v", err)
}
}