-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfund_test.go
156 lines (133 loc) · 3.9 KB
/
fund_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package fineract
import (
"fmt"
"testing"
"time"
)
func makeClient(mock bool) (*Client, error) {
if mock {
return NewClient("", "", "", FineractOption{
Transport: &MockTransport{DirectoryPath: "testdata"},
})
}
return NewClient("https://"+fineractHost, fineractUser, fineractPassword, FineractOption{SkipVerify: true})
}
func TestSuiteMock(t *testing.T) {
if !testing.Short() {
t.Skip("Skipped mock tests in long mode")
}
client, err := makeClient(true)
if err != nil {
t.Fatal(err)
}
fundId := "1884"
Suite(t, client, fundId)
}
func TestSuite(t *testing.T) {
if testing.Short() {
t.Skip("Skipped integrated tests in short mode")
}
client, err := makeClient(false)
if err != nil {
t.Fatal(err)
}
//retrieve fundID from GetFunds
req := FundsRequest{}
resp, err := client.GetFunds(&req)
if err != nil {
t.Fatalf("retrieve list of fund(s): %v", err)
}
if len(resp.FundDetail) == 0 {
t.Fatalf("No fund retrieved, atleast one fund should exists")
}
fundId := fmt.Sprintf("%v", resp.FundDetail[0].Id)
Suite(t, client, fundId)
ISuite(t, client, fundId)
}
func Suite(t *testing.T, client *Client, fundId string) {
var fundInitialBalance float64
t.Run("TestGetFundValue", func(t *testing.T) {
resp, err := client.GetFundValue(fundId, nil)
if err != nil {
t.Fatalf("Cannot get the fund value: %v", err)
}
fundInitialBalance = resp.Statement.Amount
})
t.Run("TestGetFunds", func(t *testing.T) {
req := FundsRequest{}
if _, err := client.GetFunds(&req); err != nil {
t.Fatalf("retrieve list of fund(s): %v", err)
}
})
t.Run("TestGetPaymentType", func(t *testing.T) {
resp, err := client.GetPaymentType(&GetPaymentTypeRequest{})
if err != nil {
t.Fatalf("Cannot retrieve payment types: %v", err)
}
if len(resp.PaymentMethod) == 0 {
t.Fatalf("No payment type found, atleast one is required")
}
})
}
func ISuite(t *testing.T, client *Client, fundId string) {
var txAmount float64 = 500
resp, err := client.GetPaymentType(&GetPaymentTypeRequest{})
if err != nil {
t.Fatalf("failed to get payment types: %v", err)
}
if len(resp.PaymentMethod) == 0 {
t.Fatalf("no payment type found, atleast one is required")
}
paymentId := fmt.Sprintf("%v", resp.PaymentMethod[0].Id)
t.Run("TestFundIncrement", func(t *testing.T) {
before, err := client.GetFundValue(fundId, nil)
if err != nil {
t.Fatalf("Cannot get the fund value: %v", err)
}
//increment
req := &FundIncrementRequest{
Locale: "en",
DateFormat: "dd MMMM yyyy",
TransactionDate: time.Now().Format("02 January 2006"),
TransactionAmount: fmt.Sprintf("%v", txAmount),
PaymentTypeId: paymentId,
}
_, err = client.FundIncrement(fundId, req)
if err != nil {
t.Fatalf("Could not increment the fund value: %v", err)
}
after, err := client.GetFundValue(fundId, nil)
if err != nil {
t.Fatalf("Cannot get the fund value: %v", err)
}
assertEqual(t, before.Statement.Amount+txAmount, after.Statement.Amount, "Fund balance was not incremented")
})
t.Run("TestFundDecrement", func(t *testing.T) {
before, err := client.GetFundValue(fundId, nil)
if err != nil {
t.Fatalf("Cannot get the fund value: %v", err)
}
//increment
req := &FundDecrementRequest{
Locale: "en",
DateFormat: "dd MMMM yyyy",
TransactionDate: time.Now().Format("02 January 2006"),
TransactionAmount: fmt.Sprintf("%v", txAmount),
PaymentTypeId: paymentId,
}
_, err = client.FundDecrement(fundId, req)
if err != nil {
t.Fatalf("Could not decrement the fund value: %v", err)
}
after, err := client.GetFundValue(fundId, nil)
if err != nil {
t.Fatalf("Cannot get the fund value: %v", err)
}
assertEqual(t, before.Statement.Amount-txAmount, after.Statement.Amount, "Fund balance was not decremented")
})
}
func assertEqual(t *testing.T, a interface{}, b interface{}, msg string) {
if a != b {
t.Fatalf("%s != %s : %s", a, b, msg)
}
}