github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/multibid_test.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/prebid/prebid-server/v2/util/ptrutil"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var maxBid1 = ptrutil.ToPtr(1)
    12  var maxBid2 = ptrutil.ToPtr(2)
    13  var maxBid3 = ptrutil.ToPtr(3)
    14  var maxBid9 = ptrutil.ToPtr(9)
    15  
    16  func TestValidateAndBuildExtMultiBid(t *testing.T) {
    17  	var maxBid0 = ptrutil.ToPtr(0)
    18  	var maxBid10 = ptrutil.ToPtr(10)
    19  
    20  	type args struct {
    21  		prebid *ExtRequestPrebid
    22  	}
    23  	tests := []struct {
    24  		name                   string
    25  		args                   args
    26  		wantValidatedMultiBids []*ExtMultiBid
    27  		wantErrs               []error
    28  	}{
    29  		{
    30  			name:                   "prebid nil",
    31  			wantValidatedMultiBids: nil,
    32  			wantErrs:               nil,
    33  		},
    34  		{
    35  			name: "prebid.MultiBid nil",
    36  			args: args{
    37  				prebid: &ExtRequestPrebid{MultiBid: nil},
    38  			},
    39  			wantValidatedMultiBids: nil,
    40  			wantErrs:               nil,
    41  		},
    42  		{
    43  			name: "prebid.MultiBid empty",
    44  			args: args{
    45  				prebid: &ExtRequestPrebid{MultiBid: make([]*ExtMultiBid, 0)},
    46  			},
    47  			wantValidatedMultiBids: nil,
    48  			wantErrs:               nil,
    49  		},
    50  		{
    51  			name: "MaxBids not defined",
    52  			args: args{
    53  				prebid: &ExtRequestPrebid{MultiBid: []*ExtMultiBid{{Bidder: "pubmatic"}}},
    54  			},
    55  			wantValidatedMultiBids: nil,
    56  			wantErrs:               []error{fmt.Errorf("maxBids not defined for %v", ExtMultiBid{Bidder: "pubmatic"})},
    57  		},
    58  		{
    59  			name: "Bidder or Bidders not defined",
    60  			args: args{
    61  				prebid: &ExtRequestPrebid{MultiBid: []*ExtMultiBid{{MaxBids: maxBid2}}},
    62  			},
    63  			wantValidatedMultiBids: nil,
    64  			wantErrs:               []error{fmt.Errorf("bidder(s) not specified for %v", ExtMultiBid{MaxBids: maxBid2})},
    65  		},
    66  		{
    67  			name: "Input bidder is already present in multibid",
    68  			args: args{
    69  				prebid: &ExtRequestPrebid{
    70  					MultiBid: []*ExtMultiBid{
    71  						{Bidder: "pubmatic", MaxBids: maxBid2, TargetBidderCodePrefix: "pm"},
    72  						{Bidder: "pubmatic", MaxBids: maxBid3, TargetBidderCodePrefix: "pubm"},
    73  					},
    74  				},
    75  			},
    76  			wantErrs:               []error{fmt.Errorf("multiBid already defined for pubmatic, ignoring this instance %v", ExtMultiBid{MaxBids: maxBid3, Bidder: "pubmatic", TargetBidderCodePrefix: "pubm"})},
    77  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "pubmatic", MaxBids: maxBid2, TargetBidderCodePrefix: "pm"}},
    78  		},
    79  		{
    80  			name: "Bidder and Bidders both defined (only Bidder will be used)",
    81  			args: args{
    82  				prebid: &ExtRequestPrebid{
    83  					MultiBid: []*ExtMultiBid{
    84  						{Bidder: "pubmatic", MaxBids: maxBid2, TargetBidderCodePrefix: "pm"},
    85  						{MaxBids: maxBid2, Bidder: "appnexus", Bidders: []string{"rubicon"}, TargetBidderCodePrefix: "appN"},
    86  					},
    87  				},
    88  			},
    89  			wantErrs:               []error{fmt.Errorf("ignoring bidders from %v", ExtMultiBid{MaxBids: maxBid2, Bidder: "appnexus", Bidders: []string{"rubicon"}, TargetBidderCodePrefix: "appN"})},
    90  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "pubmatic", MaxBids: maxBid2, TargetBidderCodePrefix: "pm"}, {Bidder: "appnexus", MaxBids: maxBid2, TargetBidderCodePrefix: "appN"}},
    91  		},
    92  		{
    93  			name: "Only Bidder defined",
    94  			args: args{
    95  				prebid: &ExtRequestPrebid{
    96  					MultiBid: []*ExtMultiBid{
    97  						{MaxBids: maxBid2, Bidder: "appnexus", TargetBidderCodePrefix: "appN"},
    98  					},
    99  				},
   100  			},
   101  			wantErrs:               nil,
   102  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "appnexus", MaxBids: maxBid2, TargetBidderCodePrefix: "appN"}},
   103  		},
   104  		{
   105  			name: "Only Bidders defined",
   106  			args: args{
   107  				prebid: &ExtRequestPrebid{
   108  					MultiBid: []*ExtMultiBid{
   109  						{MaxBids: maxBid2, Bidders: []string{"appnexus", "someBidder"}},
   110  					},
   111  				},
   112  			},
   113  			wantErrs:               nil,
   114  			wantValidatedMultiBids: []*ExtMultiBid{{Bidders: []string{"appnexus", "someBidder"}, MaxBids: maxBid2}},
   115  		},
   116  		{
   117  			name: "Bidders defined where a bidder is already present in multibid",
   118  			args: args{
   119  				prebid: &ExtRequestPrebid{
   120  					MultiBid: []*ExtMultiBid{
   121  						{Bidder: "pubmatic", MaxBids: maxBid9, TargetBidderCodePrefix: "pm"},
   122  						{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}},
   123  					},
   124  				},
   125  			},
   126  			wantErrs:               []error{fmt.Errorf("multiBid already defined for pubmatic, ignoring this instance %v", ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}})},
   127  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "pubmatic", MaxBids: maxBid9, TargetBidderCodePrefix: "pm"}, {Bidders: []string{"appnexus"}, MaxBids: maxBid2}},
   128  		},
   129  		{
   130  			name: "Bidders defined where all the bidders are already present in multibid",
   131  			args: args{
   132  				prebid: &ExtRequestPrebid{
   133  					MultiBid: []*ExtMultiBid{
   134  						{Bidder: "pubmatic", MaxBids: maxBid9, TargetBidderCodePrefix: "pm"},
   135  						{Bidders: []string{"appnexus"}, MaxBids: maxBid3},
   136  						{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}},
   137  					},
   138  				},
   139  			},
   140  			wantErrs: []error{
   141  				fmt.Errorf("multiBid already defined for appnexus, ignoring this instance %v", ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}}),
   142  				fmt.Errorf("multiBid already defined for pubmatic, ignoring this instance %v", ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}}),
   143  			},
   144  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "pubmatic", MaxBids: maxBid9, TargetBidderCodePrefix: "pm"}, {Bidders: []string{"appnexus"}, MaxBids: maxBid3}},
   145  		},
   146  		{
   147  			name: "Bidders defined along with TargetBidderCodePrefix",
   148  			args: args{
   149  				prebid: &ExtRequestPrebid{
   150  					MultiBid: []*ExtMultiBid{
   151  						{MaxBids: maxBid2, Bidders: []string{"appnexus"}, TargetBidderCodePrefix: "appN"},
   152  					},
   153  				},
   154  			},
   155  			wantErrs:               []error{fmt.Errorf("ignoring targetbiddercodeprefix for %v", ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus"}, TargetBidderCodePrefix: "appN"})},
   156  			wantValidatedMultiBids: []*ExtMultiBid{{Bidders: []string{"appnexus"}, MaxBids: maxBid2}},
   157  		},
   158  		{
   159  			name: "MaxBids defined below minimum limit",
   160  			args: args{
   161  				prebid: &ExtRequestPrebid{
   162  					MultiBid: []*ExtMultiBid{
   163  						{MaxBids: maxBid0, Bidder: "appnexus", TargetBidderCodePrefix: "appN"},
   164  					},
   165  				},
   166  			},
   167  			wantErrs:               []error{fmt.Errorf("invalid maxBids value, using minimum 1 limit for %v", ExtMultiBid{MaxBids: maxBid0, Bidder: "appnexus", TargetBidderCodePrefix: "appN"})},
   168  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "appnexus", TargetBidderCodePrefix: "appN", MaxBids: maxBid1}},
   169  		},
   170  		{
   171  			name: "MaxBids defined over max limit",
   172  			args: args{
   173  				prebid: &ExtRequestPrebid{
   174  					MultiBid: []*ExtMultiBid{
   175  						{MaxBids: maxBid10, Bidder: "appnexus", TargetBidderCodePrefix: "appN"},
   176  					},
   177  				},
   178  			},
   179  			wantErrs:               []error{fmt.Errorf("invalid maxBids value, using maximum 9 limit for %v", ExtMultiBid{MaxBids: maxBid10, Bidder: "appnexus", TargetBidderCodePrefix: "appN"})},
   180  			wantValidatedMultiBids: []*ExtMultiBid{{Bidder: "appnexus", TargetBidderCodePrefix: "appN", MaxBids: maxBid9}},
   181  		},
   182  	}
   183  	for _, tt := range tests {
   184  		t.Run(tt.name, func(t *testing.T) {
   185  			gotValidatedMultiBids, gotErrs := ValidateAndBuildExtMultiBid(tt.args.prebid)
   186  			assert.Equal(t, tt.wantValidatedMultiBids, gotValidatedMultiBids)
   187  			assert.Equal(t, tt.wantErrs, gotErrs)
   188  		})
   189  	}
   190  }
   191  
   192  func Test_addMultiBid(t *testing.T) {
   193  	var maxBid0 = ptrutil.ToPtr(0)
   194  	var maxBid10 = ptrutil.ToPtr(10)
   195  
   196  	type args struct {
   197  		multiBidMap map[string]struct{}
   198  		multiBid    *ExtMultiBid
   199  	}
   200  	tests := []struct {
   201  		name          string
   202  		args          args
   203  		wantErrs      []error
   204  		wantMultiBids []*ExtMultiBid
   205  	}{
   206  		{
   207  			name: "MaxBids not defined",
   208  			args: args{
   209  				multiBidMap: map[string]struct{}{},
   210  				multiBid:    &ExtMultiBid{},
   211  			},
   212  			wantErrs:      []error{fmt.Errorf("maxBids not defined for %v", ExtMultiBid{})},
   213  			wantMultiBids: nil,
   214  		},
   215  		{
   216  			name: "Bidder or Bidders not defined",
   217  			args: args{
   218  				multiBidMap: map[string]struct{}{},
   219  				multiBid:    &ExtMultiBid{MaxBids: maxBid2},
   220  			},
   221  			wantErrs:      []error{fmt.Errorf("bidder(s) not specified for %v", ExtMultiBid{MaxBids: maxBid2})},
   222  			wantMultiBids: nil,
   223  		},
   224  		{
   225  			name: "Input bidder is already present in multibid",
   226  			args: args{
   227  				multiBidMap: map[string]struct{}{"pubmatic": {}},
   228  				multiBid:    &ExtMultiBid{MaxBids: maxBid2, Bidder: "pubmatic"},
   229  			},
   230  			wantErrs:      []error{fmt.Errorf("multiBid already defined for pubmatic, ignoring this instance %v", ExtMultiBid{MaxBids: maxBid2, Bidder: "pubmatic"})},
   231  			wantMultiBids: nil,
   232  		},
   233  		{
   234  			name: "Bidder and Bidders both defined (only Bidder will be used)",
   235  			args: args{
   236  				multiBidMap: map[string]struct{}{"pubmatic": {}},
   237  				multiBid:    &ExtMultiBid{MaxBids: maxBid2, Bidder: "appnexus", Bidders: []string{"rubicon"}, TargetBidderCodePrefix: "appN"},
   238  			},
   239  			wantErrs:      []error{fmt.Errorf("ignoring bidders from %v", ExtMultiBid{MaxBids: maxBid2, Bidder: "appnexus", Bidders: []string{"rubicon"}, TargetBidderCodePrefix: "appN"})},
   240  			wantMultiBids: []*ExtMultiBid{{Bidder: "appnexus", MaxBids: maxBid2, TargetBidderCodePrefix: "appN"}},
   241  		},
   242  		{
   243  			name: "Only Bidder defined",
   244  			args: args{
   245  				multiBidMap: map[string]struct{}{},
   246  				multiBid:    &ExtMultiBid{MaxBids: maxBid2, Bidder: "appnexus", TargetBidderCodePrefix: "appN"},
   247  			},
   248  			wantErrs:      []error{},
   249  			wantMultiBids: []*ExtMultiBid{{Bidder: "appnexus", MaxBids: maxBid2, TargetBidderCodePrefix: "appN"}},
   250  		},
   251  		{
   252  			name: "Only Bidders defined",
   253  			args: args{
   254  				multiBidMap: map[string]struct{}{},
   255  				multiBid:    &ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus", "someBidder"}},
   256  			},
   257  			wantErrs:      []error{},
   258  			wantMultiBids: []*ExtMultiBid{{Bidders: []string{"appnexus", "someBidder"}, MaxBids: maxBid2}},
   259  		},
   260  		{
   261  			name: "Bidders defined where a bidder is already present in multibid",
   262  			args: args{
   263  				multiBidMap: map[string]struct{}{"pubmatic": {}},
   264  				multiBid:    &ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}},
   265  			},
   266  			wantErrs:      []error{fmt.Errorf("multiBid already defined for pubmatic, ignoring this instance %v", ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus", "pubmatic"}})},
   267  			wantMultiBids: []*ExtMultiBid{{Bidders: []string{"appnexus"}, MaxBids: maxBid2}},
   268  		},
   269  		{
   270  			name: "Bidders defined along with TargetBidderCodePrefix",
   271  			args: args{
   272  				multiBidMap: map[string]struct{}{},
   273  				multiBid:    &ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus"}, TargetBidderCodePrefix: "appN"},
   274  			},
   275  			wantErrs:      []error{fmt.Errorf("ignoring targetbiddercodeprefix for %v", ExtMultiBid{MaxBids: maxBid2, Bidders: []string{"appnexus"}, TargetBidderCodePrefix: "appN"})},
   276  			wantMultiBids: []*ExtMultiBid{{Bidders: []string{"appnexus"}, MaxBids: maxBid2}},
   277  		},
   278  		{
   279  			name: "MaxBids defined below minimum limit",
   280  			args: args{
   281  				multiBidMap: map[string]struct{}{},
   282  				multiBid:    &ExtMultiBid{MaxBids: maxBid0, Bidder: "appnexus", TargetBidderCodePrefix: "appN"},
   283  			},
   284  			wantErrs:      []error{fmt.Errorf("invalid maxBids value, using minimum 1 limit for %v", ExtMultiBid{MaxBids: maxBid0, Bidder: "appnexus", TargetBidderCodePrefix: "appN"})},
   285  			wantMultiBids: []*ExtMultiBid{{Bidder: "appnexus", TargetBidderCodePrefix: "appN", MaxBids: maxBid1}},
   286  		},
   287  		{
   288  			name: "MaxBids defined over max limit",
   289  			args: args{
   290  				multiBidMap: map[string]struct{}{},
   291  				multiBid:    &ExtMultiBid{MaxBids: maxBid10, Bidder: "appnexus", TargetBidderCodePrefix: "appN"},
   292  			},
   293  			wantErrs:      []error{fmt.Errorf("invalid maxBids value, using maximum 9 limit for %v", ExtMultiBid{MaxBids: maxBid10, Bidder: "appnexus", TargetBidderCodePrefix: "appN"})},
   294  			wantMultiBids: []*ExtMultiBid{{Bidder: "appnexus", TargetBidderCodePrefix: "appN", MaxBids: maxBid9}},
   295  		},
   296  	}
   297  	for _, tt := range tests {
   298  		t.Run(tt.name, func(t *testing.T) {
   299  			gotMultiBids, gotErrs := addMultiBid(tt.args.multiBidMap, tt.args.multiBid)
   300  			assert.Equal(t, tt.wantErrs, gotErrs)
   301  			assert.Equal(t, tt.wantMultiBids, gotMultiBids)
   302  		})
   303  	}
   304  }