Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
[currencies] fix GetInfo() null ref issue (prebid#1169)
Browse files Browse the repository at this point in the history
This CL fixes the null ref on `RateConverter.GetInfo()` when rates
are nil. Issue: prebid#1136
  • Loading branch information
benjaminch authored and guscarreon committed Jan 23, 2020
1 parent a0dceab commit 55b8f12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion currencies/rate_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,15 @@ func (rc *RateConverter) Rates() Conversions {

// GetInfo returns setup information about the converter
func (rc *RateConverter) GetInfo() ConverterInfo {
var rates *map[string]map[string]float64
if rc.Rates() != nil {
rates = rc.Rates().GetRates()
}
return converterInfo{
source: rc.syncSourceURL,
fetchingInterval: rc.fetchingInterval,
lastUpdated: rc.LastUpdated(),
rates: rc.Rates().GetRates(),
rates: rates,
}
}

Expand Down
8 changes: 8 additions & 0 deletions currencies/rate_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestFetch_Success(t *testing.T) {
rates := currencyConverter.Rates()
assert.NotNil(t, rates, "Rates() should not return nil")
assert.Equal(t, expectedRates, rates, "Rates() doesn't return expected rates")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestFetch_Fail404(t *testing.T) {
Expand All @@ -92,6 +93,7 @@ func TestFetch_Fail404(t *testing.T) {
assert.Equal(t, 1, len(calledURLs), "sync URL should have been called %d times but was %d", 1, len(calledURLs))
assert.Equal(t, currencyConverter.LastUpdated(), (time.Time{}), "LastUpdated() shouldn't return a time set")
assert.Nil(t, currencyConverter.Rates(), "Rates() should return nil")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestFetch_FailErrorHttpClient(t *testing.T) {
Expand All @@ -118,6 +120,7 @@ func TestFetch_FailErrorHttpClient(t *testing.T) {
assert.Equal(t, 1, len(calledURLs), "sync URL should have been called %d times but was %d", 1, len(calledURLs))
assert.Equal(t, currencyConverter.LastUpdated(), (time.Time{}), "LastUpdated() shouldn't return a time set")
assert.Nil(t, currencyConverter.Rates(), "Rates() should return nil")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestFetch_FailBadSyncURL(t *testing.T) {
Expand All @@ -134,6 +137,7 @@ func TestFetch_FailBadSyncURL(t *testing.T) {
// Verify:
assert.Equal(t, currencyConverter.LastUpdated(), (time.Time{}), "LastUpdated() shouldn't return a time set")
assert.Nil(t, currencyConverter.Rates(), "Rates() should return nil")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestFetch_FailBadJSON(t *testing.T) {
Expand Down Expand Up @@ -174,6 +178,7 @@ func TestFetch_FailBadJSON(t *testing.T) {
assert.Equal(t, 1, len(calledURLs), "sync URL should have been called %d times but was %d", 1, len(calledURLs))
assert.Equal(t, currencyConverter.LastUpdated(), (time.Time{}), "LastUpdated() shouldn't return a time set")
assert.Nil(t, currencyConverter.Rates(), "Rates() should return nil")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestFetch_InvalidRemoteResponseContent(t *testing.T) {
Expand Down Expand Up @@ -201,6 +206,7 @@ func TestFetch_InvalidRemoteResponseContent(t *testing.T) {
assert.Equal(t, 1, len(calledURLs), "sync URL should have been called %d times but was %d", 1, len(calledURLs))
assert.Equal(t, currencyConverter.LastUpdated(), (time.Time{}), "LastUpdated() shouldn't return a time set")
assert.Nil(t, currencyConverter.Rates(), "Rates() should return nil")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -264,6 +270,7 @@ func TestInit(t *testing.T) {
assert.NotEqual(t, currencyConverter.LastUpdated(), (time.Time{}), "LastUpdated should be set")
rates := currencyConverter.Rates()
assert.Equal(t, expectedRates, rates, "Conversions.Rates weren't the expected ones")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")

if ticksCount == expectedTicks {
currencyConverter.StopPeriodicFetching()
Expand Down Expand Up @@ -361,6 +368,7 @@ func TestInitWithZeroDuration(t *testing.T) {
assert.Equal(t, (time.Time{}), currencyConverter.LastUpdated(), "LastUpdated() shouldn't be set")
_, ok := currencyConverter.Rates().(*currencies.ConstantRates)
assert.True(t, ok, "Rates should be type of `currencies.ConstantRates`")
assert.NotNil(t, currencyConverter.GetInfo(), "GetInfo() should not return nil")
}

func TestRates(t *testing.T) {
Expand Down

0 comments on commit 55b8f12

Please sign in to comment.