package main import ( "os" "path/filepath" "testing" ) func TestReadDatabaseListForConvert_SingleFile(t *testing.T) { dir := t.TempDir() file := filepath.Join(dir, "schema.json") writeTestJSON(t, file, []string{"users"}) db, err := readDatabaseListForConvert("json", []string{file}) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(db.Schemas) == 0 { t.Fatal("expected at least one schema") } if len(db.Schemas[0].Tables) != 1 { t.Errorf("expected 1 table, got %d", len(db.Schemas[0].Tables)) } } func TestReadDatabaseListForConvert_MultipleFiles(t *testing.T) { dir := t.TempDir() file1 := filepath.Join(dir, "schema1.json") file2 := filepath.Join(dir, "schema2.json") writeTestJSON(t, file1, []string{"users"}) writeTestJSON(t, file2, []string{"comments"}) db, err := readDatabaseListForConvert("json", []string{file1, file2}) if err != nil { t.Fatalf("unexpected error: %v", err) } total := 0 for _, s := range db.Schemas { total += len(s.Tables) } if total != 2 { t.Errorf("expected 2 tables (users + comments), got %d", total) } } func TestReadDatabaseListForConvert_PathWithSpaces(t *testing.T) { spacedDir := filepath.Join(t.TempDir(), "my schema files") if err := os.MkdirAll(spacedDir, 0755); err != nil { t.Fatal(err) } file := filepath.Join(spacedDir, "my users schema.json") writeTestJSON(t, file, []string{"users"}) db, err := readDatabaseListForConvert("json", []string{file}) if err != nil { t.Fatalf("unexpected error with spaced path: %v", err) } if db == nil { t.Fatal("expected non-nil database") } } func TestReadDatabaseListForConvert_MultipleFilesPathWithSpaces(t *testing.T) { spacedDir := filepath.Join(t.TempDir(), "my schema files") if err := os.MkdirAll(spacedDir, 0755); err != nil { t.Fatal(err) } file1 := filepath.Join(spacedDir, "users schema.json") file2 := filepath.Join(spacedDir, "posts schema.json") writeTestJSON(t, file1, []string{"users"}) writeTestJSON(t, file2, []string{"posts"}) db, err := readDatabaseListForConvert("json", []string{file1, file2}) if err != nil { t.Fatalf("unexpected error with spaced paths: %v", err) } total := 0 for _, s := range db.Schemas { total += len(s.Tables) } if total != 2 { t.Errorf("expected 2 tables, got %d", total) } } func TestReadDatabaseListForConvert_EmptyList(t *testing.T) { _, err := readDatabaseListForConvert("json", []string{}) if err == nil { t.Error("expected error for empty file list") } } func TestReadDatabaseListForConvert_InvalidFile(t *testing.T) { _, err := readDatabaseListForConvert("json", []string{"/nonexistent/path/file.json"}) if err == nil { t.Error("expected error for nonexistent file") } } func TestRunConvert_FromListMutuallyExclusiveWithFromPath(t *testing.T) { saved := saveConvertState() defer restoreConvertState(saved) dir := t.TempDir() file := filepath.Join(dir, "schema.json") writeTestJSON(t, file, []string{"users"}) convertSourceType = "json" convertSourcePath = file convertFromList = []string{file} convertTargetType = "json" convertTargetPath = filepath.Join(dir, "out.json") err := runConvert(nil, nil) if err == nil { t.Error("expected error when --from-path and --from-list are both set") } } func TestRunConvert_FromListEndToEnd(t *testing.T) { saved := saveConvertState() defer restoreConvertState(saved) dir := t.TempDir() file1 := filepath.Join(dir, "users.json") file2 := filepath.Join(dir, "posts.json") outFile := filepath.Join(dir, "merged.json") writeTestJSON(t, file1, []string{"users"}) writeTestJSON(t, file2, []string{"posts"}) convertSourceType = "json" convertSourcePath = "" convertSourceConn = "" convertFromList = []string{file1, file2} convertTargetType = "json" convertTargetPath = outFile convertPackageName = "" convertSchemaFilter = "" convertFlattenSchema = false if err := runConvert(nil, nil); err != nil { t.Fatalf("runConvert() error = %v", err) } if _, err := os.Stat(outFile); os.IsNotExist(err) { t.Error("expected output file to be created") } } func TestRunConvert_FromListEndToEndPathWithSpaces(t *testing.T) { saved := saveConvertState() defer restoreConvertState(saved) spacedDir := filepath.Join(t.TempDir(), "my schema dir") if err := os.MkdirAll(spacedDir, 0755); err != nil { t.Fatal(err) } file1 := filepath.Join(spacedDir, "users schema.json") file2 := filepath.Join(spacedDir, "posts schema.json") outFile := filepath.Join(spacedDir, "merged output.json") writeTestJSON(t, file1, []string{"users"}) writeTestJSON(t, file2, []string{"posts"}) convertSourceType = "json" convertSourcePath = "" convertSourceConn = "" convertFromList = []string{file1, file2} convertTargetType = "json" convertTargetPath = outFile convertPackageName = "" convertSchemaFilter = "" convertFlattenSchema = false if err := runConvert(nil, nil); err != nil { t.Fatalf("runConvert() with spaced paths error = %v", err) } if _, err := os.Stat(outFile); os.IsNotExist(err) { t.Error("expected output file to be created") } }