tdd whee
parent
ed75a59ca0
commit
459bc54760
|
|
@ -1,6 +1,8 @@
|
||||||
package ledger
|
package ledger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -36,6 +38,12 @@ func (file File) Deltas(like ...Like) ([]Delta, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for currency, value := range sums {
|
for currency, value := range sums {
|
||||||
|
if value == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if transaction.payee == "" {
|
||||||
|
return nil, fmt.Errorf("didnt find net zero and no dumping ground payee set: %+v", transaction)
|
||||||
|
}
|
||||||
delta := newDelta(
|
delta := newDelta(
|
||||||
transaction.date,
|
transaction.date,
|
||||||
transaction.description,
|
transaction.description,
|
||||||
|
|
@ -64,6 +72,10 @@ type transactionRecipient struct {
|
||||||
currency string
|
currency string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t transaction) empty() bool {
|
||||||
|
return fmt.Sprint(t) == fmt.Sprint(transaction{})
|
||||||
|
}
|
||||||
|
|
||||||
func (file File) transactions() ([]transaction, error) {
|
func (file File) transactions() ([]transaction, error) {
|
||||||
f, err := os.Open(string(file))
|
f, err := os.Open(string(file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -74,8 +86,10 @@ func (file File) transactions() ([]transaction, error) {
|
||||||
result := make([]transaction, 0)
|
result := make([]transaction, 0)
|
||||||
for {
|
for {
|
||||||
one, err := readTransaction(f)
|
one, err := readTransaction(f)
|
||||||
|
if !one.empty() {
|
||||||
result = append(result, one)
|
result = append(result, one)
|
||||||
if err == io.EOF {
|
}
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package ledger
|
package ledger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -70,3 +73,65 @@ func TestFileDeltas(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadTransaction(t *testing.T) {
|
||||||
|
d := func(s string) time.Time {
|
||||||
|
v, _ := time.Parse("2006-01-02", s)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
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: d("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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue