github.com/prebid/prebid-server/v2@v2.18.0/currency/currency_test.go (about)

     1  package currency
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  	"github.com/prebid/prebid-server/v2/util/jsonutil"
     9  	"github.com/prebid/prebid-server/v2/util/ptrutil"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestGetAuctionCurrencyRates(t *testing.T) {
    14  	pbsRates := map[string]map[string]float64{
    15  		"MXN": {
    16  			"USD": 20.13,
    17  			"EUR": 27.82,
    18  			"JPY": 5.09, // "MXN" to "JPY" rate not found in customRates
    19  		},
    20  	}
    21  
    22  	customRates := map[string]map[string]float64{
    23  		"MXN": {
    24  			"USD": 25.00, // different rate than in pbsRates
    25  			"EUR": 27.82, // same as in pbsRates
    26  			"GBP": 31.12, // not found in pbsRates at all
    27  		},
    28  	}
    29  
    30  	expectedRateEngineRates := map[string]map[string]float64{
    31  		"MXN": {
    32  			"USD": 25.00, // rates engine will prioritize the value found in custom rates
    33  			"EUR": 27.82, // same value in both the engine reads the custom entry first
    34  			"JPY": 5.09,  // the engine will find it in the pbsRates conversions
    35  			"GBP": 31.12, // the engine will find it in the custom conversions
    36  		},
    37  	}
    38  
    39  	setupMockRateConverter := func(pbsRates map[string]map[string]float64) *RateConverter {
    40  		if pbsRates == nil {
    41  			return nil
    42  		}
    43  
    44  		jsonPbsRates, err := jsonutil.Marshal(pbsRates)
    45  		if err != nil {
    46  			t.Fatalf("Failed to marshal PBS rates: %v", err)
    47  		}
    48  
    49  		// Init mock currency conversion service
    50  		mockCurrencyClient := &MockCurrencyRatesHttpClient{
    51  			ResponseBody: `{"dataAsOf":"2018-09-12","conversions":` + string(jsonPbsRates) + `}`,
    52  		}
    53  
    54  		return NewRateConverter(
    55  			mockCurrencyClient,
    56  			"currency.fake.com",
    57  			24*time.Hour,
    58  		)
    59  	}
    60  
    61  	type args struct {
    62  		currencyConverter *RateConverter
    63  		requestRates      *openrtb_ext.ExtRequestCurrency
    64  	}
    65  	tests := []struct {
    66  		name        string
    67  		args        args
    68  		assertRates map[string]map[string]float64
    69  	}{
    70  		{
    71  			name: "valid ConversionRates, valid pbsRates, false UsePBSRates. Resulting rates identical to customRates",
    72  			args: args{
    73  				currencyConverter: setupMockRateConverter(pbsRates),
    74  				requestRates: &openrtb_ext.ExtRequestCurrency{
    75  					ConversionRates: customRates,
    76  					UsePBSRates:     ptrutil.ToPtr(false),
    77  				},
    78  			},
    79  			assertRates: customRates,
    80  		},
    81  		{
    82  			name: "valid ConversionRates, valid pbsRates, true UsePBSRates. Resulting rates are a mix but customRates gets priority",
    83  			args: args{
    84  				currencyConverter: setupMockRateConverter(pbsRates),
    85  				requestRates: &openrtb_ext.ExtRequestCurrency{
    86  					ConversionRates: customRates,
    87  					UsePBSRates:     ptrutil.ToPtr(true),
    88  				},
    89  			},
    90  			assertRates: expectedRateEngineRates,
    91  		},
    92  		{
    93  			name: "valid ConversionRates, nil pbsRates, false UsePBSRates. Resulting rates identical to customRates",
    94  			args: args{
    95  				currencyConverter: nil,
    96  				requestRates: &openrtb_ext.ExtRequestCurrency{
    97  					ConversionRates: customRates,
    98  					UsePBSRates:     ptrutil.ToPtr(false),
    99  				},
   100  			},
   101  			assertRates: customRates,
   102  		},
   103  		{
   104  			name: "valid ConversionRates, nil pbsRates, true UsePBSRates. Resulting rates identical to customRates",
   105  			args: args{
   106  				currencyConverter: nil,
   107  				requestRates: &openrtb_ext.ExtRequestCurrency{
   108  					ConversionRates: customRates,
   109  					UsePBSRates:     ptrutil.ToPtr(true),
   110  				},
   111  			},
   112  			assertRates: customRates,
   113  		},
   114  		{
   115  			name: "empty ConversionRates, valid pbsRates, false UsePBSRates. Because pbsRates cannot be used, disable currency conversion",
   116  			args: args{
   117  				currencyConverter: setupMockRateConverter(pbsRates),
   118  				requestRates: &openrtb_ext.ExtRequestCurrency{
   119  					// ConversionRates inCustomRates not initialized makes for a zero-length map
   120  					UsePBSRates: ptrutil.ToPtr(false),
   121  				},
   122  			},
   123  			assertRates: nil,
   124  		},
   125  		{
   126  			name: "nil ConversionRates, valid pbsRates, true UsePBSRates. Resulting rates will be identical to pbsRates",
   127  			args: args{
   128  				currencyConverter: setupMockRateConverter(pbsRates),
   129  				requestRates:      nil,
   130  			},
   131  			assertRates: pbsRates,
   132  		},
   133  		{
   134  			name: "empty ConversionRates, nil pbsRates, false UsePBSRates. No conversion rates available, disable currency conversion",
   135  			args: args{
   136  				currencyConverter: setupMockRateConverter(pbsRates),
   137  				requestRates: &openrtb_ext.ExtRequestCurrency{
   138  					// ConversionRates inCustomRates not initialized makes for a zero-length map
   139  					UsePBSRates: ptrutil.ToPtr(false),
   140  				},
   141  			},
   142  			assertRates: nil,
   143  		},
   144  
   145  		{
   146  			name: "empty ConversionRates, nil pbsRates, true UsePBSRates. No conversion rates available, disable currency conversion",
   147  			args: args{
   148  				currencyConverter: nil,
   149  				requestRates: &openrtb_ext.ExtRequestCurrency{
   150  					// ConversionRates inCustomRates not initialized makes for a zero-length map
   151  					UsePBSRates: ptrutil.ToPtr(true),
   152  				},
   153  			},
   154  			assertRates: nil,
   155  		},
   156  		{
   157  			name: "nil customRates, nil pbsRates. No conversion rates available, disable currency conversion",
   158  			args: args{
   159  				currencyConverter: nil,
   160  				requestRates:      nil,
   161  			},
   162  			assertRates: nil,
   163  		},
   164  		{
   165  			name: "empty ConversionRates, valid pbsRates, true UsePBSRates. Resulting rates will be identical to pbsRates",
   166  			args: args{
   167  				currencyConverter: setupMockRateConverter(pbsRates),
   168  				requestRates: &openrtb_ext.ExtRequestCurrency{
   169  					// ConversionRates inCustomRates not initialized makes for a zero-length map
   170  					UsePBSRates: ptrutil.ToPtr(true),
   171  				},
   172  			},
   173  			assertRates: pbsRates,
   174  		},
   175  	}
   176  	for _, tt := range tests {
   177  		t.Run(tt.name, func(t *testing.T) {
   178  			if tt.args.currencyConverter != nil {
   179  				tt.args.currencyConverter.Run()
   180  			}
   181  			auctionRates := GetAuctionCurrencyRates(tt.args.currencyConverter, tt.args.requestRates)
   182  			if tt.args.currencyConverter == nil && tt.args.requestRates == nil && tt.assertRates == nil {
   183  				assert.Nil(t, auctionRates)
   184  			} else if tt.assertRates == nil {
   185  				rate, err := auctionRates.GetRate("USD", "MXN")
   186  				assert.Error(t, err, tt.name)
   187  				assert.Equal(t, float64(0), rate, tt.name)
   188  			} else {
   189  				for fromCurrency, rates := range tt.assertRates {
   190  					for toCurrency, expectedRate := range rates {
   191  						actualRate, err := auctionRates.GetRate(fromCurrency, toCurrency)
   192  						assert.NoError(t, err, tt.name)
   193  						assert.Equal(t, expectedRate, actualRate, tt.name)
   194  					}
   195  				}
   196  			}
   197  		})
   198  	}
   199  }