fx_test.go100 lines, 2.9 KB
| 1 | package fx_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/fiston-user/payments/internal/fx" |
| 7 | ) |
| 8 | |
| 9 | // rate parses a rate, or fails the test. |
| 10 | func rate(t *testing.T, s string) fx.Rate { |
| 11 | t.Helper() |
| 12 | r, err := fx.ParseRate(s) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | return r |
| 17 | } |
| 18 | |
| 19 | func TestParseRate(t *testing.T) { |
| 20 | for _, s := range []string{"0.9215", "151.37", "0.0066063", "1", "0.0000000001"} { |
| 21 | if r := rate(t, s); r.String() != s { |
| 22 | t.Errorf("ParseRate(%q).String() = %q", s, r) |
| 23 | } |
| 24 | } |
| 25 | for _, s := range []string{"", "0", "-1", "+1", "1e3", ".5", "1.2.3", "0.00000000001", "NaN"} { |
| 26 | if r, err := fx.ParseRate(s); err == nil { |
| 27 | t.Errorf("ParseRate(%q) = %v, want an error", s, r) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestConvert(t *testing.T) { |
| 33 | tests := []struct { |
| 34 | name string |
| 35 | amount int64 |
| 36 | from, to string |
| 37 | rate string |
| 38 | rounding fx.Rounding |
| 39 | want int64 |
| 40 | }{ |
| 41 | {"dollars to euros", 10000, "USD", "EUR", "0.9215", fx.Down, 9215}, |
| 42 | {"half a cent, down", 1000, "USD", "EUR", "0.9215", fx.Down, 921}, // 921.5 |
| 43 | {"half a cent, up", 1000, "USD", "EUR", "0.9215", fx.Up, 922}, |
| 44 | {"dollars to yen", 1000, "USD", "JPY", "151.37", fx.Down, 1513}, // 1,513.7 yen |
| 45 | {"yen to dollars", 1000, "JPY", "USD", "0.0066063", fx.Down, 660}, |
| 46 | {"a yen is less than a cent", 1, "JPY", "USD", "0.0066063", fx.Down, 0}, |
| 47 | {"dollars to dinars", 10000, "USD", "KWD", "0.3071", fx.Down, 30710}, // 30.710 KWD |
| 48 | {"a million dollars in yen", 100_000_000, "USD", "JPY", "151.37", fx.Down, 151_370_000}, |
| 49 | } |
| 50 | for _, tt := range tests { |
| 51 | t.Run(tt.name, func(t *testing.T) { |
| 52 | got, err := fx.Convert(tt.amount, tt.from, tt.to, rate(t, tt.rate), tt.rounding) |
| 53 | if err != nil || got != tt.want { |
| 54 | t.Errorf("Convert = %d, %v; want %d", got, err, tt.want) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestCost(t *testing.T) { |
| 61 | tests := []struct { |
| 62 | name string |
| 63 | amount int64 |
| 64 | from, to string |
| 65 | rate string |
| 66 | want int64 |
| 67 | }{ |
| 68 | {"fifty euros", 5000, "USD", "EUR", "0.9215", 5426}, // 54.259 |
| 69 | {"a thousand yen", 1000, "USD", "JPY", "151.37", 661}, |
| 70 | {"exactly", 9215, "USD", "EUR", "0.9215", 10000}, |
| 71 | } |
| 72 | for _, tt := range tests { |
| 73 | t.Run(tt.name, func(t *testing.T) { |
| 74 | r := rate(t, tt.rate) |
| 75 | got, err := fx.Cost(tt.amount, tt.from, tt.to, r) |
| 76 | if err != nil || got != tt.want { |
| 77 | t.Fatalf("Cost = %d, %v; want %d", got, err, tt.want) |
| 78 | } |
| 79 | // The cheapest that buys enough: a cent less doesn't. |
| 80 | enough, _ := fx.Convert(got, tt.from, tt.to, r, fx.Down) |
| 81 | short, _ := fx.Convert(got-1, tt.from, tt.to, r, fx.Down) |
| 82 | if enough < tt.amount || short >= tt.amount { |
| 83 | t.Errorf("%d buys %d, and %d buys %d: want %d", got, enough, got-1, short, tt.amount) |
| 84 | } |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestFee(t *testing.T) { |
| 90 | for _, tt := range []struct{ amount, bps, want int64 }{ |
| 91 | {10000, 50, 50}, // half a percent of 100.00 |
| 92 | {5426, 50, 28}, // 27.13, up |
| 93 | {1, 50, 1}, // never free |
| 94 | {10000, 0, 0}, |
| 95 | } { |
| 96 | if got := fx.Fee(tt.amount, tt.bps); got != tt.want { |
| 97 | t.Errorf("Fee(%d, %d) = %d, want %d", tt.amount, tt.bps, got, tt.want) |
| 98 | } |
| 99 | } |
| 100 | } |