From 6d6ee3ea1bd5b35b610dc2cf371e4bc5e39ce671 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 29 Mar 2017 05:51:12 -0400 Subject: [PATCH] Add basic test for marshalling bank statement request Also add marshalCheckRequest(), a helper function which prints the first differences found between the two strings to aid in debugging test failures. --- banking_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ request_test.go | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 banking_test.go create mode 100644 request_test.go diff --git a/banking_test.go b/banking_test.go new file mode 100644 index 0000000..e6debf0 --- /dev/null +++ b/banking_test.go @@ -0,0 +1,73 @@ +package ofxgo_test + +import ( + "github.com/aclindsa/ofxgo" + "testing" + "time" +) + +func TestMarshalBankStatementRequest(t *testing.T) { + var expectedString string = ` + + + + + 20060115112300.000[-5:EST] + myusername + Pa$$word + ENG + + BNK + 1987 + + OFXGO + 0001 + + + + + 123 + + + 318398732 + 78346129 + CHECKING + + + Y + + + + +` + + var client = ofxgo.Client{ + AppId: "OFXGO", + AppVer: "0001", + SpecVersion: "203", + } + + var request ofxgo.Request + request.Signon.UserId = "myusername" + request.Signon.UserPass = "Pa$$word" + request.Signon.Org = "BNK" + request.Signon.Fid = "1987" + + statementRequest := ofxgo.StatementRequest{ + TrnUID: "123", + BankAcctFrom: ofxgo.BankAcct{ + BankId: "318398732", + AcctId: "78346129", + AcctType: "CHECKING", + }, + Include: true, + } + request.Banking = append(request.Banking, &statementRequest) + + request.SetClientFields(&client) + // Overwrite the DtClient value set by SetClientFields to time.Now() + EST := time.FixedZone("EST", -5*60*60) + request.Signon.DtClient = ofxgo.Date(time.Date(2006, 1, 15, 11, 23, 0, 0, EST)) + + marshalCheckRequest(t, &request, expectedString) +} diff --git a/request_test.go b/request_test.go new file mode 100644 index 0000000..e989257 --- /dev/null +++ b/request_test.go @@ -0,0 +1,48 @@ +package ofxgo_test + +import ( + "github.com/aclindsa/ofxgo" + "regexp" + "testing" +) + +var ignoreSpacesRe = regexp.MustCompile(">[ \t\r\n]+<") + +func marshalCheckRequest(t *testing.T, request *ofxgo.Request, expected string) { + buf, err := request.Marshal() + if err != nil { + t.Fatalf("Unexpected error marshalling request: %s\n", err) + } + actualString := buf.String() + + // Ignore spaces between XML elements + expectedString := ignoreSpacesRe.ReplaceAllString(expected, "><") + actualString = ignoreSpacesRe.ReplaceAllString(actualString, "><") + + if expectedString != actualString { + compareLength := len(expectedString) + if len(actualString) < compareLength { + compareLength = len(actualString) + } + + for i := 0; i < compareLength; i++ { + if expectedString[i] != actualString[i] { + displayStart := i - 10 + if displayStart < 0 { + displayStart = 0 + } + displayEnd := displayStart + 40 + if displayEnd > compareLength { + displayEnd = compareLength + } + t.Fatalf("%s expected '...%s...',\ngot '...%s...'\n", t.Name(), expectedString[displayStart:displayEnd], actualString[displayStart:displayEnd]) + } + } + + if len(actualString) > compareLength { + t.Fatalf("%s: Actual string longer than expected string\n", t.Name()) + } else { + t.Fatalf("%s: Actual string shorter than expected string\n", t.Name()) + } + } +}