forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_granularity_test.go
201 lines (194 loc) · 7.72 KB
/
price_granularity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package exchange
import (
"encoding/json"
"math"
"testing"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/util/ptrutil"
"github.com/stretchr/testify/assert"
)
func TestGetPriceBucketString(t *testing.T) {
low, _ := openrtb_ext.NewPriceGranularityFromLegacyID("low")
medium, _ := openrtb_ext.NewPriceGranularityFromLegacyID("medium")
high, _ := openrtb_ext.NewPriceGranularityFromLegacyID("high")
auto, _ := openrtb_ext.NewPriceGranularityFromLegacyID("auto")
dense, _ := openrtb_ext.NewPriceGranularityFromLegacyID("dense")
custom1 := openrtb_ext.PriceGranularity{
Precision: ptrutil.ToPtr(2),
Ranges: []openrtb_ext.GranularityRange{
{
Min: 0.0,
Max: 5.0,
Increment: 0.03,
},
{
Min: 5.0,
Max: 10.0,
Increment: 0.1,
},
},
}
custom2 := openrtb_ext.PriceGranularity{
Precision: ptrutil.ToPtr(2),
Ranges: []openrtb_ext.GranularityRange{
{
Min: 0.0,
Max: 1.5,
Increment: 1.0,
},
{
Min: 1.5,
Max: 10.0,
Increment: 1.2,
},
},
}
// Define test cases
type aTest struct {
granularityId string
targetData targetData
expectedPriceBucket string
}
testGroups := []struct {
groupDesc string
bid openrtb2.Bid
testCases []aTest
}{
{
groupDesc: "cpm below the max in every price bucket",
bid: openrtb2.Bid{Price: 1.87},
testCases: []aTest{
{"low", targetData{priceGranularity: low}, "1.50"},
{"medium", targetData{priceGranularity: medium}, "1.80"},
{"high", targetData{priceGranularity: high}, "1.87"},
{"auto", targetData{priceGranularity: auto}, "1.85"},
{"dense", targetData{priceGranularity: dense}, "1.87"},
{"custom1", targetData{priceGranularity: custom1}, "1.86"},
{"custom2", targetData{priceGranularity: custom2}, "1.50"},
},
},
{
groupDesc: "cpm above the max in low price bucket",
bid: openrtb2.Bid{Price: 5.72},
testCases: []aTest{
{"low", targetData{priceGranularity: low}, "5.00"},
{"medium", targetData{priceGranularity: medium}, "5.70"},
{"high", targetData{priceGranularity: high}, "5.72"},
{"auto", targetData{priceGranularity: auto}, "5.70"},
{"dense", targetData{priceGranularity: dense}, "5.70"},
{"custom1", targetData{priceGranularity: custom1}, "5.70"},
{"custom2", targetData{priceGranularity: custom2}, "5.10"},
},
},
{
groupDesc: "media type price granularity for bid type video",
bid: openrtb2.Bid{Price: 5.0, MType: openrtb2.MarkupVideo},
testCases: []aTest{
{"medium", targetData{priceGranularity: medium}, "5.00"},
{"video-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Video: &custom2}}, "3.90"},
{"banner-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Banner: &custom2}}, "5.00"},
},
},
{
groupDesc: "media type price granularity for bid type banner",
bid: openrtb2.Bid{Price: 5.0, MType: openrtb2.MarkupBanner},
testCases: []aTest{
{"medium", targetData{priceGranularity: medium}, "5.00"},
{"video-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Video: &custom2}}, "5.00"},
{"banner-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Banner: &custom2}}, "3.90"},
},
},
{
groupDesc: "media type price granularity for bid type native",
bid: openrtb2.Bid{Price: 5.0, MType: openrtb2.MarkupNative},
testCases: []aTest{
{"medium", targetData{priceGranularity: medium}, "5.00"},
{"video-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Video: &custom2}}, "5.00"},
{"native-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Native: &custom2}}, "3.90"},
},
},
{
groupDesc: "media type price granularity set but bid type incorrect",
bid: openrtb2.Bid{Price: 5.0, Ext: json.RawMessage(`{`)},
testCases: []aTest{
{"medium", targetData{priceGranularity: medium}, "5.00"},
{"video-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Video: &custom2}}, "5.00"},
{"banner-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Banner: &custom2}}, "5.00"},
{"native-custom2", targetData{priceGranularity: medium, mediaTypePriceGranularity: openrtb_ext.MediaTypePriceGranularity{Native: &custom2}}, "5.00"},
},
},
{
groupDesc: "cpm equal the max for custom granularity",
bid: openrtb2.Bid{Price: 10},
testCases: []aTest{
{"custom1", targetData{priceGranularity: custom1}, "10.00"},
{"custom2", targetData{priceGranularity: custom2}, "9.90"},
},
},
{
groupDesc: "Precision value corner cases",
bid: openrtb2.Bid{Price: 1.876},
testCases: []aTest{
{
"Negative precision defaults to number of digits already in CPM float",
targetData{priceGranularity: openrtb_ext.PriceGranularity{Precision: ptrutil.ToPtr(-1), Ranges: []openrtb_ext.GranularityRange{{Max: 5, Increment: 0.05}}}},
"1.85",
},
{
"Precision value equals zero, we expect to round up to the nearest integer",
targetData{priceGranularity: openrtb_ext.PriceGranularity{Precision: ptrutil.ToPtr(0), Ranges: []openrtb_ext.GranularityRange{{Max: 5, Increment: 0.05}}}},
"2",
},
{
"Largest precision value PBS supports 15",
targetData{priceGranularity: openrtb_ext.PriceGranularity{Precision: ptrutil.ToPtr(15), Ranges: []openrtb_ext.GranularityRange{{Max: 5, Increment: 0.05}}}},
"1.850000000000000",
},
},
},
{
groupDesc: "Increment value corner cases",
bid: openrtb2.Bid{Price: 1.876},
testCases: []aTest{
{
"Negative increment, return empty string",
targetData{priceGranularity: openrtb_ext.PriceGranularity{Precision: ptrutil.ToPtr(2), Ranges: []openrtb_ext.GranularityRange{{Max: 5, Increment: -0.05}}}},
"",
},
{
"Zero increment, return empty string",
targetData{priceGranularity: openrtb_ext.PriceGranularity{Precision: ptrutil.ToPtr(2), Ranges: []openrtb_ext.GranularityRange{{Max: 5}}}},
"",
},
{
"Increment value is greater than CPM itself, return zero float value",
targetData{priceGranularity: openrtb_ext.PriceGranularity{Precision: ptrutil.ToPtr(2), Ranges: []openrtb_ext.GranularityRange{{Max: 5, Increment: 1.877}}}},
"0.00",
},
},
},
{
groupDesc: "Negative Cpm, return empty string since it does not belong into any range",
bid: openrtb2.Bid{Price: -1.876},
testCases: []aTest{{"low", targetData{priceGranularity: low}, ""}},
},
{
groupDesc: "Zero value Cpm, return the same, only in string format",
bid: openrtb2.Bid{Price: 0},
testCases: []aTest{{"low", targetData{priceGranularity: low}, "0.00"}},
},
{
groupDesc: "Large Cpm, return bucket Max",
bid: openrtb2.Bid{Price: math.MaxFloat64},
testCases: []aTest{{"low", targetData{priceGranularity: low}, "5.00"}},
},
}
for _, testGroup := range testGroups {
for i, test := range testGroup.testCases {
var priceBucket string
assert.NotPanics(t, func() { priceBucket = GetPriceBucket(testGroup.bid, test.targetData) }, "Group: %s Granularity: %d", testGroup.groupDesc, i)
assert.Equal(t, test.expectedPriceBucket, priceBucket, "Group: %s Granularity: %s :: Expected %s, got %s from %f", testGroup.groupDesc, test.granularityId, test.expectedPriceBucket, priceBucket, testGroup.bid.Price)
}
}
}