ledger.newtransaction

master
Bel LaPointe 2021-08-30 10:14:35 -04:00
parent 8a7f97d230
commit 03ec3247f9
4 changed files with 87 additions and 17 deletions

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"time"
) )
type Ledger struct { type Ledger struct {
@ -24,6 +25,21 @@ func NewLedger(path string) (Ledger, error) {
}, err }, err
} }
func (ledger Ledger) NewTransaction() error {
transactions, err := ledger.Transactions()
if err != nil {
return err
}
transactions = append(transactions, Transaction{
Date: time.Now().Format("2006-01-02"),
Description: "?",
Payer: "?",
Payee: "?",
Amount: 0,
})
return ledger.SetTransactions(transactions)
}
func (ledger Ledger) SetTransaction(i int, transaction Transaction) error { func (ledger Ledger) SetTransaction(i int, transaction Transaction) error {
transactions, err := ledger.Transactions() transactions, err := ledger.Transactions()
if err != nil { if err != nil {

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io" "io/ioutil"
"os" "os"
"path" "path"
"testing" "testing"
@ -10,8 +10,19 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
func testLedgerFile(t *testing.T) string {
path := path.Join(t.TempDir(), t.Name()+".dat")
if b, err := ioutil.ReadFile("./testdata/ledger.dat"); err != nil {
t.Fatal(err)
} else if err := ioutil.WriteFile(path, b, os.ModePerm); err != nil {
t.Fatal(err)
}
return path
}
func TestLedgerTransactionsBalances(t *testing.T) { func TestLedgerTransactionsBalances(t *testing.T) {
ledger, err := NewLedger("./testdata/ledger.dat") path := testLedgerFile(t)
ledger, err := NewLedger(path)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -67,21 +78,7 @@ func TestLedgerSetTransaction(t *testing.T) {
Amount: 1.23, Amount: 1.23,
} }
} }
path := path.Join(t.TempDir(), "ledger.dat") path := testLedgerFile(t)
a, err := os.Open("./testdata/ledger.dat")
if err != nil {
t.Fatal(err)
}
b, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(b, a); err != nil {
t.Fatal(err)
}
a.Close()
b.Close()
ledger, err := NewLedger(path) ledger, err := NewLedger(path)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -109,3 +106,27 @@ func TestLedgerSetTransaction(t *testing.T) {
t.Fatal(want, got) t.Fatal(want, got)
} }
} }
func TestLedgerNewTransaction(t *testing.T) {
path := testLedgerFile(t)
ledger, err := NewLedger(path)
if err != nil {
t.Fatal(err)
}
transactions, err := ledger.Transactions()
if err != nil {
t.Fatal(err)
}
wantTransactions := append(transactions, newStubTransaction())
ledger.NewTransaction()
if err != nil {
t.Fatal(err)
}
gotTransactions, err := ledger.Transactions()
if err != nil {
t.Fatal(err)
}
if fmt.Sprint(wantTransactions) != fmt.Sprint(gotTransactions) {
t.Fatal(wantTransactions, gotTransactions)
}
}

View File

@ -8,6 +8,7 @@ import (
"io" "io"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type Transaction struct { type Transaction struct {
@ -18,6 +19,20 @@ type Transaction struct {
Amount float32 Amount float32
} }
const (
UnknownAccount = "?"
)
func newStubTransaction() Transaction {
return Transaction{
Date: time.Now().Format("2006-01-02"),
Description: UnknownAccount,
Payer: UnknownAccount,
Payee: UnknownAccount,
Amount: 0,
}
}
func readTransaction(r io.Reader) (Transaction, error) { func readTransaction(r io.Reader) (Transaction, error) {
lines := make([][]byte, 3) lines := make([][]byte, 3)
for i := range lines { for i := range lines {

View File

@ -6,6 +6,24 @@ import (
"testing" "testing"
) )
func TestNewStubTransaction(t *testing.T) {
want := Transaction{
Date: "1",
Amount: 0,
Payer: UnknownAccount,
Payee: UnknownAccount,
Description: UnknownAccount,
}
got := newStubTransaction()
if got.Date == "" {
t.Fatal(got.Date)
}
got.Date = want.Date
if want != got {
t.Fatal(want, got)
}
}
func TestWords(t *testing.T) { func TestWords(t *testing.T) {
input := ` input := `
hello world hello world