github.com/prebid/prebid-server/v2@v2.18.0/adapters/bidder_test.go (about)

     1  package adapters
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/mock"
     9  )
    10  
    11  func TestExtraRequestInfoConvertCurrency(t *testing.T) {
    12  	var (
    13  		givenValue float64 = 2
    14  		givenFrom  string  = "AAA"
    15  		givenTo    string  = "BBB"
    16  	)
    17  
    18  	testCases := []struct {
    19  		description   string
    20  		setMock       func(m *mock.Mock)
    21  		expectedValue float64
    22  		expectedError error
    23  	}{
    24  		{
    25  			description:   "Success",
    26  			setMock:       func(m *mock.Mock) { m.On("GetRate", "AAA", "BBB").Return(2.5, nil) },
    27  			expectedValue: 5,
    28  			expectedError: nil,
    29  		},
    30  		{
    31  			description:   "Error",
    32  			setMock:       func(m *mock.Mock) { m.On("GetRate", "AAA", "BBB").Return(2.5, errors.New("some error")) },
    33  			expectedValue: 0,
    34  			expectedError: errors.New("some error"),
    35  		},
    36  	}
    37  
    38  	for _, test := range testCases {
    39  		mockConversions := &mockConversions{}
    40  		test.setMock(&mockConversions.Mock)
    41  
    42  		extraRequestInfo := NewExtraRequestInfo(mockConversions)
    43  		result, err := extraRequestInfo.ConvertCurrency(givenValue, givenFrom, givenTo)
    44  
    45  		mockConversions.AssertExpectations(t)
    46  		assert.Equal(t, test.expectedValue, result, test.description+":result")
    47  		assert.Equal(t, test.expectedError, err, test.description+":err")
    48  	}
    49  }
    50  
    51  type mockConversions struct {
    52  	mock.Mock
    53  }
    54  
    55  func (m *mockConversions) GetRate(from string, to string) (float64, error) {
    56  	args := m.Called(from, to)
    57  	return args.Get(0).(float64), args.Error(1)
    58  }
    59  
    60  func (m *mockConversions) GetRates() *map[string]map[string]float64 {
    61  	args := m.Called()
    62  	return args.Get(0).(*map[string]map[string]float64)
    63  }