forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultifetcher_test.go
178 lines (152 loc) · 6.65 KB
/
multifetcher_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
package stored_requests
import (
"context"
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMultiFetcher(t *testing.T) {
f1 := &mockFetcher{}
f2 := &mockFetcher{}
fetcher := &MultiFetcher{f1, f2}
ctx := context.Background()
reqIDs := []string{"abc", "def"}
impIDs := []string{"imp-1", "imp-2"}
f1.On("FetchRequests", ctx, reqIDs, impIDs).Return(
map[string]json.RawMessage{
"abc": json.RawMessage(`{"req_id": "abc"}`),
},
map[string]json.RawMessage{
"imp-1": json.RawMessage(`{"imp_id": "imp-1"}`),
},
[]error{NotFoundError{"def", "Request"}, NotFoundError{"imp-2", "Imp"}},
)
f2.On("FetchRequests", ctx, []string{"def"}, []string{"imp-2"}).Return(
map[string]json.RawMessage{
"def": json.RawMessage(`{"req_id": "def"}`),
},
map[string]json.RawMessage{
"imp-2": json.RawMessage(`{"imp_id": "imp-2"}`),
},
[]error{},
)
reqData, impData, errs := fetcher.FetchRequests(ctx, reqIDs, impIDs)
f1.AssertExpectations(t)
f2.AssertExpectations(t)
assert.Len(t, reqData, 2, "MultiFetcher should return all the requested stored req data that exists")
assert.Len(t, impData, 2, "MultiFetcher should return all the requested stored imp data that exists")
assert.Len(t, errs, 0, "MultiFetcher shouldn't return an error")
assert.JSONEq(t, `{"req_id": "abc"}`, string(reqData["abc"]), "MultiFetcher should return the right request data")
assert.JSONEq(t, `{"req_id": "def"}`, string(reqData["def"]), "MultiFetcher should return the right request data")
assert.JSONEq(t, `{"imp_id": "imp-1"}`, string(impData["imp-1"]), "MultiFetcher should return the right imp data")
assert.JSONEq(t, `{"imp_id": "imp-2"}`, string(impData["imp-2"]), "MultiFetcher should return the right imp data")
}
func TestMissingID(t *testing.T) {
f1 := &mockFetcher{}
f2 := &mockFetcher{}
fetcher := &MultiFetcher{f1, f2}
ctx := context.Background()
reqIDs := []string{"abc", "def", "ghi"}
impIDs := []string{"imp-1", "imp-2", "imp-3"}
f1.On("FetchRequests", ctx, reqIDs, impIDs).Return(
map[string]json.RawMessage{
"abc": json.RawMessage(`{"req_id": "abc"}`),
},
map[string]json.RawMessage{
"imp-1": json.RawMessage(`{"imp_id": "imp-1"}`),
},
[]error{NotFoundError{"def", "Request"}, NotFoundError{"imp-2", "Imp"}},
)
f2.On("FetchRequests", ctx, []string{"def", "ghi"}, []string{"imp-2", "imp-3"}).Return(
map[string]json.RawMessage{
"def": json.RawMessage(`{"req_id": "def"}`),
},
map[string]json.RawMessage{
"imp-2": json.RawMessage(`{"imp_id": "imp-2"}`),
},
[]error{},
)
reqData, impData, errs := fetcher.FetchRequests(ctx, reqIDs, impIDs)
f1.AssertExpectations(t)
f2.AssertExpectations(t)
assert.Len(t, reqData, 2, "MultiFetcher should return all the requested stored req data that exists")
assert.Len(t, impData, 2, "MultiFetcher should return all the requested stored imp data that exists")
assert.Len(t, errs, 2, "MultiFetcher should return an error if there are missing IDs")
assert.JSONEq(t, `{"req_id": "abc"}`, string(reqData["abc"]), "MultiFetcher should return the right request data")
assert.JSONEq(t, `{"req_id": "def"}`, string(reqData["def"]), "MultiFetcher should return the right request data")
assert.JSONEq(t, `{"imp_id": "imp-1"}`, string(impData["imp-1"]), "MultiFetcher should return the right imp data")
assert.JSONEq(t, `{"imp_id": "imp-2"}`, string(impData["imp-2"]), "MultiFetcher should return the right imp data")
}
func TestOtherError(t *testing.T) {
f1 := &mockFetcher{}
f2 := &mockFetcher{}
fetcher := &MultiFetcher{f1, f2}
ctx := context.Background()
reqIDs := []string{"abc", "def"}
impIDs := []string{"imp-1"}
f1.On("FetchRequests", ctx, reqIDs, impIDs).Return(
map[string]json.RawMessage{
"abc": json.RawMessage(`{"req_id": "abc"}`),
},
map[string]json.RawMessage{},
[]error{NotFoundError{"def", "Request"}, errors.New("Other error")},
)
f2.On("FetchRequests", ctx, []string{"def"}, []string{"imp-1"}).Return(
map[string]json.RawMessage{
"def": json.RawMessage(`{"req_id": "def"}`),
},
map[string]json.RawMessage{
"imp-1": json.RawMessage(`{"imp_id": "imp-1"}`),
},
[]error{},
)
reqData, impData, errs := fetcher.FetchRequests(ctx, reqIDs, impIDs)
f1.AssertExpectations(t)
f2.AssertExpectations(t)
assert.Len(t, reqData, 2, "MultiFetcher should return all the requested stored req data that exists")
assert.Len(t, impData, 1, "MultiFetcher should return all the requested stored imp data that exists")
assert.Len(t, errs, 1, "MultiFetcher should return an error if one of the fetcher returns an error other than NotFoundError")
assert.JSONEq(t, `{"req_id": "abc"}`, string(reqData["abc"]), "MultiFetcher should return the right request data")
assert.JSONEq(t, `{"req_id": "def"}`, string(reqData["def"]), "MultiFetcher should return the right request data")
assert.JSONEq(t, `{"imp_id": "imp-1"}`, string(impData["imp-1"]), "MultiFetcher should return the right imp data")
}
func TestMultiFetcherAccountFoundInFirstFetcher(t *testing.T) {
f1 := &mockFetcher{}
f2 := &mockFetcher{}
fetcher := &MultiFetcher{f1, f2}
ctx := context.Background()
f1.On("FetchAccount", ctx, json.RawMessage("{}"), "ONE").Once().Return(json.RawMessage(`{"id": "ONE"}`), []error{})
account, errs := fetcher.FetchAccount(ctx, json.RawMessage("{}"), "ONE")
f1.AssertExpectations(t)
f2.AssertNotCalled(t, "FetchAccount")
assert.Empty(t, errs)
assert.JSONEq(t, `{"id": "ONE"}`, string(account))
}
func TestMultiFetcherAccountFoundInSecondFetcher(t *testing.T) {
f1 := &mockFetcher{}
f2 := &mockFetcher{}
fetcher := &MultiFetcher{f1, f2}
ctx := context.Background()
f1.On("FetchAccount", ctx, json.RawMessage("{}"), "TWO").Once().Return(json.RawMessage(``), []error{NotFoundError{"TWO", "Account"}})
f2.On("FetchAccount", ctx, json.RawMessage("{}"), "TWO").Once().Return(json.RawMessage(`{"id": "TWO"}`), []error{})
account, errs := fetcher.FetchAccount(ctx, json.RawMessage("{}"), "TWO")
f1.AssertExpectations(t)
f2.AssertExpectations(t)
assert.Empty(t, errs)
assert.JSONEq(t, `{"id": "TWO"}`, string(account))
}
func TestMultiFetcherAccountNotFound(t *testing.T) {
f1 := &mockFetcher{}
f2 := &mockFetcher{}
fetcher := &MultiFetcher{f1, f2}
ctx := context.Background()
f1.On("FetchAccount", ctx, json.RawMessage("{}"), "MISSING").Once().Return(json.RawMessage(``), []error{NotFoundError{"TWO", "Account"}})
f2.On("FetchAccount", ctx, json.RawMessage("{}"), "MISSING").Once().Return(json.RawMessage(``), []error{NotFoundError{"TWO", "Account"}})
account, errs := fetcher.FetchAccount(ctx, json.RawMessage("{}"), "MISSING")
f1.AssertExpectations(t)
f2.AssertExpectations(t)
assert.Len(t, errs, 1)
assert.Nil(t, account)
assert.EqualError(t, errs[0], NotFoundError{"MISSING", "Account"}.Error())
}