-
Notifications
You must be signed in to change notification settings - Fork 12
/
coupon_test.go
148 lines (139 loc) · 4.35 KB
/
coupon_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
package woocommerce
import (
"errors"
"github.com/brianvoe/gofakeit/v6"
"github.com/hiscaler/gox/jsonx"
"github.com/hiscaler/gox/randx"
"github.com/hiscaler/woocommerce-go/entity"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestCouponService_All(t *testing.T) {
params := CouponsQueryParams{Search: ""}
params.PerPage = 100
params.Order = SortDesc
items, _, _, _, err := wooClient.Services.Coupon.All(params)
if err != nil {
t.Errorf("wooClient.Services.Coupon.All error: %s", err.Error())
} else {
t.Logf("items = %s", jsonx.ToPrettyJson(items))
}
}
func TestCouponService_One(t *testing.T) {
couponId := 4
item, err := wooClient.Services.Coupon.One(couponId)
if err != nil {
t.Errorf("wooClient.Services.Coupon.One error: %s", err.Error())
} else {
assert.Equal(t, couponId, item.ID, "coupon id")
}
}
func TestCouponService_Create(t *testing.T) {
code := strings.ToLower(randx.Letter(8, false))
req := CreateCouponRequest{
Code: code,
DiscountType: "percent",
Amount: 1,
IndividualUse: false,
ExcludeSaleItems: false,
MinimumAmount: 2,
}
item, err := wooClient.Services.Coupon.Create(req)
if err != nil {
t.Fatalf("wooClient.Services.Coupon.Create error: %s", err.Error())
} else {
assert.Equal(t, code, item.Code, "code")
}
}
func TestCouponService_CreateUpdateDelete(t *testing.T) {
code := gofakeit.LetterN(8)
req := CreateCouponRequest{
Code: code,
DiscountType: "percent",
Amount: 1,
IndividualUse: false,
ExcludeSaleItems: false,
MinimumAmount: 2,
}
var oldItem, newItem entity.Coupon
var err error
oldItem, err = wooClient.Services.Coupon.Create(req)
if err != nil {
t.Fatalf("wooClient.Services.Coupon.Create error: %s", err.Error())
} else {
assert.Equal(t, code, oldItem.Code, "code")
}
newItem, err = wooClient.Services.Coupon.One(oldItem.ID)
if err != nil {
t.Errorf("wooClient.Services.Customer.One(%d) error: %s", oldItem.ID, err.Error())
} else {
updateReq := UpdateCouponRequest{
Amount: 11,
IndividualUse: true,
ExcludeSaleItems: true,
MinimumAmount: 22,
}
newItem, err = wooClient.Services.Coupon.Update(oldItem.ID, updateReq)
if err != nil {
t.Fatalf("wooClient.Services.Coupon.Update error: %s", err.Error())
} else {
assert.Equal(t, oldItem.Code, newItem.Code, "code")
assert.Equal(t, 11.0, newItem.Amount, "Amount")
assert.Equal(t, true, newItem.IndividualUse, "IndividualUse")
assert.Equal(t, true, newItem.ExcludeSaleItems, "ExcludeSaleItems")
assert.Equal(t, 22.0, newItem.MinimumAmount, "MinimumAmount")
}
// Only change amount
updateReq = UpdateCouponRequest{Amount: 11.23}
newItem, err = wooClient.Services.Coupon.Update(oldItem.ID, updateReq)
if err != nil {
t.Fatalf("wooClient.Services.Coupon.Update error: %s", err.Error())
} else {
assert.Equal(t, 11.23, newItem.Amount, "Amount")
assert.Equal(t, true, newItem.IndividualUse, "IndividualUse")
assert.Equal(t, true, newItem.ExcludeSaleItems, "ExcludeSaleItems")
assert.Equal(t, 22.0, newItem.MinimumAmount, "MinimumAmount")
}
_, err = wooClient.Services.Coupon.Delete(oldItem.ID, true)
if err != nil {
t.Fatalf("wooClient.Services.Coupon.Delete(%d) error: %s", oldItem.ID, err.Error())
} else {
_, err = wooClient.Services.Coupon.One(oldItem.ID)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("wooClient.Services.Coupon.Delete(%d) failed", oldItem.ID)
}
}
}
}
func TestCouponService_Batch(t *testing.T) {
n := 3
createRequests := make([]BatchCouponsCreateItem, n)
codes := make([]string, n)
for i := 0; i < n; i++ {
code := strings.ToLower(gofakeit.LetterN(8))
req := BatchCouponsCreateItem{
Code: code,
DiscountType: "percent",
Amount: float64(i),
IndividualUse: false,
ExcludeSaleItems: false,
MinimumAmount: float64(i),
}
createRequests[i] = req
codes[i] = req.Code
}
batchReq := BatchCouponsRequest{
Create: createRequests,
}
result, err := wooClient.Services.Coupon.Batch(batchReq)
if err != nil {
t.Fatalf("wooClient.Services.Coupon.Batch() error: %s", err.Error())
}
assert.Equal(t, n, len(result.Create), "Batch create return len")
returnCodes := make([]string, 0)
for _, d := range result.Create {
returnCodes = append(returnCodes, d.Code)
}
assert.Equal(t, codes, returnCodes, "check codes is equal")
}