diff --git a/pkg/solver/store/store_test.go b/pkg/solver/store/store_test.go index d48ea2ca..57090a68 100644 --- a/pkg/solver/store/store_test.go +++ b/pkg/solver/store/store_test.go @@ -24,111 +24,6 @@ type storeConfig struct { init func() (getStore func() store.SolverStore) } -func setupStores(t *testing.T) []storeConfig { - initMemory := func() func() store.SolverStore { - // Get store function creates a new memory store - // which effectively clears data between runs - return func() store.SolverStore { - s, err := memorystore.NewSolverStoreMemory() - if err != nil { - t.Fatalf("Failed to create memory store: %v", err) - } - return s - } - } - - 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) { - // Delete job offers - 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) - } - } - - // Delete resource offers - resourceOffers, err := s.GetResourceOffers(store.GetResourceOffersQuery{}) - if err != nil { - t.Fatalf("Failed to get existing resource offers: %v", err) - } - - for _, result := range resourceOffers { - err := s.RemoveResourceOffer(result.ID) - if err != nil { - t.Fatalf("Failed to remove existing resource offer: %v", err) - } - } - - // Delete deals - deals, err := s.GetDealsAll() - if err != nil { - t.Fatalf("Failed to get existing deals: %v", err) - } - - for _, deal := range deals { - err := s.RemoveDeal(deal.ID) - if err != nil { - t.Fatalf("Failed to remove existing deal: %v", err) - } - } - - // Delete results - results, err := s.GetResults() - if err != nil { - t.Fatalf("Failed to get existing results: %v", err) - } - - for _, result := range results { - err := s.RemoveResult(result.DealID) - if err != nil { - t.Fatalf("Failed to remove existing result: %v", err) - } - } - - // Delete match decisions - decisions, err := s.GetMatchDecisions() - if err != nil { - t.Fatalf("Failed to get existing match decisions: %v", err) - } - - for _, decision := range decisions { - err := s.RemoveMatchDecision(decision.ResourceOffer, decision.JobOffer) - if err != nil { - t.Fatalf("Failed to remove existing match decision: %v", err) - } - } -} - // Job offers func TestJobOfferOps(t *testing.T) { @@ -1438,6 +1333,113 @@ func TestConcurrentOps(t *testing.T) { } } +// Utilities + +func setupStores(t *testing.T) []storeConfig { + initMemory := func() func() store.SolverStore { + // Get store function creates a new memory store + // which effectively clears data between runs + return func() store.SolverStore { + s, err := memorystore.NewSolverStoreMemory() + if err != nil { + t.Fatalf("Failed to create memory store: %v", err) + } + return s + } + } + + 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) { + // Delete job offers + 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) + } + } + + // Delete resource offers + resourceOffers, err := s.GetResourceOffers(store.GetResourceOffersQuery{}) + if err != nil { + t.Fatalf("Failed to get existing resource offers: %v", err) + } + + for _, result := range resourceOffers { + err := s.RemoveResourceOffer(result.ID) + if err != nil { + t.Fatalf("Failed to remove existing resource offer: %v", err) + } + } + + // Delete deals + deals, err := s.GetDealsAll() + if err != nil { + t.Fatalf("Failed to get existing deals: %v", err) + } + + for _, deal := range deals { + err := s.RemoveDeal(deal.ID) + if err != nil { + t.Fatalf("Failed to remove existing deal: %v", err) + } + } + + // Delete results + results, err := s.GetResults() + if err != nil { + t.Fatalf("Failed to get existing results: %v", err) + } + + for _, result := range results { + err := s.RemoveResult(result.DealID) + if err != nil { + t.Fatalf("Failed to remove existing result: %v", err) + } + } + + // Delete match decisions + decisions, err := s.GetMatchDecisions() + if err != nil { + t.Fatalf("Failed to get existing match decisions: %v", err) + } + + for _, decision := range decisions { + err := s.RemoveMatchDecision(decision.ResourceOffer, decision.JobOffer) + if err != nil { + t.Fatalf("Failed to remove existing match decision: %v", err) + } + } +} + // Generators func generateCID() string {