github.com/prebid/prebid-server/v2@v2.18.0/currency/constant_rates_test.go (about) 1 package currency 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestGetRate_ConstantRates(t *testing.T) { 10 11 // Setup: 12 rates := NewConstantRates() 13 14 testCases := []struct { 15 from string 16 to string 17 expectedRate float64 18 hasError bool 19 }{ 20 {from: "USD", to: "GBP", expectedRate: 0, hasError: true}, 21 {from: "GBP", to: "USD", expectedRate: 0, hasError: true}, 22 {from: "GBP", to: "EUR", expectedRate: 0, hasError: true}, 23 {from: "CNY", to: "EUR", expectedRate: 0, hasError: true}, 24 {from: "", to: "EUR", expectedRate: 0, hasError: true}, 25 {from: "CNY", to: "", expectedRate: 0, hasError: true}, 26 {from: "", to: "", expectedRate: 0, hasError: true}, 27 {from: "USD", to: "USD", expectedRate: 1, hasError: false}, 28 {from: "EUR", to: "EUR", expectedRate: 1, hasError: false}, 29 } 30 31 for _, tc := range testCases { 32 // Execute: 33 rate, err := rates.GetRate(tc.from, tc.to) 34 35 // Verify: 36 if tc.hasError { 37 assert.NotNil(t, err, "err shouldn't be nil") 38 assert.Equal(t, float64(0), rate, "rate should be 0") 39 } else { 40 assert.Nil(t, err, "err should be nil") 41 assert.Equal(t, tc.expectedRate, rate, "rate doesn't match the expected one") 42 } 43 } 44 } 45 46 func TestGetRate_ConstantRates_NotValidISOCurrency(t *testing.T) { 47 48 // Setup: 49 rates := NewConstantRates() 50 51 testCases := []struct { 52 from string 53 to string 54 expectedRate float64 55 hasError bool 56 }{ 57 {from: "foo", to: "foo", expectedRate: 0, hasError: true}, 58 {from: "bar", to: "foo", expectedRate: 0, hasError: true}, 59 } 60 61 for _, tc := range testCases { 62 // Execute: 63 rate, err := rates.GetRate(tc.from, tc.to) 64 65 // Verify: 66 if tc.hasError { 67 assert.NotNil(t, err, "err shouldn't be nil") 68 assert.Equal(t, float64(0), rate, "rate should be 0") 69 } else { 70 assert.Nil(t, err, "err should be nil") 71 assert.Equal(t, tc.expectedRate, rate, "rate doesn't match the expected one") 72 } 73 } 74 }