forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpmBucketManager_spec.js
More file actions
257 lines (237 loc) · 8.49 KB
/
cpmBucketManager_spec.js
File metadata and controls
257 lines (237 loc) · 8.49 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { expect } from 'chai';
import { getPriceBucketString, isValidPriceConfig } from 'src/cpmBucketManager.js';
import { config } from 'src/config.js';
const cpmFixtures = require('test/fixtures/cpmInputsOutputs.json');
describe('cpmBucketManager', function () {
describe('getPriceBucketString', function () {
it('getPriceBucketString function generates the correct price strings', function () {
const input = cpmFixtures.cpmInputs;
for (let i = 0; i < input.length; i++) {
const output = getPriceBucketString(input[i]);
const jsonOutput = JSON.stringify(output);
expect(jsonOutput).to.deep.equal(JSON.stringify(cpmFixtures.priceStringOutputs[i]));
}
});
it('gets the correct custom bucket strings', function () {
const cpm = 16.50908;
const customConfig = {
'buckets': [{
'precision': 4,
'max': 3,
'increment': 0.01,
},
{
'precision': 4,
'max': 18,
'increment': 0.05,
'cap': true
}]
};
const expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"16.5000"}';
const output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets the correct custom bucket strings with irregular increment', function () {
const cpm = 14.50908;
const customConfig = {
'buckets': [{
'precision': 4,
'max': 4,
'increment': 0.01,
},
{
'precision': 4,
'max': 18,
'increment': 0.3,
'cap': true
}]
};
const expected = '{"low":"5.00","med":"14.50","high":"14.50","auto":"14.50","dense":"14.50","custom":"14.5000"}';
const output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('gets the correct custom bucket strings in non-USD currency', function () {
const cpm = 16.50908 * 110.49;
const customConfig = {
'buckets': [{
'precision': 4,
'max': 3,
'increment': 0.01,
},
{
'precision': 4,
'max': 18,
'increment': 0.05,
'cap': true
}]
};
const expected = '{"low":"552.45","med":"1823.09","high":"1823.09","auto":"1823.09","dense":"1823.09","custom":"1823.0850"}';
const 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 () {
const cpm = 16.50908;
const customConfig = {
'buckets': [
{
'precision': 0,
'max': 18,
'increment': 0.05,
}
]
};
const expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"17"}';
const 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 () {
const cpm = 16.50908;
const customConfig = {
'buckets': [
{
'max': 18,
'increment': 0.05,
}
]
};
const expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"16.50"}';
const output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('defaults to Math.floor if no rounding function is provided', function () {
const cpm = 6.516;
const customConfig = {
'buckets': [
{
'max': 18,
'increment': 0.05,
}
]
};
const expected = '{"low":"5.00","med":"6.50","high":"6.51","auto":"6.50","dense":"6.50","custom":"6.50"}';
const output = getPriceBucketString(cpm, customConfig);
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('accepts custom rounding functions', function () {
const cpm = 6.516;
const customConfig = {
'buckets': [
{
'max': 18,
'increment': 0.05,
}
]
};
const getConfig = config.getConfig;
config.getConfig = () => Math.ceil;
const expected = '{"low":"5.00","med":"6.60","high":"6.52","auto":"6.60","dense":"6.55","custom":"6.55"}';
const output = getPriceBucketString(cpm, customConfig);
config.getConfig = getConfig;
expect(JSON.stringify(output)).to.deep.equal(expected);
});
it('will default to Math.floor if passed an invalid rounding function', function () {
const cpm = 6.516;
const customConfig = {
'buckets': [
{
'max': 18,
'increment': 0.05,
}
]
};
const getConfig = config.getConfig;
config.getConfig = () => Math.ceil(5.3);
const expected = '{"low":"5.00","med":"6.50","high":"6.51","auto":"6.50","dense":"6.50","custom":"6.50"}';
const output = getPriceBucketString(cpm, customConfig);
config.getConfig = getConfig;
expect(JSON.stringify(output)).to.deep.equal(expected);
});
});
describe('isValidPriceConfig', function () {
it('checks whether custom config is valid', function () {
const badConfig = {
'buckets': [{
'max': 3,
'increment': 0.01,
},
{
'max': 18,
// missing increment prop
'cap': true
}]
};
expect(isValidPriceConfig(badConfig)).to.be.false;
const customConfig = {
'buckets': [
{
'max': 18,
'increment': 0.05,
}
]
};
expect(isValidPriceConfig(customConfig)).to.be.true;
});
});
});