package cli import ( "context" "errors" "os" "path/filepath" "strings" "testing" "git.avsdev.uk/Go/job-search/internal/config" "git.avsdev.uk/Go/job-search/internal/store" ) // No database.file means no store, reported as ErrNoDatabase. func TestOpenConfiguredStoreNone(t *testing.T) { opportunityStore, err := OpenConfiguredStore( context.Background(), &config.JobSearchConfig{}, ) if opportunityStore != nil || !errors.Is(err, ErrNoDatabase) { t.Errorf( "OpenConfiguredStore = %v, %v; want nil, ErrNoDatabase", opportunityStore, err, ) } } // A missing database isn't created; one that needs migrating says how to // migrate it; a migrated one opens. func TestOpenConfiguredStore(t *testing.T) { conf := &config.JobSearchConfig{} conf.Database.File = filepath.Join(t.TempDir(), "jobsearch.db") _, err := OpenConfiguredStore(context.Background(), conf) if err == nil || !strings.Contains(err.Error(), "migrate up --create") { t.Fatalf("missing: err = %v; want a hint to create it", err) } _, statErr := os.Stat(conf.Database.File) if !errors.Is(statErr, os.ErrNotExist) { t.Fatal("the missing database file was created") } // An empty file is a database at version 0. if err := os.WriteFile(conf.Database.File, nil, 0o600); err != nil { t.Fatal(err) } _, err = OpenConfiguredStore(context.Background(), conf) if !errors.Is(err, store.ErrNeedsMigration) || !strings.Contains(err.Error(), "run `job-search migrate`") { t.Fatalf("unmigrated: err = %v; want ErrNeedsMigration and a hint", err) } migrated, err := store.Open( context.Background(), conf.Database.File, store.WithAutoMigrate(), ) if err != nil { t.Fatal(err) } migrated.Close() opportunityStore, err := OpenConfiguredStore(context.Background(), conf) if err != nil { t.Fatalf("migrated: err = %v", err) } opportunityStore.Close() }