Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Oct 29, 2024
1 parent be7fbd4 commit 92a78f6
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/converters/ofx/ofx_data_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ofx

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/mayswind/ezbookkeeping/pkg/core"
)

func TestCreateNewOFXFileReader_OFX2(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader([]byte(
"<?xml version=\"1.0\" encoding=\"US-ASCII\"?>" +
"<?OFX OFXHEADER=\"200\" VERSION=\"211\" SECURITY=\"NONE\" OLDFILEUID=\"NONE\" NEWFILEUID=\"NONE\"?>" +
"<OFX>" +
" <BANKMSGSRSV1>" +
" <STMTTRNRS>" +
" <STMTRS>" +
" <CURDEF>CNY</CURDEF>" +
" <BANKACCTFROM>" +
" <ACCTID>123</ACCTID>" +
" </BANKACCTFROM>" +
" <BANKTRANLIST>" +
" <STMTTRN>" +
" <TRNTYPE>DEP</TRNTYPE>" +
" <DTPOSTED>20240901012345.000[+8:CST]</DTPOSTED>" +
" <TRNAMT>123.45</TRNAMT>" +
" </STMTTRN>" +
" </BANKTRANLIST>" +
" </STMTRS>" +
" </STMTTRNRS>" +
" </BANKMSGSRSV1>" +
"</OFX>"))

assert.Nil(t, err)

ofxFile, err := reader.read(context)
assert.Nil(t, err)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.BankMessageResponseV1)
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse)
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse)

assert.Equal(t, "CNY", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.DefaultCurrency)

assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom)
assert.Equal(t, "123", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom.AccountId)

assert.Equal(t, 1, len(ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions))
assert.Equal(t, ofxDepositTransaction, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].TransactionType)
assert.Equal(t, "20240901012345.000[+8:CST]", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].PostedDate)
assert.Equal(t, "123.45", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].Amount)
}

func TestCreateNewOFXFileReader_OFX2WithoutAnyHeader(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader([]byte(
"<OFX>" +
" <BANKMSGSRSV1>" +
" <STMTTRNRS>" +
" <STMTRS>" +
" <CURDEF>CNY</CURDEF>" +
" <BANKACCTFROM>" +
" <ACCTID>123</ACCTID>" +
" </BANKACCTFROM>" +
" <BANKTRANLIST>" +
" <STMTTRN>" +
" <TRNTYPE>DEP</TRNTYPE>" +
" <DTPOSTED>20240901012345.000[+8:CST]</DTPOSTED>" +
" <TRNAMT>123.45</TRNAMT>" +
" </STMTTRN>" +
" </BANKTRANLIST>" +
" </STMTRS>" +
" </STMTTRNRS>" +
" </BANKMSGSRSV1>" +
"</OFX>"))

assert.Nil(t, err)

ofxFile, err := reader.read(context)
assert.Nil(t, err)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.BankMessageResponseV1)
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse)
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse)

assert.Equal(t, "CNY", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.DefaultCurrency)

assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom)
assert.Equal(t, "123", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom.AccountId)

assert.Equal(t, 1, len(ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions))
assert.Equal(t, ofxDepositTransaction, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].TransactionType)
assert.Equal(t, "20240901012345.000[+8:CST]", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].PostedDate)
assert.Equal(t, "123.45", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].Amount)
}

0 comments on commit 92a78f6

Please sign in to comment.