From 4997264f4c69b99f9d29839e1b83a78186038ca8 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Fri, 23 May 2025 21:56:36 -0600 Subject: [PATCH] fix cache test --- src/bank/cache/cache.go | 29 ++++++++++++++++++++---- src/bank/cache/cache_integration_test.go | 9 ++++---- src/bank/teller/teller.go | 11 +++++++-- src/bank/teller/token.txt | 2 +- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/bank/cache/cache.go b/src/bank/cache/cache.go index 0fef30e..c724aa3 100644 --- a/src/bank/cache/cache.go +++ b/src/bank/cache/cache.go @@ -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 { diff --git a/src/bank/cache/cache_integration_test.go b/src/bank/cache/cache_integration_test.go index c4ea099..2530c4a 100644 --- a/src/bank/cache/cache_integration_test.go +++ b/src/bank/cache/cache_integration_test.go @@ -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) } diff --git a/src/bank/teller/teller.go b/src/bank/teller/teller.go index b6d23d6..f71108c 100644 --- a/src/bank/teller/teller.go +++ b/src/bank/teller/teller.go @@ -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 } diff --git a/src/bank/teller/token.txt b/src/bank/teller/token.txt index d536419..a913d90 100644 --- a/src/bank/teller/token.txt +++ b/src/bank/teller/token.txt @@ -1 +1 @@ -test_token_dwdwcxnnhh5du +token_2utqstwpn3pxwgvyno56hqdehq