-
Notifications
You must be signed in to change notification settings - Fork 3
/
ManagerInMemoryImpl_test.go
executable file
·68 lines (63 loc) · 1.97 KB
/
ManagerInMemoryImpl_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
package acccore
import (
"context"
"testing"
"github.com/shopspring/decimal"
)
type ExchangeTest struct {
from string
fromAmount decimal.Decimal
to string
toAmount decimal.Decimal
}
func TestInMemoryExchangeManager_CalculateExchange(t *testing.T) {
ctx := context.Background()
exchangeManager := NewInMemoryExchangeManager()
_, _ = exchangeManager.CreateCurrency(ctx, "PLATINUM", "Platinum", decimal.NewFromFloat(0.001), "superman")
_, _ = exchangeManager.CreateCurrency(ctx, "GOLD", "Gold", decimal.NewFromFloat(0.01), "superman")
_, _ = exchangeManager.CreateCurrency(ctx, "SILVER", "Silver", decimal.NewFromFloat(0.1), "superman")
_, _ = exchangeManager.CreateCurrency(ctx, "COPPER", "Copper", decimal.NewFromFloat(1.0), "superman")
testData := []*ExchangeTest{
{
from: "GOLD",
fromAmount: decimal.NewFromInt(1000),
to: "PLATINUM",
toAmount: decimal.NewFromInt(100),
},
{
from: "GOLD",
fromAmount: decimal.NewFromInt(1000),
to: "SILVER",
toAmount: decimal.NewFromInt(10000),
},
{
from: "GOLD",
fromAmount: decimal.NewFromInt(1000),
to: "GOLD",
toAmount: decimal.NewFromInt(1000),
},
}
for idx, data := range testData {
result, err := exchangeManager.CalculateExchange(ctx, data.from, data.to, data.fromAmount)
if err != nil {
t.Error(err.Error())
t.Fail()
}
if result.InexactFloat64() != data.toAmount.InexactFloat64() {
t.Errorf("#%d. Expect %f but %f", idx, data.toAmount.InexactFloat64(), result.InexactFloat64())
t.Fail()
}
}
exchangeManager.SetDenom(ctx, decimal.NewFromFloat(123.456))
for idx, data := range testData {
result, err := exchangeManager.CalculateExchange(ctx, data.from, data.to, data.fromAmount)
if err != nil {
t.Error(err.Error())
t.Fail()
}
if result.InexactFloat64() != data.toAmount.InexactFloat64() {
t.Errorf("#%d. Expect %f but %f", idx, data.toAmount.InexactFloat64(), result.InexactFloat64())
t.Fail()
}
}
}