Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pkg/importers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func (b *BaseParser) ToCreateRequests(
newTx.InternalReferenceNumbers = append(newTx.InternalReferenceNumbers, b.toKey(dup, importSource))
}

if tx.ParsingError != nil {
newTx.Extra["parsing_error"] = tx.ParsingError.Error()
requests = append(requests, newTx)
continue
}

switch tx.Type {
case TransactionTypeIncome:
sourceAccount, err := b.GetDefaultAccountAndAmount(
Expand Down Expand Up @@ -221,10 +227,7 @@ func (b *BaseParser) ToCreateRequests(
if newTx.Extra == nil {
newTx.Extra = make(map[string]string)
}
Comment on lines 227 to 229

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The newTx.Extra field is initialized with make(map[string]string) when newTx is created on line 58, so it will never be nil. This check is redundant and can be removed.

if tx.ParsingError != nil {
newTx.Extra["parsing_error"] = tx.ParsingError.Error()
}
// this is error transaction, will handle next
newTx.Extra["unknown_type"] = fmt.Sprintf("%d", tx.Type)
}

requests = append(requests, newTx)
Expand Down
201 changes: 201 additions & 0 deletions pkg/importers/common_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package importers_test

import (
"context"
"testing"
"time"

importv1 "buf.build/gen/go/xskydev/go-money-pb/protocolbuffers/go/gomoneypb/import/v1"
transactionsv1 "buf.build/gen/go/xskydev/go-money-pb/protocolbuffers/go/gomoneypb/transactions/v1"
v1 "buf.build/gen/go/xskydev/go-money-pb/protocolbuffers/go/gomoneypb/v1"
"github.com/cockroachdb/errors"
"github.com/ft-t/go-money/pkg/database"
"github.com/ft-t/go-money/pkg/importers"
"github.com/golang/mock/gomock"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetAccountMapByNumbers(t *testing.T) {
Expand Down Expand Up @@ -155,6 +163,199 @@ func TestGetAccountMapByNumbers(t *testing.T) {
})
}

func TestToCreateRequests_ParsingError_Success(t *testing.T) {
testCases := []struct {
name string
transaction *importers.Transaction
expectedErr string
}{
{
name: "expense with parsing error skips expense processing",
transaction: &importers.Transaction{
ID: "test-1",
Type: importers.TransactionTypeExpense,
SourceAmount: decimal.Zero,
SourceCurrency: "PLN",
DestinationAmount: decimal.Zero,
DestinationCurrency: "PLN",
Date: time.Now(),
Description: "Pending transaction",
ParsingError: errors.New("transaction is still pending"),
},
expectedErr: "transaction is still pending",
},
{
name: "income with parsing error skips income processing",
transaction: &importers.Transaction{
ID: "test-2",
Type: importers.TransactionTypeIncome,
SourceAmount: decimal.Zero,
SourceCurrency: "PLN",
DestinationAmount: decimal.Zero,
DestinationCurrency: "PLN",
Date: time.Now(),
Description: "Invalid income",
ParsingError: errors.New("invalid amount format"),
},
expectedErr: "invalid amount format",
},
{
name: "transfer with parsing error skips transfer processing",
transaction: &importers.Transaction{
ID: "test-3",
Type: importers.TransactionTypeInternalTransfer,
SourceAmount: decimal.Zero,
SourceCurrency: "PLN",
DestinationAmount: decimal.Zero,
DestinationCurrency: "PLN",
Date: time.Now(),
Description: "Invalid transfer",
ParsingError: errors.New("missing account"),
},
expectedErr: "missing account",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

currencyConverter := NewMockCurrencyConverterSvc(ctrl)
bp := importers.NewBaseParser(currencyConverter, nil, nil)

accountMap := map[string]*database.Account{
"test-account": {
ID: 1,
Currency: "PLN",
Type: v1.AccountType_ACCOUNT_TYPE_ASSET,
},
}

requests, err := bp.ToCreateRequests(
context.Background(),
[]*importers.Transaction{tc.transaction},
false,
accountMap,
importv1.ImportSource_IMPORT_SOURCE_BNP_PARIBAS_POLSKA,
)

require.NoError(t, err)
require.Len(t, requests, 1)

req := requests[0]
assert.Nil(t, req.Transaction)
assert.Equal(t, tc.expectedErr, req.Extra["parsing_error"])
assert.Equal(t, tc.transaction.Description, req.Title)
})
}
}

func TestToCreateRequests_UnknownType_Success(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

currencyConverter := NewMockCurrencyConverterSvc(ctrl)
bp := importers.NewBaseParser(currencyConverter, nil, nil)

transaction := &importers.Transaction{
ID: "test-unknown",
Type: importers.TransactionType(99),
SourceAmount: decimal.NewFromInt(100),
SourceCurrency: "PLN",
DestinationAmount: decimal.NewFromInt(100),
DestinationCurrency: "PLN",
Date: time.Now(),
Description: "Unknown type transaction",
}

accountMap := map[string]*database.Account{
"test-account": {
ID: 1,
Currency: "PLN",
Type: v1.AccountType_ACCOUNT_TYPE_ASSET,
},
}

requests, err := bp.ToCreateRequests(
context.Background(),
[]*importers.Transaction{transaction},
false,
accountMap,
importv1.ImportSource_IMPORT_SOURCE_BNP_PARIBAS_POLSKA,
)

require.NoError(t, err)
require.Len(t, requests, 1)

req := requests[0]
assert.Nil(t, req.Transaction)
assert.Equal(t, "99", req.Extra["unknown_type"])
}

func TestToCreateRequests_ExpenseWithValidData_Success(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

currencyConverter := NewMockCurrencyConverterSvc(ctrl)
currencyConverter.EXPECT().
Convert(gomock.Any(), "PLN", "EUR", gomock.Any()).
Return(decimal.NewFromInt(25), nil).
Times(1)

bp := importers.NewBaseParser(currencyConverter, nil, nil)

transaction := &importers.Transaction{
ID: "test-expense",
Type: importers.TransactionTypeExpense,
SourceAmount: decimal.NewFromInt(100),
SourceCurrency: "PLN",
SourceAccount: "source-acc",
DestinationAmount: decimal.NewFromInt(100),
DestinationCurrency: "PLN",
Date: time.Now(),
Description: "Valid expense",
}

accountMap := map[string]*database.Account{
"source-acc": {
ID: 1,
Currency: "PLN",
Type: v1.AccountType_ACCOUNT_TYPE_ASSET,
},
"default-expense": {
ID: 2,
Currency: "EUR",
Type: v1.AccountType_ACCOUNT_TYPE_EXPENSE,
Flags: database.AccountFlagIsDefault,
},
}

requests, err := bp.ToCreateRequests(
context.Background(),
[]*importers.Transaction{transaction},
false,
accountMap,
importv1.ImportSource_IMPORT_SOURCE_BNP_PARIBAS_POLSKA,
)

require.NoError(t, err)
require.Len(t, requests, 1)

req := requests[0]
require.NotNil(t, req.Transaction)

expense, ok := req.Transaction.(*transactionsv1.CreateTransactionRequest_Expense)
require.True(t, ok)
assert.Equal(t, int32(1), expense.Expense.SourceAccountId)
assert.Equal(t, "-100", expense.Expense.SourceAmount)
assert.Equal(t, "PLN", expense.Expense.SourceCurrency)
assert.Equal(t, int32(2), expense.Expense.DestinationAccountId)
assert.Equal(t, "25", expense.Expense.DestinationAmount)
assert.Equal(t, "EUR", expense.Expense.DestinationCurrency)
assert.Empty(t, req.Extra["parsing_error"])
}

func TestDecodeFiles(t *testing.T) {
bp := importers.NewBaseParser(nil, nil, nil)

Expand Down
Loading