fx_test.go147 lines, 4.9 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/fiston-user/payments/internal/fx" |
| 10 | "github.com/fiston-user/payments/internal/wallet" |
| 11 | "github.com/google/uuid" |
| 12 | "github.com/jackc/pgx/v5/pgconn" |
| 13 | ) |
| 14 | |
| 15 | // rates are the tests' own: fixed, whatever devrates.json says. |
| 16 | var rates = fx.Static{"USD/EUR": 9_215_000_000, "EUR/USD": 10_852_000_000} |
| 17 | |
| 18 | // sellHundred asks the price of a hundred dollars, in euros. |
| 19 | var sellHundred = wallet.QuoteRequest{From: "USD", To: "EUR", SellAmount: 10000} |
| 20 | |
| 21 | // converter returns a Service on s that converts, for a fee of half a percent, |
| 22 | // and an owner with $500 in a USD wallet, and an empty EUR wallet. |
| 23 | func converter(t *testing.T, s *wallet.Store, ttl time.Duration) ( |
| 24 | svc *wallet.Service, owner, dollars, euros uuid.UUID, |
| 25 | ) { |
| 26 | t.Helper() |
| 27 | svc = wallet.NewService(s).WithFX(wallet.FX{Rates: rates, QuoteTTL: ttl, FeeBps: 50}) |
| 28 | owner = uuid.New() |
| 29 | ids := make(map[string]uuid.UUID) |
| 30 | for _, currency := range []string{"USD", "EUR"} { |
| 31 | w, err := svc.CreateWallet(t.Context(), owner.String(), currency) |
| 32 | if err != nil { |
| 33 | t.Fatalf("create %s wallet: %v", currency, err) |
| 34 | } |
| 35 | ids[currency] = w.ID |
| 36 | } |
| 37 | if _, err := svc.Deposit(t.Context(), ids["USD"], usd(50_000)); err != nil { |
| 38 | t.Fatalf("deposit: %v", err) |
| 39 | } |
| 40 | return svc, owner, ids["USD"], ids["EUR"] |
| 41 | } |
| 42 | |
| 43 | func TestQuote(t *testing.T) { |
| 44 | t.Parallel() |
| 45 | svc, owner, _, _ := converter(t, newStore(t), time.Minute) |
| 46 | tests := []struct { |
| 47 | name string |
| 48 | from, to string |
| 49 | sell, buy int64 |
| 50 | want string |
| 51 | }{ |
| 52 | {"sell a hundred dollars", "USD", "EUR", 10000, 0, "10000 USD for 9215 EUR, fee 50"}, |
| 53 | {"buy fifty euros", "USD", "EUR", 0, 5000, "5426 USD for 5000 EUR, fee 28"}, |
| 54 | {"sell ten euros", "EUR", "USD", 1000, 0, "1000 EUR for 1085 USD, fee 5"}, |
| 55 | {"a cent buys nothing", "USD", "EUR", 1, 0, "invalid input: sell_amount"}, |
| 56 | {"sell and buy", "USD", "EUR", 100, 100, "invalid input: sell_amount"}, |
| 57 | {"no conversion", "USD", "USD", 100, 0, "invalid input: to"}, |
| 58 | {"unknown currency", "USD", "XXX", 100, 0, "invalid input: to"}, |
| 59 | } |
| 60 | for _, tt := range tests { |
| 61 | t.Run(tt.name, func(t *testing.T) { |
| 62 | t.Parallel() |
| 63 | req := wallet.QuoteRequest{From: tt.from, To: tt.to, SellAmount: tt.sell, BuyAmount: tt.buy} |
| 64 | q, err := svc.Quote(t.Context(), owner, req) |
| 65 | got := fmt.Sprintf("%d %s for %d %s, fee %d", |
| 66 | q.SellAmount, q.SellCurrency, q.BuyAmount, q.BuyCurrency, q.Fee) |
| 67 | if err != nil { |
| 68 | got = err.Error() |
| 69 | } |
| 70 | if got != tt.want { |
| 71 | t.Errorf("Quote = %q, want %q", got, tt.want) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestConvertOnce(t *testing.T) { |
| 78 | t.Parallel() |
| 79 | svc, owner, dollars, euros := converter(t, newStore(t), time.Minute) |
| 80 | quote, err := svc.Quote(t.Context(), owner, sellHundred) |
| 81 | if err != nil { |
| 82 | t.Fatalf("quote: %v", err) |
| 83 | } |
| 84 | |
| 85 | // Ten requests for the same quote, at once: one converts, nine find it used. |
| 86 | errs := concurrently(10, func() error { |
| 87 | _, err := svc.Convert(t.Context(), owner, quote.ID) |
| 88 | return err |
| 89 | }) |
| 90 | var converted, used int |
| 91 | for _, err := range errs { |
| 92 | switch { |
| 93 | case err == nil: |
| 94 | converted++ |
| 95 | case errors.Is(err, wallet.ErrQuoteUsed): |
| 96 | used++ |
| 97 | default: |
| 98 | t.Errorf("convert: %v", err) |
| 99 | } |
| 100 | } |
| 101 | if converted != 1 || used != 9 { |
| 102 | t.Errorf("%d converted, %d found it used; want 1 and 9", converted, used) |
| 103 | } |
| 104 | |
| 105 | // $100, and the 50-cent fee, out; €92.15 in. After the test, checkBooks |
| 106 | // makes sure every currency still balances on its own. |
| 107 | for id, want := range map[uuid.UUID]int64{dollars: 50_000 - 10_050, euros: 9215} { |
| 108 | if b, err := svc.Balance(t.Context(), id); err != nil || b.Balance != want { |
| 109 | t.Errorf("%s balance = %d, %v; want %d", b.Currency, b.Balance, err, want) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestConvertRefuses(t *testing.T) { |
| 115 | t.Parallel() |
| 116 | svc, owner, _, _ := converter(t, newStore(t), 50*time.Millisecond) |
| 117 | quote, err := svc.Quote(t.Context(), owner, sellHundred) |
| 118 | if err != nil { |
| 119 | t.Fatalf("quote: %v", err) |
| 120 | } |
| 121 | // Someone else's quote: to them, it doesn't exist. |
| 122 | if _, err := svc.Convert(t.Context(), uuid.New(), quote.ID); !errors.Is(err, wallet.ErrQuoteNotFound) { |
| 123 | t.Errorf("someone else's quote: %v, want ErrQuoteNotFound", err) |
| 124 | } |
| 125 | time.Sleep(100 * time.Millisecond) |
| 126 | if _, err := svc.Convert(t.Context(), owner, quote.ID); !errors.Is(err, wallet.ErrQuoteExpired) { |
| 127 | t.Errorf("an expired quote: %v, want ErrQuoteExpired", err) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // A conversion in two entries, dollars out and euros in, balances in no |
| 132 | // currency at all. Our trigger from episode 2 refuses it. |
| 133 | func TestTwoEntryConversionIsUnbalanced(t *testing.T) { |
| 134 | t.Parallel() |
| 135 | s := newStore(t) |
| 136 | _, _, dollars, euros := converter(t, s, time.Minute) |
| 137 | _, err := s.PostTransaction(t.Context(), wallet.NewTransaction{ |
| 138 | Description: "a conversion, done wrong", |
| 139 | Postings: []wallet.Posting{ |
| 140 | {AccountID: dollars, Direction: wallet.Debit, Amount: 10000, Currency: "USD"}, |
| 141 | {AccountID: euros, Direction: wallet.Credit, Amount: 9215, Currency: "EUR"}, |
| 142 | }, |
| 143 | }) |
| 144 | if pgErr, ok := errors.AsType[*pgconn.PgError](err); !ok || pgErr.Code != "23514" { |
| 145 | t.Fatalf("got %v, want a check_violation (23514) from Postgres", err) |
| 146 | } |
| 147 | } |