statement_test.go52 lines, 1.5 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/fiston-user/payments/internal/wallet" |
| 8 | ) |
| 9 | |
| 10 | // TestStatementPages posts more deposits than fit on two pages of a statement, |
| 11 | // in one statement, so they all have the same created_at: keyset pagination's |
| 12 | // hardest case, where only the id tells them apart. Every one must come back, |
| 13 | // once, in order, with a balance that adds up. |
| 14 | func TestStatementPages(t *testing.T) { |
| 15 | t.Parallel() |
| 16 | pool := newPool(t) |
| 17 | store := wallet.NewStore(pool) |
| 18 | svc := wallet.NewService(store) |
| 19 | w := newWallet(t, svc, 0) |
| 20 | settlement, err := store.SystemAccount(t.Context(), wallet.SettlementAccount, "USD") |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | // 1,001 deposits of a cent: two full pages, and one more line. |
| 25 | _, err = pool.Exec(t.Context(), `WITH txs AS ( |
| 26 | INSERT INTO transactions (description) |
| 27 | SELECT 'deposit' FROM generate_series(1, 1001) RETURNING id) |
| 28 | INSERT INTO entries (transaction_id, account_id, direction, amount, currency) |
| 29 | SELECT id, account, direction::entry_direction, 1, 'USD' |
| 30 | FROM txs, (VALUES ($1::uuid, 'credit'), ($2::uuid, 'debit')) AS e (account, direction)`, |
| 31 | w, settlement.ID) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | day := time.Now().UTC().Truncate(24 * time.Hour) |
| 36 | lines, err := svc.Statement(t.Context(), w, day, day) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | var n int64 |
| 41 | for line, err := range lines { |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if n++; line.Balance != n { |
| 46 | t.Fatalf("line %d: balance %d, want %d", n, line.Balance, n) |
| 47 | } |
| 48 | } |
| 49 | if n != 1001 { |
| 50 | t.Errorf("%d lines, want 1001", n) |
| 51 | } |
| 52 | } |