156 lines
3.1 KiB
Go
156 lines
3.1 KiB
Go
package main
|
|
|
|
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
|
|
}{
|
|
"simple": {
|
|
transaction: Transaction{
|
|
ID: uuid.New().String(),
|
|
Bank: Chase,
|
|
Amount: "1.10",
|
|
Vendor: "my's totally realistic!!! VENDOR 11",
|
|
Date: "today",
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
t.Run(name, func(t *testing.T) {
|
|
config := Config{
|
|
TodoAddr: path.Join(t.TempDir(), name),
|
|
Uploader: UploaderLedger,
|
|
Storage: storage.NewMap(),
|
|
}
|
|
if err := uploadLedger(config, &c.transaction); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := ioutil.ReadFile(config.TodoAddr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Contains(b, []byte("mystotallyrealisticVENDOR11")) {
|
|
t.Fatal(string(b))
|
|
}
|
|
t.Logf("\n%s", b)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFormatGMailDate(t *testing.T) {
|
|
cases := map[string]struct {
|
|
input string
|
|
want string
|
|
}{
|
|
"ok": {
|
|
input: "[Tue, 20 Jul 2021 23:46:00 -0400 (EDT)]",
|
|
want: "2021-07-20",
|
|
},
|
|
"bad": {
|
|
input: "2021-07-20",
|
|
want: "2021-07-20",
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
t.Run(name, func(t *testing.T) {
|
|
got := formatGMailDate(c.input)
|
|
if got != c.want {
|
|
t.Fatal(c.input, c.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|