-
Notifications
You must be signed in to change notification settings - Fork 739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rubicon: Use currency conversion function #1924
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import ( | |
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"github.com/stretchr/testify/mock" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -572,52 +574,105 @@ func TestResolveVideoSizeId(t *testing.T) { | |
} | ||
} | ||
|
||
func TestResolveBidFloorAttributes(t *testing.T) { | ||
func TestOpenRTBRequestWithDifferentBidFloorAttributes(t *testing.T) { | ||
testScenarios := []struct { | ||
bidFloor float64 | ||
bidFloorCur string | ||
expectedBidFloor float64 | ||
expectedBidFloorCur string | ||
bidFloor float64 | ||
bidFloorCur string | ||
setMock func(m *mock.Mock) | ||
expectedBidFloor float64 | ||
expectedErrors []error | ||
}{ | ||
{ | ||
bidFloor: 1, | ||
bidFloorCur: "EUR", | ||
expectedBidFloor: 1.2, | ||
expectedBidFloorCur: "USD", | ||
}, | ||
{ | ||
bidFloor: 1, | ||
bidFloorCur: "Eur", | ||
expectedBidFloor: 1.2, | ||
expectedBidFloorCur: "USD", | ||
}, | ||
{ | ||
bidFloor: 0, | ||
bidFloorCur: "EUR", | ||
expectedBidFloor: 0, | ||
expectedBidFloorCur: "EUR", | ||
bidFloor: 1, | ||
bidFloorCur: "WRONG", | ||
setMock: func(m *mock.Mock) { m.On("GetRate", "WRONG", "USD").Return(2.5, errors.New("some error")) }, | ||
expectedBidFloor: 0, | ||
expectedErrors: []error{ | ||
&errortypes.BadInput{Message: "Unable to convert provided bid floor currency from WRONG to USD"}, | ||
}, | ||
}, | ||
{ | ||
bidFloor: -1, | ||
bidFloorCur: "EUR", | ||
expectedBidFloor: -1, | ||
expectedBidFloorCur: "EUR", | ||
bidFloor: 1, | ||
bidFloorCur: "USD", | ||
setMock: func(m *mock.Mock) {}, | ||
expectedBidFloor: 1, | ||
expectedErrors: nil, | ||
}, | ||
{ | ||
bidFloor: 1, | ||
bidFloorCur: "USD", | ||
expectedBidFloor: 1, | ||
expectedBidFloorCur: "USD", | ||
bidFloor: 1, | ||
bidFloorCur: "EUR", | ||
setMock: func(m *mock.Mock) { m.On("GetRate", "EUR", "USD").Return(1.2, nil) }, | ||
expectedBidFloor: 1.2, | ||
expectedErrors: nil, | ||
}, | ||
} | ||
|
||
for _, scenario := range testScenarios { | ||
bidFloor, bidFloorCur := resolveBidFloorAttributes(scenario.bidFloor, scenario.bidFloorCur) | ||
assert.Equal(t, scenario.expectedBidFloor, bidFloor) | ||
assert.Equal(t, scenario.expectedBidFloorCur, bidFloorCur) | ||
mockConversions := &mockCurrencyConversion{} | ||
scenario.setMock(&mockConversions.Mock) | ||
|
||
extraRequestInfo := adapters.ExtraRequestInfo{ | ||
CurrencyConversions: mockConversions, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the only reason why you changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually yes, it there is any better way, for my case? Or everything is fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is fine @snahornyi in the future we plan to improve the JSON testing framework. We also encourage tests to be written in JSON instead of coded in Golang but, for the moment we agree with this approach |
||
} | ||
|
||
SIZE_ID := getTestSizes() | ||
bidder := new(RubiconAdapter) | ||
|
||
request := &openrtb2.BidRequest{ | ||
ID: "test-request-id", | ||
Imp: []openrtb2.Imp{{ | ||
ID: "test-imp-id", | ||
BidFloorCur: scenario.bidFloorCur, | ||
BidFloor: scenario.bidFloor, | ||
Banner: &openrtb2.Banner{ | ||
Format: []openrtb2.Format{ | ||
SIZE_ID[15], | ||
SIZE_ID[10], | ||
}, | ||
}, | ||
Ext: json.RawMessage(`{"bidder": { | ||
"zoneId": 8394, | ||
"siteId": 283282, | ||
"accountId": 7891 | ||
}}`), | ||
}}, | ||
App: &openrtb2.App{ | ||
ID: "com.test", | ||
Name: "testApp", | ||
}, | ||
} | ||
|
||
reqs, errs := bidder.MakeRequests(request, &extraRequestInfo) | ||
|
||
mockConversions.AssertExpectations(t) | ||
|
||
if scenario.expectedErrors == nil { | ||
rubiconReq := &openrtb2.BidRequest{} | ||
if err := json.Unmarshal(reqs[0].Body, rubiconReq); err != nil { | ||
t.Fatalf("Unexpected error while decoding request: %s", err) | ||
} | ||
assert.Equal(t, scenario.expectedBidFloor, rubiconReq.Imp[0].BidFloor) | ||
assert.Equal(t, "USD", rubiconReq.Imp[0].BidFloorCur) | ||
} else { | ||
assert.Equal(t, scenario.expectedErrors, errs) | ||
} | ||
} | ||
} | ||
|
||
type mockCurrencyConversion struct { | ||
mock.Mock | ||
} | ||
|
||
func (m mockCurrencyConversion) GetRate(from string, to string) (float64, error) { | ||
args := m.Called(from, to) | ||
return args.Get(0).(float64), args.Error(1) | ||
} | ||
|
||
func (m mockCurrencyConversion) GetRates() *map[string]map[string]float64 { | ||
args := m.Called() | ||
return args.Get(0).(*map[string]map[string]float64) | ||
} | ||
|
||
func TestNoContentResponse(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,8 +120,6 @@ | |
"w": 1024, | ||
"h": 576 | ||
}, | ||
"bidfloor": 1, | ||
"bidfloorcur": "EuR", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This newly coded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gus, guys, please correct me if I'm wrong.
EUR is no longer hardcoded and it uses real currency conversion in
Not sure if it's possible to control it in json tests. BidFloor functionality is covered in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have described all my thoughts, thanks @VeronikaSolovei9 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. Thank you @VeronikaSolovei9 |
||
"ext": { | ||
"bidder": { | ||
"video": { | ||
|
@@ -298,7 +296,6 @@ | |
"w": 1024, | ||
"h": 576 | ||
}, | ||
"bidfloor": 1.2, | ||
"bidfloorcur": "USD", | ||
"ext": { | ||
"rp": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a given request comes with:
Line 760 above will change the "EUR" value to "USD" anyways. Is that ok?