Skip to content

Commit

Permalink
refactor: Move test utilities to botttom
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Dec 10, 2024
1 parent d47b91b commit 68567de
Showing 1 changed file with 107 additions and 105 deletions.
212 changes: 107 additions & 105 deletions pkg/solver/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 68567de

Please sign in to comment.