package main import ( "bytes" "io/ioutil" "os" "path" "path/filepath" "testing" "gitea.inhome.blapointe.com/local/storage" "github.com/google/uuid" ) func TestUploadPTTodo(t *testing.T) { addr := path.Join(t.TempDir(), "test.upload.pttodo") config := Config{TodoAddr: addr, TodoTag: "expense"} reset := func(t *testing.T) { files, err := filepath.Glob(addr + "*") if err != nil { t.Fatal(err) } for _, f := range files { if f != addr { os.Remove(f) } } } 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) { defer reset(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) } files, err := filepath.Glob(addr + ".todo.*") if err != nil { t.Error(err) } if len(files) != 1 { t.Fatal(files) } b, err := ioutil.ReadFile(files[0]) if err != nil { t.Error(err) } if bytes.Compare(bytes.TrimSpace(b), bytes.TrimSpace([]byte(`- {"todo":"(today) /UCCU: 1.23 @ vendor vendor", "tags":"expense"}`))) != 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) { defer reset(t) os.Remove(addr) err := uploadPTTodo(config, xaction()) if err != nil { t.Error(err) } files, err := filepath.Glob(addr + ".todo.*") if err != nil { t.Error(err) } if len(files) != 1 { t.Fatal(files) } b, err := ioutil.ReadFile(files[0]) 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) { defer reset(t) if err := ioutil.WriteFile(addr, []byte{}, os.ModePerm); err != nil { t.Fatal(err) } err := uploadPTTodo(config, xaction()) if err != nil { t.Error(err) } files, err := filepath.Glob(addr + ".todo.*") if err != nil { t.Error(err) } if len(files) != 1 { t.Fatal(files) } b, err := ioutil.ReadFile(files[0]) 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) } }) } }