Skip to content

Commit

Permalink
preload GiftResponse when getting GiftRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddusichka committed Oct 9, 2023
1 parent a046d20 commit 23a8372
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetAllExampleGiftsFromDB(db *gorm.DB) ([]ExampleGift, error) {

func GetIncompleteGiftRequestsFromDB(db *gorm.DB) ([]GiftRequest, error) {
var requests []GiftRequest
if err := db.Where("gift_response_id IS NULL").Find(&requests).Error; err != nil {
if err := db.Where("gift_response_id IS NULL").Preload("GiftResponse").Find(&requests).Error; err != nil {
return nil, err
}
return requests, nil
Expand Down
8 changes: 7 additions & 1 deletion api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"sort"
"time"

"github.com/stretchr/testify/assert"

"github.com/lib/pq"

"os"
Expand Down Expand Up @@ -277,6 +278,11 @@ func TestGetCompleteGiftRequests(t *testing.T) {
assert.Equal(t, request.BudgetMax, requestsRetrieved[0].BudgetMax)
assert.Equal(t, request.BudgetMin, requestsRetrieved[0].BudgetMin)
assert.Equal(t, request.GiftResponseID, requestsRetrieved[0].GiftResponseID)
assert.Equal(t, request.GiftResponse.ID, giftResponse.ID)
assert.NotNil(t, request.GiftResponse) // make sure preloading works
assert.Equal(t, request.GiftResponse.GiftCollectionID, giftResponse.GiftCollectionID)
assert.Equal(t, request.GiftResponse.GiftCollection, giftResponse.GiftCollection)
assert.Equal(t, request.GiftResponse.CustomMessage, giftResponse.CustomMessage)
assert.Equal(t, request.DateNeeded.In(time.UTC).Round(time.Millisecond),
requestsRetrieved[0].DateNeeded.In(time.UTC).Round(time.Millisecond))
}

0 comments on commit 23a8372

Please sign in to comment.