Skip to content

Commit

Permalink
fix(segment): prevent panic because of non mandatory fields
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
mitch000001 committed Apr 28, 2019
1 parent 5492d09 commit 1e39700
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
26 changes: 16 additions & 10 deletions segment/basic_account_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ func (a *AccountInformationV5) referencedId() string { return "HKVVB" }
func (a *AccountInformationV5) sender() string { return senderBank }

func (a *AccountInformationV5) Account() domain.AccountInformation {
accountConnection := a.AccountConnection.Val()
info := domain.AccountInformation{
AccountConnection: accountConnection,
UserID: a.UserID.Val(),
Currency: a.AccountCurrency.Val(),
Name1: a.Name1.Val(),
UserID: a.UserID.Val(),
Name1: a.Name1.Val(),
}
if a.AccountConnection != nil {
info.AccountConnection = a.AccountConnection.Val()
}
if a.AccountCurrency != nil {
info.Currency = a.AccountCurrency.Val()
}
if a.Name2 != nil {
info.Name2 = a.Name2.Val()
Expand Down Expand Up @@ -152,12 +155,15 @@ func (a *AccountInformationV6) referencedId() string { return "HKVVB" }
func (a *AccountInformationV6) sender() string { return senderBank }

func (a *AccountInformationV6) Account() domain.AccountInformation {
accountConnection := a.AccountConnection.Val()
info := domain.AccountInformation{
AccountConnection: accountConnection,
UserID: a.UserID.Val(),
Currency: a.AccountCurrency.Val(),
Name1: a.Name1.Val(),
UserID: a.UserID.Val(),
Name1: a.Name1.Val(),
}
if a.AccountConnection != nil {
info.AccountConnection = a.AccountConnection.Val()
}
if a.AccountCurrency != nil {
info.Currency = a.AccountCurrency.Val()
}
if a.Name2 != nil {
info.Name2 = a.Name2.Val()
Expand Down
75 changes: 75 additions & 0 deletions segment/basic_account_information_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package segment

import (
"reflect"
"testing"

"github.com/mitch000001/go-hbci/domain"
"github.com/mitch000001/go-hbci/element"
)

func TestAccountInformationSegment_Account(t *testing.T) {
testCases := []struct {
desc string
segment AccountInformation
expectedAccount domain.AccountInformation
}{
{
desc: "v4 all mandatory fields set",
segment: &AccountInformationV4{
AccountConnection: element.NewAccountConnection(
domain.AccountConnection{
AccountID: "123456789",
CountryCode: 280,
BankID: "1000000",
},
),
UserID: element.NewIdentification("user"),
AccountCurrency: element.NewCurrency("EUR"),
Name1: element.NewAlphaNumeric("name", 27),
},
expectedAccount: domain.AccountInformation{
AccountConnection: domain.AccountConnection{
AccountID: "123456789",
CountryCode: 280,
BankID: "1000000",
},
UserID: "user",
Currency: "EUR",
Name1: "name",
},
},
{
desc: "v5 all mandatory fields set",
segment: &AccountInformationV5{
UserID: element.NewIdentification("user"),
Name1: element.NewAlphaNumeric("name", 27),
},
expectedAccount: domain.AccountInformation{
UserID: "user",
Name1: "name",
},
},
{
desc: "v6 all mandatory fields set",
segment: &AccountInformationV6{
UserID: element.NewIdentification("user"),
Name1: element.NewAlphaNumeric("name", 27),
},
expectedAccount: domain.AccountInformation{
UserID: "user",
Name1: "name",
},
},
}

for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
account := tt.segment.Account()

if !reflect.DeepEqual(tt.expectedAccount, account) {
t.Errorf("Expected accout to equal\n%v\n\tgot\n%v\n", tt.expectedAccount, account)
}
})
}
}

0 comments on commit 1e39700

Please sign in to comment.