With WithStore, the Find a Tender and mock workers save each run's opportunities with SaveOpportunities and log what changed; a failed save fails the run, so the checkpoint stays put and the next run retries. Without a store nothing is persisted. The mock's opportunities are saved with Source "FTSMock" rather than the converter's "FindATender". fetcher service and job-search serve open the store at database.file (cli.OpenConfiguredStore), or warn and run without one. A missing file isn't created, and a database needing migration says to run job-search migrate. Fixed upsertOpportunity, which never wrote the source column, so every saved opportunity had an empty Source.
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
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()
|
|
}
|