store_test.go135 lines, 3.8 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/fiston-user/payments/internal/platform/postgres" |
| 8 | "github.com/fiston-user/payments/internal/wallet" |
| 9 | "github.com/google/uuid" |
| 10 | "github.com/jackc/pgx/v5/pgconn" |
| 11 | "github.com/jackc/pgx/v5/pgxpool" |
| 12 | ) |
| 13 | |
| 14 | // newPool connects to the package's Postgres. It's an integration test: with |
| 15 | // -short, it's skipped. |
| 16 | func newPool(t *testing.T) *pgxpool.Pool { |
| 17 | t.Helper() |
| 18 | if testing.Short() { |
| 19 | t.Skip("integration test") |
| 20 | } |
| 21 | pool, err := postgres.NewPool(t.Context(), postgresURL, 4) |
| 22 | if err != nil { |
| 23 | t.Fatalf("connect: %v", err) |
| 24 | } |
| 25 | t.Cleanup(pool.Close) |
| 26 | // Whatever a test did, the books must balance when it's over. |
| 27 | t.Cleanup(func() { checkBooks(t, pool) }) |
| 28 | return pool |
| 29 | } |
| 30 | |
| 31 | // newStore returns a Store on the test database. |
| 32 | func newStore(t *testing.T) *wallet.Store { |
| 33 | t.Helper() |
| 34 | return wallet.NewStore(newPool(t)) |
| 35 | } |
| 36 | |
| 37 | // newAccount opens a USD wallet for a new owner, so every test has its own data. |
| 38 | func newAccount(t *testing.T, s *wallet.Store, name string, side wallet.Direction) uuid.UUID { |
| 39 | t.Helper() |
| 40 | owner := uuid.New() |
| 41 | acc, err := s.CreateAccount(t.Context(), wallet.NewAccount{ |
| 42 | Kind: wallet.KindWallet, |
| 43 | OwnerID: &owner, |
| 44 | Name: name, |
| 45 | Currency: "USD", |
| 46 | NormalBalance: side, |
| 47 | }) |
| 48 | if err != nil { |
| 49 | t.Fatalf("create account %s: %v", name, err) |
| 50 | } |
| 51 | return acc.ID |
| 52 | } |
| 53 | |
| 54 | // balance returns an account's current balance in cents. |
| 55 | func balance(t *testing.T, s *wallet.Store, id uuid.UUID) int64 { |
| 56 | t.Helper() |
| 57 | b, err := s.Balance(t.Context(), id) |
| 58 | if err != nil { |
| 59 | t.Fatalf("balance: %v", err) |
| 60 | } |
| 61 | return b.Balance |
| 62 | } |
| 63 | |
| 64 | func TestPostTransaction(t *testing.T) { |
| 65 | t.Parallel() |
| 66 | s := newStore(t) |
| 67 | bank := newAccount(t, s, "bank", wallet.Debit) |
| 68 | alice := newAccount(t, s, "alice", wallet.Credit) |
| 69 | |
| 70 | tx, err := s.PostTransaction(t.Context(), wallet.NewTransaction{ |
| 71 | Description: "alice deposits $50", |
| 72 | Postings: []wallet.Posting{ |
| 73 | {AccountID: bank, Direction: wallet.Debit, Amount: 5000, Currency: "USD"}, |
| 74 | {AccountID: alice, Direction: wallet.Credit, Amount: 5000, Currency: "USD"}, |
| 75 | }, |
| 76 | }) |
| 77 | if err != nil { |
| 78 | t.Fatalf("post transaction: %v", err) |
| 79 | } |
| 80 | |
| 81 | if got := balance(t, s, bank); got != 5000 { |
| 82 | t.Errorf("bank balance = %d, want 5000", got) |
| 83 | } |
| 84 | if got := balance(t, s, alice); got != 5000 { |
| 85 | t.Errorf("alice balance = %d, want 5000", got) |
| 86 | } |
| 87 | |
| 88 | entries, err := s.Entries(t.Context(), alice, 0, 10) |
| 89 | if err != nil { |
| 90 | t.Fatalf("entries: %v", err) |
| 91 | } |
| 92 | if len(entries) != 1 || entries[0].TransactionID != tx.ID { |
| 93 | t.Errorf("alice has %d entries, want 1 in transaction %s", len(entries), tx.ID) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestPostTransactionRejectsUnbalanced(t *testing.T) { |
| 98 | t.Parallel() |
| 99 | s := newStore(t) |
| 100 | alice := newAccount(t, s, "alice", wallet.Credit) |
| 101 | bob := newAccount(t, s, "bob", wallet.Credit) |
| 102 | |
| 103 | // Alice pays $20, but Bob only receives $15: five dollars would vanish. |
| 104 | _, err := s.PostTransaction(t.Context(), wallet.NewTransaction{ |
| 105 | Description: "alice pays bob $20", |
| 106 | Postings: []wallet.Posting{ |
| 107 | {AccountID: alice, Direction: wallet.Debit, Amount: 2000, Currency: "USD"}, |
| 108 | {AccountID: bob, Direction: wallet.Credit, Amount: 1500, Currency: "USD"}, |
| 109 | }, |
| 110 | }) |
| 111 | |
| 112 | var pgErr *pgconn.PgError |
| 113 | if !errors.As(err, &pgErr) || pgErr.Code != "23514" { |
| 114 | t.Fatalf("got error %v, want a check_violation (23514) from Postgres", err) |
| 115 | } |
| 116 | t.Logf("rejected by Postgres: %s", pgErr.Message) |
| 117 | |
| 118 | // Atomic: the entries inserted before the failed commit were rolled back too. |
| 119 | if got := balance(t, s, alice); got != 0 { |
| 120 | t.Errorf("alice balance = %d, want 0", got) |
| 121 | } |
| 122 | if got := balance(t, s, bob); got != 0 { |
| 123 | t.Errorf("bob balance = %d, want 0", got) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestAccountNotFound(t *testing.T) { |
| 128 | t.Parallel() |
| 129 | s := newStore(t) |
| 130 | |
| 131 | _, err := s.Account(t.Context(), uuid.New()) |
| 132 | if !errors.Is(err, wallet.ErrAccountNotFound) { |
| 133 | t.Fatalf("got error %v, want ErrAccountNotFound", err) |
| 134 | } |
| 135 | } |