-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency_test.go
60 lines (51 loc) · 1.72 KB
/
currency_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Author(s): Michael Koeppl
package dinero
import (
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)
const (
num float64 = 5000
cur = JPY
)
func TestExchangeRate(t *testing.T) {
var u Currency
for i := 0; i < len(currencyCodes)-2; i++ {
u = Currency(i)
rate, err := u.ExchangeRate(Currency(i + 1))
if err != nil {
t.Error(err)
}
assert.NotZero(t, rate, "Exchange rate must not be 0")
}
}
func TestExchangeRateFloat(t *testing.T) {
// sourceCurrency and targetCurrency defined in amount_test.go
res, _ := queryAPI(currencyCodes[sourceCurrency], currencyCodes[targetCurrency])
c := sourceCurrency
f, _, _ := c.ExchangeRateFloat(targetCurrency)
assert.Equal(t, res.Rate, f, "Exchange rates in float should match")
}
func TestAmount(t *testing.T) {
decnum := decimal.NewFromFloat(num)
c := cur
a := c.Amount(decnum)
assert.NotEmpty(t, a, "Created amount must not be empty")
assert.Equal(t, decimal.NewFromFloat(5000), a.Value, "Amount values should match")
assert.Equal(t, cur, a.Currency, "Amount currencies should match")
}
func TestAmountFromFloat(t *testing.T) {
u := sourceCurrency
a := u.AmountFromFloat(5000.652)
assert.NotEmpty(t, a, "Amount from float must not be empty")
assert.Equal(t, decimal.NewFromFloat(5000.652), a.Value, "Amount values should match")
assert.Equal(t, sourceCurrency, a.Currency, "Amount currencies should match")
}
func TestAmountFromString(t *testing.T) {
u := sourceCurrency
a, _ := u.AmountFromString("5000.245")
assert.NotEmpty(t, a, "Created amount must not be empty")
assert.Equal(t, decimal.NewFromFloat(5000.245), a.Value, "Amount values should match")
assert.Equal(t, sourceCurrency, a.Currency, "Amount currencies should match")
}