-
Notifications
You must be signed in to change notification settings - Fork 12
/
product_category_test.go
91 lines (83 loc) · 2.6 KB
/
product_category_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
package woocommerce
import (
"errors"
"github.com/brianvoe/gofakeit/v6"
"github.com/hiscaler/gox/jsonx"
"github.com/stretchr/testify/assert"
"testing"
)
func TestProductCategoryService_All(t *testing.T) {
params := ProductCategoriesQueryParams{}
items, _, _, _, err := wooClient.Services.ProductCategory.All(params)
if err != nil {
t.Errorf("wooClient.Services.ProductCategory.All: %s", err.Error())
} else {
t.Logf("Items: %s", jsonx.ToPrettyJson(items))
}
}
func TestProductCategoryService_One(t *testing.T) {
item, err := wooClient.Services.ProductCategory.One(15)
if err != nil {
t.Errorf("wooClient.Services.ProductCategory.One: %s", err.Error())
} else {
assert.Equal(t, 15, item.ID, "one")
}
}
func TestProductCategoryService_CreateUpdateDelete(t *testing.T) {
name := gofakeit.BeerName()
req := CreateProductCategoryRequest{
Name: name,
}
item, err := wooClient.Services.ProductCategory.Create(req)
if err != nil {
t.Fatalf("wooClient.Services.ProductCategory.Create: %s", err.Error())
}
assert.Equal(t, name, item.Name, "product category name")
categoryId := item.ID
// Update
newName := gofakeit.BeerName()
updateReq := UpdateProductCategoryRequest{
Name: newName,
}
item, err = wooClient.Services.ProductCategory.Update(categoryId, updateReq)
if err != nil {
t.Fatalf("wooClient.Services.ProductCategory.Update: %s", err.Error())
}
assert.Equal(t, newName, item.Name, "product category name")
// Delete
_, err = wooClient.Services.ProductCategory.Delete(categoryId, true)
if err != nil {
t.Fatalf("wooClient.Services.ProductCategory.Delete: %s", err.Error())
}
// Check is exists
_, err = wooClient.Services.ProductCategory.One(categoryId)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("%d is not deleted, error: %s", categoryId, err.Error())
}
}
func TestProductCategoryService_Batch(t *testing.T) {
n := 3
createRequests := make([]BatchProductCategoriesCreateItem, n)
names := make([]string, n)
for i := 0; i < n; i++ {
req := BatchProductCategoriesCreateItem{
Name: gofakeit.Word(),
Description: gofakeit.Address().Address,
}
createRequests[i] = req
names[i] = req.Name
}
batchReq := BatchProductCategoriesRequest{
Create: createRequests,
}
result, err := wooClient.Services.ProductCategory.Batch(batchReq)
if err != nil {
t.Fatalf("wooClient.Services.ProductCategory.Batch() error: %s", err.Error())
}
assert.Equal(t, n, len(result.Create), "Batch create return len")
returnNames := make([]string, 0)
for _, d := range result.Create {
returnNames = append(returnNames, d.Name)
}
assert.Equal(t, names, returnNames, "check names is equal")
}