-
Notifications
You must be signed in to change notification settings - Fork 0
/
money_test.go
100 lines (91 loc) · 2.78 KB
/
money_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gomoney
import (
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
httpmock "gopkg.in/jarcoal/httpmock.v1"
)
func TestNewMoney(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
t.Log("When the currency is an unsupported currency")
for _, curr := range []string{"xxx", "yyy", "345", "galleon"} {
_, err := NewMoney(100, curr)
assert.Equal(t, ErrUnsupportedCurrency, err, "Expected the currency to be unsupported")
}
t.Log("When the currency is valid")
data, err := ioutil.ReadFile("./fixtures/exchange_rates/1.xml")
if err != nil {
panic(err)
}
httpmock.RegisterResponder("GET", urlEUExchangeRate,
httpmock.NewStringResponder(200, string(data)))
validCurrs := []string{
"EUR",
"USD",
"JPY",
"BGN",
"CZK",
"DKK",
"GBP",
"HUF",
"PLN",
"RON",
"SEK",
"CHF",
"NOK",
"HRK",
"RUB",
"TRY",
"AUD",
"BRL",
"CAD",
"CNY",
"HKD",
"IDR",
"ILS",
"INR",
"KRW",
"MXN",
"MYR",
"NZD",
"PHP",
"SGD",
"THB",
"ZAR",
}
expectedExchangeRate := expectedEurExchangeRate1()
for _, curr := range validCurrs {
m, err := NewMoney(42, strings.ToLower(curr))
assert.Nil(t, err, "Expected no error")
assert.Equal(t, &Money{Amount: float64(42), BaseCurrency: strings.ToUpper(curr), exchangeRate: expectedExchangeRate}, m, "the money struct does not match")
}
}
func TestMoneyConvert(t *testing.T) {
t.Log("When invalid currency is passed")
for _, curr := range []string{"xxx", "yyy", "345", "galleon"} {
m := Money{Amount: 100, BaseCurrency: "EUR"}
_, err := m.Convert(curr)
assert.Equal(t, ErrUnsupportedCurrency, err, "Expected the currency to be unsupported")
}
t.Log("When the money struct is invalid")
m1 := Money{BaseCurrency: "invalid curr"}
_, err := m1.Convert("EUR")
assert.Equal(t, ErrInvalidMoneyObject, err, "Expected invalid money object error")
m2 := Money{BaseCurrency: "EUR"}
_, err = m2.Convert("USD")
assert.Equal(t, ErrInvalidMoneyObject, err, "Expected invalid money object error")
t.Log("When the money struct is valid and a valid currency is passed")
m3 := Money{BaseCurrency: "EUR", Amount: 10, exchangeRate: &eurExchangeRate{Rates: map[string]float64{"EUR": 1}}}
resM, err := m3.Convert("EUR")
assert.Nil(t, err)
assert.Equal(t, float64(10), resM.Amount, "Expected amount to be the same")
assert.Equal(t, "EUR", resM.BaseCurrency, "Expected the same base currency")
m4 := Money{BaseCurrency: "USD", Amount: 100, exchangeRate: &eurExchangeRate{Rates: map[string]float64{"EUR": 1, "USD": 1.1, "CHF": 0.9}}}
resM, err = m4.Convert("CHF")
assert.Nil(t, err)
expectedAmount := float64((100 / 1.1) * 0.9)
assert.Equal(t, expectedAmount, resM.Amount, "Expected amount incorrectly converted")
assert.Equal(t, "CHF", resM.BaseCurrency, "Expected the base currency to be CHF")
}