Skip to content

Commit

Permalink
test: Add database store to test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Dec 3, 2024
1 parent d679dc1 commit 36ec499
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/solver/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/solver/store"
databasestore "github.com/lilypad-tech/lilypad/pkg/solver/store/db"
memorystore "github.com/lilypad-tech/lilypad/pkg/solver/store/memory"
"golang.org/x/exp/rand"
)

const DB_CONN_STR = "postgres://postgres:postgres@localhost:5432/solver-db?sslmode=disable"

type storeConfig struct {
name string
init func() (getStore func() store.SolverStore)
Expand All @@ -31,8 +34,42 @@ func setupStores(t *testing.T) []storeConfig {
}
}

initDatabase := func() func() store.SolverStore {
db, err := databasestore.NewSolverStoreDatabase(DB_CONN_STR, true)
if err != nil {
t.Fatalf("Failed to create database store: %v", err)
}

// Clear data at initialization
clearStoreDatabase(t, db)

// Get store functions clear data and returns
// the same store instance
return func() store.SolverStore {
clearStoreDatabase(t, db)
return db
}
}

return []storeConfig{
{name: "memory", init: initMemory},
{name: "database", init: initDatabase},
}
}

func clearStoreDatabase(t *testing.T, s store.SolverStore) {
jobOffers, err := s.GetJobOffers(store.GetJobOffersQuery{
IncludeCancelled: true,
})
if err != nil {
t.Fatalf("Failed to get existing job offers: %v", err)
}

for _, result := range jobOffers {
err := s.RemoveJobOffer(result.ID)
if err != nil {
t.Fatalf("Failed to remove existing job offer: %v", err)
}
}
}

Expand Down

0 comments on commit 36ec499

Please sign in to comment.