diff --git a/pkg/exchangerates/bank_of_israel_datasource.go b/pkg/exchangerates/bank_of_israel_datasource.go index f4b6eaba..dec22867 100644 --- a/pkg/exchangerates/bank_of_israel_datasource.go +++ b/pkg/exchangerates/bank_of_israel_datasource.go @@ -112,6 +112,11 @@ func (e *BankOfIsraelExchangeRate) ToLatestExchangeRate(c core.Context) *models. return nil } + if unit <= 0 { + log.Warnf(c, "[bank_of_israel_datasource.ToLatestExchangeRate] unit is less or equal zero, currency is %s, unit is %s", e.Currency, e.Unit) + return nil + } + finalRate := unit / rate if math.IsInf(finalRate, 0) { diff --git a/pkg/exchangerates/bank_of_israel_datasource_test.go b/pkg/exchangerates/bank_of_israel_datasource_test.go index f81512c0..11091cd2 100644 --- a/pkg/exchangerates/bank_of_israel_datasource_test.go +++ b/pkg/exchangerates/bank_of_israel_datasource_test.go @@ -177,4 +177,17 @@ func TestBankOfIsraelDataSource_InvalidUnit(t *testing.T) { "")) assert.Equal(t, nil, err) assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("\n"+ + " \n"+ + " \n"+ + " 1\n"+ + " USD\n"+ + " 2024-11-11T13:26:05.6590204Z\n"+ + " 0\n"+ + " \n"+ + " \n"+ + "")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) } diff --git a/pkg/exchangerates/central_bank_of_hungary_datasource.go b/pkg/exchangerates/central_bank_of_hungary_datasource.go index 7a2dafb1..f9f264ae 100644 --- a/pkg/exchangerates/central_bank_of_hungary_datasource.go +++ b/pkg/exchangerates/central_bank_of_hungary_datasource.go @@ -134,6 +134,11 @@ func (e *CentralBankOfHungaryExchangeRate) ToLatestExchangeRate(c core.Context) return nil } + if unit <= 0 { + log.Warnf(c, "[central_bank_of_hungary_datasource.ToLatestExchangeRate] unit is less or equal zero, currency is %s, unit is %s", e.Currency, e.Unit) + return nil + } + finalRate := unit / rate if math.IsInf(finalRate, 0) { diff --git a/pkg/exchangerates/central_bank_of_hungary_datasource_test.go b/pkg/exchangerates/central_bank_of_hungary_datasource_test.go index bb1a15a9..8f132cc5 100644 --- a/pkg/exchangerates/central_bank_of_hungary_datasource_test.go +++ b/pkg/exchangerates/central_bank_of_hungary_datasource_test.go @@ -236,7 +236,7 @@ func TestCentralBankOfHungaryDataSource_InvalidUnit(t *testing.T) { ""+ "<MNBCurrentExchangeRates>"+ "<Day date=\"2024-11-15\">"+ - "<Rate unit=\"\" curr=\"USD\">384,48</Rate>"+ + "<Rate unit=\"0\" curr=\"USD\">384,48</Rate>"+ "</Day>"+ "</MNBCurrentExchangeRates>"+ ""+ diff --git a/pkg/exchangerates/norges_bank_datasource.go b/pkg/exchangerates/norges_bank_datasource.go index 22985955..f745a467 100644 --- a/pkg/exchangerates/norges_bank_datasource.go +++ b/pkg/exchangerates/norges_bank_datasource.go @@ -144,6 +144,9 @@ func (e *NorgesBankExchangeRate) ToLatestExchangeRate(c core.Context, exchangeRa if unitExponent > 0 { finalRate = finalRate / math.Pow10(-unitExponent) + } else if unitExponent < 0 { + log.Warnf(c, "[norges_bank_datasource.ToLatestExchangeRate] unit exponent is less than zero, currency is %s, unit is %s", e.BaseCurrency, e.UnitExponent) + return nil } if math.IsInf(finalRate, 0) { diff --git a/pkg/exchangerates/norges_bank_datasource_test.go b/pkg/exchangerates/norges_bank_datasource_test.go index 926425ad..ddee0e67 100644 --- a/pkg/exchangerates/norges_bank_datasource_test.go +++ b/pkg/exchangerates/norges_bank_datasource_test.go @@ -208,4 +208,15 @@ func TestNorgesBankDataSource_InvalidUnit(t *testing.T) { "")) assert.Equal(t, nil, err) assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte(""+ + ""+ + " \n"+ + " \n"+ + " \n"+ + " \n"+ + " \n"+ + "")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) } diff --git a/pkg/exchangerates/swiss_national_bank_datasource.go b/pkg/exchangerates/swiss_national_bank_datasource.go index 10c4f2e0..814133e9 100644 --- a/pkg/exchangerates/swiss_national_bank_datasource.go +++ b/pkg/exchangerates/swiss_national_bank_datasource.go @@ -174,6 +174,9 @@ func (e *SwissNationalBankExchangeRate) ToLatestExchangeRate(c core.Context) *mo finalRate = finalRate / math.Pow10(unitExponent-1) } else if unitExponent < 0 { finalRate = finalRate * math.Pow10(-unitExponent) + } else if unitExponent == 0 { + log.Warnf(c, "[swiss_national_bank_datasource.ToLatestExchangeRate] unit exponent is zero, currency is %s", e.TargetCurrency) + return nil } if math.IsInf(finalRate, 0) { diff --git a/pkg/exchangerates/swiss_national_bank_datasource_test.go b/pkg/exchangerates/swiss_national_bank_datasource_test.go index 5c585413..ece3e3d6 100644 --- a/pkg/exchangerates/swiss_national_bank_datasource_test.go +++ b/pkg/exchangerates/swiss_national_bank_datasource_test.go @@ -385,4 +385,29 @@ func TestSwissNationalBankDataSource_InvalidUnit(t *testing.T) { "")) assert.Equal(t, nil, err) assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("\n"+ + "\n"+ + " \n"+ + " Tue, 12 Nov 2024 11:00:50 GMT\n"+ + " \n"+ + " \n"+ + " \n"+ + " \n"+ + " 0.9378\n"+ + " CHF\n"+ + " 0\n"+ + " \n"+ + " CHF\n"+ + " EUR\n"+ + " \n"+ + " 2024-11-12\n"+ + " \n"+ + " \n"+ + " \n"+ + " \n"+ + " \n"+ + "")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) }