From 92a78f6f122595162b2568ee81983ef4570210a5 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Wed, 30 Oct 2024 00:55:46 +0800 Subject: [PATCH] add unit tests --- pkg/converters/ofx/ofx_data_reader_test.go | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 pkg/converters/ofx/ofx_data_reader_test.go diff --git a/pkg/converters/ofx/ofx_data_reader_test.go b/pkg/converters/ofx/ofx_data_reader_test.go new file mode 100644 index 00000000..2728ed08 --- /dev/null +++ b/pkg/converters/ofx/ofx_data_reader_test.go @@ -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( + "" + + "" + + "" + + " " + + " " + + " " + + " CNY" + + " " + + " 123" + + " " + + " " + + " " + + " DEP" + + " 20240901012345.000[+8:CST]" + + " 123.45" + + " " + + " " + + " " + + " " + + " " + + "")) + + 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( + "" + + " " + + " " + + " " + + " CNY" + + " " + + " 123" + + " " + + " " + + " " + + " DEP" + + " 20240901012345.000[+8:CST]" + + " 123.45" + + " " + + " " + + " " + + " " + + " " + + "")) + + 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) +}