forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpmBucketManager_spec.js
195 lines (179 loc) · 6.1 KB
/
cpmBucketManager_spec.js
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
import { expect } from 'chai';
import {getPriceBucketString, isValidPriceConfig} from 'src/cpmBucketManager.js';
let cpmFixtures = require('test/fixtures/cpmInputsOutputs.json');
describe('cpmBucketManager', function () {
it('getPriceBucketString function generates the correct price strings', function () {
let input = cpmFixtures.cpmInputs;
for (let i = 0; i < input.length; i++) {
let output = getPriceBucketString(input[i]);
let jsonOutput = JSON.stringify(output);
expect(jsonOutput).to.deep.equal(JSON.stringify(cpmFixtures.priceStringOutputs[i]));
}
});
it('gets the correct custom bucket strings', function () {
let cpm = 16.50908;
let customConfig = {
'buckets': [{
'precision': 4,
'max': 3,
'increment': 0.01,
},
{
'precision': 4,
'max': 18,
'increment': 0.05,
'cap': true
}
]
};
let expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"16.5000"}';
let output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets the correct custom bucket strings with irregular increment', function () {
let cpm = 14.50908;
let customConfig = {
'buckets': [{
'precision': 4,
'max': 4,
'increment': 0.01,
},
{
'precision': 4,
'max': 18,
'increment': 0.3,
'cap': true
}
]
};
let expected = '{"low":"5.00","med":"14.50","high":"14.50","auto":"14.50","dense":"14.50","custom":"14.5000"}';
let output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets the correct custom bucket strings in non-USD currency', function () {
let cpm = 16.50908 * 110.49;
let customConfig = {
'buckets': [{
'precision': 4,
'max': 3,
'increment': 0.01,
},
{
'precision': 4,
'max': 18,
'increment': 0.05,
'cap': true
}
]
};
let expected = '{"low":"552.45","med":"1823.09","high":"1823.09","auto":"1823.09","dense":"1823.09","custom":"1823.0850"}';
let output = getPriceBucketString(cpm, customConfig, 110.49);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets the correct custom bucket strings with specific cpms that round oddly with certain increments', function () {
let customConfig = {
'buckets': [{
'precision': 4,
'max': 4,
'increment': 0.10,
}]
};
let cpm = 2.21;
let expected = '{"low":"2.00","med":"2.20","high":"2.21","auto":"2.20","dense":"2.21","custom":"2.2000"}';
let output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
cpm = 3.15;
expected = '{"low":"3.00","med":"3.10","high":"3.15","auto":"3.15","dense":"3.15","custom":"3.1000"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
customConfig = {
'buckets': [{
'precision': 3,
'max': 6,
'increment': 0.08,
}]
};
cpm = 4.89;
expected = '{"low":"4.50","med":"4.80","high":"4.89","auto":"4.85","dense":"4.85","custom":"4.880"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
customConfig = {
'buckets': [{
'precision': 3,
'max': 6,
'increment': 0.05,
}]
};
cpm = 2.98;
expected = '{"low":"2.50","med":"2.90","high":"2.98","auto":"2.95","dense":"2.98","custom":"2.950"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
cpm = 2.99;
expected = '{"low":"2.50","med":"2.90","high":"2.99","auto":"2.95","dense":"2.99","custom":"2.950"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
customConfig = {
'buckets': [{
'precision': 2,
'max': 6,
'increment': 0.01,
}]
};
cpm = 4.01;
expected = '{"low":"4.00","med":"4.00","high":"4.01","auto":"4.00","dense":"4.00","custom":"4.01"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
cpm = 4.68;
expected = '{"low":"4.50","med":"4.60","high":"4.68","auto":"4.65","dense":"4.65","custom":"4.68"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
cpm = 4.69;
expected = '{"low":"4.50","med":"4.60","high":"4.69","auto":"4.65","dense":"4.65","custom":"4.69"}';
output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets custom bucket strings and it should honor 0', function () {
let cpm = 16.50908;
let customConfig = {
'buckets': [
{
'precision': 0,
'max': 18,
'increment': 0.05,
}
]
};
let expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"17"}';
let output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets the custom bucket strings without passing precision and it should honor the default precision', function () {
let cpm = 16.50908;
let customConfig = {
'buckets': [
{
'max': 18,
'increment': 0.05,
}
]
};
let expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"16.50"}';
let output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('checks whether custom config is valid', function () {
let badConfig = {
'buckets': [{
'max': 3,
'increment': 0.01,
},
{
'max': 18,
// missing increment prop
'cap': true
}
]
};
expect(isValidPriceConfig(badConfig)).to.be.false;
});
});