add pttodo export file option

This commit is contained in:
Bel LaPointe
2022-01-06 22:13:22 -05:00
parent 19aa5f82fb
commit fc95242c94
3 changed files with 126 additions and 1 deletions

View File

@@ -4,12 +4,92 @@ import (
"bytes"
"io/ioutil"
"local/storage"
"os"
"path"
"testing"
"github.com/google/uuid"
)
func TestUploadPTTodo(t *testing.T) {
addr := path.Join(t.TempDir(), "test.upload.pttodo")
config := Config{TodoAddr: addr, TodoTag: "expense"}
xaction := func() *Transaction {
return &Transaction{
ID: "id",
Bank: UCCU,
Amount: "1.23",
Vendor: "vendor vendor",
Date: "today",
}
}
t.Run("full file", func(t *testing.T) {
if err := ioutil.WriteFile(addr, []byte(`
todo:
- first
- todo: second
scheduled: []
done: []
`), os.ModePerm); err != nil {
t.Fatal(err)
}
err := uploadPTTodo(config, xaction())
if err != nil {
t.Error(err)
}
b, err := ioutil.ReadFile(addr)
if err != nil {
t.Error(err)
}
if bytes.Compare(bytes.TrimSpace(b), bytes.TrimSpace([]byte(`
todo:
- {"todo":"(today) /UCCU: 1.23 @ vendor vendor", "tags":"expense"}
- first
- todo: second
scheduled: []
done: []
`))) != 0 {
t.Errorf("full file came out wrong: got %s", b)
}
if !bytes.Contains(b, []byte(xaction().Format())) {
t.Errorf("full file didnt get target: %s", string(b))
}
t.Logf("%s", b)
})
t.Run("no file", func(t *testing.T) {
os.Remove(addr)
err := uploadPTTodo(config, xaction())
if err != nil {
t.Error(err)
}
b, err := ioutil.ReadFile(addr)
if err != nil {
t.Error(err)
}
if !bytes.Contains(b, []byte(xaction().Format())) {
t.Errorf("no file didnt get target: %s", string(b))
}
t.Logf("%s", b)
})
t.Run("empty file", func(t *testing.T) {
if err := ioutil.WriteFile(addr, []byte{}, os.ModePerm); err != nil {
t.Fatal(err)
}
err := uploadPTTodo(config, xaction())
if err != nil {
t.Error(err)
}
b, err := ioutil.ReadFile(addr)
if err != nil {
t.Error(err)
}
if !bytes.Contains(b, []byte(xaction().Format())) {
t.Errorf("empty file didnt get target: %s", string(b))
}
t.Logf("%s", b)
})
}
func TestUploadLedger(t *testing.T) {
cases := map[string]struct {
transaction Transaction