-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_test.gno
198 lines (165 loc) · 5.66 KB
/
payment_test.gno
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package zentasktic_user
import (
"testing"
"std"
"gno.land/p/demo/avl"
"gno.land/p/demo/zentasktic"
)
func Test_AddPayment(t *testing.T) {
payment := Payment{Id: "1", ObjectId: "task1", ObjectType: "task", Amount: std.Coins{{Denom: "ugnot", Amount: 100}}, Status: "Allocated",}
// Test adding a payment successfully.
err := payment.AddPayment()
if err != nil {
t.Errorf("Failed to add payment: %v", err)
}
// Test adding a duplicate payment.
cerr := payment.AddPayment()
if cerr != ErrPaymentIdAlreadyExists {
t.Errorf("Expected ErrPaymentIdAlreadyExists, got %v", cerr)
}
}
func Test_UpdatePayment(t *testing.T) {
// Test editing an existing payment
t.Run("Edit existing payment", func(t *testing.T) {
updatedPayment := Payment{Id: "1", ObjectId: "task1", ObjectType: "task", Amount: std.Coins{{Denom: "ugnot", Amount: 100}}, Status: "Disbursed",}
err := updatedPayment.UpdatePayment()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
// Verify the payment is updated
editedPayment, exist := Payments.Get(updatedPayment.Id)
if !exist {
t.Errorf("expected payment to exist")
}
if editedPayment.(Payment).ObjectId != "task1" {
t.Errorf("expected payment ObjectId to be 'task1', got %s", editedPayment.(Payment).ObjectId)
}
})
// Test editing a non-existing payment
t.Run("Edit non-existing payment", func(t *testing.T) {
nonExistingP := Payment{Id: "999", ObjectId: "non-existing", ObjectType: "non-existing", Amount: std.Coins{{Denom: "non-existing", Amount: 100}}, Status: "non-existing",}
err := nonExistingP.UpdatePayment()
if err != ErrPaymentIdNotFound {
t.Errorf("expected error %v, got %v", ErrPaymentIdNotFound, err)
}
})
}
func Test_RemovePayment(t *testing.T) {
pId := "2"
payment := Payment{Id: pId, ObjectId: "project1", ObjectType: "project", Amount: std.Coins{{Denom: "ugnot", Amount: 400}}, Status: "Allocated",}
// Test removing a payment
t.Run("Remove existing payment", func(t *testing.T) {
// setting up the environment
payment.AddPayment()
err := payment.RemovePayment()
if err != nil {
t.Errorf("Failed to remove payment object: %v", err)
}
})
// Test removing a payment that does not exist
t.Run("Remove non-existing payment object", func(t *testing.T) {
nonExistingP := Payment{Id: "999", ObjectId: "non-existing", ObjectType: "non-existing", Amount: std.Coins{{Denom: "non-existing", Amount: 100}}, Status: "non-existing",}
err := nonExistingP.RemovePayment()
if err != ErrPaymentIdNotFound {
t.Errorf("Expected error %v, got %v", ErrPaymentIdNotFound, err)
}
})
}
func Test_GetPaymentByOjectId(t *testing.T) {
payment := Payment{Id: "1", ObjectId: "task1", ObjectType: "task", Amount: std.Coins{{Denom: "ugnot", Amount: 100}}, Status: "Disbursed"}
result, err := GetPaymentByOjectId("task1", "task")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result.Id != payment.Id || result.Status != payment.Status {
t.Errorf("Expected %v, got %v", payment, result)
}
// Test with non-existing data
result, err = GetPaymentByOjectId("nonexistent", "task")
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if (result != Payment{}) {
t.Errorf("Expected empty Payment, got %v", result)
}
}
func Test_GetAllPayments(t *testing.T){
// Initialize test data
payment1 := Payment{
Id: "1",
ObjectId: "task1",
ObjectType: "task",
Amount: std.Coins{{Denom: "token", Amount: 20}},
Status: "Disbursed",
}
payment2 := Payment{
Id: "2",
ObjectId: "task2",
ObjectType: "task",
Amount: std.Coins{{Denom: "token", Amount: 30}},
Status: "Assigned",
}
payment1.AddPayment()
payment2.AddPayment()
// Test the function
result, err := GetAllPayments()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
// Define the expected result
expectedResult := `{"payments":[{"paymentId":"1","objectId":"task1","objectType":"task","paymentAmount":"100ugnot","paymentStatus":"Disbursed"},{"paymentId":"2","objectId":"task2","objectType":"task","paymentAmount":"30token","paymentStatus":"Assigned"}]}`
if result != expectedResult {
t.Errorf("Expected %v, got %v", expectedResult, result)
}
// Test with no rewards points
payment1.RemovePayment()
payment2.RemovePayment()
result, err = GetAllPayments()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result != `{"payments":[]}` {
t.Errorf("Expected empty payments, got %v", result)
}
}
/*
func Test_GetTotalRewardsPoints(t *testing.T) {
// Add test data to the RewardsPoints tree.
rewardsPoint1 := RewardsPoint{
Id: "1",
ObjectId: "task1",
ObjectType: "task",
Amount: std.Coins{{Denom: "token", Amount: 10}},
Status: "Disbursed",
}
rewardsPoint2 := RewardsPoint{
Id: "2",
ObjectId: "task2",
ObjectType: "task",
Amount: std.Coins{{Denom: "token", Amount: 20}},
Status: "Disbursed",
}
rewardsPoint1.AddRewardsPoints()
rewardsPoint2.AddRewardsPoints()
// Call the GetTotalRewardsPoints function.
totalRewardsPoints, err := GetTotalRewardsPoints()
if err != nil {
t.Errorf("Failed to get total rewards points: %v", err)
}
// Check if the total rewards points is correct.
expectedTotal := std.Coins{{Denom: "token", Amount: 30}}
if !totalRewardsPoints.IsEqual(expectedTotal) {
t.Errorf("Expected total rewards points to be %v, but got %v", totalRewardsPoints, expectedTotal)
}
}
func (coins std.Coins) IsEqual(other std.Coins) bool {
if len(coins) != len(other) {
return false
}
for i := range coins {
if coins[i].Denom != other[i].Denom || coins[i].Amount != other[i].Amount {
return false
}
}
return true
}*/