github.com/prebid/prebid-server/v2@v2.18.0/exchange/seat_non_bids_test.go (about)

     1  package exchange
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/openrtb/v20/openrtb2"
     7  	"github.com/prebid/prebid-server/v2/exchange/entities"
     8  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestSeatNonBidsAdd(t *testing.T) {
    13  	type fields struct {
    14  		seatNonBidsMap map[string][]openrtb_ext.NonBid
    15  	}
    16  	type args struct {
    17  		bid          *entities.PbsOrtbBid
    18  		nonBidReason int
    19  		seat         string
    20  	}
    21  	tests := []struct {
    22  		name   string
    23  		fields fields
    24  		args   args
    25  		want   map[string][]openrtb_ext.NonBid
    26  	}{
    27  		{
    28  			name:   "nil-seatNonBidsMap",
    29  			fields: fields{seatNonBidsMap: nil},
    30  			args:   args{},
    31  			want:   nil,
    32  		},
    33  		{
    34  			name:   "nil-seatNonBidsMap-with-bid-object",
    35  			fields: fields{seatNonBidsMap: nil},
    36  			args:   args{bid: &entities.PbsOrtbBid{Bid: &openrtb2.Bid{}}, seat: "bidder1"},
    37  			want:   sampleSeatNonBidMap("bidder1", 1),
    38  		},
    39  		{
    40  			name:   "multiple-nonbids-for-same-seat",
    41  			fields: fields{seatNonBidsMap: sampleSeatNonBidMap("bidder2", 1)},
    42  			args:   args{bid: &entities.PbsOrtbBid{Bid: &openrtb2.Bid{}}, seat: "bidder2"},
    43  			want:   sampleSeatNonBidMap("bidder2", 2),
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			snb := &nonBids{
    49  				seatNonBidsMap: tt.fields.seatNonBidsMap,
    50  			}
    51  			snb.addBid(tt.args.bid, tt.args.nonBidReason, tt.args.seat)
    52  			assert.Equalf(t, tt.want, snb.seatNonBidsMap, "expected seatNonBidsMap not nil")
    53  		})
    54  	}
    55  }
    56  
    57  func TestSeatNonBidsGet(t *testing.T) {
    58  	type fields struct {
    59  		snb *nonBids
    60  	}
    61  	tests := []struct {
    62  		name   string
    63  		fields fields
    64  		want   []openrtb_ext.SeatNonBid
    65  	}{
    66  		{
    67  			name:   "get-seat-nonbids",
    68  			fields: fields{&nonBids{sampleSeatNonBidMap("bidder1", 2)}},
    69  			want:   sampleSeatBids("bidder1", 2),
    70  		},
    71  		{
    72  			name:   "nil-seat-nonbids",
    73  			fields: fields{nil},
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			if got := tt.fields.snb.get(); !assert.Equal(t, tt.want, got) {
    79  				t.Errorf("seatNonBids.get() = %v, want %v", got, tt.want)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  var sampleSeatNonBidMap = func(seat string, nonBidCount int) map[string][]openrtb_ext.NonBid {
    86  	nonBids := make([]openrtb_ext.NonBid, 0)
    87  	for i := 0; i < nonBidCount; i++ {
    88  		nonBids = append(nonBids, openrtb_ext.NonBid{
    89  			Ext: openrtb_ext.NonBidExt{Prebid: openrtb_ext.ExtResponseNonBidPrebid{Bid: openrtb_ext.NonBidObject{}}},
    90  		})
    91  	}
    92  	return map[string][]openrtb_ext.NonBid{
    93  		seat: nonBids,
    94  	}
    95  }
    96  
    97  var sampleSeatBids = func(seat string, nonBidCount int) []openrtb_ext.SeatNonBid {
    98  	seatNonBids := make([]openrtb_ext.SeatNonBid, 0)
    99  	seatNonBid := openrtb_ext.SeatNonBid{
   100  		Seat:   seat,
   101  		NonBid: make([]openrtb_ext.NonBid, 0),
   102  	}
   103  	for i := 0; i < nonBidCount; i++ {
   104  		seatNonBid.NonBid = append(seatNonBid.NonBid, openrtb_ext.NonBid{
   105  			Ext: openrtb_ext.NonBidExt{Prebid: openrtb_ext.ExtResponseNonBidPrebid{Bid: openrtb_ext.NonBidObject{}}},
   106  		})
   107  	}
   108  	seatNonBids = append(seatNonBids, seatNonBid)
   109  	return seatNonBids
   110  }