// TestBiddingPacerFalse tests that the bidder will not bid when the pacer replys false to CanBid // Expected result is nil func TestBiddingPacerFalse(t *testing.T) { r := new(rtb.BidRequest) r.Imp = make([]rtb.Imp, 1) r.Imp[0] = rtb.Imp{} pacer := mocks.NewMockPacer(map[int64]bool{100: false}) campaign := mocks.NewMockCampaign(100, rtb.CpmToMicroCents(0.25), rtb.DollarsToMicroCents(1), nil) campaignsReturnedByTargeting := []rtb.Campaign{campaign} cp := mocks.NewMockCampaignProvider(campaignsReturnedByTargeting, map[int64]int64{100: rtb.DollarsToMicroCents(1)}, map[int64]error{100: nil}, nil) b := NewBidRequestBidder(r, cp, pacer, time.Now().UTC()) response, _, err := b.Bid() if err != nil { t.Fail() } if response != nil { t.FailNow() } }
// If the bid is nil, the remaining remainingDailyBudgetInMicroCents and campaign id are invalid func (b *BidRequestBidder) impressionBid(imp *rtb.Imp, userTargets []rtb.Target) (bid *rtb.Bid, remainingDailyBudgetInMicroCents int64) { targets := append(userTargets, imp.Targeting()...) campaigns := b.CampaignProvider.ReadByTargeting(rtb.CpmToMicroCents(imp.Bidfloor), targets) if len(campaigns) == 0 { return nil, 0 } // Returns nil if none of the campaigns have available budget at the time of the call // We need to double check this, in case the budget is spent by the time we've decided to bid. // To be fair, we're committed to using the result of this call. campaign, remainingDailyBudgetInMicroCents := b.highestBidder(campaigns) // Either the pacer rejected them all, or none of them had available remainingDailyBudgetInMicroCents if campaign == nil { return nil, 0 } bid = new(rtb.Bid) bid.Price = rtb.MicroCentsToCpm(campaign.BidCpmInMicroCents()) bid.Impid = imp.ID bid.Cid = strconv.FormatInt(campaign.Id(), 10) // Stub the rest of the fields bid.ID = "stub" bid.Adid = "stub" bid.Adm = "<span>stub</span>" bid.Adomain = []string{"stub.go2mobi.com"} bid.Crid = "stub" return bid, remainingDailyBudgetInMicroCents }
// TestBiddingMatchedTarget tests that the bidder will bid on the first campaign returned by the bidder that has a remaining daily budget // Expected result is a bid response that is populated by a single bid with a campaign ID matching the first campaigned returned by the provider func TestBiddingFirstCampaignOutOfFunds(t *testing.T) { r := new(rtb.BidRequest) r.Imp = make([]rtb.Imp, 1) r.Imp[0] = rtb.Imp{} pacer := mocks.NewMockPacer(map[int64]bool{100: true, 101: true}) campaign1 := mocks.NewMockCampaign(100, rtb.CpmToMicroCents(0.25), rtb.DollarsToMicroCents(1), nil) campaign2 := mocks.NewMockCampaign(101, rtb.CpmToMicroCents(0.25), rtb.DollarsToMicroCents(1), nil) campaignsReturnedByTargeting := []rtb.Campaign{campaign1, campaign2} cp := mocks.NewMockCampaignProvider(campaignsReturnedByTargeting, map[int64]int64{100: rtb.DollarsToMicroCents(1), 101: rtb.DollarsToMicroCents(1)}, map[int64]error{100: rtb.NewTransactionError("test", true), 101: nil}, nil) b := NewBidRequestBidder(r, cp, pacer, time.Now().UTC()) response, _, err := b.Bid() if err != nil { t.Fail() } if response == nil { t.FailNow() } if response.Seatbid == nil { t.FailNow() } if len(response.Seatbid) != 1 { t.Fail() } if response.Seatbid[0].Bid == nil { t.FailNow() } if len(response.Seatbid[0].Bid) != 1 { t.FailNow() } if response.Seatbid[0].Bid[0].Cid != strconv.FormatInt(campaignsReturnedByTargeting[1].Id(), 10) { t.Fail() } }
// TestBiddingMatchedTargetCorrectAmount tests that the bidder bids the correct amount // Expected result is a bid response that is populated by a single bid with the correct bid amount func TestBiddingMatchedTargetCorrectAmount(t *testing.T) { r := new(rtb.BidRequest) r.Imp = make([]rtb.Imp, 1) r.Imp[0] = rtb.Imp{} pacer := mocks.NewMockPacer(map[int64]bool{100: true}) campaign := mocks.NewMockCampaign(100, rtb.CpmToMicroCents(0.32), rtb.DollarsToMicroCents(1), nil) campaignsReturnedByTargeting := []rtb.Campaign{campaign} cp := mocks.NewMockCampaignProvider(campaignsReturnedByTargeting, map[int64]int64{100: rtb.DollarsToMicroCents(1)}, map[int64]error{100: nil}, nil) b := NewBidRequestBidder(r, cp, pacer, time.Now().UTC()) expectedBidAmount := float64(0.32) response, _, err := b.Bid() if err != nil { t.Fail() } if response == nil { t.FailNow() } if response.Seatbid == nil { t.FailNow() } if len(response.Seatbid) != 1 { t.Fail() } if response.Seatbid[0].Bid == nil { t.FailNow() } if len(response.Seatbid[0].Bid) != 1 { t.FailNow() } if response.Seatbid[0].Bid[0].Cid != strconv.FormatInt(campaignsReturnedByTargeting[0].Id(), 10) { t.Fail() } if response.Seatbid[0].Bid[0].Price != expectedBidAmount { t.Fail() } }