-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbalances_test.go
169 lines (151 loc) · 3.47 KB
/
balances_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ledger
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"testing"
"time"
"github.com/howeyc/ledger/decimal"
)
type testBalCase struct {
name string
data string
balances []Account
err error
}
var testBalCases = []testBalCase{
{
"simple case",
`1970/01/01 Payee
Expense/test (123 * 3)
Assets
1970/01/01 Payee
Expense/test 123
Assets
`,
[]Account{
{
Name: "Assets",
Balance: decimal.NewFromFloat(-4 * 123),
},
{
Name: "Expense/test",
Balance: decimal.NewFromFloat(123 + 369),
},
},
nil,
},
{
"heirarchy",
`1970/01/01 Payee
Expense:test (123 * 3)
Assets
1970/01/01 Payee
Expense:foo 123
Assets
`,
[]Account{
{
Name: "Assets",
Balance: decimal.NewFromFloat(-4 * 123),
},
{
Name: "Expense",
Balance: decimal.NewFromFloat(123 + 369),
},
{
Name: "Expense:foo",
Balance: decimal.NewFromFloat(123),
},
{
Name: "Expense:test",
Balance: decimal.NewFromFloat(369),
},
},
nil,
},
}
func TestBalanceLedger(t *testing.T) {
for _, tc := range testBalCases {
b := bytes.NewBufferString(tc.data)
transactions, err := ParseLedger(b)
bals := GetBalances(transactions, []string{})
if (err != nil && tc.err == nil) || (err != nil && tc.err != nil && err.Error() != tc.err.Error()) {
t.Errorf("Error: expected `%s`, got `%s`", tc.err, err)
}
exp, _ := json.Marshal(tc.balances)
got, _ := json.Marshal(bals)
if string(exp) != string(got) {
t.Errorf("Error(%s): expected \n`%s`, \ngot \n`%s`", tc.name, exp, got)
}
}
}
func BenchmarkGetBalances(b *testing.B) {
trans := make([]*Transaction, 0, 100000)
for i := range 100000 {
a := rand.Intn(50)
b := rand.Intn(10)
c := rand.Intn(5)
d := rand.Intn(50)
e := rand.Intn(10)
f := rand.Intn(5)
amt := rand.Float64() * 10000
trans = append(trans, &Transaction{
Date: time.Now(),
Payee: fmt.Sprintf("Trans %d", i),
AccountChanges: []Account{
{
Name: fmt.Sprintf("Acc%d:Acc%d:Acc%d", a, b, c),
Balance: decimal.NewFromFloat(amt),
},
{
Name: fmt.Sprintf("Acc%d:Acc%d:Acc%d", d, e, f),
Balance: decimal.NewFromFloat(-amt),
},
},
})
}
b.ResetTimer()
for range b.N {
GetBalances(trans, []string{})
}
}
func TestBalancesByPeriod(t *testing.T) {
b := bytes.NewBufferString(`
2022/02/02 Payee
Assets 50
Income
2022/01/02 Payee
Assets 50
Income
2022/03/02 Payee
Assets 50
Income
2022/04/02 Payee
Assets 50
Income
2022/05/02 Payee
Assets 50
Income
`)
trans, _ := ParseLedger(b)
partitionRb := BalancesByPeriod(trans, PeriodQuarter, RangePartition)
snapshotRb := BalancesByPeriod(trans, PeriodQuarter, RangeSnapshot)
if partitionRb[len(partitionRb)-1].Balances[0].Balance.Abs().Cmp(decimal.NewFromInt(100)) != 0 {
t.Error("range balance by partition not accurate")
}
if snapshotRb[len(snapshotRb)-1].Balances[0].Balance.Abs().Cmp(decimal.NewFromInt(250)) != 0 {
t.Error("range balance by snapshot not accurate")
}
transPeriod := TransactionsByPeriod(trans, PeriodQuarter)
lastBals := GetBalances(transPeriod[len(transPeriod)-1].Transactions, []string{})
if partitionRb[len(partitionRb)-1].Balances[0].Balance.Abs().Cmp(lastBals[0].Balance.Abs()) != 0 {
t.Error("range balance by partition not equal to trans by period balance")
}
var blanktrans []*Transaction
rb := BalancesByPeriod(blanktrans, PeriodDay, RangeSnapshot)
if len(rb) > 1 {
t.Error("range balances for non-existent transactions")
}
}