-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcoupon_test.go
More file actions
118 lines (110 loc) · 2.86 KB
/
Copy pathcoupon_test.go
File metadata and controls
118 lines (110 loc) · 2.86 KB
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
package woocommerce
import (
"testing"
"time"
)
func TestCouponServiceOp_List(t *testing.T) {
options := CouponListOption{
ListOptions: ListOptions{
Context: "view",
Order: "desc",
Orderby: "date",
Page: 1,
PerPage: 10,
},
}
coupons, err := client.Coupon.List(options)
if err != nil {
t.Logf("error listing coupons: %v", err)
}
for _, coupon := range coupons {
t.Logf("coupon: id=%d, code=%s, amount=%s", coupon.ID, coupon.Code, coupon.Amount)
}
}
func TestCouponServiceOp_Create(t *testing.T) {
coupon := Coupon{
Code: "TEST" + time.Now().Format("20060102150405"),
DiscountType: "fixed_cart",
Amount: "10.00",
Description: "Test coupon",
UsageCount: 0,
FreeShipping: false,
IndividualUse: false,
}
res, err := client.Coupon.Create(coupon)
if err != nil {
t.Logf("create coupon error: %v", err)
} else {
t.Logf("created coupon: id=%d, code=%s", res.ID, res.Code)
}
}
func TestCouponServiceOp_Get(t *testing.T) {
coupon, err := client.Coupon.Get(1, nil)
if err != nil {
t.Logf("get coupon error: %v", err)
} else {
t.Logf("got coupon: id=%d, code=%s", coupon.ID, coupon.Code)
}
}
func TestCouponServiceOp_Update(t *testing.T) {
coupon, err := client.Coupon.Get(1, nil)
if coupon == nil || err != nil {
t.Skip("skipping update test: cannot get coupon 1")
return
}
coupon.Description = "Updated description " + time.Now().Format("20060102150405")
res, err := client.Coupon.Update(coupon)
if err != nil {
t.Logf("update coupon error: %v", err)
} else {
t.Logf("updated coupon: id=%d, description=%s", res.ID, res.Description)
}
}
func TestCouponServiceOp_Delete(t *testing.T) {
coupon := Coupon{
Code: "DELETE" + time.Now().Format("20060102150405"),
DiscountType: "fixed_cart",
Amount: "5.00",
Description: "Test coupon to delete",
}
created, err := client.Coupon.Create(coupon)
if err != nil {
t.Skipf("skipping delete test: cannot create coupon: %v", err)
return
}
optionsDel := DeleteOption{Force: false}
res, err := client.Coupon.Delete(created.ID, optionsDel)
if err != nil {
t.Logf("delete coupon error: %v", err)
} else {
t.Logf("deleted coupon: id=%d", res.ID)
}
}
func TestCouponServiceOp_Batch(t *testing.T) {
timeNow := time.Now().Format("20060102150405")
data := CouponBatchOption{
Create: []Coupon{
{
Code: "BATCH1" + timeNow,
DiscountType: "fixed_cart",
Amount: "10.00",
Description: "Batch coupon 1",
},
{
Code: "BATCH2" + timeNow,
DiscountType: "percent",
Amount: "15",
Description: "Batch coupon 2",
},
},
}
res, err := client.Coupon.Batch(data)
if err != nil {
t.Logf("batch coupons error: %v", err)
} else {
t.Logf("batch created %d coupons", len(res.Create))
for _, c := range res.Create {
t.Logf("batch created coupon: id=%d, code=%s", c.ID, c.Code)
}
}
}