update exchange rates data api of National Bank Of Poland

This commit is contained in:
MaysWind
2024-03-03 12:12:11 +08:00
parent 008c58f52b
commit 6511c4e810
2 changed files with 98 additions and 40 deletions
@@ -14,9 +14,9 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/validators"
)
const nationalBankOfPolandDailyExchangeRateUrl = "https://www.nbp.pl/kursy/xml/en/lastaen.xml"
const nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl = "https://www.nbp.pl/kursy/xml/en/lastben.xml"
const nationalBankOfPolandExchangeRateReferenceUrl = "https://www.nbp.pl/homen.aspx?f=/kursy/kursyen.htm"
const nationalBankOfPolandDailyExchangeRateUrl = "https://api.nbp.pl/api/exchangerates/tables/A?format=xml"
const nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl = "https://api.nbp.pl/api/exchangerates/tables/B?format=xml"
const nationalBankOfPolandExchangeRateReferenceUrl = "https://nbp.pl/en/statistic-and-financial-reporting/rates/"
const nationalBankOfPolandDataSource = "Narodowy Bank Polski"
const nationalBankOfPolandBaseCurrency = "PLN"
@@ -30,16 +30,15 @@ type NationalBankOfPolandDataSource struct {
// NationalBankOfPolandExchangeRateData represents the whole data from National Bank of Poland
type NationalBankOfPolandExchangeRateData struct {
XMLName xml.Name `xml:"exchange_rates"`
Date string `xml:"date,attr"`
AllExchangeRates []*NationalBankOfPolandExchangeRate `xml:"mid-rate"`
XMLName xml.Name `xml:"ArrayOfExchangeRatesTable"`
Date string `xml:"ExchangeRatesTable>EffectiveDate"`
AllExchangeRates []*NationalBankOfPolandExchangeRate `xml:"ExchangeRatesTable>Rates>Rate"`
}
// NationalBankOfPolandExchangeRate represents the exchange rate data from National Bank of Poland
type NationalBankOfPolandExchangeRate struct {
Currency string `xml:"code,attr"`
Units string `xml:"units,attr"`
Rate string `xml:",chardata"`
Currency string `xml:"Code"`
Rate string `xml:"Mid"`
}
// ToLatestExchangeRateResponse returns a view-object according to original data from National Bank of Poland
@@ -95,13 +94,6 @@ func (e *NationalBankOfPolandExchangeRateData) ToLatestExchangeRateResponse(c *c
// ToLatestExchangeRate returns a data pair according to original data from National Bank of Poland
func (e *NationalBankOfPolandExchangeRate) ToLatestExchangeRate(c *core.Context) *models.LatestExchangeRate {
amount, err := utils.StringToInt64(e.Units)
if err != nil {
log.WarnfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRate] failed to parse amount, currency is %s, amount is %s", e.Currency, e.Units)
return nil
}
rate, err := utils.StringToFloat64(e.Rate)
if err != nil {
@@ -114,7 +106,7 @@ func (e *NationalBankOfPolandExchangeRate) ToLatestExchangeRate(c *core.Context)
return nil
}
finalRate := float64(amount) / rate
finalRate := 1 / rate
if math.IsInf(finalRate, 0) {
return nil
@@ -10,11 +10,22 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/models"
)
const nationalBankOfPolandMinimumRequiredContent = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
"<exchange_rates table=\"A\" date=\"2021-04-02\" number=\"064/A/NBP/2021\" uid=\"21a064\">\n" +
" <mid-rate currency=\"US Dollar\" units=\"1\" code=\"USD\">3.8986</mid-rate>\n" +
" <mid-rate currency=\"Yuan Renminbi\" units=\"1\" code=\"CNY\">0.5941</mid-rate>\n" +
"</exchange_rates>"
const nationalBankOfPolandMinimumRequiredContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
" <ExchangeRatesTable>\n" +
" <EffectiveDate>2024-02-28</EffectiveDate>\n" +
" <Rates>\n" +
" <Rate>\n" +
" <Code>USD</Code>\n" +
" <Mid>3.9922</Mid>\n" +
" </Rate>\n" +
" <Rate>\n" +
" <Code>CNY</Code>\n" +
" <Mid>0.5545</Mid>\n" +
" </Rate>\n" +
" </Rates>\n" +
" </ExchangeRatesTable>\n" +
"</ArrayOfExchangeRatesTable>"
func TestNationalBankOfPolandDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
dataSource := &NationalBankOfPolandDataSource{}
@@ -37,11 +48,11 @@ func TestNationalBankOfPolandDataSource_StandardDataExtractExchangeRates(t *test
assert.Equal(t, nil, err)
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
Currency: "USD",
Rate: "0.25650233417124096",
Rate: "0.2504884524823406",
})
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
Currency: "CNY",
Rate: "1.68321831341525",
Rate: "1.8034265103697025",
})
}
@@ -61,7 +72,33 @@ func TestNationalBankOfPolandDataSource_OnlyXMLHeader(t *testing.T) {
Context: &gin.Context{},
}
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"))
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfPolandDataSource_EmptyArrayOfExchangeRatesTable(t *testing.T) {
dataSource := &NationalBankOfPolandDataSource{}
context := &core.Context{
Context: &gin.Context{},
}
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+
"</ArrayOfExchangeRatesTable>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfPolandDataSource_EmptyExchangeRatesTable(t *testing.T) {
dataSource := &NationalBankOfPolandDataSource{}
context := &core.Context{
Context: &gin.Context{},
}
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+
" <ExchangeRatesTable>\n"+
" </ExchangeRatesTable>\n"+
"</ArrayOfExchangeRatesTable>"))
assert.NotEqual(t, nil, err)
}
@@ -71,9 +108,14 @@ func TestNationalBankOfPolandDataSource_EmptyExchangeRatesContent(t *testing.T)
Context: &gin.Context{},
}
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
"<exchange_rates table=\"A\" date=\"2021-04-02\" number=\"064/A/NBP/2021\" uid=\"21a064\">\n"+
"</exchange_rates>"))
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+
" <ExchangeRatesTable>\n"+
" <EffectiveDate>2024-02-28</EffectiveDate>\n"+
" <Rates>\n"+
" </Rates>\n"+
" </ExchangeRatesTable>\n"+
"</ArrayOfExchangeRatesTable>"))
assert.NotEqual(t, nil, err)
}
@@ -83,10 +125,18 @@ func TestNationalBankOfPolandDataSource_InvalidCurrency(t *testing.T) {
Context: &gin.Context{},
}
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
"<exchange_rates table=\"A\" date=\"2021-04-02\" number=\"064/A/NBP/2021\" uid=\"21a064\">\n"+
" <mid-rate currency=\"XXX\" units=\"1\" code=\"XXX\">1</mid-rate>\n"+
"</exchange_rates>"))
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+
" <ExchangeRatesTable>\n"+
" <EffectiveDate>2024-02-28</EffectiveDate>\n"+
" <Rates>\n"+
" <Rate>\n"+
" <Code>XXX</Code>\n"+
" <Mid>1</Mid>\n"+
" </Rate>\n"+
" </Rates>\n"+
" </ExchangeRatesTable>\n"+
"</ArrayOfExchangeRatesTable>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
@@ -97,10 +147,18 @@ func TestNationalBankOfPolandDataSource_EmptyRate(t *testing.T) {
Context: &gin.Context{},
}
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
"<exchange_rates table=\"A\" date=\"2021-04-02\" number=\"064/A/NBP/2021\" uid=\"21a064\">\n"+
" <mid-rate currency=\"US Dollar\" units=\"1\" code=\"USD\"></mid-rate>\n"+
"</exchange_rates>"))
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+
" <ExchangeRatesTable>\n"+
" <EffectiveDate>2024-02-28</EffectiveDate>\n"+
" <Rates>\n"+
" <Rate>\n"+
" <Code>USD</Code>\n"+
" <Mid></Mid>\n"+
" </Rate>\n"+
" </Rates>\n"+
" </ExchangeRatesTable>\n"+
"</ArrayOfExchangeRatesTable>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
@@ -111,10 +169,18 @@ func TestNationalBankOfPolandDataSource_InvalidRate(t *testing.T) {
Context: &gin.Context{},
}
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"+
"<exchange_rates table=\"A\" date=\"2021-04-02\" number=\"064/A/NBP/2021\" uid=\"21a064\">\n"+
" <mid-rate currency=\"US Dollar\" units=\"1\" code=\"USD\">null</mid-rate>\n"+
"</exchange_rates>"))
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<ArrayOfExchangeRatesTable xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"+
" <ExchangeRatesTable>\n"+
" <EffectiveDate>2024-02-28</EffectiveDate>\n"+
" <Rates>\n"+
" <Rate>\n"+
" <Code>USD</Code>\n"+
" <Mid>null</Mid>\n"+
" </Rate>\n"+
" </Rates>\n"+
" </ExchangeRatesTable>\n"+
"</ArrayOfExchangeRatesTable>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}