forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestcache_test.go
110 lines (100 loc) · 2.88 KB
/
requestcache_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
package adservertargeting
import (
"encoding/json"
"testing"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/stretchr/testify/assert"
)
func TestRequestImpCache(t *testing.T) {
testCases := []struct {
description string
inputRequest json.RawMessage
expectedReqSize int
expectedImpsNum int
expectedError bool
}{
{
description: "valid request with 2 imps",
inputRequest: json.RawMessage(reqValid),
expectedReqSize: 355,
expectedImpsNum: 2,
expectedError: false,
},
{
description: "invalid request ",
inputRequest: json.RawMessage(reqInvalid),
expectedReqSize: 88,
expectedImpsNum: 0,
expectedError: true,
},
{
description: "valid request with no imps",
inputRequest: json.RawMessage(reqNoImps),
expectedReqSize: 52,
expectedImpsNum: 0,
expectedError: true,
},
}
for _, test := range testCases {
reqImpCache := requestCache{resolvedReq: test.inputRequest}
actualReq := reqImpCache.GetReqJson()
assert.Len(t, actualReq, test.expectedReqSize, "incorrect request returned")
actualImps, err := reqImpCache.GetImpsData()
assert.Len(t, actualImps, test.expectedImpsNum, "incorrect number of impressions returned")
if test.expectedError {
assert.Error(t, err, "expected error not returned")
} else {
assert.NoError(t, err, "unexpected error returned")
}
}
}
func TestBidsCache(t *testing.T) {
testCases := []struct {
description string
inputBidder string
inputBidId string
inputBid openrtb2.Bid
expectedBidBytes []byte
expectedError bool
}{
{
description: "valid bid not in cache for existing bidder",
inputBidder: "bidderA",
inputBidId: "bid3",
inputBid: openrtb2.Bid{ID: "test_bid3"},
expectedBidBytes: []byte(`{"id":"test_bid3","impid":"","price":0}`),
expectedError: false,
},
{
description: "valid bid and not existing bidder",
inputBidder: "bidderB",
inputBidId: "bid1",
inputBid: openrtb2.Bid{ID: "test_bid1"},
expectedBidBytes: []byte(`{"id":"test_bid1","impid":"","price":0}`),
expectedError: false,
},
{
description: "valid bid in cache",
inputBidder: "bidderA",
inputBidId: "bid2",
inputBid: openrtb2.Bid{},
expectedBidBytes: []byte(`{"bidid":"test_bid2"}`),
expectedError: false,
},
}
bCache := bidsCache{bids: map[string]map[string][]byte{
"bidderA": {
"bid1": []byte(`{"bidid":"test_bid1"}`),
"bid2": []byte(`{"bidid":"test_bid2"}`),
},
}}
for _, test := range testCases {
bidBytes, err := bCache.GetBid(test.inputBidder, test.inputBidId, test.inputBid)
if test.expectedError {
assert.Error(t, err, "expected error not returned")
} else {
assert.NoError(t, err, "unexpected error returned")
assert.Equal(t, test.expectedBidBytes, bidBytes, "incorrect bid returned")
}
}
}