forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request Provided Currency Rates (prebid#1753)
- Loading branch information
1 parent
9af0a24
commit 07e3fa4
Showing
47 changed files
with
1,368 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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 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, | ||
}, | ||
}) | ||
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 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 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 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 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.