github.com/prebid/prebid-server/v2@v2.18.0/currency/aggregate_conversions_test.go (about) 1 package currency 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestGroupedGetRate(t *testing.T) { 11 12 // Setup: 13 customRates := NewRates(map[string]map[string]float64{ 14 "USD": { 15 "GBP": 3.00, 16 "EUR": 2.00, 17 }, 18 }) 19 20 pbsRates := NewRates(map[string]map[string]float64{ 21 "USD": { 22 "GBP": 4.00, 23 "MXN": 10.00, 24 }, 25 }) 26 aggregateConversions := NewAggregateConversions(customRates, pbsRates) 27 28 // Test cases: 29 type aTest struct { 30 desc string 31 from string 32 to string 33 expectedRate float64 34 } 35 36 testGroups := []struct { 37 expectedError error 38 testCases []aTest 39 }{ 40 { 41 expectedError: nil, 42 testCases: []aTest{ 43 {"Found in both, return custom rate", "USD", "GBP", 3.00}, 44 {"Found in both, return inverse custom rate", "GBP", "USD", 1 / 3.00}, 45 {"Found in custom rates only", "USD", "EUR", 2.00}, 46 {"Found in PBS rates only", "USD", "MXN", 10.00}, 47 {"Found in PBS rates only, return inverse", "MXN", "USD", 1 / 10.00}, 48 {"Same currency, return unitary rate", "USD", "USD", 1}, 49 }, 50 }, 51 { 52 expectedError: errors.New("currency: tag is not well-formed"), 53 testCases: []aTest{ 54 {"From-currency three-digit code malformed", "XX", "EUR", 0}, 55 {"To-currency three-digit code malformed", "GBP", "", 0}, 56 {"Both currencies malformed", "", "", 0}, 57 }, 58 }, 59 { 60 expectedError: errors.New("currency: tag is not a recognized currency"), 61 testCases: []aTest{ 62 {"From-currency three-digit code not found", "FOO", "EUR", 0}, 63 {"To-currency three-digit code not found", "GBP", "BAR", 0}, 64 }, 65 }, 66 { 67 expectedError: ConversionNotFoundError{FromCur: "GBP", ToCur: "EUR"}, 68 testCases: []aTest{ 69 {"Valid three-digit currency codes, but conversion rate not found", "GBP", "EUR", 0}, 70 }, 71 }, 72 } 73 74 for _, group := range testGroups { 75 for _, tc := range group.testCases { 76 // Execute: 77 rate, err := aggregateConversions.GetRate(tc.from, tc.to) 78 79 // Verify: 80 assert.Equal(t, tc.expectedRate, rate, "conversion rate doesn't match the expected rate: %s\n", tc.desc) 81 if group.expectedError != nil { 82 assert.Error(t, err, "error doesn't match expected: %s\n", tc.desc) 83 } else { 84 assert.NoError(t, err, "err should be nil: %s\n", tc.desc) 85 } 86 } 87 } 88 }