github.com/prebid/prebid-server/v2@v2.18.0/bidadjustment/validate_test.go (about)

     1  package bidadjustment
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestValidate(t *testing.T) {
    11  	testCases := []struct {
    12  		name                string
    13  		givenBidAdjustments *openrtb_ext.ExtRequestPrebidBidAdjustments
    14  		expected            bool
    15  	}{
    16  		{
    17  			name: "OneAdjustmentValid",
    18  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
    19  				MediaType: openrtb_ext.MediaType{
    20  					Banner: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    21  						"bidderA": {
    22  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: 1.1}},
    23  						},
    24  					},
    25  				},
    26  			},
    27  			expected: true,
    28  		},
    29  		{
    30  			name: "MultipleAdjustmentsValid",
    31  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
    32  				MediaType: openrtb_ext.MediaType{
    33  					Audio: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    34  						"bidderA": {
    35  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: 1.1}},
    36  						},
    37  					},
    38  					VideoInstream: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    39  						"bidderA": {
    40  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeStatic, Value: 3.0, Currency: "USD"}},
    41  						},
    42  					},
    43  				},
    44  			},
    45  			expected: true,
    46  		},
    47  		{
    48  			name: "MixOfValidandInvalid",
    49  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
    50  				MediaType: openrtb_ext.MediaType{
    51  					Native: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    52  						"bidderA": {
    53  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: 1.1}},
    54  						},
    55  					},
    56  					VideoOutstream: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    57  						"bidderA": {
    58  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeCPM, Value: 3.0, Currency: ""}},
    59  						},
    60  					},
    61  				},
    62  			},
    63  			expected: false,
    64  		},
    65  		{
    66  			name: "WildCardInvalid",
    67  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
    68  				MediaType: openrtb_ext.MediaType{
    69  					WildCard: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    70  						"bidderA": {
    71  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeCPM, Value: -1.1, Currency: "USD"}},
    72  						},
    73  					},
    74  				},
    75  			},
    76  			expected: false,
    77  		},
    78  		{
    79  			name: "AudioInvalid",
    80  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
    81  				MediaType: openrtb_ext.MediaType{
    82  					Audio: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    83  						"bidderA": {
    84  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeStatic, Value: 5.0, Currency: ""}},
    85  						},
    86  					},
    87  				},
    88  			},
    89  			expected: false,
    90  		},
    91  		{
    92  			name: "NativeInvalid",
    93  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
    94  				MediaType: openrtb_ext.MediaType{
    95  					Native: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
    96  						"bidderA": {
    97  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeCPM, Value: -1.1, Currency: "USD"}},
    98  						},
    99  					},
   100  				},
   101  			},
   102  			expected: false,
   103  		},
   104  		{
   105  			name: "BannerInvalid",
   106  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
   107  				MediaType: openrtb_ext.MediaType{
   108  					Banner: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   109  						"bidderA": {
   110  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: 150}},
   111  						},
   112  					},
   113  				},
   114  			},
   115  			expected: false,
   116  		},
   117  		{
   118  			name: "InstreamInvalid",
   119  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{
   120  				MediaType: openrtb_ext.MediaType{
   121  					VideoInstream: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   122  						"bidderA": {
   123  							"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: 150}},
   124  						},
   125  					},
   126  				},
   127  			},
   128  			expected: false,
   129  		},
   130  		{
   131  			name:                "EmptyBidAdjustments",
   132  			givenBidAdjustments: &openrtb_ext.ExtRequestPrebidBidAdjustments{},
   133  			expected:            true,
   134  		},
   135  		{
   136  			name:                "NilBidAdjustments",
   137  			givenBidAdjustments: nil,
   138  			expected:            true,
   139  		},
   140  	}
   141  
   142  	for _, test := range testCases {
   143  		t.Run(test.name, func(t *testing.T) {
   144  			actual := Validate(test.givenBidAdjustments)
   145  			assert.Equal(t, test.expected, actual)
   146  		})
   147  	}
   148  }
   149  
   150  func TestValidateForMediaType(t *testing.T) {
   151  	testCases := []struct {
   152  		name        string
   153  		givenBidAdj map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID
   154  		expected    bool
   155  	}{
   156  		{
   157  			name: "OneAdjustmentValid",
   158  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   159  				"bidderA": {
   160  					"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: 1.1}},
   161  				},
   162  			},
   163  			expected: true,
   164  		},
   165  		{
   166  			name: "OneAdjustmentInvalid",
   167  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   168  				"bidderA": {
   169  					"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeMultiplier, Value: -1.1}},
   170  				},
   171  			},
   172  			expected: false,
   173  		},
   174  		{
   175  			name: "MultipleAdjustmentsValid",
   176  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   177  				"bidderA": {
   178  					"dealId": []openrtb_ext.Adjustment{
   179  						{Type: AdjustmentTypeMultiplier, Value: 1.1},
   180  						{Type: AdjustmentTypeStatic, Value: 3.0, Currency: "USD"},
   181  					},
   182  				},
   183  			},
   184  			expected: true,
   185  		},
   186  		{
   187  			name: "MultipleAdjustmentsInvalid",
   188  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   189  				"bidderA": {
   190  					"dealId": []openrtb_ext.Adjustment{
   191  						{Type: AdjustmentTypeMultiplier, Value: -1.1},
   192  						{Type: AdjustmentTypeCPM, Value: -3.0, Currency: "USD"},
   193  					},
   194  				},
   195  			},
   196  			expected: false,
   197  		},
   198  		{
   199  			name: "MultipleDealIdsValid",
   200  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   201  				"bidderA": {
   202  					"dealId": []openrtb_ext.Adjustment{
   203  						{Type: AdjustmentTypeStatic, Value: 3.0, Currency: "USD"},
   204  					},
   205  					"diffDealId": []openrtb_ext.Adjustment{
   206  						{Type: AdjustmentTypeMultiplier, Value: 1.1},
   207  					},
   208  				},
   209  			},
   210  			expected: true,
   211  		},
   212  		{
   213  			name: "MultipleBiddersValid",
   214  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   215  				"bidderA": {
   216  					"dealId": []openrtb_ext.Adjustment{
   217  						{Type: AdjustmentTypeStatic, Value: 5.0, Currency: "USD"},
   218  					},
   219  				},
   220  				"bidderB": {
   221  					"dealId": []openrtb_ext.Adjustment{
   222  						{Type: AdjustmentTypeCPM, Value: 3.0, Currency: "USD"},
   223  					},
   224  				},
   225  			},
   226  			expected: true,
   227  		},
   228  		{
   229  			name:        "NilBidAdj",
   230  			givenBidAdj: nil,
   231  			expected:    true,
   232  		},
   233  		{
   234  			name: "NilBidderToAdjustmentsByDealID",
   235  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   236  				"bidderA": nil,
   237  			},
   238  			expected: false,
   239  		},
   240  		{
   241  			name: "NilDealIdToAdjustments",
   242  			givenBidAdj: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
   243  				"bidderA": {
   244  					"dealId": nil,
   245  				},
   246  			},
   247  			expected: false,
   248  		},
   249  	}
   250  
   251  	for _, test := range testCases {
   252  		t.Run(test.name, func(t *testing.T) {
   253  			actual := validateForMediaType(test.givenBidAdj)
   254  			assert.Equal(t, test.expected, actual)
   255  		})
   256  	}
   257  }
   258  
   259  func TestValidateAdjustment(t *testing.T) {
   260  	testCases := []struct {
   261  		name            string
   262  		givenAdjustment openrtb_ext.Adjustment
   263  		expected        bool
   264  	}{
   265  		{
   266  			name: "ValidCpm",
   267  			givenAdjustment: openrtb_ext.Adjustment{
   268  				Type:     AdjustmentTypeCPM,
   269  				Value:    5.0,
   270  				Currency: "USD",
   271  			},
   272  			expected: true,
   273  		},
   274  		{
   275  			name: "ValidMultiplier",
   276  			givenAdjustment: openrtb_ext.Adjustment{
   277  				Type:  AdjustmentTypeMultiplier,
   278  				Value: 2.0,
   279  			},
   280  			expected: true,
   281  		},
   282  		{
   283  			name: "ValidStatic",
   284  			givenAdjustment: openrtb_ext.Adjustment{
   285  				Type:     AdjustmentTypeStatic,
   286  				Value:    3.0,
   287  				Currency: "USD",
   288  			},
   289  			expected: true,
   290  		},
   291  		{
   292  			name: "InvalidCpm",
   293  			givenAdjustment: openrtb_ext.Adjustment{
   294  				Type:     AdjustmentTypeCPM,
   295  				Value:    5.0,
   296  				Currency: "",
   297  			},
   298  			expected: false,
   299  		},
   300  		{
   301  			name: "InvalidMultiplier",
   302  			givenAdjustment: openrtb_ext.Adjustment{
   303  				Type:  AdjustmentTypeMultiplier,
   304  				Value: 200,
   305  			},
   306  			expected: false,
   307  		},
   308  		{
   309  			name: "InvalidStatic",
   310  			givenAdjustment: openrtb_ext.Adjustment{
   311  				Type:     AdjustmentTypeStatic,
   312  				Value:    -3.0,
   313  				Currency: "USD",
   314  			},
   315  			expected: false,
   316  		},
   317  		{
   318  			name: "InvalidAdjType",
   319  			givenAdjustment: openrtb_ext.Adjustment{
   320  				Type:  "Invalid",
   321  				Value: 1.0,
   322  			},
   323  			expected: false,
   324  		},
   325  		{
   326  			name:            "EmptyAdjustment",
   327  			givenAdjustment: openrtb_ext.Adjustment{},
   328  			expected:        false,
   329  		},
   330  	}
   331  
   332  	for _, test := range testCases {
   333  		t.Run(test.name, func(t *testing.T) {
   334  			actual := validateAdjustment(test.givenAdjustment)
   335  			assert.Equal(t, test.expected, actual)
   336  		})
   337  	}
   338  }