Skip to content

Commit

Permalink
Updated the review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kalidas-alkimi committed Nov 9, 2023
1 parent 773bf92 commit 87a2333
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 25 deletions.
47 changes: 23 additions & 24 deletions adapters/alkimi/alkimi.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func buildBidderRequest(adapter *adapter, encoded []byte) *adapters.RequestData
func (adapter *adapter) MakeBids(request *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) {
var errs []error

if request == nil || response == nil || adapters.IsResponseStatusCodeNoContent(response) {
if adapters.IsResponseStatusCodeNoContent(response) {
return nil, nil
}

Expand All @@ -138,35 +138,34 @@ func (adapter *adapter) MakeBids(request *openrtb2.BidRequest, externalRequest *
}

bidCount := len(bidResp.SeatBid)
if bidCount > 0 {
bidResponse := adapters.NewBidderResponseWithBidsCapacity(bidCount)
for _, seatBid := range bidResp.SeatBid {
for _, bid := range seatBid.Bid {
copyBid := bid
resolveMacros(&copyBid)
impId := copyBid.ImpID
imp := request.Imp
bidType, err := getMediaTypeForImp(impId, imp)
if err != nil {
errs = append(errs, err)
}
bidderBid := &adapters.TypedBid{
Bid: &copyBid,
BidType: bidType,
Seat: "alkimi",
}
bidResponse.Bids = append(bidResponse.Bids, bidderBid)
if bidCount == 0 {
return nil, []error{&errortypes.BadServerResponse{
Message: "Empty SeatBid array",
}}
}

bidResponse := adapters.NewBidderResponseWithBidsCapacity(bidCount)
for _, seatBid := range bidResp.SeatBid {
for _, bid := range seatBid.Bid {
copyBid := bid
resolveMacros(&copyBid)
impId := copyBid.ImpID
imp := request.Imp
bidType, err := getMediaTypeForImp(impId, imp)
if err != nil {
errs = append(errs, err)
}
bidderBid := &adapters.TypedBid{
Bid: &copyBid,
BidType: bidType,
}
bidResponse.Bids = append(bidResponse.Bids, bidderBid)
}
return bidResponse, errs
}
return nil, nil
return bidResponse, errs
}

func resolveMacros(bid *openrtb2.Bid) {
if bid == nil {
return
}
strPrice := strconv.FormatFloat(bid.Price, 'f', -1, 64)
bid.NURL = strings.Replace(bid.NURL, price_macro, strPrice, -1)
bid.AdM = strings.Replace(bid.AdM, price_macro, strPrice, -1)
Expand Down
103 changes: 102 additions & 1 deletion adapters/alkimi/alkimi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,60 @@ func TestMakeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed(t *testing.T) {
assert.Equal(t, "adm:1", bids.Bids[0].Bid.AdM)
}

func TestMakeBidsShouldReturnErrorIfResponseBodyContainsIncorrectImp(t *testing.T) {
// given
bidder, _ := buildBidder()
bid := openrtb2.Bid{
ImpID: "impId-1-incorrect",
AdM: "adm:${AUCTION_PRICE}",
NURL: "nurl:${AUCTION_PRICE}",
Price: 1,
}
sb := openrtb2.SeatBid{Bid: []openrtb2.Bid{bid}}
resp := openrtb2.BidResponse{SeatBid: []openrtb2.SeatBid{sb}}
respJson, jsonErr := json.Marshal(resp)
request := openrtb2.BidRequest{
Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Banner: &openrtb2.Banner{}}),
}
// when
bids, errs := bidder.MakeBids(&request, nil, &adapters.ResponseData{
StatusCode: 200,
Body: respJson,
})
// then
if jsonErr != nil {
t.Fatalf("Failed to serialize test bid %v: %v", bid, jsonErr)
}
assert.Len(t, bids.Bids, 1)
assert.Len(t, errs, 1)
}

func TestMakeBidsShouldReturnEmptyListIfBidResponseIsNull(t *testing.T) {
// given
bidder, _ := buildBidder()
// when
bids, errs := bidder.MakeBids(&openrtb2.BidRequest{}, nil, nil)
bids, errs := bidder.MakeBids(&openrtb2.BidRequest{}, nil, &adapters.ResponseData{
StatusCode: 204,
})
// then
if len(errs) > 0 {
t.Fatalf("Failed to make bids: %v", errs)
}
assert.Nil(t, bids)
}

func TestMakeBidsShouldReturnEmptyListIfBidResponseIsError(t *testing.T) {
// given
bidder, _ := buildBidder()
// when
bids, errs := bidder.MakeBids(&openrtb2.BidRequest{}, nil, &adapters.ResponseData{
StatusCode: 500,
})
// then
assert.Len(t, errs, 1)
assert.Nil(t, bids)
}

func TestMakeBidsShouldReturnBidWithResolvedMacros(t *testing.T) {
// given
bidder, _ := buildBidder()
Expand Down Expand Up @@ -161,6 +203,65 @@ func TestMakeBidsShouldReturnBidWithResolvedMacros(t *testing.T) {
assert.Equal(t, "adm:1", bids.Bids[0].Bid.AdM)
}

func TestMakeBidsShouldReturnBidForAllTypes(t *testing.T) {
// given
bidder, _ := buildBidder()
bid := openrtb2.Bid{
ImpID: "impId-1",
AdM: "adm:${AUCTION_PRICE}",
NURL: "nurl:${AUCTION_PRICE}",
Price: 1,
}
seatBid := openrtb2.SeatBid{Bid: []openrtb2.Bid{bid}}
resp := openrtb2.BidResponse{SeatBid: []openrtb2.SeatBid{seatBid}}
respJson, jsonErr := json.Marshal(resp)

request := openrtb2.BidRequest{
Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Video: &openrtb2.Video{}}),
}
// when
bids, errs := bidder.MakeBids(&request, nil, &adapters.ResponseData{
StatusCode: 200,
Body: respJson,
})
// then
if jsonErr != nil {
t.Fatalf("Failed to serialize test bid %v: %v", bid, jsonErr)
}
if len(errs) > 0 {
t.Fatalf("Failed to make bids: %v", errs)
}
assert.Len(t, bids.Bids, 1)

request = openrtb2.BidRequest{
Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Audio: &openrtb2.Audio{}}),
}
// when
bids, errs = bidder.MakeBids(&request, nil, &adapters.ResponseData{
StatusCode: 200,
Body: respJson,
})
// then
if len(errs) > 0 {
t.Fatalf("Failed to make bids: %v", errs)
}
assert.Len(t, bids.Bids, 1)

request = openrtb2.BidRequest{
Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Native: &openrtb2.Native{}}),
}
// when
bids, errs = bidder.MakeBids(&request, nil, &adapters.ResponseData{
StatusCode: 200,
Body: respJson,
})
// then
if len(errs) > 0 {
t.Fatalf("Failed to make bids: %v", errs)
}
assert.Len(t, bids.Bids, 1)
}

func buildBidder() (adapters.Bidder, error) {
return Builder(
openrtb_ext.BidderAlkimi,
Expand Down

0 comments on commit 87a2333

Please sign in to comment.