1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2025-07-01 19:58:37 -04:00

Rename ofxgo_test package to ofxgo and remove self-imports/references

* Fix package in generate_constants.py
* Update generate_constants.py to use the new imports
This commit is contained in:
John Starich
2020-03-30 23:19:18 -05:00
committed by Aaron Lindsay
parent f19189de45
commit 8ad638c7e2
15 changed files with 712 additions and 721 deletions

View File

@ -1,8 +1,7 @@
package ofxgo_test
package ofxgo
import (
"fmt"
"github.com/aclindsa/ofxgo"
"github.com/aclindsa/xml"
"reflect"
"testing"
@ -57,7 +56,7 @@ func unmarshalHelper(t *testing.T, input string, expected interface{}, overwritt
}
func TestMarshalInt(t *testing.T) {
var i ofxgo.Int = 927
var i Int = 927
marshalHelper(t, "927", &i)
i = 0
marshalHelper(t, "0", &i)
@ -66,7 +65,7 @@ func TestMarshalInt(t *testing.T) {
}
func TestUnmarshalInt(t *testing.T) {
var i, overwritten ofxgo.Int = -48394, 0
var i, overwritten Int = -48394, 0
unmarshalHelper(t, "-48394", &i, &overwritten)
i = 0
unmarshalHelper(t, "0", &i, &overwritten)
@ -78,7 +77,7 @@ func TestUnmarshalInt(t *testing.T) {
}
func TestMarshalAmount(t *testing.T) {
var a ofxgo.Amount
var a Amount
a.SetFrac64(8, 1)
marshalHelper(t, "8", &a)
@ -95,13 +94,13 @@ func TestMarshalAmount(t *testing.T) {
}
func TestUnmarshalAmount(t *testing.T) {
var a, overwritten ofxgo.Amount
var a, overwritten Amount
// Amount/big.Rat needs a special equality test because reflect.DeepEqual
// doesn't always return equal for two values that big.Rat.Cmp() does
eq := func(a, b interface{}) bool {
if amountA, ok := a.(*ofxgo.Amount); ok {
if amountB, ok2 := b.(*ofxgo.Amount); ok2 {
if amountA, ok := a.(*Amount); ok {
if amountB, ok2 := b.(*Amount); ok2 {
return amountA.Cmp(&amountB.Rat) == 0
}
}
@ -127,18 +126,18 @@ func TestUnmarshalAmount(t *testing.T) {
}
func TestAmountEqual(t *testing.T) {
assertEq := func(a, b ofxgo.Amount) {
assertEq := func(a, b Amount) {
if !a.Equal(b) {
t.Fatalf("Amounts should be equal but Equal returned false: %s and %s\n", a, b)
}
}
assertNEq := func(a, b ofxgo.Amount) {
assertNEq := func(a, b Amount) {
if a.Equal(b) {
t.Fatalf("Amounts should not be equal but Equal returned true: %s and %s\n", a, b)
}
}
var a, b ofxgo.Amount
var a, b Amount
a.SetInt64(-19487135)
b.SetInt64(-19487135)
assertEq(a, b)
@ -154,7 +153,7 @@ func TestAmountEqual(t *testing.T) {
}
func TestMarshalDate(t *testing.T) {
var d *ofxgo.Date
var d *Date
UTC := time.FixedZone("UTC", 0)
GMT_nodesc := time.FixedZone("", 0)
EST := time.FixedZone("EST", -5*60*60)
@ -162,35 +161,35 @@ func TestMarshalDate(t *testing.T) {
IST := time.FixedZone("IST", (5*60+30)*60)
NST := time.FixedZone("NST", -(3*60+30)*60)
d = ofxgo.NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
d = NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
marshalHelper(t, "20170314150926.053[0:GMT]", d)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, NPT)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, NPT)
marshalHelper(t, "20170314150926.053[5.75:NPT]", d)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, EST)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, EST)
marshalHelper(t, "20170314150926.053[-5:EST]", d)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, UTC)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, UTC)
marshalHelper(t, "20170314150926.053[0:UTC]", d)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, IST)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, IST)
marshalHelper(t, "20170314150926.053[5.50:IST]", d)
d = ofxgo.NewDate(9999, 11, 1, 23, 59, 59, 1000, EST)
d = NewDate(9999, 11, 1, 23, 59, 59, 1000, EST)
marshalHelper(t, "99991101235959.000[-5:EST]", d)
d = ofxgo.NewDate(0, 1, 1, 0, 0, 0, 0, IST)
d = NewDate(0, 1, 1, 0, 0, 0, 0, IST)
marshalHelper(t, "00000101000000.000[5.50:IST]", d)
d = &ofxgo.Date{Time: time.Unix(0, 0).In(UTC)}
d = &Date{Time: time.Unix(0, 0).In(UTC)}
marshalHelper(t, "19700101000000.000[0:UTC]", d)
d = ofxgo.NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, EST)
d = NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, EST)
marshalHelper(t, "20170314000026.053[-5:EST]", d)
d = ofxgo.NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, NST)
d = NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, NST)
marshalHelper(t, "20170314000026.053[-3.50:NST]", d)
// Time zone without textual description
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT_nodesc)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT_nodesc)
marshalHelper(t, "20170314150926.053[0]", d)
}
func TestUnmarshalDate(t *testing.T) {
var d *ofxgo.Date
var overwritten ofxgo.Date
var d *Date
var overwritten Date
GMT := time.FixedZone("GMT", 0)
EST := time.FixedZone("EST", -5*60*60)
NPT := time.FixedZone("NPT", (5*60+45)*60)
@ -199,8 +198,8 @@ func TestUnmarshalDate(t *testing.T) {
NST_nodesc := time.FixedZone("", -(3*60+30)*60)
eq := func(a, b interface{}) bool {
if dateA, ok := a.(*ofxgo.Date); ok {
if dateB, ok2 := b.(*ofxgo.Date); ok2 {
if dateA, ok := a.(*Date); ok {
if dateB, ok2 := b.(*Date); ok2 {
return dateA.Equal(*dateB)
}
}
@ -208,14 +207,14 @@ func TestUnmarshalDate(t *testing.T) {
}
// Ensure omitted fields default to the correct values
d = ofxgo.NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
d = NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
unmarshalHelper2(t, "20170314150926.053[0]", d, &overwritten, eq)
unmarshalHelper2(t, "20170314150926.053", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 0, 0, 0, 0, GMT)
d = NewDate(2017, 3, 14, 0, 0, 0, 0, GMT)
unmarshalHelper2(t, "20170314", d, &overwritten, eq)
// Ensure all signs on time zone offsets are properly handled
d = ofxgo.NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
d = NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
unmarshalHelper2(t, "20170314150926.053[0:GMT]", d, &overwritten, eq)
unmarshalHelper2(t, "20170314150926.053[+0:GMT]", d, &overwritten, eq)
unmarshalHelper2(t, "20170314150926.053[-0:GMT]", d, &overwritten, eq)
@ -223,38 +222,38 @@ func TestUnmarshalDate(t *testing.T) {
unmarshalHelper2(t, "20170314150926.053[+0]", d, &overwritten, eq)
unmarshalHelper2(t, "20170314150926.053[-0]", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, NPT)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, NPT)
unmarshalHelper2(t, "20170314150926.053[5.75:NPT]", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, EST)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, EST)
unmarshalHelper2(t, "20170314150926.053[-5:EST]", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT)
unmarshalHelper2(t, "20170314150926.053[0:GMT]", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, IST)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, IST)
unmarshalHelper2(t, "20170314150926.053[5.50:IST]", d, &overwritten, eq)
d = ofxgo.NewDate(2018, 11, 1, 23, 59, 58, 0, EST)
d = NewDate(2018, 11, 1, 23, 59, 58, 0, EST)
unmarshalHelper2(t, "20181101235958.000[-5:EST]", d, &overwritten, eq)
d = ofxgo.NewDate(0, 1, 1, 0, 0, 0, 0, IST)
d = NewDate(0, 1, 1, 0, 0, 0, 0, IST)
unmarshalHelper2(t, "00000101000000.000[5.50:IST]", d, &overwritten, eq)
d = &ofxgo.Date{Time: time.Unix(0, 0).In(GMT)}
d = &Date{Time: time.Unix(0, 0).In(GMT)}
unmarshalHelper2(t, "19700101000000.000[0:GMT]", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, EST)
d = NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, EST)
unmarshalHelper2(t, "20170314000026.053[-5:EST]", d, &overwritten, eq)
d = ofxgo.NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, NST)
d = NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, NST)
unmarshalHelper2(t, "20170314000026.053[-3.50:NST]", d, &overwritten, eq)
// Autopopulate zone without textual description for GMT
d = ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT)
d = NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT)
unmarshalHelper2(t, "20170314150926.053[0]", d, &overwritten, eq)
// but not for others:
d = ofxgo.NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, NST_nodesc)
d = NewDate(2017, 3, 14, 0, 0, 26, 53*1000*1000, NST_nodesc)
unmarshalHelper2(t, "20170314000026.053[-3.50]", d, &overwritten, eq)
// Make sure we handle poorly-formatted dates (from Vanguard)
d = ofxgo.NewDate(2016, 12, 7, 16, 0, 0, 0, EST)
d = NewDate(2016, 12, 7, 16, 0, 0, 0, EST)
unmarshalHelper2(t, "20161207160000.000[-5:EST]610900.500[-9:BST]", d, &overwritten, eq) // extra part intentionally different to ensure the first timezone is parsed
// Make sure we properly handle ending newlines
d = ofxgo.NewDate(2018, 11, 1, 23, 59, 58, 0, EST)
d = NewDate(2018, 11, 1, 23, 59, 58, 0, EST)
unmarshalHelper2(t, "20181101235958.000[-5:EST]\n", d, &overwritten, eq)
unmarshalHelper2(t, "20181101235958.000[-5:EST]\n\t", d, &overwritten, eq)
}
@ -263,23 +262,23 @@ func TestDateEqual(t *testing.T) {
GMT := time.FixedZone("GMT", 0)
EST := time.FixedZone("EST", -5*60*60)
assertEq := func(a, b *ofxgo.Date) {
assertEq := func(a, b *Date) {
if !a.Equal(*b) {
t.Fatalf("Dates should be equal but Equal returned false: %s and %s\n", *a, *b)
}
}
assertNEq := func(a, b *ofxgo.Date) {
assertNEq := func(a, b *Date) {
if a.Equal(*b) {
t.Fatalf("Dates should not be equal but Equal returned true: %s and %s\n", *a, *b)
}
}
// Ensure omitted fields default to the correct values
gmt1 := ofxgo.NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
gmt2 := ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT)
est1 := ofxgo.NewDate(2017, 3, 14, 10, 9, 26, 53*1000*1000, EST)
est2 := ofxgo.NewDate(2017, 3, 14, 10, 9, 26, 53*1000*1000+1, EST)
est3 := ofxgo.NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, EST)
gmt1 := NewDateGMT(2017, 3, 14, 15, 9, 26, 53*1000*1000)
gmt2 := NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, GMT)
est1 := NewDate(2017, 3, 14, 10, 9, 26, 53*1000*1000, EST)
est2 := NewDate(2017, 3, 14, 10, 9, 26, 53*1000*1000+1, EST)
est3 := NewDate(2017, 3, 14, 15, 9, 26, 53*1000*1000, EST)
assertEq(gmt1, gmt2)
assertEq(gmt2, gmt1)
@ -291,7 +290,7 @@ func TestDateEqual(t *testing.T) {
}
func TestMarshalString(t *testing.T) {
var s ofxgo.String = ""
var s String = ""
marshalHelper(t, "", &s)
s = "foo&bar"
marshalHelper(t, "foo&bar", &s)
@ -302,7 +301,7 @@ func TestMarshalString(t *testing.T) {
}
func TestUnmarshalString(t *testing.T) {
var s, overwritten ofxgo.String = "", ""
var s, overwritten String = "", ""
unmarshalHelper(t, "", &s, &overwritten)
s = "foo&bar"
unmarshalHelper(t, "foo&bar", &s, &overwritten)
@ -318,14 +317,14 @@ func TestUnmarshalString(t *testing.T) {
}
func TestMarshalBoolean(t *testing.T) {
var b ofxgo.Boolean = true
var b Boolean = true
marshalHelper(t, "Y", &b)
b = false
marshalHelper(t, "N", &b)
}
func TestUnmarshalBoolean(t *testing.T) {
var b, overwritten ofxgo.Boolean = true, false
var b, overwritten Boolean = true, false
unmarshalHelper(t, "Y", &b, &overwritten)
b = false
unmarshalHelper(t, "N", &b, &overwritten)
@ -335,12 +334,12 @@ func TestUnmarshalBoolean(t *testing.T) {
}
func TestMarshalUID(t *testing.T) {
var u ofxgo.UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04"
var u UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04"
marshalHelper(t, "d1cf3d3d-9ef9-4a97-b180-81706829cb04", &u)
}
func TestUnmarshalUID(t *testing.T) {
var u, overwritten ofxgo.UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04", ""
var u, overwritten UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04", ""
unmarshalHelper(t, "d1cf3d3d-9ef9-4a97-b180-81706829cb04", &u, &overwritten)
// Make sure stray newlines are handled properly
u = "0f94ce83-13b7-7568-e4fc-c02c7b47e7ab"
@ -349,7 +348,7 @@ func TestUnmarshalUID(t *testing.T) {
}
func TestUIDRecommendedFormat(t *testing.T) {
var u ofxgo.UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04"
var u UID = "d1cf3d3d-9ef9-4a97-b180-81706829cb04"
if ok, err := u.RecommendedFormat(); !ok || err != nil {
t.Fatalf("UID unexpectedly failed validation\n")
}
@ -368,7 +367,7 @@ func TestUIDRecommendedFormat(t *testing.T) {
}
func TestUIDValid(t *testing.T) {
var u ofxgo.UID = ""
var u UID = ""
if ok, err := u.Valid(); ok || err == nil {
t.Fatalf("Empty UID unexpectedly valid\n")
}
@ -383,7 +382,7 @@ func TestUIDValid(t *testing.T) {
}
func TestRandomUID(t *testing.T) {
uid, err := ofxgo.RandomUID()
uid, err := RandomUID()
if err != nil {
t.Fatalf("Unexpected error when calling RandomUID: %s\n", err)
}
@ -393,46 +392,46 @@ func TestRandomUID(t *testing.T) {
}
func TestMarshalCurrSymbol(t *testing.T) {
c, _ := ofxgo.NewCurrSymbol("USD")
c, _ := NewCurrSymbol("USD")
marshalHelper(t, "USD", &c)
}
func TestUnmarshalCurrSymbol(t *testing.T) {
var overwritten ofxgo.CurrSymbol
c, _ := ofxgo.NewCurrSymbol("USD")
var overwritten CurrSymbol
c, _ := NewCurrSymbol("USD")
unmarshalHelper(t, "USD", c, &overwritten)
// Make sure stray newlines are handled properly
c, _ = ofxgo.NewCurrSymbol("EUR")
c, _ = NewCurrSymbol("EUR")
unmarshalHelper(t, "EUR\n", c, &overwritten)
unmarshalHelper(t, "EUR\n\t", c, &overwritten)
}
func TestCurrSymbolEqual(t *testing.T) {
usd1, _ := ofxgo.NewCurrSymbol("USD")
usd2, _ := ofxgo.NewCurrSymbol("USD")
usd1, _ := NewCurrSymbol("USD")
usd2, _ := NewCurrSymbol("USD")
if !usd1.Equal(*usd2) {
t.Fatalf("Two \"USD\" CurrSymbols returned !Equal()\n")
}
xxx, _ := ofxgo.NewCurrSymbol("XXX")
xxx, _ := NewCurrSymbol("XXX")
if usd1.Equal(*xxx) {
t.Fatalf("\"USD\" and \"XXX\" CurrSymbols returned Equal()\n")
}
}
func TestCurrSymbolValid(t *testing.T) {
var initial ofxgo.CurrSymbol
var initial CurrSymbol
ok, err := initial.Valid()
if ok || err == nil {
t.Fatalf("CurrSymbol unexpectedly returned Valid() for initial value\n")
}
ars, _ := ofxgo.NewCurrSymbol("ARS")
ars, _ := NewCurrSymbol("ARS")
ok, err = ars.Valid()
if !ok || err != nil {
t.Fatalf("CurrSymbol unexpectedly returned !Valid() for \"ARS\": %s\n", err.Error())
}
xxx, _ := ofxgo.NewCurrSymbol("XXX")
xxx, _ := NewCurrSymbol("XXX")
ok, err = xxx.Valid()
if ok || err == nil {
t.Fatalf("CurrSymbol unexpectedly returned Valid() for \"XXX\"\n")
@ -440,21 +439,21 @@ func TestCurrSymbolValid(t *testing.T) {
}
func TestNewCurrSymbol(t *testing.T) {
curr, err := ofxgo.NewCurrSymbol("GBP")
curr, err := NewCurrSymbol("GBP")
if err != nil {
t.Fatalf("Unexpected error calling NewCurrSymbol: %s\n", err)
}
if curr.String() != "GBP" {
t.Fatalf("Created CurrSymbol doesn't print \"GBP\" as string representation\n")
}
curr, err = ofxgo.NewCurrSymbol("AFN")
curr, err = NewCurrSymbol("AFN")
if err != nil {
t.Fatalf("Unexpected error calling NewCurrSymbol: %s\n", err)
}
if curr.String() != "AFN" {
t.Fatalf("Created CurrSymbol doesn't print \"AFN\" as string representation\n")
}
curr, err = ofxgo.NewCurrSymbol("BLAH")
curr, err = NewCurrSymbol("BLAH")
if err == nil {
t.Fatalf("NewCurrSymbol didn't error on invalid currency identifier\n")
}