code refactor
This commit is contained in:
@@ -36,9 +36,9 @@ type BankOfCanadaExchangeRateData struct {
|
||||
type BankOfCanadaObservationData map[string]any
|
||||
|
||||
// ToLatestExchangeRateResponse returns a view-object according to original data from bank of Canada
|
||||
func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c *core.Context) *models.LatestExchangeRateResponse {
|
||||
func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse {
|
||||
if len(e.Observations) < 1 {
|
||||
log.ErrorfWithRequestId(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] observations is empty")
|
||||
log.Errorf(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] observations is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -82,12 +82,12 @@ func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c *core.Cont
|
||||
rate, err := utils.StringToFloat64(exchangeRate)
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] failed to parse rate, rate is %s", exchangeRate)
|
||||
log.Warnf(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] failed to parse rate, rate is %s", exchangeRate)
|
||||
continue
|
||||
}
|
||||
|
||||
if rate <= 0 {
|
||||
log.WarnfWithRequestId(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] rate is invalid, rate is %s", exchangeRate)
|
||||
log.Warnf(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] rate is invalid, rate is %s", exchangeRate)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c *core.Cont
|
||||
timezone, err := time.LoadLocation(bankOfCanadaDataUpdateDateTimezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", bankOfCanadaDataUpdateDateTimezone)
|
||||
log.Errorf(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", bankOfCanadaDataUpdateDateTimezone)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c *core.Cont
|
||||
updateTime, err := time.ParseInLocation(bankOfCanadaDataUpdateDateFormat, updateDateTime, timezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
log.Errorf(c, "[bank_of_canada_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -135,19 +135,19 @@ func (e *BankOfCanadaDataSource) GetRequestUrls() []string {
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the bank of Canada data source raw response
|
||||
func (e *BankOfCanadaDataSource) Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
func (e *BankOfCanadaDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
bankOfCanadaData := &BankOfCanadaExchangeRateData{}
|
||||
err := json.Unmarshal(content, bankOfCanadaData)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[bank_of_canada_datasource.Parse] failed to parse json data, content is %s, because %s", string(content), err.Error())
|
||||
log.Errorf(c, "[bank_of_canada_datasource.Parse] failed to parse json data, content is %s, because %s", string(content), err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse := bankOfCanadaData.ToLatestExchangeRateResponse(c)
|
||||
|
||||
if latestExchangeRateResponse == nil {
|
||||
log.ErrorfWithRequestId(c, "[bank_of_canada_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
log.Errorf(c, "[bank_of_canada_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package exchangerates
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
@@ -32,9 +31,7 @@ const bankOfCanadaMinimumRequiredContent = "{\n" +
|
||||
|
||||
func TestBankOfCanadaDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(bankOfCanadaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -43,9 +40,7 @@ func TestBankOfCanadaDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(bankOfCanadaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -65,9 +60,7 @@ func TestBankOfCanadaDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_BlankContent(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte(""))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -75,9 +68,7 @@ func TestBankOfCanadaDataSource_BlankContent(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_EmptyJsonObject(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("{}"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -85,9 +76,7 @@ func TestBankOfCanadaDataSource_EmptyJsonObject(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_EmptyObservationsContent(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("{"+
|
||||
" \"observations\": []"+
|
||||
@@ -97,9 +86,7 @@ func TestBankOfCanadaDataSource_EmptyObservationsContent(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_InvalidObservationFormat(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("{"+
|
||||
" \"observations\": [\n"+
|
||||
@@ -117,9 +104,7 @@ func TestBankOfCanadaDataSource_InvalidObservationFormat(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_InvalidObservationFormat2(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("{"+
|
||||
" \"observations\": [\n"+
|
||||
@@ -137,9 +122,7 @@ func TestBankOfCanadaDataSource_InvalidObservationFormat2(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_InvalidCurrency(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("{"+
|
||||
" \"observations\": [\n"+
|
||||
@@ -157,9 +140,7 @@ func TestBankOfCanadaDataSource_InvalidCurrency(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_EmptyRate(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("{"+
|
||||
" \"observations\": [\n"+
|
||||
@@ -177,9 +158,7 @@ func TestBankOfCanadaDataSource_EmptyRate(t *testing.T) {
|
||||
|
||||
func TestBankOfCanadaDataSource_InvalidRate(t *testing.T) {
|
||||
dataSource := &BankOfCanadaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("{"+
|
||||
" \"observations\": [\n"+
|
||||
|
||||
@@ -33,18 +33,18 @@ func (e *CzechNationalBankDataSource) GetRequestUrls() []string {
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the czech nation bank data source raw response
|
||||
func (e *CzechNationalBankDataSource) Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
func (e *CzechNationalBankDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
lines := strings.Split(string(content), "\n")
|
||||
|
||||
if len(lines) < 3 {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] content is invalid, content is %s", string(content))
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] content is invalid, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
headerLineItems := strings.Split(lines[0], "#")
|
||||
|
||||
if len(headerLineItems) != 2 {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] first line of content is invalid, content is %s", lines[0])
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] first line of content is invalid, content is %s", lines[0])
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
@@ -60,21 +60,21 @@ func (e *CzechNationalBankDataSource) Parse(c *core.Context, content []byte) (*m
|
||||
currencyCodeColumnIndex, exists := titleItemMap["Code"]
|
||||
|
||||
if !exists {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] missing currency code column in title line, title line is %s", lines[1])
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] missing currency code column in title line, title line is %s", lines[1])
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
amountColumnIndex, exists := titleItemMap["Amount"]
|
||||
|
||||
if !exists {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] missing amount column in title line, title line is %s", lines[1])
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] missing amount column in title line, title line is %s", lines[1])
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
rateColumnIndex, exists := titleItemMap["Rate"]
|
||||
|
||||
if !exists {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] missing rate column in title line, title line is %s", lines[1])
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] missing rate column in title line, title line is %s", lines[1])
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ func (e *CzechNationalBankDataSource) Parse(c *core.Context, content []byte) (*m
|
||||
timezone, err := time.LoadLocation(czechNationalBankDataUpdateDateTimezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] failed to get timezone, timezone name is %s", czechNationalBankDataUpdateDateTimezone)
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] failed to get timezone, timezone name is %s", czechNationalBankDataUpdateDateTimezone)
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func (e *CzechNationalBankDataSource) Parse(c *core.Context, content []byte) (*m
|
||||
updateTime, err := time.ParseInLocation(czechNationalBankDataUpdateDateFormat, updateDateTime, timezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[czech_national_bank_datasource.Parse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
log.Errorf(c, "[czech_national_bank_datasource.Parse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ func (e *CzechNationalBankDataSource) Parse(c *core.Context, content []byte) (*m
|
||||
return latestExchangeRateResp, nil
|
||||
}
|
||||
|
||||
func (e *CzechNationalBankDataSource) parseExchangeRate(c *core.Context, line string, currencyCodeColumnIndex int, amountColumnIndex int, rateColumnIndex int) *models.LatestExchangeRate {
|
||||
func (e *CzechNationalBankDataSource) parseExchangeRate(c core.Context, line string, currencyCodeColumnIndex int, amountColumnIndex int, rateColumnIndex int) *models.LatestExchangeRate {
|
||||
if len(line) < 1 {
|
||||
return nil
|
||||
}
|
||||
@@ -123,7 +123,7 @@ func (e *CzechNationalBankDataSource) parseExchangeRate(c *core.Context, line st
|
||||
items := strings.Split(line, "|")
|
||||
|
||||
if currencyCodeColumnIndex >= len(items) || amountColumnIndex >= len(items) || rateColumnIndex >= len(items) {
|
||||
log.WarnfWithRequestId(c, "[czech_national_bank_datasource.parseExchangeRate] missing column in data line, line is %s", line)
|
||||
log.Warnf(c, "[czech_national_bank_datasource.parseExchangeRate] missing column in data line, line is %s", line)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -136,19 +136,19 @@ func (e *CzechNationalBankDataSource) parseExchangeRate(c *core.Context, line st
|
||||
amount, err := utils.StringToInt64(items[amountColumnIndex])
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[czech_national_bank_datasource.parseExchangeRate] failed to parse amount, line is %s", line)
|
||||
log.Warnf(c, "[czech_national_bank_datasource.parseExchangeRate] failed to parse amount, line is %s", line)
|
||||
return nil
|
||||
}
|
||||
|
||||
rate, err := utils.StringToFloat64(items[rateColumnIndex])
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[czech_national_bank_datasource.parseExchangeRate] failed to parse rate, line is %s", line)
|
||||
log.Warnf(c, "[czech_national_bank_datasource.parseExchangeRate] failed to parse rate, line is %s", line)
|
||||
return nil
|
||||
}
|
||||
|
||||
if rate <= 0 {
|
||||
log.WarnfWithRequestId(c, "[czech_national_bank_datasource.parseExchangeRate] rate is invalid, line is %s", line)
|
||||
log.Warnf(c, "[czech_national_bank_datasource.parseExchangeRate] rate is invalid, line is %s", line)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package exchangerates
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
@@ -17,9 +16,7 @@ const czechNationalBankMinimumRequiredContent = "01 Apr 2021 #64\n" +
|
||||
|
||||
func TestCzechNationalBankDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(czechNationalBankMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -28,9 +25,7 @@ func TestCzechNationalBankDataSource_StandardDataExtractBaseCurrency(t *testing.
|
||||
|
||||
func TestCzechNationalBankDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(czechNationalBankMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -46,9 +41,7 @@ func TestCzechNationalBankDataSource_StandardDataExtractExchangeRates(t *testing
|
||||
|
||||
func TestCzechNationalBankDataSource_BlankContent(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte(""))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -56,9 +49,7 @@ func TestCzechNationalBankDataSource_BlankContent(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_OnlyHeader(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("01 Apr 2021 #64"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -66,9 +57,7 @@ func TestCzechNationalBankDataSource_OnlyHeader(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_OnlyHeaderAndTitle(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("01 Apr 2021 #64\n"+
|
||||
"Country|Currency|Amount|Code|Rate"))
|
||||
@@ -77,9 +66,7 @@ func TestCzechNationalBankDataSource_OnlyHeaderAndTitle(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_TitleMissingCode(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("01 Apr 2021 #64\n"+
|
||||
"Country|Currency|Amount|Rate\n"+
|
||||
@@ -90,9 +77,7 @@ func TestCzechNationalBankDataSource_TitleMissingCode(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_TitleMissingRate(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("01 Apr 2021 #64\n"+
|
||||
"Country|Currency|Amount|Code\n"+
|
||||
@@ -103,9 +88,7 @@ func TestCzechNationalBankDataSource_TitleMissingRate(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_InvalidCurrency(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("01 Apr 2021 #64\n"+
|
||||
"Country|Currency|Amount|Code|Rate\n"+
|
||||
@@ -116,9 +99,7 @@ func TestCzechNationalBankDataSource_InvalidCurrency(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_EmptyRate(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("01 Apr 2021 #64\n"+
|
||||
"Country|Currency|Amount|Code|Rate\n"+
|
||||
@@ -129,9 +110,7 @@ func TestCzechNationalBankDataSource_EmptyRate(t *testing.T) {
|
||||
|
||||
func TestCzechNationalBankDataSource_InvalidRate(t *testing.T) {
|
||||
dataSource := &CzechNationalBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("01 Apr 2021 #64\n"+
|
||||
"Country|Currency|Amount|Code|Rate\n"+
|
||||
|
||||
@@ -44,16 +44,16 @@ type EuroCentralBankExchangeRate struct {
|
||||
}
|
||||
|
||||
// ToLatestExchangeRateResponse returns a view-object according to original data from euro central bank
|
||||
func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse(c *core.Context) *models.LatestExchangeRateResponse {
|
||||
func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse {
|
||||
if len(e.AllExchangeRates) < 1 {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
|
||||
log.Errorf(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
latestEuroCentralBankExchangeRate := e.AllExchangeRates[0]
|
||||
|
||||
if len(latestEuroCentralBankExchangeRate.ExchangeRates) < 1 {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] exchange rates is empty")
|
||||
log.Errorf(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] exchange rates is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse(c *core.C
|
||||
timezone, err := time.LoadLocation(euroCentralBankDataUpdateDateTimezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", euroCentralBankDataUpdateDateTimezone)
|
||||
log.Errorf(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", euroCentralBankDataUpdateDateTimezone)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse(c *core.C
|
||||
updateTime, err := time.ParseInLocation(euroCentralBankDataUpdateDateFormat, updateDateTime, timezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
log.Errorf(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -113,19 +113,19 @@ func (e *EuroCentralBankDataSource) GetRequestUrls() []string {
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the euro central bank data source raw response
|
||||
func (e *EuroCentralBankDataSource) Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
func (e *EuroCentralBankDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
euroCentralBankData := &EuroCentralBankExchangeRateData{}
|
||||
err := xml.Unmarshal(content, euroCentralBankData)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
log.Errorf(c, "[euro_central_bank_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse := euroCentralBankData.ToLatestExchangeRateResponse(c)
|
||||
|
||||
if latestExchangeRateResponse == nil {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
log.Errorf(c, "[euro_central_bank_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package exchangerates
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
@@ -22,9 +21,7 @@ const euroCentralBankMinimumRequiredContent = "<?xml version=\"1.0\" encoding=\"
|
||||
|
||||
func TestEuroCentralBankDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(euroCentralBankMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -33,9 +30,7 @@ func TestEuroCentralBankDataSource_StandardDataExtractBaseCurrency(t *testing.T)
|
||||
|
||||
func TestEuroCentralBankDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(euroCentralBankMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -51,9 +46,7 @@ func TestEuroCentralBankDataSource_StandardDataExtractExchangeRates(t *testing.T
|
||||
|
||||
func TestEuroCentralBankDataSource_BlankContent(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte(""))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -61,9 +54,7 @@ func TestEuroCentralBankDataSource_BlankContent(t *testing.T) {
|
||||
|
||||
func TestEuroCentralBankDataSource_OnlyXMLHeader(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -71,9 +62,7 @@ func TestEuroCentralBankDataSource_OnlyXMLHeader(t *testing.T) {
|
||||
|
||||
func TestEuroCentralBankDataSource_EmptyEnvelopeContent(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
|
||||
"<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">"+
|
||||
@@ -83,9 +72,7 @@ func TestEuroCentralBankDataSource_EmptyEnvelopeContent(t *testing.T) {
|
||||
|
||||
func TestEuroCentralBankDataSource_EmptyCubeContent(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
|
||||
"<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">"+
|
||||
@@ -97,9 +84,7 @@ func TestEuroCentralBankDataSource_EmptyCubeContent(t *testing.T) {
|
||||
|
||||
func TestEuroCentralBankDataSource_InvalidCurrency(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
|
||||
"<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">"+
|
||||
@@ -115,9 +100,7 @@ func TestEuroCentralBankDataSource_InvalidCurrency(t *testing.T) {
|
||||
|
||||
func TestEuroCentralBankDataSource_EmptyRate(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
|
||||
"<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">"+
|
||||
@@ -133,9 +116,7 @@ func TestEuroCentralBankDataSource_EmptyRate(t *testing.T) {
|
||||
|
||||
func TestEuroCentralBankDataSource_InvalidRate(t *testing.T) {
|
||||
dataSource := &EuroCentralBankDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
|
||||
"<gesmes:Envelope xmlns:gesmes=\"http://www.gesmes.org/xml/2002-08-01\" xmlns=\"http://www.ecb.int/vocabulary/2002-08-01/eurofxref\">"+
|
||||
|
||||
@@ -11,5 +11,5 @@ type ExchangeRatesDataSource interface {
|
||||
GetRequestUrls() []string
|
||||
|
||||
// Parse returns the common response entity according to the data source raw response
|
||||
Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error)
|
||||
Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error)
|
||||
}
|
||||
|
||||
@@ -42,9 +42,9 @@ type NationalBankOfPolandExchangeRate struct {
|
||||
}
|
||||
|
||||
// ToLatestExchangeRateResponse returns a view-object according to original data from National Bank of Poland
|
||||
func (e *NationalBankOfPolandExchangeRateData) ToLatestExchangeRateResponse(c *core.Context) *models.LatestExchangeRateResponse {
|
||||
func (e *NationalBankOfPolandExchangeRateData) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse {
|
||||
if len(e.AllExchangeRates) < 1 {
|
||||
log.ErrorfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
|
||||
log.Errorf(c, "[national_bank_of_poland_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (e *NationalBankOfPolandExchangeRateData) ToLatestExchangeRateResponse(c *c
|
||||
timezone, err := time.LoadLocation(nationalBankOfPolandDataUpdateDateTimezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", nationalBankOfPolandDataUpdateDateTimezone)
|
||||
log.Errorf(c, "[national_bank_of_poland_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", nationalBankOfPolandDataUpdateDateTimezone)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (e *NationalBankOfPolandExchangeRateData) ToLatestExchangeRateResponse(c *c
|
||||
updateTime, err := time.ParseInLocation(nationalBankOfPolandDataUpdateDateFormat, updateDateTime, timezone)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
log.Errorf(c, "[national_bank_of_poland_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -93,16 +93,16 @@ 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 {
|
||||
func (e *NationalBankOfPolandExchangeRate) ToLatestExchangeRate(c core.Context) *models.LatestExchangeRate {
|
||||
rate, err := utils.StringToFloat64(e.Rate)
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRate] failed to parse rate, currency is %s, rate is %s", e.Currency, e.Rate)
|
||||
log.Warnf(c, "[national_bank_of_poland_datasource.ToLatestExchangeRate] failed to parse rate, currency is %s, rate is %s", e.Currency, e.Rate)
|
||||
return nil
|
||||
}
|
||||
|
||||
if rate <= 0 {
|
||||
log.WarnfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRate] rate is invalid, currency is %s, rate is %s", e.Currency, e.Rate)
|
||||
log.Warnf(c, "[national_bank_of_poland_datasource.ToLatestExchangeRate] rate is invalid, currency is %s, rate is %s", e.Currency, e.Rate)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ func (e *NationalBankOfPolandDataSource) GetRequestUrls() []string {
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the National Bank of Poland data source raw response
|
||||
func (e *NationalBankOfPolandDataSource) Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
func (e *NationalBankOfPolandDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
nationalBankOfPolandData := &NationalBankOfPolandExchangeRateData{}
|
||||
|
||||
xmlDecoder := xml.NewDecoder(bytes.NewReader(content))
|
||||
@@ -132,14 +132,14 @@ func (e *NationalBankOfPolandDataSource) Parse(c *core.Context, content []byte)
|
||||
err := xmlDecoder.Decode(&nationalBankOfPolandData)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[national_bank_of_poland_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
log.Errorf(c, "[national_bank_of_poland_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse := nationalBankOfPolandData.ToLatestExchangeRateResponse(c)
|
||||
|
||||
if latestExchangeRateResponse == nil {
|
||||
log.ErrorfWithRequestId(c, "[national_bank_of_poland_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
log.Errorf(c, "[national_bank_of_poland_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package exchangerates
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
@@ -29,9 +28,7 @@ const nationalBankOfPolandMinimumRequiredContent = "<?xml version=\"1.0\" encodi
|
||||
|
||||
func TestNationalBankOfPolandDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfPolandMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -40,9 +37,7 @@ func TestNationalBankOfPolandDataSource_StandardDataExtractBaseCurrency(t *testi
|
||||
|
||||
func TestNationalBankOfPolandDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfPolandMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -58,9 +53,7 @@ func TestNationalBankOfPolandDataSource_StandardDataExtractExchangeRates(t *test
|
||||
|
||||
func TestNationalBankOfPolandDataSource_BlankContent(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte(""))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -68,9 +61,7 @@ func TestNationalBankOfPolandDataSource_BlankContent(t *testing.T) {
|
||||
|
||||
func TestNationalBankOfPolandDataSource_OnlyXMLHeader(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -78,9 +69,7 @@ func TestNationalBankOfPolandDataSource_OnlyXMLHeader(t *testing.T) {
|
||||
|
||||
func TestNationalBankOfPolandDataSource_EmptyArrayOfExchangeRatesTable(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, 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"+
|
||||
@@ -90,9 +79,7 @@ func TestNationalBankOfPolandDataSource_EmptyArrayOfExchangeRatesTable(t *testin
|
||||
|
||||
func TestNationalBankOfPolandDataSource_EmptyExchangeRatesTable(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, 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"+
|
||||
@@ -104,9 +91,7 @@ func TestNationalBankOfPolandDataSource_EmptyExchangeRatesTable(t *testing.T) {
|
||||
|
||||
func TestNationalBankOfPolandDataSource_EmptyExchangeRatesContent(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, 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"+
|
||||
@@ -121,9 +106,7 @@ func TestNationalBankOfPolandDataSource_EmptyExchangeRatesContent(t *testing.T)
|
||||
|
||||
func TestNationalBankOfPolandDataSource_InvalidCurrency(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
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"+
|
||||
@@ -143,9 +126,7 @@ func TestNationalBankOfPolandDataSource_InvalidCurrency(t *testing.T) {
|
||||
|
||||
func TestNationalBankOfPolandDataSource_EmptyRate(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
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"+
|
||||
@@ -165,9 +146,7 @@ func TestNationalBankOfPolandDataSource_EmptyRate(t *testing.T) {
|
||||
|
||||
func TestNationalBankOfPolandDataSource_InvalidRate(t *testing.T) {
|
||||
dataSource := &NationalBankOfPolandDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
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"+
|
||||
|
||||
@@ -60,14 +60,14 @@ type ReserveBankOfAustraliaExchangeRateObservation struct {
|
||||
}
|
||||
|
||||
// ToLatestExchangeRateResponse returns a view-object according to original data from the reserve bank of Australia
|
||||
func (e *ReserveBankOfAustraliaData) ToLatestExchangeRateResponse(c *core.Context) *models.LatestExchangeRateResponse {
|
||||
func (e *ReserveBankOfAustraliaData) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse {
|
||||
if e.Channel == nil {
|
||||
log.ErrorfWithRequestId(c, "[reserve_bank_of_australia_datasource.ToLatestExchangeRateResponse] rss channel does not exist")
|
||||
log.Errorf(c, "[reserve_bank_of_australia_datasource.ToLatestExchangeRateResponse] rss channel does not exist")
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(e.Items) < 1 {
|
||||
log.ErrorfWithRequestId(c, "[reserve_bank_of_australia_datasource.ToLatestExchangeRateResponse] rss items is empty")
|
||||
log.Errorf(c, "[reserve_bank_of_australia_datasource.ToLatestExchangeRateResponse] rss items is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (e *ReserveBankOfAustraliaData) ToLatestExchangeRateResponse(c *core.Contex
|
||||
updateTime, err := time.Parse(reserveBankOfAustraliaDataUpdateDateFormat, updateDateTime)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[reserve_bank_of_australia_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
log.Errorf(c, "[reserve_bank_of_australia_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -128,19 +128,19 @@ func (e *ReserveBankOfAustraliaDataSource) GetRequestUrls() []string {
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the the reserve bank of Australia data source raw response
|
||||
func (e *ReserveBankOfAustraliaDataSource) Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
func (e *ReserveBankOfAustraliaDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
reserveBankOfAustraliaData := &ReserveBankOfAustraliaData{}
|
||||
err := xml.Unmarshal(content, reserveBankOfAustraliaData)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[reserve_bank_of_australia_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
log.Errorf(c, "[reserve_bank_of_australia_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse := reserveBankOfAustraliaData.ToLatestExchangeRateResponse(c)
|
||||
|
||||
if latestExchangeRateResponse == nil {
|
||||
log.ErrorfWithRequestId(c, "[reserve_bank_of_australia_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
log.Errorf(c, "[reserve_bank_of_australia_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package exchangerates
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
@@ -43,9 +42,7 @@ const reserveBankOfAustraliaMinimumRequiredContent = "<?xml version=\"1.0\" enco
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(reserveBankOfAustraliaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -54,9 +51,7 @@ func TestReserveBankOfAustraliaDataSource_StandardDataExtractBaseCurrency(t *tes
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(reserveBankOfAustraliaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
@@ -72,9 +67,7 @@ func TestReserveBankOfAustraliaDataSource_StandardDataExtractExchangeRates(t *te
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_BlankContent(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte(""))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -82,9 +75,7 @@ func TestReserveBankOfAustraliaDataSource_BlankContent(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_OnlyXMLHeader(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
@@ -92,9 +83,7 @@ func TestReserveBankOfAustraliaDataSource_OnlyXMLHeader(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_EmptyRDFContent(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -104,9 +93,7 @@ func TestReserveBankOfAustraliaDataSource_EmptyRDFContent(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_EmptyChannelContent(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -130,9 +117,7 @@ func TestReserveBankOfAustraliaDataSource_EmptyChannelContent(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_NoItem(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -145,9 +130,7 @@ func TestReserveBankOfAustraliaDataSource_NoItem(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_BaseCurrencyNotEqualPreset(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -173,9 +156,7 @@ func TestReserveBankOfAustraliaDataSource_BaseCurrencyNotEqualPreset(t *testing.
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_UnitCurrencyNotEqualPreset(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -201,9 +182,7 @@ func TestReserveBankOfAustraliaDataSource_UnitCurrencyNotEqualPreset(t *testing.
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_InvalidCurrency(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -229,9 +208,7 @@ func TestReserveBankOfAustraliaDataSource_InvalidCurrency(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_EmptyRate(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
@@ -257,9 +234,7 @@ func TestReserveBankOfAustraliaDataSource_EmptyRate(t *testing.T) {
|
||||
|
||||
func TestReserveBankOfAustraliaDataSource_InvalidRate(t *testing.T) {
|
||||
dataSource := &ReserveBankOfAustraliaDataSource{}
|
||||
context := &core.Context{
|
||||
Context: &gin.Context{},
|
||||
}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rba=\"https://www.rba.gov.au/statistics/frequency/exchange-rates.html\" 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:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://purl.org/rss/1.0/\" xsi:schemaLocation=\"http://www.w3.org/1999/02/22-rdf-syntax-ns# rdf.xsd\">\n"+
|
||||
|
||||
Reference in New Issue
Block a user