Skip to content

Commit

Permalink
improve robustness and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Nov 17, 2024
1 parent a4b2637 commit ec0cb0b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/exchangerates/bank_of_israel_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/exchangerates/bank_of_israel_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,17 @@ func TestBankOfIsraelDataSource_InvalidUnit(t *testing.T) {
"</ExchangeRatesResponseCollectioDTO>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)

actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("<ExchangeRatesResponseCollectioDTO xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/BOI.Core.Models.HotData\">\n"+
" <ExchangeRates>\n"+
" <ExchangeRateResponseDTO>\n"+
" <CurrentExchangeRate>1</CurrentExchangeRate>\n"+
" <Key>USD</Key>\n"+
" <LastUpdate>2024-11-11T13:26:05.6590204Z</LastUpdate>\n"+
" <Unit>0</Unit>\n"+
" </ExchangeRateResponseDTO>\n"+
" </ExchangeRates>\n"+
"</ExchangeRatesResponseCollectioDTO>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
5 changes: 5 additions & 0 deletions pkg/exchangerates/central_bank_of_hungary_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestCentralBankOfHungaryDataSource_InvalidUnit(t *testing.T) {
"<GetCurrentExchangeRatesResult>"+
"&lt;MNBCurrentExchangeRates&gt;"+
"&lt;Day date=\"2024-11-15\"&gt;"+
"&lt;Rate unit=\"\" curr=\"USD\"&gt;384,48&lt;/Rate&gt;"+
"&lt;Rate unit=\"0\" curr=\"USD\"&gt;384,48&lt;/Rate&gt;"+
"&lt;/Day&gt;"+
"&lt;/MNBCurrentExchangeRates&gt;"+
"</GetCurrentExchangeRatesResult>"+
Expand Down
3 changes: 3 additions & 0 deletions pkg/exchangerates/norges_bank_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/exchangerates/norges_bank_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,15 @@ func TestNorgesBankDataSource_InvalidUnit(t *testing.T) {
"</message:StructureSpecificData>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)

actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<message:StructureSpecificData xmlns:ss=\"http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific\" xmlns:footer=\"http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message/footer\" xmlns:ns1=\"urn:sdmx:org.sdmx.infomodel.datastructure.Dataflow=NB:EXR(1.0):ObsLevelDim:TIME_PERIOD\" xmlns:message=\"http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message\" xmlns:common=\"http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xsi:schemaLocation=\"http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message https://registry.sdmx.org/schemas/v2_1/SDMXMessage.xsd urn:sdmx:org.sdmx.infomodel.datastructure.Dataflow=NB:EXR(1.0):ObsLevelDim:TIME_PERIOD https://data.norges-bank.no/api/schema/dataflow/NB/EXR/1.0?format=sdmx-2.1\">"+
" <message:DataSet>\n"+
" <Series BASE_CUR=\"USD\" QUOTE_CUR=\"NOK\" UNIT_MULT=\"-1\">\n"+
" <Obs TIME_PERIOD=\"2024-11-15\" OBS_VALUE=\"11.0545\" />\n"+
" </Series>\n"+
" </message:DataSet>\n"+
"</message:StructureSpecificData>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
3 changes: 3 additions & 0 deletions pkg/exchangerates/swiss_national_bank_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions pkg/exchangerates/swiss_national_bank_datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,29 @@ func TestSwissNationalBankDataSource_InvalidUnit(t *testing.T) {
"</rss>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)

actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<rss xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cb=\"http://www.cbwiki.net/wiki/index.php/Specification_1.2/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" version=\"2.0\">\n"+
" <channel>\n"+
" <pubDate>Tue, 12 Nov 2024 11:00:50 GMT</pubDate>\n"+
" <item>\n"+
" <cb:statistics rdf:parseType=\"Resource\">\n"+
" <cb:exchangeRate rdf:parseType=\"Resource\">\n"+
" <cb:observation rdf:parseType=\"Resource\">\n"+
" <cb:value>0.9378</cb:value>\n"+
" <cb:unit>CHF</cb:unit>\n"+
" <cb:unit_mult>0</cb:unit_mult>\n"+
" </cb:observation>\n"+
" <cb:baseCurrency>CHF</cb:baseCurrency>\n"+
" <cb:targetCurrency>EUR</cb:targetCurrency>\n"+
" <cb:observationPeriod rdf:parseType=\"Resource\">\n"+
" <cb:period>2024-11-12</cb:period>\n"+
" </cb:observationPeriod>\n"+
" </cb:exchangeRate>\n"+
" </cb:statistics>\n"+
" </item>\n"+
" </channel>\n"+
"</rss>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}

0 comments on commit ec0cb0b

Please sign in to comment.