fix cache test

main
Bel LaPointe 2025-05-23 21:56:36 -06:00
parent f69a850bd8
commit 4997264f4c
4 changed files with 38 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path"
"time"
@ -22,7 +23,11 @@ func New(client bank.Agg) Client {
}
func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) {
if result := []bank.Account{}; fromCache("accounts", &result) == nil {
k := "accounts"
result := []bank.Account{}
if err := fromCache(k, &result); err != nil {
log.Printf("%q not in cache: %v", k, err)
} else {
return result, nil
}
@ -31,13 +36,17 @@ func (c Client) Accounts(ctx context.Context) ([]bank.Account, error) {
return nil, err
}
toCache("accounts", result)
toCache(k, result)
return result, nil
}
func (c Client) Transactions(ctx context.Context, a bank.Account) ([]bank.Transaction, error) {
if result := []bank.Transaction{}; fromCache(path.Join("accounts", a.Account), &result) == nil {
k := path.Join("accounts.d", a.Account)
result := []bank.Transaction{}
if err := fromCache(k, &result); err != nil {
log.Printf("%q not in cache: %v", k, err)
} else {
return result, nil
}
@ -46,7 +55,7 @@ func (c Client) Transactions(ctx context.Context, a bank.Account) ([]bank.Transa
return nil, err
}
toCache(path.Join("accounts", a.Account), result)
toCache(k, result)
return result, nil
}
@ -56,15 +65,25 @@ var (
)
func toCache(k string, v interface{}) {
if err := _toCache(k, v); err != nil {
log.Printf("failed to cache %s: %v", k, err)
}
}
func _toCache(k string, v interface{}) error {
b, err := json.Marshal(v)
if err != nil {
return
return err
}
p := path.Join(d, k)
os.MkdirAll(path.Dir(p), os.ModePerm)
if err := os.WriteFile(p, b, os.ModePerm); err != nil {
os.Remove(p)
return err
}
return nil
}
func fromCache(k string, ptr interface{}) error {

View File

@ -12,12 +12,11 @@ import (
)
func Test(t *testing.T) {
c, err := teller.New()
tellerC, err := teller.New()
if err != nil {
t.Fatal(err)
}
client := cache.New(c)
client := cache.New(tellerC)
ctx := context.Background()
@ -25,7 +24,7 @@ func Test(t *testing.T) {
i := i
client := client
t.Run(strconv.Itoa(i), func(t *testing.T) {
accounts, err := c.Accounts(ctx)
accounts, err := client.Accounts(ctx)
if err != nil {
t.Fatal(err)
}
@ -33,7 +32,7 @@ func Test(t *testing.T) {
for _, account := range accounts {
account := account
t.Run(account.Account, func(t *testing.T) {
transactions, err := c.Transactions(ctx, account)
transactions, err := client.Transactions(ctx, account)
if err != nil {
t.Fatal(err)
}

View File

@ -5,7 +5,10 @@ import (
"crypto/tls"
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"gogs.inhome.blapointe.com/ana-ledger/src/bank"
@ -56,7 +59,7 @@ func (c Client) get(ctx context.Context, url string, ptr interface{}) error {
if err != nil {
return err
}
req.SetBasicAuth(Token, "")
req.SetBasicAuth(strings.TrimSpace(Token), "")
req = req.WithContext(ctx)
resp, err := httpc.Do(req)
@ -65,5 +68,9 @@ func (c Client) get(ctx context.Context, url string, ptr interface{}) error {
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(ptr)
b, _ := io.ReadAll(resp.Body)
if err := json.Unmarshal(b, &ptr); err != nil {
return fmt.Errorf("cannot unmarshal: %w: %s", err, b)
}
return nil
}

View File

@ -1 +1 @@
test_token_dwdwcxnnhh5du
token_2utqstwpn3pxwgvyno56hqdehq