-
Notifications
You must be signed in to change notification settings - Fork 806
Request Provided Currency Rates #1753
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
2989689
First draft
guscarreon 4d3196c
update with latest master
guscarreon f50db9f
Last test case working
guscarreon 1e69876
Merge branch 'PBS-698' of https://github.com/guscarreon/prebid-server…
40afa09
Added Json test, corrected descriptions
5605a98
Mansi's review and resolve merge conflict
guscarreon 57dde30
resolved openrtb2 merge conflicts
8b17152
Added inverse custom conversion rates
86b4350
Scott's eedback
95c82d3
Corrected error assertion
177e1e1
errortypes import in exchange.go
c1f136a
Merge branch 'master' into PBS-698
c0c81d7
Corrected test to not have a nil e.bidIDGenerator
5376694
update with latest master
348d83b
Created rateEngine Rates implementation and reingeneered getAuctionCu…
ed48b9c
remove .swp file
dc36e68
Corrected test cases
8f7df0a
Merge branch 'master' into PBS-698
3731241
Corrected native json test
00dda98
Scott's review. Renaming objects, getting rid of []Conversions
72aa90c
Merge branch 'master' into PBS-698
a7bba04
Corrected expected error message
9adf8b2
Renamed GroupedConversions to AggregateConversions and rate_engines.g…
11fa5d2
Removed logically dead code in getAuctionCurrencyRates
6ff91ed
Merge branch 'master' into PBS-698
e454459
Implemented NoConversionRate error type and Scott's review
b60b68f
Conversions disabled logic, warning and aggregateConversions GetRate …
561dff8
Mansi's review June 1st
b3db4a2
Mansi's review Jun 8th
663dde0
Scott's review June 8th
32a3a22
Scott's review June 9th
c9ee8e1
Mansi's feedback in regards of file and directory naming and addition…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package currency | ||
|
||
// AggregateConversions contains both the request-defined currency rate | ||
// map found in request.ext.prebid.currency and the currencies conversion | ||
// rates fetched with the RateConverter object defined in rate_converter.go | ||
// It implements the Conversions interface. | ||
type AggregateConversions struct { | ||
customRates, serverRates Conversions | ||
} | ||
|
||
// NewAggregateConversions expects both customRates and pbsRates to not be nil | ||
func NewAggregateConversions(customRates, pbsRates Conversions) *AggregateConversions { | ||
return &AggregateConversions{ | ||
customRates: customRates, | ||
serverRates: pbsRates, | ||
} | ||
} | ||
|
||
// GetRate returns the conversion rate between two currencies prioritizing | ||
// the customRates currency rate over that of the PBS currency rate service | ||
// returns an error if both Conversions objects return error. | ||
func (re *AggregateConversions) GetRate(from string, to string) (float64, error) { | ||
rate, err := re.customRates.GetRate(from, to) | ||
if err == nil { | ||
return rate, nil | ||
} else if _, isMissingRateErr := err.(ConversionRateNotFound); !isMissingRateErr { | ||
// other error, return the error | ||
return 0, err | ||
} | ||
|
||
// because the custom rates' GetRate() call returned an error other than "conversion | ||
// rate not found", there's nothing wrong with the 3 letter currency code so let's | ||
// try the PBS rates instead | ||
return re.serverRates.GetRate(from, to) | ||
} | ||
|
||
// GetRates is not implemented for AggregateConversions . There is no need to call | ||
// this function for this scenario. | ||
func (r *AggregateConversions) GetRates() *map[string]map[string]float64 { | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package currency | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGroupedGetRate(t *testing.T) { | ||
|
||
// Setup: | ||
customRates := NewRates(time.Now(), map[string]map[string]float64{ | ||
"USD": { | ||
"GBP": 3.00, | ||
"EUR": 2.00, | ||
}, | ||
}) | ||
|
||
pbsRates := NewRates(time.Now(), map[string]map[string]float64{ | ||
"USD": { | ||
"GBP": 4.00, | ||
"MXN": 10.00, | ||
}, | ||
}) | ||
mansinahar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
aggregateConversions := NewAggregateConversions(customRates, pbsRates) | ||
|
||
// Test cases: | ||
type aTest struct { | ||
desc string | ||
from string | ||
to string | ||
expectedRate float64 | ||
} | ||
|
||
testGroups := []struct { | ||
expectedError error | ||
testCases []aTest | ||
}{ | ||
{ | ||
expectedError: nil, | ||
testCases: []aTest{ | ||
{"Found in both, return custom rate", "USD", "GBP", 3.00}, | ||
{"Found in both, return inverse custom rate", "GBP", "USD", 1 / 3.00}, | ||
{"Found in custom rates only", "USD", "EUR", 2.00}, | ||
{"Found in PBS rates only", "USD", "MXN", 10.00}, | ||
{"Found in PBS rates only, return inverse", "MXN", "USD", 1 / 10.00}, | ||
{"Same currency, return unitary rate", "USD", "USD", 1}, | ||
}, | ||
}, | ||
{ | ||
expectedError: errors.New("currency: tag is not well-formed"), | ||
testCases: []aTest{ | ||
{"From-currency three-digit code malformed", "XX", "EUR", 0}, | ||
{"To-currency three-digit code malformed", "GBP", "", 0}, | ||
{"Both currencies malformed", "", "", 0}, | ||
}, | ||
}, | ||
{ | ||
expectedError: errors.New("currency: tag is not a recognized currency"), | ||
testCases: []aTest{ | ||
{"From-currency three-digit code not found", "FOO", "EUR", 0}, | ||
{"To-currency three-digit code not found", "GBP", "BAR", 0}, | ||
}, | ||
}, | ||
{ | ||
expectedError: ConversionRateNotFound{"GBP", "EUR"}, | ||
testCases: []aTest{ | ||
{"Valid three-digit currency codes, but conversion rate not found", "GBP", "EUR", 0}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, group := range testGroups { | ||
for _, tc := range group.testCases { | ||
// Execute: | ||
rate, err := aggregateConversions.GetRate(tc.from, tc.to) | ||
|
||
// Verify: | ||
assert.Equal(t, tc.expectedRate, rate, "conversion rate doesn't match the expected rate: %s\n", tc.desc) | ||
if group.expectedError != nil { | ||
assert.Error(t, err, "error doesn't match expected: %s\n", tc.desc) | ||
} else { | ||
assert.NoError(t, err, "err should be nil: %s\n", tc.desc) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package currency | ||
|
||
import "fmt" | ||
|
||
// ConversionRateNotFound is thrown by the currency.Conversions GetRate(from string, to string) method | ||
// when the conversion rate between the two currencies, nor its reciprocal, can be found. | ||
type ConversionRateNotFound struct { | ||
FromCur, ToCur string | ||
} | ||
|
||
func (err ConversionRateNotFound) Error() string { | ||
return fmt.Sprintf("Currency conversion rate not found: '%s' => '%s'", err.FromCur, err.ToCur) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.