-
Notifications
You must be signed in to change notification settings - Fork 10
/
customer_test.go
188 lines (177 loc) · 5.32 KB
/
customer_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
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
package woocommerce
import (
"errors"
"github.com/brianvoe/gofakeit/v6"
"github.com/hiscaler/gox/jsonx"
"github.com/hiscaler/woocommerce-go/entity"
"github.com/stretchr/testify/assert"
"testing"
)
func getCustomerId(t *testing.T) {
t.Log("Execute getCustomerId test")
params := CustomersQueryParams{}
params.Page = 1
params.PerPage = 1
items, _, _, _, err := wooClient.Services.Customer.All(params)
if err != nil || len(items) == 0 {
t.FailNow()
}
if len(items) == 0 {
t.Fatalf("getCustomerId not found one customer")
}
mainId = items[0].ID
}
func TestCustomerService_All(t *testing.T) {
params := CustomersQueryParams{}
_, _, _,_, err := wooClient.Services.Customer.All(params)
if err != nil {
t.Errorf("wooClient.Services.Customer.All error: %s", err.Error())
}
}
func TestCustomerService_One(t *testing.T) {
t.Run("getCustomerId", getCustomerId)
item, err := wooClient.Services.Customer.One(mainId)
if err != nil {
t.Fatalf("wooClient.Services.Customer.One error: %s", err.Error())
}
assert.Equal(t, mainId, item.ID, "customer id")
}
func TestCustomerService_Create(t *testing.T) {
gofakeit.Seed(0)
address := gofakeit.Address()
req := CreateCustomerRequest{
Email: gofakeit.Email(),
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Username: gofakeit.Username(),
Password: gofakeit.Password(true, true, true, false, false, 10),
MetaData: nil,
Billing: &entity.Billing{
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Company: gofakeit.Company(),
Address1: address.Address,
Address2: "",
City: address.City,
State: address.State,
Postcode: address.Zip,
Country: address.Country,
Email: gofakeit.Email(),
Phone: gofakeit.Phone(),
},
}
item, err := wooClient.Services.Customer.Create(req)
if err != nil {
t.Errorf("wooClient.Services.Customer.Create error: %s", err.Error())
} else {
t.Logf("item = %#v", item)
}
}
func TestCustomerService_CreateUpdateDelete(t *testing.T) {
gofakeit.Seed(0)
// Create
var oldItem, newItem entity.Customer
var err error
address := gofakeit.Address()
req := CreateCustomerRequest{
Email: gofakeit.Email(),
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Username: gofakeit.Username(),
Password: gofakeit.Password(true, true, true, false, false, 10),
MetaData: nil,
Billing: &entity.Billing{
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Company: gofakeit.Company(),
Address1: address.Address,
Address2: "",
City: address.City,
State: address.State,
Postcode: address.Zip,
Country: address.Country,
Email: gofakeit.Email(),
Phone: gofakeit.Phone(),
},
}
oldItem, err = wooClient.Services.Customer.Create(req)
if err != nil {
t.Fatalf("wooClient.Services.Customer.Create error: %s", err.Error())
}
// Update
afterData := struct {
email string
billingFirstName string
billingLastName string
}{
email: gofakeit.Email(),
billingFirstName: gofakeit.FirstName(),
billingLastName: gofakeit.LastName(),
}
updateReq := UpdateCustomerRequest{
Email: afterData.email,
Billing: &entity.Billing{
FirstName: afterData.billingFirstName,
LastName: afterData.billingLastName,
},
}
newItem, err = wooClient.Services.Customer.Update(oldItem.ID, updateReq)
if err != nil {
t.Fatalf("wooClient.Services.Customer.Update error: %s", err.Error())
} else {
assert.Equal(t, afterData.email, newItem.Email, "email")
assert.Equal(t, afterData.billingFirstName, newItem.Billing.FirstName, "billing first name")
assert.Equal(t, afterData.billingLastName, newItem.Billing.LastName, "billing last name")
}
// Delete
_, err = wooClient.Services.Customer.Delete(oldItem.ID, customerDeleteParams{Force: true})
if err != nil {
t.Fatalf("wooClient.Services.Customer.Delete(%d) error: %s", oldItem.ID, err.Error())
}
// Query check is exists
_, err = wooClient.Services.Customer.One(oldItem.ID)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("wooClient.Services.Customer.Delete(%d) failed", oldItem.ID)
}
}
func TestCustomerService_Batch(t *testing.T) {
n := 3
createRequests := make([]BatchCreateCustomerRequest, n)
emails := make([]string, n)
for i := 0; i < n; i++ {
req := BatchCreateCustomerRequest{
Email: gofakeit.Email(),
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Username: gofakeit.Username(),
Password: gofakeit.Password(true, true, true, false, false, 10),
Billing: nil,
Shipping: nil,
MetaData: nil,
}
createRequests[i] = req
emails[i] = req.Email
}
batchReq := BatchCustomerRequest{
Create: createRequests,
}
result, err := wooClient.Services.Customer.Batch(batchReq)
if err != nil {
t.Fatalf("wooClient.Services.Customer.Batch() error: %s", err.Error())
}
assert.Equal(t, n, len(result.Create), "Batch create return len")
returnEmails := make([]string, 0)
for _, d := range result.Create {
returnEmails = append(returnEmails, d.Email)
}
assert.Equal(t, emails, returnEmails, "check emails is equal")
}
func TestCustomerService_Downloads(t *testing.T) {
// todo
items, err := wooClient.Services.Customer.Downloads(0)
if err != nil {
t.Fatalf("wooClient.Services.Customer.Downloads() error: %s", err.Error())
} else {
t.Logf("items = %s", jsonx.ToPrettyJson(items))
}
}