Skip to content
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

Merged
merged 3 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions adapters/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ func (r *RequestData) SetBasicAuth(username string, password string) {
type ExtraRequestInfo struct {
PbsEntryPoint metrics.RequestType
GlobalPrivacyControlHeader string
currencyConversions currency.Conversions
CurrencyConversions currency.Conversions
}

func NewExtraRequestInfo(c currency.Conversions) ExtraRequestInfo {
return ExtraRequestInfo{
currencyConversions: c,
CurrencyConversions: c,
}
}

Expand All @@ -153,7 +153,7 @@ func NewExtraRequestInfo(c currency.Conversions) ExtraRequestInfo {
// - ConversionNotFoundError if the conversion mapping is unknown to Prebid Server
// and not provided in the bid request.
func (r ExtraRequestInfo) ConvertCurrency(value float64, from, to string) (float64, error) {
if rate, err := r.currencyConversions.GetRate(from, to); err == nil {
if rate, err := r.CurrencyConversions.GetRate(from, to); err == nil {
return value * rate, nil
} else {
return 0, err
Expand Down
21 changes: 13 additions & 8 deletions adapters/rubicon/rubicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ func (a *RubiconAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ada
numRequests := len(request.Imp)
errs := make([]error, 0, len(request.Imp))
var err error

requestData := make([]*adapters.RequestData, 0, numRequests)
headers := http.Header{}
headers.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -750,8 +749,15 @@ func (a *RubiconAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ada
continue
}

resolvedBidFloor, resolvedBidFloorCur := resolveBidFloorAttributes(thisImp.BidFloor, thisImp.BidFloorCur)
thisImp.BidFloorCur = resolvedBidFloorCur
resolvedBidFloor, err := resolveBidFloor(thisImp.BidFloor, thisImp.BidFloorCur, reqInfo)
if err != nil {
errs = append(errs, &errortypes.BadInput{
Message: fmt.Sprintf("Unable to convert provided bid floor currency from %s to USD",
thisImp.BidFloorCur),
})
continue
}
thisImp.BidFloorCur = "USD"
Copy link
Contributor

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:

        "bidfloor": 0.00,
        "bidfloorcur": "EUR",

Line 760 above will change the "EUR" value to "USD" anyways. Is that ok?

thisImp.BidFloor = resolvedBidFloor

if request.User != nil {
Expand Down Expand Up @@ -908,15 +914,14 @@ func (a *RubiconAdapter) MakeRequests(request *openrtb2.BidRequest, reqInfo *ada
return requestData, errs
}

// Will be replaced after https://github.com/prebid/prebid-server/issues/1482 resolution
func resolveBidFloorAttributes(bidFloor float64, bidFloorCur string) (float64, string) {
func resolveBidFloor(bidFloor float64, bidFloorCur string, reqInfo *adapters.ExtraRequestInfo) (float64, error) {
if bidFloor > 0 {
if strings.ToUpper(bidFloorCur) == "EUR" {
return bidFloor * 1.2, "USD"
if strings.ToUpper(bidFloorCur) != "USD" {
return reqInfo.ConvertCurrency(bidFloor, bidFloorCur, "USD")
}
}

return bidFloor, bidFloorCur
return bidFloor, nil
}

func updateExtWithIabAttribute(target json.RawMessage, data []openrtb2.Data, segTaxes []int) (json.RawMessage, error) {
Expand Down
119 changes: 87 additions & 32 deletions adapters/rubicon/rubicon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/stretchr/testify/mock"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only reason why you changed CurrencyConversions to public?

Copy link
Contributor Author

@SerhiiNahornyi SerhiiNahornyi Jul 15, 2021

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down
3 changes: 0 additions & 3 deletions adapters/rubicon/rubicontest/exemplary/simple-video.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@
"w": 1024,
"h": 576
},
"bidfloor": 1,
"bidfloorcur": "EuR",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This newly coded bidFloor currency conversion functionality seems to not be tested in any JSON test. Can we include one? I realize a test case asserting this functionality has already been coded but I believe a JSON test would be useful to prove end to end functionality even further.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gus, guys, please correct me if I'm wrong.
I think test cases with "EUR" were removed due to this update:

if bidFloor > 0 {
-		if strings.ToUpper(bidFloorCur) == "EUR" {
-			return bidFloor * 1.2, "USD"
+		if strings.ToUpper(bidFloorCur) != "USD" {
+			return reqInfo.ConvertCurrency(bidFloor, bidFloorCur, "USD")
		}

EUR is no longer hardcoded and it uses real currency conversion in GetRates function. To make it work in test mock function is used:

setMock:          func(m *mock.Mock) { m.On("GetRate", "EUR", "USD").Return(1.2, nil) },

Not sure if it's possible to control it in json tests.

BidFloor functionality is covered in TestOpenRTBRequestWithDifferentBidFloorAttributes

Copy link
Contributor Author

@SerhiiNahornyi SerhiiNahornyi Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have described all my thoughts, thanks @VeronikaSolovei9
@guscarreon What do you think, about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Thank you @VeronikaSolovei9

"ext": {
"bidder": {
"video": {
Expand Down Expand Up @@ -298,7 +296,6 @@
"w": 1024,
"h": 576
},
"bidfloor": 1.2,
"bidfloorcur": "USD",
"ext": {
"rp": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@
"w": 1024,
"h": 576
},
"bidfloor": 1,
"bidfloorcur": "EuR",
"ext": {
"bidder": {
"video": {
Expand Down Expand Up @@ -221,7 +219,6 @@
"w": 1024,
"h": 576
},
"bidfloor": 1.2,
"bidfloorcur": "USD",
"ext": {
"rp": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@
"w": 1024,
"h": 576
},
"bidfloor": 1,
"bidfloorcur": "EuR",
"ext": {
"bidder": {
"video": {
Expand Down Expand Up @@ -217,7 +215,6 @@
"w": 1024,
"h": 576
},
"bidfloor": 1.2,
"bidfloorcur": "USD",
"ext": {
"rp": {
Expand Down