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

     1  package openrtb_ext
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/prebid/prebid-server/v2/util/ptrutil"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func getFlag(in bool) *bool {
    12  	return &in
    13  }
    14  
    15  func TestPriceFloorRulesGetEnforcePBS(t *testing.T) {
    16  	tests := []struct {
    17  		name   string
    18  		floors *PriceFloorRules
    19  		want   bool
    20  	}{
    21  		{
    22  			name: "EnforcePBS_Enabled",
    23  			floors: &PriceFloorRules{
    24  				Enabled: getFlag(true),
    25  				Enforcement: &PriceFloorEnforcement{
    26  					EnforcePBS: getFlag(true),
    27  				},
    28  			},
    29  			want: true,
    30  		},
    31  		{
    32  			name: "EnforcePBS_NotProvided",
    33  			floors: &PriceFloorRules{
    34  				Enabled:     getFlag(true),
    35  				Enforcement: &PriceFloorEnforcement{},
    36  			},
    37  			want: true,
    38  		},
    39  		{
    40  			name: "EnforcePBS_Disabled",
    41  			floors: &PriceFloorRules{
    42  				Enabled: getFlag(true),
    43  				Enforcement: &PriceFloorEnforcement{
    44  					EnforcePBS: getFlag(false),
    45  				},
    46  			},
    47  			want: false,
    48  		},
    49  	}
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			got := tt.floors.GetEnforcePBS()
    53  			assert.Equal(t, tt.want, got, tt.name)
    54  		})
    55  	}
    56  }
    57  
    58  func TestPriceFloorRulesGetFloorsSkippedFlag(t *testing.T) {
    59  	tests := []struct {
    60  		name   string
    61  		floors *PriceFloorRules
    62  		want   bool
    63  	}{
    64  		{
    65  			name: "Skipped_true",
    66  			floors: &PriceFloorRules{
    67  				Enabled: getFlag(true),
    68  				Skipped: getFlag(true),
    69  			},
    70  			want: true,
    71  		},
    72  		{
    73  			name: "Skipped_false",
    74  			floors: &PriceFloorRules{
    75  				Enabled: getFlag(true),
    76  				Skipped: getFlag(false),
    77  			},
    78  			want: false,
    79  		},
    80  		{
    81  			name: "Skipped_NotProvided",
    82  			floors: &PriceFloorRules{
    83  				Enabled: getFlag(true),
    84  			},
    85  			want: false,
    86  		},
    87  	}
    88  
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			got := tt.floors.GetFloorsSkippedFlag()
    92  			assert.Equal(t, tt.want, got, tt.name)
    93  		})
    94  	}
    95  }
    96  
    97  func TestPriceFloorRulesGetEnforceRate(t *testing.T) {
    98  	tests := []struct {
    99  		name   string
   100  		floors *PriceFloorRules
   101  		want   int
   102  	}{
   103  		{
   104  			name: "EnforceRate_100",
   105  			floors: &PriceFloorRules{
   106  				Enabled: getFlag(true),
   107  				Enforcement: &PriceFloorEnforcement{
   108  					EnforcePBS:  getFlag(true),
   109  					EnforceRate: 100,
   110  				},
   111  			},
   112  			want: 100,
   113  		},
   114  		{
   115  			name: "EnforceRate_0",
   116  			floors: &PriceFloorRules{
   117  				Enabled: getFlag(true),
   118  				Enforcement: &PriceFloorEnforcement{
   119  					EnforcePBS:  getFlag(true),
   120  					EnforceRate: 0,
   121  				},
   122  			},
   123  			want: 0,
   124  		},
   125  		{
   126  			name: "EnforceRate_NotProvided",
   127  			floors: &PriceFloorRules{
   128  				Enabled: getFlag(true),
   129  			},
   130  			want: 0,
   131  		},
   132  	}
   133  
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			got := tt.floors.GetEnforceRate()
   137  			assert.Equal(t, tt.want, got, tt.name)
   138  		})
   139  	}
   140  }
   141  
   142  func TestPriceFloorRulesGetEnforceDealsFlag(t *testing.T) {
   143  	tests := []struct {
   144  		name   string
   145  		floors *PriceFloorRules
   146  		want   bool
   147  	}{
   148  		{
   149  			name: "FloorDeals_true",
   150  			floors: &PriceFloorRules{
   151  				Enabled: getFlag(true),
   152  				Enforcement: &PriceFloorEnforcement{
   153  					EnforcePBS:  getFlag(true),
   154  					EnforceRate: 0,
   155  					FloorDeals:  getFlag(true),
   156  				},
   157  			},
   158  			want: true,
   159  		},
   160  		{
   161  			name: "FloorDeals_false",
   162  			floors: &PriceFloorRules{
   163  				Enabled: getFlag(true),
   164  				Enforcement: &PriceFloorEnforcement{
   165  					EnforcePBS: getFlag(true),
   166  					FloorDeals: getFlag(false),
   167  				},
   168  				Skipped: getFlag(false),
   169  			},
   170  			want: false,
   171  		},
   172  		{
   173  			name: "FloorDeals_NotProvided",
   174  			floors: &PriceFloorRules{
   175  				Enabled: getFlag(true),
   176  			},
   177  			want: false,
   178  		},
   179  	}
   180  
   181  	for _, tt := range tests {
   182  		t.Run(tt.name, func(t *testing.T) {
   183  			got := tt.floors.GetEnforceDealsFlag()
   184  			assert.Equal(t, tt.want, got, tt.name)
   185  		})
   186  	}
   187  }
   188  
   189  func TestPriceFloorRulesGetEnabled(t *testing.T) {
   190  	tests := []struct {
   191  		name   string
   192  		floors *PriceFloorRules
   193  		want   bool
   194  	}{
   195  		{
   196  			name: "Enabled_true",
   197  			floors: &PriceFloorRules{
   198  				Enabled: getFlag(true),
   199  			},
   200  			want: true,
   201  		},
   202  		{
   203  			name: "Enabled_false",
   204  			floors: &PriceFloorRules{
   205  				Enabled: getFlag(false),
   206  			},
   207  			want: false,
   208  		},
   209  		{
   210  			name:   "Enabled_NotProvided",
   211  			floors: &PriceFloorRules{},
   212  			want:   true,
   213  		},
   214  	}
   215  
   216  	for _, tt := range tests {
   217  		t.Run(tt.name, func(t *testing.T) {
   218  			got := tt.floors.GetEnabled()
   219  			assert.Equal(t, tt.want, got, tt.name)
   220  		})
   221  	}
   222  }
   223  
   224  func TestPriceFloorRulesDeepCopy(t *testing.T) {
   225  	type fields struct {
   226  		FloorMin           float64
   227  		FloorMinCur        string
   228  		SkipRate           int
   229  		Location           *PriceFloorEndpoint
   230  		Data               *PriceFloorData
   231  		Enforcement        *PriceFloorEnforcement
   232  		Enabled            *bool
   233  		Skipped            *bool
   234  		FloorProvider      string
   235  		FetchStatus        string
   236  		PriceFloorLocation string
   237  	}
   238  	tests := []struct {
   239  		name   string
   240  		fields fields
   241  	}{
   242  		{
   243  			name: "DeepCopy does not share same reference",
   244  			fields: fields{
   245  				FloorMin:    10,
   246  				FloorMinCur: "INR",
   247  				SkipRate:    0,
   248  				Location: &PriceFloorEndpoint{
   249  					URL: "https://test/floors",
   250  				},
   251  				Data: &PriceFloorData{
   252  					Currency: "INR",
   253  					SkipRate: 0,
   254  					ModelGroups: []PriceFloorModelGroup{
   255  						{
   256  							Currency:    "INR",
   257  							ModelWeight: ptrutil.ToPtr(1),
   258  							SkipRate:    0,
   259  							Values: map[string]float64{
   260  								"banner|300x600|www.website5.com": 20,
   261  								"*|*|*":                           50,
   262  							},
   263  							Schema: PriceFloorSchema{
   264  								Fields:    []string{"mediaType", "size", "domain"},
   265  								Delimiter: "|",
   266  							},
   267  						},
   268  					},
   269  				},
   270  			},
   271  		},
   272  	}
   273  	for _, tt := range tests {
   274  		t.Run(tt.name, func(t *testing.T) {
   275  			pf := &PriceFloorRules{
   276  				FloorMin:           tt.fields.FloorMin,
   277  				FloorMinCur:        tt.fields.FloorMinCur,
   278  				SkipRate:           tt.fields.SkipRate,
   279  				Location:           tt.fields.Location,
   280  				Data:               tt.fields.Data,
   281  				Enforcement:        tt.fields.Enforcement,
   282  				Enabled:            tt.fields.Enabled,
   283  				Skipped:            tt.fields.Skipped,
   284  				FloorProvider:      tt.fields.FloorProvider,
   285  				FetchStatus:        tt.fields.FetchStatus,
   286  				PriceFloorLocation: tt.fields.PriceFloorLocation,
   287  			}
   288  			got := pf.DeepCopy()
   289  			if got == pf {
   290  				t.Errorf("Rules reference are same")
   291  			}
   292  			if got.Data == pf.Data {
   293  				t.Errorf("Floor data reference is same")
   294  			}
   295  		})
   296  	}
   297  }
   298  
   299  func TestFloorRulesDeepCopy(t *testing.T) {
   300  	type fields struct {
   301  		FloorMin           float64
   302  		FloorMinCur        string
   303  		SkipRate           int
   304  		Location           *PriceFloorEndpoint
   305  		Data               *PriceFloorData
   306  		Enforcement        *PriceFloorEnforcement
   307  		Enabled            *bool
   308  		Skipped            *bool
   309  		FloorProvider      string
   310  		FetchStatus        string
   311  		PriceFloorLocation string
   312  	}
   313  	tests := []struct {
   314  		name   string
   315  		fields fields
   316  		want   *PriceFloorRules
   317  	}{
   318  		{
   319  			name: "Copy entire floors object",
   320  			fields: fields{
   321  				FloorMin:    10,
   322  				FloorMinCur: "INR",
   323  				SkipRate:    0,
   324  				Location: &PriceFloorEndpoint{
   325  					URL: "http://prebid.com/floor",
   326  				},
   327  				Data: &PriceFloorData{
   328  					Currency:            "INR",
   329  					SkipRate:            0,
   330  					FloorsSchemaVersion: 2,
   331  					ModelTimestamp:      123,
   332  					ModelGroups: []PriceFloorModelGroup{
   333  						{
   334  							Currency:     "INR",
   335  							ModelWeight:  ptrutil.ToPtr(50),
   336  							ModelVersion: "version 1",
   337  							SkipRate:     0,
   338  							Schema: PriceFloorSchema{
   339  								Fields:    []string{"a", "b", "c"},
   340  								Delimiter: "|",
   341  							},
   342  							Values: map[string]float64{
   343  								"*|*|*": 20,
   344  							},
   345  							Default: 1,
   346  						},
   347  					},
   348  					FloorProvider: "prebid",
   349  				},
   350  				Enforcement: &PriceFloorEnforcement{
   351  					EnforceJS:     ptrutil.ToPtr(true),
   352  					EnforcePBS:    ptrutil.ToPtr(true),
   353  					FloorDeals:    ptrutil.ToPtr(true),
   354  					BidAdjustment: ptrutil.ToPtr(true),
   355  					EnforceRate:   100,
   356  				},
   357  				Enabled:            ptrutil.ToPtr(true),
   358  				Skipped:            ptrutil.ToPtr(false),
   359  				FloorProvider:      "Prebid",
   360  				FetchStatus:        "success",
   361  				PriceFloorLocation: "fetch",
   362  			},
   363  			want: &PriceFloorRules{
   364  				FloorMin:    10,
   365  				FloorMinCur: "INR",
   366  				SkipRate:    0,
   367  				Location: &PriceFloorEndpoint{
   368  					URL: "http://prebid.com/floor",
   369  				},
   370  				Data: &PriceFloorData{
   371  					Currency:            "INR",
   372  					SkipRate:            0,
   373  					FloorsSchemaVersion: 2,
   374  					ModelTimestamp:      123,
   375  					ModelGroups: []PriceFloorModelGroup{
   376  						{
   377  							Currency:     "INR",
   378  							ModelWeight:  ptrutil.ToPtr(50),
   379  							ModelVersion: "version 1",
   380  							SkipRate:     0,
   381  							Schema: PriceFloorSchema{
   382  								Fields:    []string{"a", "b", "c"},
   383  								Delimiter: "|",
   384  							},
   385  							Values: map[string]float64{
   386  								"*|*|*": 20,
   387  							},
   388  							Default: 1,
   389  						},
   390  					},
   391  					FloorProvider: "prebid",
   392  				},
   393  				Enforcement: &PriceFloorEnforcement{
   394  					EnforceJS:     ptrutil.ToPtr(true),
   395  					EnforcePBS:    ptrutil.ToPtr(true),
   396  					FloorDeals:    ptrutil.ToPtr(true),
   397  					BidAdjustment: ptrutil.ToPtr(true),
   398  					EnforceRate:   100,
   399  				},
   400  				Enabled:            ptrutil.ToPtr(true),
   401  				Skipped:            ptrutil.ToPtr(false),
   402  				FloorProvider:      "Prebid",
   403  				FetchStatus:        "success",
   404  				PriceFloorLocation: "fetch",
   405  			},
   406  		},
   407  		{
   408  			name: "Copy entire floors object",
   409  			fields: fields{
   410  				FloorMin:    10,
   411  				FloorMinCur: "INR",
   412  				SkipRate:    0,
   413  				Location: &PriceFloorEndpoint{
   414  					URL: "http://prebid.com/floor",
   415  				},
   416  				Data: nil,
   417  				Enforcement: &PriceFloorEnforcement{
   418  					EnforceJS:     ptrutil.ToPtr(true),
   419  					EnforcePBS:    ptrutil.ToPtr(true),
   420  					FloorDeals:    ptrutil.ToPtr(true),
   421  					BidAdjustment: ptrutil.ToPtr(true),
   422  					EnforceRate:   100,
   423  				},
   424  				Enabled:            ptrutil.ToPtr(true),
   425  				Skipped:            ptrutil.ToPtr(false),
   426  				FloorProvider:      "Prebid",
   427  				FetchStatus:        "success",
   428  				PriceFloorLocation: "fetch",
   429  			},
   430  			want: &PriceFloorRules{
   431  				FloorMin:    10,
   432  				FloorMinCur: "INR",
   433  				SkipRate:    0,
   434  				Location: &PriceFloorEndpoint{
   435  					URL: "http://prebid.com/floor",
   436  				},
   437  				Data: nil,
   438  				Enforcement: &PriceFloorEnforcement{
   439  					EnforceJS:     ptrutil.ToPtr(true),
   440  					EnforcePBS:    ptrutil.ToPtr(true),
   441  					FloorDeals:    ptrutil.ToPtr(true),
   442  					BidAdjustment: ptrutil.ToPtr(true),
   443  					EnforceRate:   100,
   444  				},
   445  				Enabled:            ptrutil.ToPtr(true),
   446  				Skipped:            ptrutil.ToPtr(false),
   447  				FloorProvider:      "Prebid",
   448  				FetchStatus:        "success",
   449  				PriceFloorLocation: "fetch",
   450  			},
   451  		},
   452  	}
   453  	for _, tt := range tests {
   454  		t.Run(tt.name, func(t *testing.T) {
   455  			pf := &PriceFloorRules{
   456  				FloorMin:           tt.fields.FloorMin,
   457  				FloorMinCur:        tt.fields.FloorMinCur,
   458  				SkipRate:           tt.fields.SkipRate,
   459  				Location:           tt.fields.Location,
   460  				Data:               tt.fields.Data,
   461  				Enforcement:        tt.fields.Enforcement,
   462  				Enabled:            tt.fields.Enabled,
   463  				Skipped:            tt.fields.Skipped,
   464  				FloorProvider:      tt.fields.FloorProvider,
   465  				FetchStatus:        tt.fields.FetchStatus,
   466  				PriceFloorLocation: tt.fields.PriceFloorLocation,
   467  			}
   468  			if got := pf.DeepCopy(); !reflect.DeepEqual(got, tt.want) {
   469  				t.Errorf("PriceFloorRules.DeepCopy() = %v, want %v", got, tt.want)
   470  			}
   471  		})
   472  	}
   473  }
   474  
   475  func TestFloorRuleDeepCopyNil(t *testing.T) {
   476  	var priceFloorRule *PriceFloorRules
   477  	got := priceFloorRule.DeepCopy()
   478  
   479  	if got != nil {
   480  		t.Errorf("PriceFloorRules.DeepCopy() = %v, want %v", got, nil)
   481  	}
   482  }