-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallet_test.go
88 lines (68 loc) · 2.09 KB
/
wallet_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
package main
import (
"testing"
"github.com/shopspring/decimal"
)
func TestWallet(t *testing.T) {
assertCorrectMessage := func(t testing.TB, got, want decimal.Decimal) {
t.Helper()
if got.Equal(want) == false {
t.Errorf("got %q want %q", got, want)
}
}
//Model Balance Test
t.Run("Model balance test", func(t *testing.T) {
dummy_wallet := getEmptyWallet(1, decimal.NewFromFloat(15))
got := dummy_wallet.Balance()
want := decimal.NewFromFloat(15)
assertCorrectMessage(t, got, want)
})
//Model Credit Test
t.Run("Model credit test", func(t *testing.T) {
dummy_wallet := getEmptyWallet(1, decimal.NewFromFloat(15.0))
dummy_wallet.Credit(decimal.NewFromFloat(5.0))
got := dummy_wallet.Balance()
want := decimal.NewFromFloat(10.0)
assertCorrectMessage(t, got, want)
})
//Negative Credit Test
t.Run("Wallet negative credit test", func(t *testing.T) {
dummy_wallet := getEmptyWallet(1, decimal.NewFromFloat(15))
got := dummy_wallet.Credit(decimal.NewFromFloat(-5))
if got == nil {
t.Errorf("expected error got nil")
}
})
//Model Debit Test
t.Run("Model debit test", func(t *testing.T) {
dummy_wallet := getEmptyWallet(1, decimal.NewFromFloat(15))
dummy_wallet.Debit(decimal.NewFromFloat(5))
got := dummy_wallet.Balance()
wanted := decimal.NewFromFloat(20.0)
if got.Equal(wanted) == false {
t.Errorf("expected %v, got %v", wanted, got)
}
})
//Model credit can not higher than balance Test
t.Run("Credit can not higher than balance", func(t *testing.T) {
dummy_wallet := getEmptyWallet(1, decimal.NewFromFloat(5))
got := dummy_wallet.Credit(decimal.NewFromFloat(10))
if got == nil {
t.Errorf("expected error got nil")
}
})
//Model Credit can not be negative Test
t.Run("Credit can not higher than balance", func(t *testing.T) {
dummy_wallet := getEmptyWallet(1, decimal.NewFromFloat(5))
got := dummy_wallet.Credit(decimal.NewFromFloat(-5))
if got == nil {
t.Errorf("expected error got nil")
}
})
}
func getEmptyWallet(id int, amonut decimal.Decimal) Wallet {
return Wallet{
ID: id,
Wallet_balance: amonut,
}
}