129 lines
2.2 KiB
Go
129 lines
2.2 KiB
Go
package ledger
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileDeltas(t *testing.T) {
|
|
happy := []Delta{
|
|
{
|
|
Date: "2022-12-12",
|
|
Account: "AssetAccount:Cash:Fidelity76",
|
|
Value: -97.92,
|
|
Currency: USD,
|
|
},
|
|
{
|
|
Date: "2022-12-12",
|
|
Account: "Withdrawal:0:SharedHome:DominionEnergy",
|
|
Value: 97.92,
|
|
Currency: USD,
|
|
},
|
|
{
|
|
Date: "2022-12-12",
|
|
Account: "AssetAccount:Cash:Fidelity76",
|
|
Value: -1.00,
|
|
Currency: USD,
|
|
},
|
|
{
|
|
Date: "2022-12-12",
|
|
Account: "Debts:Credit:ChaseFreedomUltdVisa",
|
|
Value: 1.00,
|
|
Currency: USD,
|
|
},
|
|
}
|
|
|
|
cases := map[string][]Delta{
|
|
"empty": nil,
|
|
"one": happy[:2],
|
|
"happy": happy[:],
|
|
}
|
|
|
|
for name, d := range cases {
|
|
want := d
|
|
t.Run(name, func(t *testing.T) {
|
|
f, err := NewFile("./testdata/" + name + ".dat")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
deltas, err := f.Deltas()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(deltas) != len(want) {
|
|
t.Error(len(deltas))
|
|
}
|
|
for i := range want {
|
|
if i >= len(deltas) {
|
|
break
|
|
}
|
|
if want[i] != deltas[i] {
|
|
t.Errorf("[%d] \n\twant=%+v, \n\t got=%+v", i, want[i], deltas[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReadTransaction(t *testing.T) {
|
|
cases := map[string]struct {
|
|
input string
|
|
want transaction
|
|
err error
|
|
}{
|
|
"empty": {
|
|
input: "",
|
|
want: transaction{},
|
|
err: io.EOF,
|
|
},
|
|
"white space": {
|
|
input: " ",
|
|
want: transaction{},
|
|
err: io.EOF,
|
|
},
|
|
"verbose": {
|
|
input: `
|
|
2003-04-05 Reasoning here
|
|
A:B $1.00
|
|
C:D $-1.00
|
|
`,
|
|
want: transaction{
|
|
date: "2003-04-05",
|
|
description: "Reasoning here",
|
|
payee: "",
|
|
recipients: []transactionRecipient{
|
|
{
|
|
name: "A:B",
|
|
value: 1.0,
|
|
currency: "$",
|
|
},
|
|
{
|
|
name: "C:D",
|
|
value: -1.0,
|
|
currency: "$",
|
|
},
|
|
},
|
|
},
|
|
err: io.EOF,
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
t.Run(name, func(t *testing.T) {
|
|
_, got, err := readTransaction(strings.NewReader(c.input))
|
|
if err != c.err {
|
|
t.Error(err)
|
|
}
|
|
|
|
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) {
|
|
t.Errorf("want\n\t%+v, got\n\t%+v", c.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|