code refactor

This commit is contained in:
MaysWind
2024-11-02 22:47:33 +08:00
parent f2e89da724
commit 0e062ed065
8 changed files with 1252 additions and 1043 deletions
+48 -45
View File
@@ -1,6 +1,8 @@
package ofx
import (
"encoding/xml"
"github.com/mayswind/ezbookkeeping/pkg/models"
)
@@ -69,9 +71,10 @@ var ofxTransactionTypeMapping = map[ofxTransactionType]models.TransactionType{
// ofxFile represents the struct of open financial exchange (ofx) file
type ofxFile struct {
XMLName xml.Name `xml:"OFX"`
FileHeader *ofxFileHeader
BankMessageResponseV1 *ofxBankMessageResponseV1
CreditCardMessageResponseV1 *ofxCreditCardMessageResponseV1
BankMessageResponseV1 *ofxBankMessageResponseV1 `xml:"BANKMSGSRSV1"`
CreditCardMessageResponseV1 *ofxCreditCardMessageResponseV1 `xml:"CREDITCARDMSGSRSV1"`
}
// ofxFileHeader represents the struct of open financial exchange (ofx) file header
@@ -85,101 +88,101 @@ type ofxFileHeader struct {
// ofxBankMessageResponseV1 represents the struct of open financial exchange (ofx) bank message response v1
type ofxBankMessageResponseV1 struct {
StatementTransactionResponse *ofxBankStatementTransactionResponse
StatementTransactionResponse *ofxBankStatementTransactionResponse `xml:"STMTTRNRS"`
}
// ofxCreditCardMessageResponseV1 represents the struct of open financial exchange (ofx) credit card message response v1
type ofxCreditCardMessageResponseV1 struct {
StatementTransactionResponse *ofxCreditCardStatementTransactionResponse
StatementTransactionResponse *ofxCreditCardStatementTransactionResponse `xml:"CCSTMTTRNRS"`
}
// ofxBankStatementTransactionResponse represents the struct of open financial exchange (ofx) bank statement transaction response
type ofxBankStatementTransactionResponse struct {
StatementResponse *ofxBankStatementResponse
StatementResponse *ofxBankStatementResponse `xml:"STMTRS"`
}
// ofxCreditCardStatementTransactionResponse represents the struct of open financial exchange (ofx) credit card statement transaction response
type ofxCreditCardStatementTransactionResponse struct {
StatementResponse *ofxCreditCardStatementResponse
StatementResponse *ofxCreditCardStatementResponse `xml:"CCSTMTRS"`
}
// ofxBankStatementResponse represents the struct of open financial exchange (ofx) bank statement response
type ofxBankStatementResponse struct {
DefaultCurrency string
AccountFrom *ofxBankAccount
TransactionList *ofxBankTransactionList
DefaultCurrency string `xml:"CURDEF"`
AccountFrom *ofxBankAccount `xml:"BANKACCTFROM"`
TransactionList *ofxBankTransactionList `xml:"BANKTRANLIST"`
}
// ofxCreditCardStatementResponse represents the struct of open financial exchange (ofx) credit card statement response
type ofxCreditCardStatementResponse struct {
DefaultCurrency string
AccountFrom *ofxCreditCardAccount
TransactionList *ofxCreditCardTransactionList
DefaultCurrency string `xml:"CURDEF"`
AccountFrom *ofxCreditCardAccount `xml:"CCACCTFROM"`
TransactionList *ofxCreditCardTransactionList `xml:"BANKTRANLIST"`
}
// ofxBankAccount represents the struct of open financial exchange (ofx) bank account
type ofxBankAccount struct {
BankId string
BranchId string
AccountId string
AccountType ofxAccountType
AccountKey string
BankId string `xml:"BANKID"`
BranchId string `xml:"BRANCHID"`
AccountId string `xml:"ACCTID"`
AccountType ofxAccountType `xml:"ACCTTYPE"`
AccountKey string `xml:"ACCTKEY"`
}
// ofxCreditCardAccount represents the struct of open financial exchange (ofx) credit card account
type ofxCreditCardAccount struct {
AccountId string
AccountKey string
AccountId string `xml:"ACCTID"`
AccountKey string `xml:"ACCTKEY"`
}
// ofxBankTransactionList represents the struct of open financial exchange (ofx) bank transaction list
type ofxBankTransactionList struct {
StartDate string
EndDate string
StatementTransactions []*ofxBankStatementTransaction
StartDate string `xml:"DTSTART"`
EndDate string `xml:"DTEND"`
StatementTransactions []*ofxBankStatementTransaction `xml:"STMTTRN"`
}
// ofxCreditCardTransactionList represents the struct of open financial exchange (ofx) credit card transaction list
type ofxCreditCardTransactionList struct {
StartDate string
EndDate string
StatementTransactions []*ofxCreditCardStatementTransaction
StartDate string `xml:"DTSTART"`
EndDate string `xml:"DTEND"`
StatementTransactions []*ofxCreditCardStatementTransaction `xml:"STMTTRN"`
}
// ofxBaseStatementTransaction represents the struct of open financial exchange (ofx) base statement transaction
type ofxBaseStatementTransaction struct {
TransactionId string
TransactionType ofxTransactionType
PostedDate string
Amount string
Name string
Payee *ofxPayee
Memo string
Currency string
OriginalCurrency string
TransactionId string `xml:"FITID"`
TransactionType ofxTransactionType `xml:"TRNTYPE"`
PostedDate string `xml:"DTPOSTED"`
Amount string `xml:"TRNAMT"`
Name string `xml:"NAME"`
Payee *ofxPayee `xml:"PAYEE"`
Memo string `xml:"MEMO"`
Currency string `xml:"CURRENCY"`
OriginalCurrency string `xml:"ORIGCURRENCY"`
}
// ofxBankStatementTransaction represents the struct of open financial exchange (ofx) bank statement transaction
type ofxBankStatementTransaction struct {
ofxBaseStatementTransaction
AccountTo *ofxBankAccount
AccountTo *ofxBankAccount `xml:"BANKACCTTO"`
}
// ofxCreditCardStatementTransaction represents the struct of open financial exchange (ofx) credit card statement transaction
type ofxCreditCardStatementTransaction struct {
ofxBaseStatementTransaction
AccountTo *ofxCreditCardAccount
AccountTo *ofxCreditCardAccount `xml:"CCACCTTO"`
}
// ofxPayee represents the struct of open financial exchange (ofx) payee info
type ofxPayee struct {
Name string
Address1 string
Address2 string
Address3 string
City string
State string
PostalCode string
Country string
Phone string
Name string `xml:"NAME"`
Address1 string `xml:"ADDR1"`
Address2 string `xml:"ADDR2"`
Address3 string `xml:"ADDR3"`
City string `xml:"CITY"`
State string `xml:"STATE"`
PostalCode string `xml:"POSTALCODE"`
Country string `xml:"COUNTRY"`
Phone string `xml:"PHONE"`
}
File diff suppressed because it is too large Load Diff
+487 -2
View File
@@ -131,7 +131,372 @@ func TestCreateNewOFXFileReader_OFX1WithoutBreakLine(t *testing.T) {
assert.Equal(t, "123.45", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].Amount)
}
func TestCreateNewOFXFileReader_OFX1WithBlanklines(t *testing.T) {
func TestCreateNewOFXFileReader_OFX1ParseBankAccountFrom(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<BANKMSGSRSV1>\n"+
"<STMTTRNRS>\n"+
"<STMTRS>\n"+
"<BANKACCTFROM>\n"+
"<BANKID>1234567890\n"+
"<BRANCHID>2345678901\n"+
"<ACCTID>3456789012\n"+
"<ACCTTYPE>CHECKING\n"+
"<ACCTKEY>4567890123\n"+
"</BANKACCTFROM>\n"+
"</STMTRS>\n"+
"</STMTTRNRS>\n"+
"</BANKMSGSRSV1>\n"+
"</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.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom)
account := ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom
assert.Equal(t, "1234567890", account.BankId)
assert.Equal(t, "2345678901", account.BranchId)
assert.Equal(t, "3456789012", account.AccountId)
assert.Equal(t, ofxCheckingAccount, account.AccountType)
assert.Equal(t, "4567890123", account.AccountKey)
}
func TestCreateNewOFXFileReader_OFX1ParseCreditCardAccountFrom(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<CREDITCARDMSGSRSV1>\n"+
"<CCSTMTTRNRS>\n"+
"<CCSTMTRS>\n"+
"<CCACCTFROM>\n"+
"<ACCTID>3456789012\n"+
"<ACCTKEY>4567890123\n"+
"</CCACCTFROM>\n"+
"</CCSTMTRS>\n"+
"</CCSTMTTRNRS>\n"+
"</CREDITCARDMSGSRSV1>\n"+
"</OFX>"))
assert.Nil(t, err)
ofxFile, err := reader.read(context)
assert.Nil(t, err)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse.StatementResponse)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom)
account := ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse.StatementResponse.AccountFrom
assert.Equal(t, "3456789012", account.AccountId)
assert.Equal(t, "4567890123", account.AccountKey)
}
func TestCreateNewOFXFileReader_OFX1ParseBankTransactionList(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<BANKMSGSRSV1>\n"+
"<STMTTRNRS>\n"+
"<STMTRS>\n"+
"<BANKTRANLIST>\n"+
"<DTSTART>20240901012345.000[+8:CST]\n"+
"<DTEND>20240901235959.000[+8:CST]\n"+
"</BANKTRANLIST>\n"+
"</STMTRS>\n"+
"</STMTTRNRS>\n"+
"</BANKMSGSRSV1>\n"+
"</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.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList)
transactionList := ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList
assert.Equal(t, "20240901012345.000[+8:CST]", transactionList.StartDate)
assert.Equal(t, "20240901235959.000[+8:CST]", transactionList.EndDate)
}
func TestCreateNewOFXFileReader_OFX1ParseCreditTransactionList(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<CREDITCARDMSGSRSV1>\n"+
"<CCSTMTTRNRS>\n"+
"<CCSTMTRS>\n"+
"<BANKTRANLIST>\n"+
"<DTSTART>20240901012345.000[+8:CST]\n"+
"<DTEND>20240901235959.000[+8:CST]\n"+
"</BANKTRANLIST>\n"+
"</CCSTMTRS>\n"+
"</CCSTMTTRNRS>\n"+
"</CREDITCARDMSGSRSV1>\n"+
"</OFX>"))
assert.Nil(t, err)
ofxFile, err := reader.read(context)
assert.Nil(t, err)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse.StatementResponse)
assert.NotNil(t, ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList)
transactionList := ofxFile.CreditCardMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList
assert.Equal(t, "20240901012345.000[+8:CST]", transactionList.StartDate)
assert.Equal(t, "20240901235959.000[+8:CST]", transactionList.EndDate)
}
func TestCreateNewOFXFileReader_OFX1ParseTransaction(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<BANKMSGSRSV1>\n"+
"<STMTTRNRS>\n"+
"<STMTRS>\n"+
"<BANKACCTFROM>\n"+
"<ACCTID>123\n"+
"</BANKACCTFROM>\n"+
"<BANKTRANLIST>\n"+
"<STMTTRN>\n"+
"<FITID>1234567890\n"+
"<TRNTYPE>CASH\n"+
"<DTPOSTED>20240901012345.000[+8:CST]\n"+
"<TRNAMT>123.45\n"+
"<NAME>Test Name\n"+
"<MEMO>Some Text\n"+
"<CURRENCY>CNY\n"+
"<ORIGCURRENCY>USD\n"+
"</STMTTRN>\n"+
"</BANKTRANLIST>\n"+
"</STMTRS>\n"+
"</STMTTRNRS>\n"+
"</BANKMSGSRSV1>\n"+
"</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.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList)
assert.Equal(t, 1, len(ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions))
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0])
transaction := ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0]
assert.Equal(t, "1234567890", transaction.TransactionId)
assert.Equal(t, ofxCashWithdrawalTransaction, transaction.TransactionType)
assert.Equal(t, "20240901012345.000[+8:CST]", transaction.PostedDate)
assert.Equal(t, "123.45", transaction.Amount)
assert.Equal(t, "Test Name", transaction.Name)
assert.Equal(t, "Some Text", transaction.Memo)
assert.Equal(t, "CNY", transaction.Currency)
assert.Equal(t, "USD", transaction.OriginalCurrency)
}
func TestCreateNewOFXFileReader_OFX1ParseTransactionPayee(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<BANKMSGSRSV1>\n"+
"<STMTTRNRS>\n"+
"<STMTRS>\n"+
"<BANKACCTFROM>\n"+
"<ACCTID>123\n"+
"</BANKACCTFROM>\n"+
"<BANKTRANLIST>\n"+
"<STMTTRN>\n"+
"<TRNTYPE>DEP\n"+
"<DTPOSTED>20240901012345.000[+8:CST]\n"+
"<TRNAMT>123.45\n"+
"<PAYEE>\n"+
"<NAME>Test Name\n"+
"<ADDR1>Address 1\n"+
"<ADDR2>Address 2\n"+
"<ADDR3>Address 3\n"+
"<CITY>City Name\n"+
"<STATE>State Name\n"+
"<POSTALCODE>10000000\n"+
"<COUNTRY>Country Name\n"+
"<PHONE>11111111111\n"+
"</PAYEE>\n"+
"</STMTTRN>\n"+
"</BANKTRANLIST>\n"+
"</STMTRS>\n"+
"</STMTTRNRS>\n"+
"</BANKMSGSRSV1>\n"+
"</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.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList)
assert.Equal(t, 1, len(ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions))
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0])
assert.NotNil(t, ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].Payee)
payee := ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].Payee
assert.Equal(t, "Test Name", payee.Name)
assert.Equal(t, "Address 1", payee.Address1)
assert.Equal(t, "Address 2", payee.Address2)
assert.Equal(t, "Address 3", payee.Address3)
assert.Equal(t, "City Name", payee.City)
assert.Equal(t, "State Name", payee.State)
assert.Equal(t, "10000000", payee.PostalCode)
assert.Equal(t, "Country Name", payee.Country)
assert.Equal(t, "11111111111", payee.Phone)
}
func TestCreateNewOFXFileReader_OFX1WithEndElement(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"<BANKMSGSRSV1>\n"+
"<STMTTRNRS>\n"+
"<STMTRS>\n"+
"<CURDEF>CNY</CURDEF>\n"+
"<BANKACCTFROM>\n"+
"<ACCTID>123</ACCTID>\n"+
"</BANKACCTFROM>\n"+
"<BANKTRANLIST>\n"+
"<STMTTRN>\n"+
"<TRNTYPE>DEP</TRNTYPE>\n"+
"<DTPOSTED>20240901012345.000[+8:CST]</DTPOSTED>\n"+
"<TRNAMT>123.45</TRNAMT>\n"+
"</STMTTRN>\n"+
"</BANKTRANLIST>\n"+
"</STMTRS>\n"+
"</STMTTRNRS>\n"+
"</BANKMSGSRSV1>\n"+
"</OFX>"))
assert.Nil(t, err)
ofxFile, err := reader.read(context)
assert.Nil(t, err)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.FileHeader)
assert.Equal(t, ofxVersion1, ofxFile.FileHeader.OFXDeclarationVersion)
assert.Equal(t, "103", ofxFile.FileHeader.OFXDataVersion)
assert.Equal(t, "NONE", ofxFile.FileHeader.Security)
assert.Equal(t, "NONE", ofxFile.FileHeader.OldFileUid)
assert.Equal(t, "NONE", ofxFile.FileHeader.NewFileUid)
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_OFX1WithBlanklinesInHeader(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"\n"+
@@ -194,6 +559,104 @@ func TestCreateNewOFXFileReader_OFX1WithBlanklines(t *testing.T) {
assert.Equal(t, "123.45", ofxFile.BankMessageResponseV1.StatementTransactionResponse.StatementResponse.TransactionList.StatementTransactions[0].Amount)
}
func TestCreateNewOFXFileReader_OFX1WithoutCharset(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"FOO:BAR\n"+
"\n"+
"<OFX>\n"+
"</OFX>"))
assert.Nil(t, err)
ofxFile, err := reader.read(context)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.FileHeader)
assert.Equal(t, ofxVersion1, ofxFile.FileHeader.OFXDeclarationVersion)
assert.Equal(t, "103", ofxFile.FileHeader.OFXDataVersion)
assert.Equal(t, "NONE", ofxFile.FileHeader.Security)
assert.Equal(t, "NONE", ofxFile.FileHeader.OldFileUid)
assert.Equal(t, "NONE", ofxFile.FileHeader.NewFileUid)
}
func TestCreateNewOFXFileReader_OFX1WithInvalidHeaderVersion(t *testing.T) {
context := core.NewNullContext()
_, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:200\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"</OFX>"))
assert.EqualError(t, err, errs.ErrInvalidOFXFile.Message)
}
func TestCreateNewOFXFileReader_OFX1WithInvalidHeader(t *testing.T) {
context := core.NewNullContext()
_, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:XML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"\n"+
"<OFX>\n"+
"</OFX>"))
assert.EqualError(t, err, errs.ErrInvalidOFXFile.Message)
}
func TestCreateNewOFXFileReader_OFX1WithUnknownHeader(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"OFXHEADER:100\n"+
"DATA:OFXSGML\n"+
"VERSION:103\n"+
"SECURITY:NONE\n"+
"ENCODING:USASCII\n"+
"CHARSET:1252\n"+
"COMPRESSION:NONE\n"+
"OLDFILEUID:NONE\n"+
"NEWFILEUID:NONE\n"+
"FOO:BAR\n"+
"\n"+
"<OFX>\n"+
"</OFX>"))
assert.Nil(t, err)
ofxFile, err := reader.read(context)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.FileHeader)
assert.Equal(t, ofxVersion1, ofxFile.FileHeader.OFXDeclarationVersion)
assert.Equal(t, "103", ofxFile.FileHeader.OFXDataVersion)
assert.Equal(t, "NONE", ofxFile.FileHeader.Security)
assert.Equal(t, "NONE", ofxFile.FileHeader.OldFileUid)
assert.Equal(t, "NONE", ofxFile.FileHeader.NewFileUid)
}
func TestCreateNewOFXFileReader_OFX2(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
@@ -328,14 +791,15 @@ func TestCreateNewOFXFileReader_OFX2WithInvalidHeader(t *testing.T) {
"<?OFX?>"+
"<OFX>"+
"</OFX>"))
assert.EqualError(t, err, errs.ErrInvalidOFXFile.Message)
_, err = createNewOFXFileReader(context, []byte(
"<?xml version=\"1.0\" encoding=\"US-ASCII\"?>"+
"<?OFX OFXHEADER=200?>"+
"<OFX>"+
"</OFX>"))
assert.EqualError(t, err, errs.ErrInvalidOFXFile.Message)
_, err = createNewOFXFileReader(context, []byte(
"<?xml version=\"1.0\" encoding=\"US-ASCII\"?>"+
"<?OFX OFXHEADER=\"200\" VERSION=\"211\" SECURITY=\"NONE\" OLDFILEUID=\"NONE\" NEWFILEUID=\"NONE\" test=\"\"?>"+
@@ -344,6 +808,27 @@ func TestCreateNewOFXFileReader_OFX2WithInvalidHeader(t *testing.T) {
assert.EqualError(t, err, errs.ErrInvalidOFXFile.Message)
}
func TestCreateNewOFXFileReader_OFX2WithUnknownHeader(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(
"<?xml version=\"1.0\" encoding=\"US-ASCII\"?>"+
"<?OFX OFXHEADER=\"200\" VERSION=\"211\" SECURITY=\"NONE\" OLDFILEUID=\"NONE\" NEWFILEUID=\"NONE\" FOO=\"BAR\"?>"+
"<OFX>"+
"</OFX>"))
assert.Nil(t, err)
ofxFile, err := reader.read(context)
assert.NotNil(t, ofxFile)
assert.NotNil(t, ofxFile.FileHeader)
assert.Equal(t, ofxVersion2, ofxFile.FileHeader.OFXDeclarationVersion)
assert.Equal(t, "211", ofxFile.FileHeader.OFXDataVersion)
assert.Equal(t, "NONE", ofxFile.FileHeader.Security)
assert.Equal(t, "NONE", ofxFile.FileHeader.OldFileUid)
assert.Equal(t, "NONE", ofxFile.FileHeader.NewFileUid)
}
func TestCreateNewOFXFileReader_OFX2WithSGML(t *testing.T) {
context := core.NewNullContext()
reader, err := createNewOFXFileReader(context, []byte(